简体   繁体   English

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

[英]Accessing the least element of list in dictionary of lists

The keras model.fit(..) returns a callback history object from which we can retrieve a history of model's metrics logs for all epochs ie: keras model.fit(..)返回一个回调历史 object ,我们可以从中检索所有时期的模型指标日志的历史,即:

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

So that:以便:

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]}

I am interested in retrieving the minimum val_loss for all model fit.我有兴趣检索所有 model 拟合的最小val_loss I know that I can do:我知道我可以做到:

off_history['val_loss'][-1]

But since I'm not sure if keras actually returns a sorted list for accuracy, loss, val_accuracy... all the time, I cannot rely on this operation.但是由于我不确定 keras 是否一直返回一个accuracy, loss, val_accuracy...的排序列表,所以我不能依赖这个操作。

What then is the best way to get the min val_loss in all cases of model.fit() ?那么在model.fit()的所有情况下获得最小val_loss的最佳方法是什么?

Keras fit() returns a history object of training logs. Keras fit()返回训练日志的历史 object。 So if you set:所以如果你设置:

hist = model.fit()

This history object contains all epoch logs of model training:这段历史 object 包含 model 训练的所有epoch日志:

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

So the hist.history dictionary you refers to, contains the corresponding metrics values for each epoch in history.epoch in the list above.因此,您引用的hist.history字典包含上面列表中history.epoch中每个时期的相应指标值。

To answer your question, keras fit() returns those metrics in the order of epochs run.为了回答您的问题,keras fit()按 epoch 运行的顺序返回这些指标。 1,2,3,4,5.... 1,2,3,4,5....

A way around your goal would be to use numpy 's min function, like this:解决您的目标的一种方法是使用numpymin function,如下所示:

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

to retrieve minimum validation loss across all epochs of keras fit()检索 keras fit()的所有时期的最小验证损失

If you need the exact output then you can try using for loop如果您需要确切的 output 那么您可以尝试使用 for 循环

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

This gives:这给出了:

1.1881093978881836

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM