aeon-toolkit / aeon

A toolkit for machine learning from time series
https://aeon-toolkit.org/
BSD 3-Clause "New" or "Revised" License
892 stars 93 forks source link

[BUG] Unable to save trained deep learning model #1158

Closed natbukowski closed 4 months ago

natbukowski commented 5 months ago

Describe the bug

Attempted to save a trained AEFCNClusterer model but received an error saying that the model couldn't be pickled.

Steps/Code to reproduce the bug

from aeon.datasets import load_arrow_head
from aeon.datasets import load_classification
from aeon.clustering.deep_learning import AEFCNClusterer

X_train, _ = load_classification(name="ArrowHead", split="train")

aefcn = AEFCNClusterer(
    n_clusters=2,
    temporal_latent_space=False,
    clustering_algorithm="kmeans",
    n_epochs=20,
)

aefcn.fit(X=X_train)

aefcn.save()

Expected results

According to the AEFCNCluster the save functions description states the there are two possible return options:

RETURNS: if path is None - in-memory serialized self if path is file location - ZipFile with reference to the file.

Link to AEFCNCluster.save() documentation: https://www.aeon-toolkit.org/en/stable/api_reference/auto_generated/aeon.clustering.deep_learning.AEFCNClusterer.html#aeon.clustering.deep_learning.AEFCNClusterer.save

Actual results

AttributeError                            Traceback (most recent call last)
Cell In[25], line 1
----> 1 aefcn.save()

File /usr/local/lib/python3.10/site-packages/aeon/base/_base.py:708, in BaseObject.save(self, path)
    705 from zipfile import ZipFile
    707 if path is None:
--> 708     return (type(self), pickle.dumps(self))
    709 if not isinstance(path, (str, Path)):
    710     raise TypeError(
    711         "`path` is expected to either be a string or a Path object "
    712         f"but found of type:{type(path)}."
    713     )

AttributeError: Can't pickle local object 'ReduceLROnPlateau._reset.<locals>.<lambda>'

Versions

System: python: 3.10.12 (main, Aug 3 2023, 19:49:04) [GCC 9.4.0] executable: /usr/local/bin/python machine: Linux-4.14.336-253.554.amzn2.x86_64-x86_64-with-glibc2.31 Python dependencies: pip: 23.2.1 setuptools: 68.0.0 scikit-learn: 1.3.0 aeon: 0.6.0 statsmodels: None numpy: 1.24.3 scipy: 1.11.1 pandas: 1.5.3 matplotlib: 3.7.2 joblib: 1.3.1 numba: 0.57.1 pmdarima: None tsfresh: None
hadifawaz1999 commented 5 months ago

Thank you for mentioning this, as you can see in the documentation of AEFCNClusterer, and any deep learner in aeon, you have to use the save_best_model flag parameter to True, and you can choose the name using best_file_name . Best means the model that has the lowest training loss during training

Same thing goes if you wish the last model, use the save_last_model flag and the last_file_name string parameter

Deep Learners cannot be pickled, thats why you get this error, and thats why we have these parameters i described above.

Thanks for the mention @MatthewMiddlehurst , we should think of adding a warning or something about this somewhere so that users dont try to save it like that.

Let us know if this answers to your issue @natbukowski !

TonyBagnall commented 4 months ago

as I understand it you just cant pickle tensorflow models?

hadifawaz1999 commented 4 months ago

as I understand it you just cant pickle tensorflow models?

yes

natbukowski commented 4 months ago

Thank you for mentioning this, as you can see in the documentation of AEFCNClusterer, and any deep learner in aeon, you have to use the save_best_model flag parameter to True, and you can choose the name using best_file_name . Best means the model that has the lowest training loss during training

Same thing goes if you wish the last model, use the save_last_model flag and the last_file_name string parameter

Deep Learners cannot be pickled, thats why you get this error, and thats why we have these parameters i described above.

Thanks for the mention @MatthewMiddlehurst , we should think of adding a warning or something about this somewhere so that users dont try to save it like that.

Let us know if this answers to your issue @natbukowski !

Yes this answered my question, thank you!

TonyBagnall commented 4 months ago

I think we can close this now, feel free to reopen or raise a new issue