簡體   English   中英

如何在 Keras 中返回多輸出模型的回波損耗歷史?

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

我使用 Python 3.7 和 Keras 2.2.4。 我用兩個 output 層創建了一個 Keras model:

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

由於損失歷史每個時期只返回一個損失值,我想獲得每個 output 層的損失。 如何在每個時期獲得兩個損失值,每個 output 層一個?

Keras 中的每個 model 都有一個默認的History回調,它存儲所有時期的所有損失和度量值,包括聚合值以及每個 output 層。 此回調創建一個History object ,它在fit model 被調用時返回,您可以使用該 object 的history屬性訪問所有這些值(它實際上是一個字典):

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

一個最小的可重現示例:

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