gpt4 book ai didi

tensorflow - 为什么在TensorFlow Keras中损失函数和指标之间获得不同的值?

转载 作者:行者123 更新时间:2023-12-02 04:25:35 24 4
gpt4 key购买 nike

在使用TensorFlow进行CNN训练时,我使用Keras.losses.poisson作为损失函数。现在,我想与该损失函数一起计算许多指标,并且我观察到Keras.metrics.poisson给出的结果不同-尽管两者是相同的函数。

参见以下示例输出:losspoisson输出具有不同的范围,即0.5与0.12:

Epoch 1/20
Epoch 00001: val_loss improved from inf to 0.53228, saving model to P:\Data\xyz.h5
- 8174s - loss: 0.5085 - binary_crossentropy: 0.1252 - poisson: 0.1271 - mean_squared_error: 1.2530e-04 - mean_absolute_error: 0.0035 - mean_absolute_percentage_error: 38671.1055 - val_loss: 0.5323 - val_binary_crossentropy: 0.1305 - val_poisson: 0.1331 - val_mean_squared_error: 5.8477e-05 - val_mean_absolute_error: 0.0035 - val_mean_absolute_percentage_error: 1617.8346

Epoch 2/20
Epoch 00002: val_loss improved from 0.53228 to 0.53218, saving model to P:\Data\xyz.h5
- 8042s - loss: 0.5067 - binary_crossentropy: 0.1246 - poisson: 0.1267 - mean_squared_error: 1.0892e-05 - mean_absolute_error: 0.0017 - mean_absolute_percentage_error: 410.8044 - val_loss: 0.5322 - val_binary_crossentropy: 0.1304 - val_poisson: 0.1330 - val_mean_squared_error: 4.9087e-05 - val_mean_absolute_error: 0.0035 - val_mean_absolute_percentage_error: 545.5222

Epoch 3/20
Epoch 00003: val_loss improved from 0.53218 to 0.53199, saving model to P:\Data\xyz.h5
- 8038s - loss: 0.5066 - binary_crossentropy: 0.1246 - poisson: 0.1266 - mean_squared_error: 6.6870e-06 - mean_absolute_error: 0.0013 - mean_absolute_percentage_error: 298.9844 - val_loss: 0.5320 - val_binary_crossentropy: 0.1304 - val_poisson: 0.1330 - val_mean_squared_error: 4.3858e-05 - val_mean_absolute_error: 0.0031 - val_mean_absolute_percentage_error: 452.3541

我在输入此问题时发现了类似的问题:
Keras - Loss and Metric calculated differently?
但是,我没有使用正则化。

此外,我遇到了这一问题,至少可以帮助我重现此问题: Same function in Keras Loss and Metric give different values even without regularization
from tensorflow import keras

layer = keras.layers.Input(shape=(1, 1, 1))
model = keras.models.Model(inputs=layer, outputs=layer)
model.compile(optimizer='adam', loss='poisson', metrics=['poisson'])
data = [[[[[1]]], [[[2]]], [[[3]]]]]
model.fit(x=data, y=data, batch_size=2, verbose=1)

然后,我发现基本上是引发此问题的维度。从以下扩展示例中,您可以看到
  • 该问题可以通过许多损失函数(不是以mean_开头的函数)重现,
  • tensorflow.keras替换为keras
  • 时,问题消失了
    如果数据的维数大于3,则
  • tensorflow.keras似乎按批大小缩放指标。至少那是我的拙劣解释。

  • 代码:
    import numpy as np
    from tensorflow import keras
    # import keras

    nSamples = 98765
    nBatch = 2345

    metric = 'poisson'
    # metric = 'squared_hinge'
    # metric = 'logcosh'
    # metric = 'cosine_proximity'
    # metric = 'binary_crossentropy'

    # example data: always the same samples
    np.random.seed(0)
    dataIn = np.random.rand(nSamples)
    dataOut = np.random.rand(nSamples)

    for dataDim in range(1, 10):
    # reshape samples into size (1,), ..., (1, 1, ...) according to dataDim
    dataIn = np.expand_dims(dataIn, axis=-1)
    dataOut = np.expand_dims(dataOut, axis=-1)

    # build a model that does absolutely nothing
    Layer = keras.layers.Input(shape=np.ones(dataDim))
    model = keras.models.Model(inputs=Layer, outputs=Layer)

    # compile, fit and observe loss ratio
    model.compile(optimizer='adam', loss=metric, metrics=[metric])
    history = model.fit(x=dataIn, y=dataOut, batch_size=nBatch, verbose=1)
    lossRatio = history.history['loss'][0] / history.history[metric][0]
    print(lossRatio)

    我发现这种行为至少是不一致的。我应该认为它是错误还是功能?

    更新:经过进一步调查,我发现可以正确地计算出度量值,而损失值却没有得到正确计算。实际上,损失是样本损失的加权总和,其中每个样本的权重是该样本所在批次的大小。这有两个含义:
  • 如果批次大小除以样本数量,则所有样本的权重相同,并且损失等于该批次大小的因数。
  • 如果批次大小未除以样本数,则由于通常会对批次进行混洗,因此权重(因此计算得出的损失)会从一个时期变化到另一个时期,尽管没有其他变化。这也适用于MSE等指标。

  • 以下代码证明了这些观点:
    import numpy as np
    import tensorflow as tf
    from tensorflow import keras

    # metric = keras.metrics.poisson
    # metricName = 'poisson'
    metric = keras.metrics.mse
    metricName = 'mean_squared_error'

    nSamples = 3
    nBatchSize = 2

    dataIn = np.random.rand(nSamples, 1, 1, 1)
    dataOut = np.random.rand(nSamples, 1, 1, 1)

    tf.InteractiveSession()
    layer = keras.layers.Input(shape=(1, 1, 1))
    model = keras.models.Model(inputs=layer, outputs=layer)
    model.compile(optimizer='adam', loss=metric, metrics=[metric])

    h = model.fit(x=dataIn, y=dataOut, batch_size=nBatchSize, verbose=1, epochs=10)

    for (historyMetric, historyLoss) in zip(h.history[metricName], h.history['loss']):

    # the metric value is correct and can be reproduced in a number of ways

    kerasMetricOfData = metric(dataOut, dataIn).eval()
    averageMetric = np.mean(kerasMetricOfData)
    assert np.isclose(historyMetric, averageMetric), "..."

    flattenedMetric = metric(dataOut.flatten(), dataIn.flatten()).eval()
    assert np.isclose(historyMetric, flattenedMetric), "..."

    if metric == keras.metrics.poisson:
    numpyMetric = np.mean(dataIn - np.log(dataIn) * dataOut)
    assert np.isclose(historyMetric, numpyMetric), "..."

    # the loss value is incorrect by at least a scaling factor (~ batch size).
    # also varies *randomly* if the batch size does not divide the # of samples:

    if nSamples == 3:
    incorrectLoss = np.array([
    np.mean(kerasMetricOfData.flatten() * [1, nBatchSize, nBatchSize]),
    np.mean(kerasMetricOfData.flatten() * [nBatchSize, 1, nBatchSize]),
    np.mean(kerasMetricOfData.flatten() * [nBatchSize, nBatchSize, 1]),
    ])
    elif nSamples == 4:
    incorrectLoss = np.mean(kerasMetricOfData) * nBatchSize
    assert np.any(np.isclose(historyLoss, incorrectLoss)), "..."

    它输出:
    Epoch 1/10

    2/3 [===================>..........] - ETA: 0s - loss: 0.0044 - mean_squared_error: 0.0022
    3/3 [==============================] - 0s 5ms/sample - loss: 0.0099 - mean_squared_error: 0.0084
    Epoch 2/10

    2/3 [===================>..........] - ETA: 0s - loss: 0.0238 - mean_squared_error: 0.0119
    3/3 [==============================] - 0s 2ms/sample - loss: 0.0163 - mean_squared_error: 0.0084
    Epoch 3/10

    2/3 [===================>..........] - ETA: 0s - loss: 0.0238 - mean_squared_error: 0.0119
    3/3 [==============================] - 0s 2ms/sample - loss: 0.0163 - mean_squared_error: 0.0084
    Epoch 4/10

    2/3 [===================>..........] - ETA: 0s - loss: 0.0238 - mean_squared_error: 0.0119
    3/3 [==============================] - 0s 2ms/sample - loss: 0.0163 - mean_squared_error: 0.0084
    Epoch 5/10

    2/3 [===================>..........] - ETA: 0s - loss: 0.0238 - mean_squared_error: 0.0119
    3/3 [==============================] - 0s 2ms/sample - loss: 0.0163 - mean_squared_error: 0.0084
    Epoch 6/10

    2/3 [===================>..........] - ETA: 0s - loss: 0.0222 - mean_squared_error: 0.0111
    3/3 [==============================] - 0s 2ms/sample - loss: 0.0158 - mean_squared_error: 0.0084
    Epoch 7/10

    2/3 [===================>..........] - ETA: 0s - loss: 0.0222 - mean_squared_error: 0.0111
    3/3 [==============================] - 0s 2ms/sample - loss: 0.0158 - mean_squared_error: 0.0084
    Epoch 8/10

    2/3 [===================>..........] - ETA: 0s - loss: 0.0238 - mean_squared_error: 0.0119
    3/3 [==============================] - 0s 2ms/sample - loss: 0.0163 - mean_squared_error: 0.0084
    Epoch 9/10

    2/3 [===================>..........] - ETA: 0s - loss: 0.0222 - mean_squared_error: 0.0111
    3/3 [==============================] - 0s 2ms/sample - loss: 0.0158 - mean_squared_error: 0.0084
    Epoch 10/10

    2/3 [===================>..........] - ETA: 0s - loss: 0.0044 - mean_squared_error: 0.0022
    3/3 [==============================] - 0s 2ms/sample - loss: 0.0099 - mean_squared_error: 0.0084

    更新:最后,使用 keras.metrics.mse'mse'似乎有所不同,如以下示例所示:
    import numpy as np
    from tensorflow import keras

    # these three reproduce the issue:
    # metric = keras.metrics.poisson
    # metric = 'poisson'
    # metric = keras.metrics.mse

    # this one does not:
    metric = 'mse'

    nSamples = 3
    nBatchSize = 2

    dataIn = np.random.rand(nSamples, 1, 1, 1)
    dataOut = np.random.rand(nSamples, 1, 1, 1)

    layer = keras.layers.Input(shape=(1, 1, 1))
    model = keras.models.Model(inputs=layer, outputs=layer)
    model.compile(optimizer='adam', loss=metric, metrics=[metric])
    model.fit(x=dataIn, y=dataOut, batch_size=2, verbose=1, epochs=10)

    我开始相信这一定是bug和 reported it here

    最佳答案

    这已被确认为错误并已修复。
    有关更多信息,请参见https://github.com/tensorflow/tensorflow/issues/25970

    关于tensorflow - 为什么在TensorFlow Keras中损失函数和指标之间获得不同的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54802328/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com