Suppose that I have something like this.
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)
After training the model, I want to reset everything (weights and biases) in the model. So I want to restore the model after compile
function (Step 1). What is the fastest way to that in Keras?
Whether it's the fastest is probably up in the air, but it's certainly straightforward and might be good enough for your case: Serialize the initial weights, then deserialize when necessary, and use something like io.BytesIO
to avoid the disk I/O hit (and having to clean up afterwards):
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.