You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Steps to reproduce :
The code below does the following:
Train a model
Save the model
(Re)Load the model
Predict with both the original model and the reloaded model
The predictions should be equal but they are different
from melusine.models.neural_architectures import cnn_model
from melusine.models.train import NeuralModel
from melusine.nlp_tools.embedding import Embedding
X, y = MY_DATA
pretrained_embedding = Embedding().load("MY_EMBEDDING")
model = NeuralModel(
architecture_function = cnn_model,
pretrained_embedding = pretrained_embedding,
)
model.fit(X, y)
model.save_nn_model("MY_MODEL")
# Load the saved model
loaded_model = NeuralModel(
architecture_function = cnn_model,
pretrained_embedding = pretrained_embedding,
)
loaded_model.load_nn_model("MY_MODEL")
y_pred_base = model.predict(X)
y_pred_loaded = loaded_model.predict(X)
print((y_pred_base == y_pred_loaded).all())
Bug explanation :
When making a prediction (NeuralModel.predict), the NeuralModel uses the attribute vocabulary_dict.
This attribute is an empty dict at model initialization and it is filled when the model is fitted.
The vocabulary_dict attribute is not saved by the save_nn_model method,
therefore when the model is loaded and the predict method is called, all the tokens are mapped to the unknown token and the predicted values make no sens.
Suggestions :
Quick Fix : If you are in a rush, you can use the following code to save and load the model:
The NeuralModel save and load methods should be refactored (save all the relevant attributes and make load a class method) to fix the bug.
However, the current NeuralModel class is far from current Deep Learning standards (ex: PyTorch Lightning modules) and could be refactored entirely.
In particular, we could separate the model itself from the trainer class (Training attributes are unnecessary when doing inference).
The text was updated successfully, but these errors were encountered:
Python version :
3.6
Melusine version :
2.3.1
Operating System :
Mac
Steps to reproduce :
The code below does the following:
The predictions should be equal but they are different
Bug explanation :
When making a prediction (
NeuralModel.predict
), theNeuralModel
uses the attributevocabulary_dict
.This attribute is an empty dict at model initialization and it is filled when the model is fitted.
The
vocabulary_dict
attribute is not saved by thesave_nn_model
method,therefore when the model is loaded and the
predict
method is called, all the tokens are mapped to the unknown token and the predicted values make no sens.Suggestions :
Quick Fix : If you are in a rush, you can use the following code to save and load the model:
The
NeuralModel
save and load methods should be refactored (save all the relevant attributes and makeload
a class method) to fix the bug.However, the current NeuralModel class is far from current Deep Learning standards (ex: PyTorch Lightning modules) and could be refactored entirely.
In particular, we could separate the model itself from the trainer class (Training attributes are unnecessary when doing inference).
The text was updated successfully, but these errors were encountered: