How can I get the a keras models' history after loading it from a file in Python?

前端 未结 2 524
一整个雨季
一整个雨季 2020-12-14 22:28

I have saved a keras module as a h5py file and am loading it from disk.

When training the model I use:

from keras.models im         


        
相关标签:
2条回答
  • 2020-12-14 23:19

    Using pickle to save the history object threw a whole host of errors. As it turns out you can instead use pickle on H.history instead of H to save your history file!

    Kind of annoying having to have a model and a history file saved, but whatever

    0 讨论(0)
  • 2020-12-14 23:26

    Unfortunately it seems that Keras hasn't implemented the possibility of loading the history directly from a loaded model. Instead you have to set it up in advance. This is how I solved it using CSVLogger (it's actually very convenient storing the entire training history in a separate file. This way you can always come back later and plot whatever history you want instead of being dependent on a variable you can easily lose stored in the RAM):

    First we have to set up the logger before initiating the training.

    from keras.callbacks import CSVLogger
    
    csv_logger = CSVLogger('training.log', separator=',', append=False)
    model.fit(X_train, Y_train, callbacks=[csv_logger])
    

    The entire log history will now be stored in the file 'training.log' (the same information you would get, by in your case, calling H.history). When the training is finished, the next step would simply be to load the data stored in this file. You can do that with pandas read_csv:

    import pandas as pd
    log_data = pd.read_csv('training.log', sep=',', engine='python')
    

    From heron you can treat the data stored in csv_logger just as you would by loading it from K.history.

    More information in Keras callbacks docs.

    0 讨论(0)
提交回复
热议问题