簡體   English   中英

在Keras中重置模型的所有權重和偏差(訓練后恢復模型)

[英]Reset all weights and biases of the model in Keras (restore the model after training)

假設我有這樣的事情。

model = Sequential()
model.add(LSTM(units = 10 input_shape = (x1, x2)))
model.add(Activation('tanh'))
model.compile(optimizer = 'adam', loss = 'mse')

## Step 1.
model.fit(X_train, Y_train, epochs = 10)

訓練模型后,我想重設模型中的所有內容(權重和偏差)。 所以我想在compile函數后還原模型(步驟1)。 在Keras中最快的方法是什么?

是否最快是一個io.BytesIO ,但是它很簡單,並且可能適合您的情況:序列化初始權重,然后在必要時進行反序列化,並使用類似io.BytesIO來避免磁盤I / O命中(然后必須清理):

from io import BytesIO

model = Sequential()
model.add(LSTM(units = 10, input_shape = (x1, x2)))
model.add(Activation('tanh'))
model.compile(optimizer = 'adam', loss = 'mse')
f = BytesIO()
model.save_weights(f)  # Stores the weights
model.fit(X_train, Y_train, epochs = 10)
# [Do whatever you want with your trained model here]
model.load_weights(f)  # Resets the weights

暫無
暫無

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

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