keras:如何保存培训历史记录

jin*_*imo 39 python machine-learning neural-network deep-learning keras

在Keras,我们可以将输出返回model.fit到历史记录,如下所示:

 history = model.fit(X_train, y_train, 
                     batch_size=batch_size, 
                     nb_epoch=nb_epoch,
                     validation_data=(X_test, y_test))
Run Code Online (Sandbox Code Playgroud)

现在,如何将历史记录保存到文件中以供进一步使用(例如,绘制针对时期的acc或loss的绘制图)?

AEn*_*drs 45

我使用的是以下内容:

    with open('/trainHistoryDict', 'wb') as file_pi:
        pickle.dump(history.history, file_pi)
Run Code Online (Sandbox Code Playgroud)

通过这种方式,我将历史保存为字典,以防我想稍后绘制损失或准确性.

  • 您可以使用 [pickle.load](https://docs.python.org/3/library/pickle.html#pickle.load) 加载导出的文件。例如,`history = pickle.load(open('/trainHistoryDict'), "rb")` (9认同)
  • 只是好奇,为什么JSON格式无法在这里使用?与二进制pickle文件不同,它是一个直接的文本文件,可以在Python之外轻松读取(也许JSON格式会导致更大的文件) (4认同)
  • @ArturoMoncada-Torres,您的代码片段中有一个括号`)`在错误的位置结束。它应该是这样的:`history = pickle.load(open('/trainHistoryDict', "rb"))`。不过是小问题。 (4认同)
  • 现在如何加载导出的文件? (2认同)

小智 20

最简单的方法:

保存:

np.save('my_history.npy',history.history)
Run Code Online (Sandbox Code Playgroud)

加载:

history=np.load('my_history.npy',allow_pickle='TRUE').item()
Run Code Online (Sandbox Code Playgroud)

那么历史就是一本字典,您可以使用键检索所有想要的值。


Ash*_*man 12

model历史可以如下保存到一个文件

import json
hist = model.fit(X_train, y_train, epochs=5, batch_size=batch_size,validation_split=0.1)
with open('file.json', 'w') as f:
    json.dump(hist.history, f)
Run Code Online (Sandbox Code Playgroud)

  • 这在tensorflow keras中不再起作用。我遇到以下问题:TypeError:“ float32”类型的对象不可JSON序列化。我不得不使用json.dump(str(hist.history,f))。 (4认同)

Mar*_*jko 10

history对象具有history字段是helds跨越每个训练时期跨越不同的训练指标的字典.因此,例如,history.history['loss'][99]将在第100个训练时期中丢失您的模型.为了节省您可以使用pickle此词典或简单将此词典中的不同列表保存到适当的文件中.


Kev*_*n91 7

我遇到了 keras 列表中的值不是 json 可序列化的问题。因此,我为我的使用原因编写了这两个方便的函数。

import json,codecs
import numpy as np
def saveHist(path,history):
    
    new_hist = {}
    for key in list(history.history.keys()):
        new_hist[key]=history.history[key]
        if type(history.history[key]) == np.ndarray:
            new_hist[key] = history.history[key].tolist()
        elif type(history.history[key]) == list:
           if  type(history.history[key][0]) == np.float64:
               new_hist[key] = list(map(float, history.history[key]))
            
    print(new_hist)
    with codecs.open(path, 'w', encoding='utf-8') as file:
        json.dump(new_hist, file, separators=(',', ':'), sort_keys=True, indent=4) 

def loadHist(path):
    with codecs.open(path, 'r', encoding='utf-8') as file:
        n = json.loads(file.read())
    return n
Run Code Online (Sandbox Code Playgroud)

其中 saveHist 只需要获取 json 文件应该保存的路径,以及从 kerasfitfit_generator方法返回的历史对象。


s.k*_*s.k 7

An other way to do this:

As history.history is a dict, you can convert it as well to a pandas DataFrame object, which can then be saved to suit your needs.

Step by step:

import pandas as pd

# assuming you stored your model.fit results in a 'history' variable:
history = model.fit(x_train, y_train, epochs=10)

# convert the history.history dict to a pandas DataFrame:     
hist_df = pd.DataFrame(history.history) 

# save to json:  
hist_json_file = 'history.json' 
with open(hist_json_file, mode='w') as f:
    hist_df.to_json(f)

# or save to csv: 
hist_csv_file = 'history.csv'
with open(hist_csv_file, mode='w') as f:
    hist_df.to_csv(f)
Run Code Online (Sandbox Code Playgroud)