Search code examples
machine-learningneural-networkpicklejoblib

How to save neural network model using joblib


I'm trying to save my neural_network model in pandas using joblib as it gives me 96% accuracy. My dataset has 9 columns - features to predict breast cancer.

y_train_categorical = to_categorical(y_train)
y_test_categorical = to_categorical(y_test)

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

neural_model = Sequential()
neural_model.add(Dense(units=6, activation='relu', input_dim=9))
neural_model.add(Dense(units=2, activation='softmax')) 

neural_model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

neural_model = neural_model.fit(
    X_train_scaled,
    y_train_categorical,
    epochs=200,
    shuffle=True,
    verbose=2
)

from sklearn.externals import joblib 

joblib.dump(neural_model, 'neural.pkl') 
# also tried dump(neural_model, 'neural.joblib')```

Error message: can't pickle _thread.RLock objects

Solution

  • It is not recommended to use pickle or cPickle to save a Keras model.

    You need just do: model.save(filepath)

    For more information, please, look into the documentation.