LoginSignup
0
2

More than 3 years have passed since last update.

Kerasの損失関数履歴をデータフレームに変換する

Posted at

Kerasの損失関数の履歴が欲しい

 自分の作ったモデルがどのように収束していくかは非常に大事な情報です。
そこで、Kerasの損失関数(などの)履歴をファイルに残すために、Pandasのデータフレームに変換するスクリプトを紹介します

history_callback = model.fit(x_train, y_train, ....)
loss_history = history_callback.history

これで履歴が記録されます

loss_history #dict型
list_losss_history = list(loss_history.items()) #listへ変換
print(type(loss_history),type(list_losss_history))
print(list_losss_history)

出力

<class 'dict'> <class 'list'>
[('loss', [0.5525253211575696, 0.4036650247471646, 0.36141999579291056, 0.33595747268301807, 0.3177382633604555, 0.29891023940306444, 0.2881778943508099, 0.27792962343876176, 0.26560747584624167, 0.25740950032279025]), ('val_acc', [0.8358461538461538, 0.8772307692307693, 0.8713846153846154, 0.8776923076923077, 0.8813846153846154, 0.8852307692307693, 0.8812307692307693, 0.8816923076923077, 0.8872307692307693, 0.9003076923076923]), ('val_loss', [0.4148623236967967, 0.35430374641487233, 0.3401262025305858, 0.33813549585525804, 0.3276629987571102, 0.30808376993124303, 0.32132247155217025, 0.340628393916843, 0.30153624791250777, 0.2850437596107905]), ('acc', [0.8024273504273505, 0.8550427350427351, 0.8704615384615385, 0.8781880341880342, 0.8857264957264958, 0.8917264957264958, 0.8942222222222223, 0.8978803418803418, 0.9025982905982906, 0.9046666666666666])]

変換するスクリプト

list2D=[]
colname=[]
for i in range(len(list_losss_history)):
    list2D.append(list_losss_history[i][1])#単純にリストに追加していく
    colname.append(list_losss_history[i][0])
hist_data = pd.DataFrame(np.array(list2D).T,columns=colname)
print(hist_data)

出力

       loss   val_acc  val_loss       acc
0  0.552525  0.835846  0.414862  0.802427
1  0.403665  0.877231  0.354304  0.855043
2  0.361420  0.871385  0.340126  0.870462
3  0.335957  0.877692  0.338135  0.878188
4  0.317738  0.881385  0.327663  0.885726
5  0.298910  0.885231  0.308084  0.891726
6  0.288178  0.881231  0.321322  0.894222
7  0.277930  0.881692  0.340628  0.897880
8  0.265607  0.887231  0.301536  0.902598
9  0.257410  0.900308  0.285044  0.904667

あとは、ファイルにセーブすればOKです。

参考URL

https://codeday.me/jp/qa/20190301/346082.html
python – Kerasの損失出力をファイルに記録する方法
Lossのみの結果を取り出す方法が書いてあります

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2