Fin M.
Fin M.

Reputation: 179

Keras Tuner Save Best Model

I am currently attempting to used keras tuner to create a model for my CNN, though I am having some issues with saving my model for future use.

As I am used to it, I could regularly just save my model with model.save(filename) to receive a .model file; however, when attempting this with code such as:

tuner = RandomSearch(
    build_model,
    objective = "val_accuracy",
    max_trials = 5,
    executions_per_trial = 1,
    directory = LOG_DIR
)

tuner.search(x= x_train, y= y_train, epochs= 1, batch_size=64, validation_data= (x_test, y_test))

bestModels = tuner.get_best_models(num_models=1)
highestScoreModel= models[0]

highestScoreModel.fit(x=x_train, y=y_train, batch_size=64, epochs=5, verbose=1, validation_split=0.2)

highestScoreModel.save("Trained_Model")

I received a Trained_Model folder with no model within it, only the parameters. If anyone could assist me with saving the actual trained model I'd be very thankful.

================= Edit / Update ================

I have now found a way to obtain the trial_id by sifting the generated trial files. Though, when I run:

trial_id = getID()
tuner.save_model(trial_id=trial_id, model=highestScoreModel, step=0)

Nothing seems to happen, no save file appears. Again, I would be grateful for any help on this matter.

Upvotes: 1

Views: 3383

Answers (1)

Nikhil Thakre
Nikhil Thakre

Reputation: 11

Try this

Tuner.save_model(trial_id, model, step=0)

where

  • trial_id is your model trial number
  • model is your trained model
  • and step is epochs numbers

Upvotes: 1

Related Questions