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)
通过这种方式,我将历史保存为字典,以防我想稍后绘制损失或准确性.
小智 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)
Mar*_*jko 10
甲history
对象具有history
字段是helds跨越每个训练时期跨越不同的训练指标的字典.因此,例如,history.history['loss'][99]
将在第100个训练时期中丢失您的模型.为了节省您可以使用pickle
此词典或简单将此词典中的不同列表保存到适当的文件中.
我遇到了 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 文件应该保存的路径,以及从 kerasfit
或fit_generator
方法返回的历史对象。
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)
归档时间: |
|
查看次数: |
28975 次 |
最近记录: |