训练 CNN 时用于大输入的 NaN 中的 Tensorflow 熵

Tensorflow entropy in NaN for large inputs when training CNN

我用 TensorFlow 创建了一个简单的卷积神经元网络。 当我使用 edge = 32px 的输入图像时,网络工作正常,但如果我将 edge 增加两次到 64px,则熵返回为 NaN。问题是如何解决?

CNN 结构非常简单,如下所示: 输入->conv->pool2->conv->pool2->conv->pool2->fc->softmax

熵计算如下:

prediction = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction), reduction_indices=[1]))       # loss
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
train_pred = tf.equal(tf.argmax(prediction, 1), tf.argmax(ys, 1))
train_accuracy = tf.reduce_mean(tf.cast(train_pred, tf.float32))

对于 64px 我有:

train_accuracy=0.09000000357627869, cross_entropy=nan, test_accuracy=0.1428571492433548
train_accuracy=0.2800000011920929, cross_entropy=nan, test_accuracy=0.1428571492433548
train_accuracy=0.27000001072883606, cross_entropy=nan, test_accuracy=0.1428571492433548

对于 32px 看起来不错,训练给出了结果:

train_accuracy=0.07999999821186066, cross_entropy=20.63970184326172, test_accuracy=0.15000000596046448
train_accuracy=0.18000000715255737, cross_entropy=15.00744342803955, test_accuracy=0.1428571492433548
train_accuracy=0.18000000715255737, cross_entropy=12.469900131225586, test_accuracy=0.13571429252624512
train_accuracy=0.23000000417232513, cross_entropy=10.289153099060059, test_accuracy=0.11428571492433548

据我所知,NAN 在您计算 log(0) 时发生。我遇到了同样的问题。

tf.log(prediction) #This is a problem when the predicted value is 0.

您可以通过在预测中添加一点噪音来避免这种情况 (related 1, )。

tf.log(prediction + 1e-10)

或者使用 tensorflow 中的 clip_by_value 函数,它为传递的张量定义了最小值和最大值。像这样 (Documentation):

tf.log(tf.clip_by_value(prediction, 1e-10,1.0))

希望对您有所帮助。