【问题标题】:How to save neural network model using joblib如何使用 joblib 保存神经网络模型
【发布时间】:2019-08-04 22:57:05
【问题描述】:

我正在尝试使用 joblib 将我的神经网络模型保存在 pandas 中,因为它可以提供 96% 的准确率。我的数据集有 9 列 - 预测乳腺癌的特征。

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

【问题讨论】:

    标签: machine-learning neural-network pickle joblib


    【解决方案1】:

    不建议使用 pickle 或 cPickle 来保存 Keras 模型。

    你只需要这样做:model.save(filepath)

    如需了解更多信息,请查看documentation

    【讨论】:

      【解决方案2】:

      完全没有必要使用 joblib 来保存您的 keras 模型。使用 .save() 方法,它应该可以正常工作。

      保存模型:

      neural_model.save('path/to/location')
      

      重新加载模型:

      neural_model = model = keras.models.load_model('path/to/location')
      

      请参阅 tensor-flow 团队的本指南: https://www.tensorflow.org/guide/keras/save_and_serialize

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-07-20
        • 1970-01-01
        • 2021-07-25
        • 2020-07-20
        • 2012-08-05
        • 2012-10-18
        • 2015-06-25
        相关资源
        最近更新 更多