Reputation: 3783
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?
Upvotes: 1
Views: 2652
Reputation: 18201
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
Upvotes: 4