Tensorflow 训练结果为 NAN

    xiaoxiao2021-03-26  5

    Tensorflow 预测的中间结果有很多 NAN,如下图

    看一下计算 loss 的时候是否有类似下面的代码

    cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction), reduction_indices=[1])) 由于 prediction 可能为 0, 导致 log 出错,最后结果会出现 NA

    把上面的代码改为下面这样即可解决

    cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(tf.clip_by_value(prediction, 1e-10,1.0)), reduction_indices=[1]))

    参考:http://stackoverflow.com/questions/33712178/tensorflow-nan-bug

    转载请注明原文地址: https://ju.6miu.com/read-500365.html

    最新回复(0)