Closed ghost closed 5 years ago
My guess (I'm not related to the development of Talos) is that you can recover the Keras model from the Talos one doing this:
First, you get the index of the best model (given by Talos) using:
t = ta.Scan(...)
best_idx = t.best_model(metric='loss', asc=True) # Change the configurations accordingly to your case
Then transform it back to a Keras model using:
from talos.utils.best_model import activate_model
Talos_Model = activate_model(t, best_idx)
This way Talos_Model
will be the Keras model corresponding to the best one trained by Talos and you can do whatever you want with it (I use Talos_Model.predict(X)
to make my predictions).
You can also change the best_idx
to any model that Talos has trained, in case you want to check the other trained models.
Thank you! I get how I would retrieve the best model, however when I try to do so I get the following error:
best_idx = t.best_model(metric='mean_absolute_error', asc=True)
Talos_Model = activate_model(t, best_idx)
Traceback (most recent call last):
File "/home/emiel/.local/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3296, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-13-2e176e3c01dd>", line 3, in <module>
Talos_Model = activate_model(t, best_idx)
File "/home/emiel/.local/lib/python3.7/site-packages/talos/utils/best_model.py", line 20, in activate_model
model = model_from_json(self.saved_models[model_id])
TypeError: list indices must be integers or slices, not Sequential
Did you encounter this as well? Let me know if you need more information!
The error you get means that best_idx
is actually the model, not the index of it. My mistake, sorry. So you can just do
best_model = t.best_model(metric='mean_absolute_error', asc=True)
best_model.predict(X)
If you want to check other models, you can do the following:
# Make a report on the scan
r = ta.Reporting(t)
# Show the models
display(r.data)
This will output a Pandas DataFrame with all the models and the metrics, so you can choose one of them and use the previous code:
Talos_Model = activate_model(t, best_idx)
And predict normally. An advantage of r.data being a Pandas DataFrame is that you can do operations with it, for example:
r.data.sort_values('loss')
This will sort the results table by loss, in ascending order, very useful if you want to compare models.
Edit: I forgot to mention another cool tip: you can get the index of the model that satisfies a condition like so:
column = 'loss'
display(r.data.loc[r.data[column] == np.min(r.data[column])])
In this case it will print the model with the lowest loss. You can change the np.min to np.max to get the opposite. To get the index only, simply do:
r.data.loc[r.data[column] == np.min(r.data[column])].index.values[0]
After evaluating the best model, is there a way to get it back to a normal keras model? I tried importing the files from Restore() but that h5 file is not a complete one, so it failed.