访问列表字典中列表的最小元素

Accessing the least element of list in dictionary of lists

keras model.fit(..) returns 一个回调历史对象,我们可以从中检索所有时期的模型指标日志的历史,即:

hist = model.fit(..)
off_history = hist.history

这样:

pprint(off_history)
{'accuracy': [0.5884908437728882,
              0.6860442757606506,
              0.7116397619247437,
              0.7207977771759033,
              0.7276809811592102],
 'loss': [1.5214883089065552,
          0.9418172836303711,
          0.8826016187667847,
          0.8565425276756287,
          0.8384354114532471],
 'val_accuracy': [0.569063663482666,
                  0.6021720170974731,
                  0.6034634709358215,
                  0.6066920757293701,
                  0.607513964176178],
 'val_loss': [1.3138036727905273,
              1.2316004037857056,
              1.201686143875122,
              1.1939105987548828,
              1.1881093978881836]}

我有兴趣检索所有模型拟合的最小值 val_loss。我知道我能做到:

off_history['val_loss'][-1]

但是因为我不确定 keras 是否真的 returns 一直都是 accuracy, loss, val_accuracy... 的排序列表,所以我不能依赖这个操作。

那么在所有 model.fit() 的情况下获得最小值 val_loss 的最佳方法是什么?

如果您需要准确的输出,那么您可以尝试使用 for 循环

for k,v in off_history.items():
    if k == 'val_loss':
        print(v[-1])

这给出:

1.1881093978881836

Keras fit() returns 训练日志的历史对象。 所以如果你设置:

hist = model.fit()

此历史对象包含模型训练的所有 epoch 日志:

type(history.epoch) #epochs list [0,1,2,,...n_epochs]
list

因此,您引用的 hist.history 字典包含上面列表中 history.epoch 中每个时期的相应指标值。

为了回答您的问题,keras fit() returns 那些按时期 运行 排序的指标。 1,2,3,4,5....

解决您的目标的一种方法是使用 numpymin 函数,如下所示:

validation_loss = np.min(hist.history['val_loss'])

检索所有 keras 时期的最小验证损失 fit()