简体   繁体   English

如何在 Keras 中返回多输出模型的回波损耗历史?

[英]How to return loss history of multi-output models in Keras?

I use Python 3.7 and Keras 2.2.4.我使用 Python 3.7 和 Keras 2.2.4。 I created a Keras model with two output layers:我用两个 output 层创建了一个 Keras model:

self.df_model = Model(inputs=input, outputs=[out1,out2])

As the loss history only returns one loss value per epoch, I want to get the loss of each output layer.由于损失历史每个时期只返回一个损失值,我想获得每个 output 层的损失。 How is it possible to get two loss values per epoch, one for each output layer?如何在每个时期获得两个损失值,每个 output 层一个?

Each model in Keras has a default History callback which stores all the loss and metric values of all the epochs, both the aggregate values as well as per output layer. Keras 中的每个 model 都有一个默认的History回调,它存储所有时期的所有损失和度量值,包括聚合值以及每个 output 层。 This callback creates a History object which is returned when fit model is called and you can access all of these values by using the history property of that object (it is actually a dictionary):此回调创建一个History object ,它在fit model 被调用时返回,您可以使用该 object 的history属性访问所有这些值(它实际上是一个字典):

history = model.fit(...)
print(history.history)  # <-- a dict which contains all the loss and metric values per epoch

A minimal reproducible example:一个最小的可重现示例:

from keras import layers
from keras import Model
import numpy as np

inp = layers.Input((1,))
out1 = layers.Dense(2, name="output1")(inp)
out2 = layers.Dense(3, name="output2")(inp)

model = Model(inp, [out1, out2])
model.compile(loss='mse', optimizer='adam')

x = np.random.rand(2, 1)
y1 = np.random.rand(2, 2)
y2 = np.random.rand(2, 3)
history = model.fit(x, [y1,y2], epochs=5)

print(history.history)

#{'loss': [1.0881365537643433, 1.084699034690857, 1.081269383430481, 1.0781562328338623, 1.0747418403625488],
# 'output1_loss': [0.87154925, 0.8690172, 0.86648905, 0.8641926, 0.8616721],
# 'output2_loss': [0.21658726, 0.21568182, 0.2147803, 0.21396361, 0.2130697]}

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

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