本文介绍了为什么在TensorFlow Keras中损失函数和指标之间获得不同的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my CNN training using TensorFlow, I am using Keras.losses.poisson as a loss function. Now, I like to calculate many metrics alongside that loss function, and I am observing that Keras.metrics.poisson gives different results - although the two are the same function.

有关某些示例输出,请参见此处:losspoisson输出具有不同的范围,即0.5与0.12:

See here for some example output: loss and poisson outputs have different ranges, 0.5 vs. 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-损失和指标的计算方式不同?但是,我没有使用正则化.

I have found a similar questions while typing this one:Keras - Loss and Metric calculated differently?However, I am not using regularization.

此外,我遇到了这个问题,至少可以帮助我重现此问题:

In addition, I have come across this one, which at least helped me reproduce the issue: 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)

然后我发现基本上是引发此问题的维度.从下面的扩展示例中,您可以看到

What I have found then is that, basically, it's the dimensionality that triggers this issue. From the following extended example, you can see that

  • 这个问题可以用很多损失函数来重现(不是以mean_开头的)
  • tensorflow.keras替换为keras时,问题消失了,并且
  • 如果数据的维数大于3,则
  • tensorflow.keras 似乎可以按批大小缩放度量标准.至少那是我的拙劣解释.
  • the issue can be reproduced with many loss functions (the ones hat don't begin with mean_),
  • the issue goes away when replacing tensorflow.keras with keras, and
  • tensorflow.keras seems to scale the metrics by the batch size if the dimensionality of the data is larger than three. At least that is my humble interpretation.

代码:

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)

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

I find this behavior inconsistent at least. Should I consider it a bug or a feature?

更新:经过进一步调查,我发现度量值被正确计算,而损失值却没有被正确计算.实际上,损失是样本损失的加权总和,其中每个样本的权重是该样本所在批次的大小.这有两个含义:

Update: After further investigation, I have found out that the metrics values seen to be computed correctly, while the loss values are not; in fact, the losses are weighted sums of the sample losses, where the weighting of each sample is the size of the batch that sample is in. This has two implications:

  1. 如果批次大小除以样品数量,则所有样品的权重都相同,并且损失等于该批次大小的因数.
  2. 如果批次大小未除以样本数,则由于通常会对批次进行混洗,因此权重(因此计算得出的损失)会从一个时期变化到另一个时期,尽管没有其他变化.这也适用于MSE等指标.

以下代码证明了这些观点:

The following code proves these points:

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'之间似乎有所不同,如以下示例所示:

Update: Finally, there seems to be a difference between using keras.metrics.mse and 'mse', as this example shows:

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)

我开始相信这一定是一个错误,并且在这里报告了它.

I begin to believe that this must be a bug and reported it here.

推荐答案

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

This has been confirmed as a bug and fixed.For more information, see https://github.com/tensorflow/tensorflow/issues/25970.

这篇关于为什么在TensorFlow Keras中损失函数和指标之间获得不同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 16:11