Python keras:如何保存历史对象的训练历史属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41061457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:24:06  来源:igfitidea点击:

keras: how to save the training history attribute of the history object

pythonmachine-learningneural-networkdeep-learningkeras

提问by jingweimo

In Keras, we can return the output of model.fitto a history as follows:

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

 history = model.fit(X_train, y_train, 
                     batch_size=batch_size, 
                     nb_epoch=nb_epoch,
                     validation_data=(X_test, y_test))

Now, how to save the history attribute of the history object to a file for further uses (e.g. draw plots of acc or loss against epochs)?

现在,如何将 history 对象的 history 属性保存到文件中以供进一步使用(例如,针对 epoch 绘制 acc 或 loss 的图)?

回答by AEndrs

What I use is the following:

我使用的是以下内容:

    with open('/trainHistoryDict', 'wb') as file_pi:
        pickle.dump(history.history, file_pi)

In this way I save the history as a dictionary in case I want to plot the loss or accuracy later on.

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

回答by s.k

An other way to do this:

另一种方法来做到这一点:

As history.historyis a dict, you can convert it as well to a pandasDataFrameobject, which can then be saved to suit your needs.

由于history.historydict,你可以把它转换以及一个pandasDataFrame对象,然后可以保存,以满足您的需求。

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)

回答by Ashok Kumar Jayaraman

The modelhistory can be saved into a file as follows

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)

回答by Marcin Mo?ejko

A historyobjects has a historyfield is a dictionary which helds different training metrics spanned across every training epoch. So e.g. history.history['loss'][99]will return a loss of your model in a 100th epoch of training. In order to save that you could picklethis dictionary or simple save different lists from this dictionary to appropriate file.

一个history对象有一个history字段是一个字典,它包含跨越每个训练时期的不同训练指标。因此,例如history.history['loss'][99]将在第 100 个训练时期返回模型的损失。为了保存,您可以pickle将此字典或简单地将来自此字典的不同列表保存到适当的文件中。

回答by sobhan hemati

The easiest way:

最简单的方法:

Saving:

np.save('my_history.npy',history.history)

Loading:

history=np.load('my_history.npy',allow_pickle='TRUE').item()

保存:

np.save('my_history.npy',history.history)

加载:

history=np.load('my_history.npy',allow_pickle='TRUE').item()

Then history is a dictionary and you can retrieve all desirable values using the keys.

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

回答by Kev1n91

I came across the problem that the values inside of the list in keras are not json seriazable. Therefore I wrote this two handy functions for my use cause.

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

import json,codecs
import numpy as np
def saveHist(path,history):

    new_hist = {}
    for key in list(history.history.keys()):
        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

where saveHist just needs to get the path to where the json file should be saved, and the history object returned from the keras fitor fit_generatormethod.

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

回答by Mark Cramer

I'm sure there are many ways to do this, but I fiddled around and came up with a version of my own.

我敢肯定有很多方法可以做到这一点,但我摸索着想出了一个我自己的版本。

First, a custom callback enables grabbing and updating the history at the end of every epoch. In there I also have a callback to save the model. Both of these are handy because if you crash, or shutdown, you can pick up training at the last completed epoch.

首先,自定义回调可以在每个 epoch 结束时获取和更新历史记录。在那里我还有一个回调来保存模型。这两个都很方便,因为如果您崩溃或关机,您可以在最后一个完成的 epoch 中继续训练。

class LossHistory(Callback):

    # https://stackoverflow.com/a/53653154/852795
    def on_epoch_end(self, epoch, logs = None):
        new_history = {}
        for k, v in logs.items(): # compile new history from logs
            new_history[k] = [v] # convert values into lists
        current_history = loadHist(history_filename) # load history from current training
        current_history = appendHist(current_history, new_history) # append the logs
        saveHist(history_filename, current_history) # save history from current training

model_checkpoint = ModelCheckpoint(model_filename, verbose = 0, period = 1)
history_checkpoint = LossHistory()
callbacks_list = [model_checkpoint, history_checkpoint]

Second, here are some 'helper' functions to do exactly the things that they say they do. These are all called from the LossHistory()callback.

其次,这里有一些“辅助”功能,可以完全按照他们所说的去做。这些都是从LossHistory()回调中调用的。

# https://stackoverflow.com/a/54092401/852795
import json, codecs

def saveHist(path, history):
    with codecs.open(path, 'w', encoding='utf-8') as f:
        json.dump(history, f, separators=(',', ':'), sort_keys=True, indent=4) 

def loadHist(path):
    n = {} # set history to empty
    if os.path.exists(path): # reload history if it exists
        with codecs.open(path, 'r', encoding='utf-8') as f:
            n = json.loads(f.read())
    return n

def appendHist(h1, h2):
    if h1 == {}:
        return h2
    else:
        dest = {}
        for key, value in h1.items():
            dest[key] = value + h2[key]
        return dest

After that, all you need is to set history_filenameto something like data/model-history.json, as well as set model_filesnameto something like data/model.h5. One final tweak to make sure not to mess up your history at the end of training, assuming you stop and start, as well as stick in the callbacks, is to do this:

之后,您只需要设置history_filename为类似data/model-history.json,以及设置model_filesname为类似data/model.h5. 确保在训练结束时不会弄乱你的历史的最后一个调整,假设你停止和开始,以及坚持回调,是这样做的:

new_history = model.fit(X_train, y_train, 
                     batch_size = batch_size, 
                     nb_epoch = nb_epoch,
                     validation_data=(X_test, y_test),
                     callbacks=callbacks_list)

history = appendHist(history, new_history.history)

Whenever you want, history = loadHist(history_filename)gets your history back.

只要你愿意,history = loadHist(history_filename)就可以找回你的历史。

The funkiness comes from the json and the lists but I wasn't able to get it to work without converting it by iterating. Anyway, I know that this works because I've been cranking on it for days now. The pickled.dumpanswer at https://stackoverflow.com/a/44674337/852795might be better, but I don't know what that is. If I missed anything here or you can't get it to work, let me know.

时髦来自 json 和列表,但我无法在不通过迭代转换的情况下使其工作。无论如何,我知道这很有效,因为我已经研究它好几天了。https://stackoverflow.com/a/44674337/852795 上pickled.dump答案可能更好,但我不知道那是什么。如果我在这里遗漏了任何内容或者您无法使用它,请告诉我。