Search code examples
pythonscikit-learndeep-learningneural-network

How do I pickle my neural net prediction models, so that i don't have to re-train them everytime?


As the title says, I want to pickle my NNs, but I get a TypeError

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_16896/2912615623.py in <module>
      5 
      6 with open('lstm_model.pkl', 'wb') as file:
----> 7     pickle.dump(mod, file)

TypeError: cannot pickle 'weakref' object

I tried the solutions from this SO question, but i got the same error.
btw the model is LSTM from the sklearn lib

EDIT:
Sorry, as mentioned by @Ben Reiniger LSTM is from keras.layer

Here's the reproducible code.

model = Sequential()
model.add(LSTM(50, return_sequences=True, input_shape = (x_train.shape[1], 1)))
model.add(LSTM(50, return_sequences = False))
model.add(Dense(25))
model.add(Dense(1))

model.compile(optimizer="adam", loss='mean_squared_error')

model.fit(x_train, y_train, batch_size = 1, epochs = 1)


# Saving the model
import pickle

mod = {'Model': model}

with open('lstm_model.pkl', 'wb') as file:
    pickle.dump(mod, file)

The error i get


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_16896/2912615623.py in <module>
      5 
      6 with open('lstm_model.pkl', 'wb') as file:
----> 7     pickle.dump(mod, file)

TypeError: cannot pickle 'weakref' object


Solution

  • Keras has a save method described in the docs. They state that

    it is not recommended to use pickle or cPickle to save a Keras model

    Therefore, do:

    Saving:

    model.save('your/path')
    

    Loading:

    model = keras.models.load_model('your/path')