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

pes8fvy9  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(67)

我看到Keras自定义指标如何工作,指标函数中的tf.printmodel.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,and model.fit:0.0267. OK.
Epoch 1/2 2/2 tf.print:0.109848022,but model.fit:0.0544. Not OK.
我如何理解这些匹配和不匹配?0.0544从何而来?

von4xj4u

von4xj4u1#

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

相关问题