benfred / implicit

Fast Python Collaborative Filtering for Implicit Feedback Datasets
https://benfred.github.io/implicit/
MIT License
3.57k stars 612 forks source link

Loading model of type AlternatingLeastSquares fails #575

Closed jacobmalmberg closed 2 years ago

jacobmalmberg commented 2 years ago

As the title says, cannot load an AlternatingLeastSquares model. Error message:

Traceback (most recent call last):
  File "/Users/jaub/gitlab/als-recommendations/a.py", line 3, in <module>
    for k, v in data.items():
  File "/usr/local/Cellar/python@3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/_collections_abc.py", line 851, in __iter__
    yield (key, self._mapping[key])
  File "/usr/local/lib/python3.9/site-packages/numpy/lib/npyio.py", line 253, in __getitem__
    return format.read_array(bytes,
  File "/usr/local/lib/python3.9/site-packages/numpy/lib/format.py", line 727, in read_array
    raise ValueError("Object arrays cannot be loaded when "
ValueError: Object arrays cannot be loaded when allow_pickle=False

To recreate:

import implicit
model = implicit.als.AlternatingLeastSquares(factors=20, regularization=0.1)
model.save('model.npz')
implicit.als.AlternatingLeastSquares().load('model.npz')

Workaround is to set allow_pickle = Truein https://github.com/benfred/implicit/blob/f9050ae6340fbd549cf915dbf6e96a2ed713bac0/implicit/cpu/als.py#L444

Python version: 3.7, 3.9 Implicit version: https://github.com/benfred/implicit.git@f9050ae6340fbd549cf915dbf6e96a2ed713bac0

benfred commented 2 years ago

Thanks for the heads up! The issue is in saving models where the .fit method hasn't been called yet. This meant the user_factors and item_factors members were None - which couldn't be loaded back by np.load without using pickle.

I have a fix in https://github.com/benfred/implicit/pull/577