question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot load dumped dok_matrix objects

See original GitHub issue

I had problems trying to load a dok_matrix. Here’s the minimal script to replicate:

from __future__ import print_function
import sys
import joblib
import numpy as np
from scipy.sparse import bsr_matrix, coo_matrix, csc_matrix, csr_matrix, dia_matrix, dok_matrix, lil_matrix

for cls in (bsr_matrix, coo_matrix, csc_matrix, csr_matrix, dia_matrix, lil_matrix, dok_matrix):
    print('Dumping/loading {}'.format(cls))
    m = cls(np.random.normal(size=(100, 100)))
    joblib.dump(m, 'foo')
    joblib.load('foo')

It looks like shape isn’t appropriately set as we load the matrix back out (pickle/cPickle seems to work though). I get the following:

Dumping/loading <class 'scipy.sparse.bsr.bsr_matrix'>
Dumping/loading <class 'scipy.sparse.coo.coo_matrix'>
Dumping/loading <class 'scipy.sparse.csc.csc_matrix'>
Dumping/loading <class 'scipy.sparse.csr.csr_matrix'>
Dumping/loading <class 'scipy.sparse.dia.dia_matrix'>
Dumping/loading <class 'scipy.sparse.lil.lil_matrix'>
Dumping/loading <class 'scipy.sparse.dok.dok_matrix'>
Traceback (most recent call last):
  File "/Users/wradford/Desktop/fail.py", line 11, in <module>
    joblib.load('foo')
  File "/Users/wradford/repos/joblib/joblib/numpy_pickle.py", line 425, in load
    obj = unpickler.load()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 858, in load
    dispatch[key](self)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 1206, in load_setitems
    dict[stack[i]] = stack[i + 1]
  File "/Users/wradford/repos/joblib/ve/lib/python2.7/site-packages/scipy/sparse/dok.py", line 236, in __setitem__
    0 <= i < self.shape[0] and 0 <= j < self.shape[1]):
  File "/Users/wradford/repos/joblib/ve/lib/python2.7/site-packages/scipy/sparse/base.py", line 499, in __getattr__
    raise AttributeError(attr + " not found")
AttributeError: shape not found

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
aabadiecommented, Jan 6, 2016

Digging this up.

Looking at your stacktrace, I think the problem comes from pickle itself. Following http://stackoverflow.com/a/12181159, it seems the problem with matrix_dok comes from the pickle protocol version.

In joblib, the pickle protocol version used is 3 (Default) for python 2 and 4 for python 3 (highest). When calling pickle.dump directly with python 2, the pickle protocol is by default ‘None’ (0) and it’s the only case where the matrix_dok can be reloaded. With any pickle protocol > 0, it fails, hence joblib will fail.

One potential solution is to explicitly set protocol=0 when calling joblib.dump. For instance, the following code works:

from __future__ import print_function
import sys
import joblib
import numpy as np
from scipy.sparse import bsr_matrix, coo_matrix, csc_matrix, csr_matrix, dia_matrix, dok_matrix, lil_matrix

for cls in (bsr_matrix, coo_matrix, csc_matrix, csr_matrix, dia_matrix, lil_matrix, dok_matrix):
    print('Dumping/loading {}'.format(cls))
    m = cls(np.random.normal(size=(100, 100)))
    joblib.dump(m, 'foo', protocol=0)
    joblib.load('foo')
0reactions
wejradfordcommented, Jan 14, 2016

Thanks guys!

Read more comments on GitHub >

github_iconTop Results From Across the Web

flexible type error - python dict to sparse matrix - Stack Overflow
I'm thrown TypeError: cannot perform reduce with flexible type but is it cuz I'm using Python2? python · numpy · scipy · sparse-matrix ......
Read more >
Cannot load file containing pickled data - Python .npy I/O-numpy
I am trying to save a dataframe and a matrix as .npy files with np.save() and then read them using np.load() but I...
Read more >
scipy.sparse.dok_matrix — SciPy v1.9.3 Manual
Returns a copy of row i of the matrix, as a (1 x n) sparse matrix (row vector). items (). keys ().
Read more >
Source code for graphtools.base - Read the Docs
[docs]class Base(object): """Class that deals with key-word arguments but is ... n_pca = n_pcaR if n_pca < 0: raise ValueError( "n_pca cannot be...
Read more >
Sparse - OCaml scikit-learn interface
Object.t. Convert this matrix to Dictionary Of Keys format. With copy=False, the data/indices may be shared between this matrix and the resultant dok_matrix...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found