Keras 何时以及如何计算每批样本的指标?

问题描述 投票:0回答:1

我看到 Keras 自定义指标如何工作,并且指标函数中的

tf.print
model.fit
的回调打印之间的计算不匹配。

import tensorflow as tf  # tf2.4.1
import numpy as np
model = tf.keras.models.Sequential(
    tf.keras.layers.Dense(1, input_shape=(1,))
)
def my_metric_fn(y_true, y_pred):
    squared_difference = tf.square(y_true - y_pred)
    loss =  tf.reduce_mean(squared_difference, axis=-1)
    tf.print(y_true.shape, y_pred.shape, loss, tf.reduce_mean(squared_difference))
    return loss
model.compile(optimizer='adam', loss='mean_squared_error', metrics=[my_metric_fn])
x = np.random.rand(4,1)
y = x ** 2
history = model.fit(x=x, y=y, batch_size=2, epochs=2)
print(history.history)

输出(格式化以获得更好的可读性)

Epoch 1/2
TensorShape([2, 1]) TensorShape([2, 1]) [9.79962078e-06 0.0534314588] 0.02672063
1/2 [==============>...............] - ETA: 0s - loss: 0.0267 - my_metric_fn: 0.0267
TensorShape([2, 1]) TensorShape([2, 1]) [0.0397406667 0.179955378] 0.109848022
2/2 [==============================] - 0s 7ms/step - loss: 0.0544 - my_metric_fn: 0.0544

Epoch 2/2
TensorShape([2, 1]) TensorShape([2, 1]) [0.0392204635 0.0521505736] 0.0456855185
1/2 [==============>...............] - ETA: 0s - loss: 0.0457 - my_metric_fn: 0.0457
TensorShape([2, 1]) TensorShape([2, 1]) [0.177408844 2.45939535e-08] 0.088704437
2/2 [==============================] - 0s 5ms/step - loss: 0.0600 - my_metric_fn: 0.0600
{'loss': [0.06828432530164719, 0.06719497591257095], 'my_metric_fn': [0.06828432530164719, 0.06719497591257095]}

在上面的输出中查看批次的打印损失。

Epoch 1/2 1/2 tf.print:0.02672063,model.fit:0.0267。好的。
Epoch 1/2 2/2 tf.print:0.109848022,但 model.fit:0.0544。不行。

如何理解这些匹配和不匹配? 0.0544 从哪里来?

python tensorflow machine-learning keras deep-learning
1个回答
1
投票

在 keras 中,训练损失/指标在每个 epoch 结束时计算为每个批次中损失/指标的平均值。所以在你的情况下:

EPOCH 1: (0.02672063 + 0.109848022) / 2 = 0.068284326
EPOCH 2: (0.0456855185 + 0.088704437) / 2 = 0.06719497775

对应于:

history.history['loss'] ==> [0.06828432530164719, 0.06719497591257095]
© www.soinside.com 2019 - 2024. All rights reserved.