December 20, 2022, 5:10am
For my task I need to save the best model, the best model I found by sweeping. Once I have the best model, I want to use its configuration (and everything) to train it again but without its weigths; it should begin untrained.
If I then use model.fit with the loaded model. Could I let it know what batch size, learning rate and optimizer was used to train the loaded model? How?
You can use the
get_config() method which returns a dictionary containing the configuration of the model you trained earlier.
model_config = best_model.get_config().
model_config Create a dictionary that contains the configuration of the model. You can save this dictionary to a file using the
To use the saved configuration to create a new untrained model with the same hyperparameters, you can use the
from_config() like below.
from tensorflow.keras.models import Sequential
new_model = Sequential.from_config(model_config)
Once you have created the new model, you can compile it with the same hyperparameters as the original model using the
new_model.compile(optimizer=' ', loss=' ', metrics=[' '])
Note that you need to specify the same optimizer and loss function as used in the original model. You can also specify the same metrics to evaluate the model’s performance during training.
Now finally, you can train the new untrained model using
model.fit() , and pass in the training data and validation data.
new_model.fit(x_train, y_train, batch_size='your choice' , epochs='your choice', learning_rate='your choice' , optimizer=' your choice' , validation_data=(x_val, y_val))
Please let me know if it helps you.