簡體   English   中英

如何腌制我的神經網絡預測模型,這樣我就不必每次都重新訓練它們?

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

正如標題所說,我想腌制我的神經網絡,但我得到一個 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

我嘗試了這個 SO question的解決方案,但我得到了同樣的錯誤。
順便說一句,model 是來自 sklearn 庫的 LSTM

編輯:
抱歉,正如@Ben Reiniger 所說,LSTM 來自keras.layer

這是可重現的代碼。

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)

我得到的錯誤


---------------------------------------------------------------------------
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

Keras 具有文檔中描述的save方法。 他們 state 那

不建議使用 pickle 或 cPickle 來保存 Keras model

因此,請執行以下操作:

保存:

model.save('your/path')

加載:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM