The Wayback Machine - https://web.archive.org/web/20220220083443/https://github.com/tslearn-team/tslearn/issues/387
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to Save and Load fitted LearningShapelets model #387

Open
balint-daniel opened this issue Feb 18, 2022 · 1 comment
Open

How to Save and Load fitted LearningShapelets model #387

balint-daniel opened this issue Feb 18, 2022 · 1 comment

Comments

@balint-daniel
Copy link

@balint-daniel balint-daniel commented Feb 18, 2022

Describe the bug
How can I load the LearningShapelets model which is already fitted?
When I run the code below, I get this error: NotFittedError: This LearningShapelets instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.

To Reproduce
A code sample to reproduce the problem:

from joblib import dump, load
from keras.models import load_model
from tslearn.shapelets.shapelets import LocalSquaredDistanceLayer, GlobalMinPooling1D
from tslearn.shapelets import LearningShapelets
from tslearn.datasets import UCR_UEA_datasets

# Load a dataset
X_train, y_train, X_test, y_test = UCR_UEA_datasets().load_dataset('Coffee')

# Define and fit an instance of LearningShapelets
clf = LearningShapelets(random_state=42)
clf.fit(X_train, y_train)

# Save the model by accessing the 'model_' attribute
clf.model_.save('shapelet_model.h5')

# Save all the attributes
clf.model_ = None
clf.transformer_model_ = None
clf.locator_model_ = None
dump(clf.__dict__, 'attributes.gz')

# Delete the instance
del clf

# Create a new instance
clf = LearningShapelets()

# Define the 'model_' attribute of this instance
model = load_model(
    'shapelet_model.h5',
    custom_objects={'LocalSquaredDistanceLayer': LocalSquaredDistanceLayer,
                    'GlobalMinPooling1D': GlobalMinPooling1D}
)
clf.model_ = model

# Define the other necessary attributes
attributes = load('attributes.gz')
for i in attributes:
    clf.i = attributes[i]

# Predict on the test set
clf.score(X_test, y_test)

Environment:

  • OS: Windows 10
  • tslearn version: 0.5.2
@GillesVandewiele
Copy link
Contributor

@GillesVandewiele GillesVandewiele commented Feb 20, 2022

Thank you for reporting this. This works:

from joblib import dump, load
from keras.models import load_model
from tslearn.shapelets.shapelets import LocalSquaredDistanceLayer, GlobalMinPooling1D
from tslearn.shapelets import LearningShapelets
from tslearn.datasets import UCR_UEA_datasets

# Load a dataset
X_train, y_train, X_test, y_test = UCR_UEA_datasets().load_dataset('Coffee')
clf = LearningShapelets(random_state=42, max_iter=100)
clf.fit(X_train, y_train)
clf.to_json('shapelet_model.json')
clf = LearningShapelets.from_json('shapelet_model.json')
print(clf.score(X_test, y_test))

X_train, y_train, X_test, y_test = UCR_UEA_datasets().load_dataset('Coffee')
clf = LearningShapelets(random_state=42, max_iter=100)
clf.fit(X_train, y_train)
clf.to_pickle('shapelet_model.p')
clf = LearningShapelets.from_pickle('shapelet_model.p')
print(clf.score(X_test, y_test))

However:

  • It is not clear that the from_ methods are CLASS METHODS. This should be made more clear in the documentation and through and example. I just made the mistake myself of doing:
clf = LearningShapelets()
clf.from_json(...)
  • The to_hdf5 method is bugging out:
TypeError: <class 'list'> for item: [0.6951727271080017, 0.6951701045036316, 0.695167601108551, 0.6951650977134705, 0.6951624751091003, 0.6951599717140198, 0.6951574683189392, 0.6951548457145691, 0.6951522827148438, 0.6951497793197632] not supported by HDF5

So I'll leave this issue open for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment