【问题标题】:nan on loss function tensorflownan 关于损失函数张量流
【发布时间】:2018-09-18 15:22:34
【问题描述】:

我尝试构建一个可以通过数据识别的模型并尝试查看 LOSS 函数 损失 =tf.reduce_mean(-(y_ * tf.log(y)+(1- y_)* tf.log (1-y))) 但截至目前,我只在 LOSS 函数中的预测和打印 NAN 得到 NAN

np_labels = np.array(labels)
np_labels = np_labels.reshape([np_labels.shape[0], 1])
features = 910
hidden_layer_nodes = 100
x = tf.placeholder(tf.float32, [None, features])
y_ = tf.placeholder(tf.float32, [None, 1])
W1 = tf.Variable(tf.truncated_normal([features,hidden_layer_nodes], stddev=0.1))
b1 = tf.Variable(tf.constant(0.1, shape=[hidden_layer_nodes]))
z1 = tf.add(tf.matmul(x,W1),b1)
a1 = tf.nn.relu(z1)
W2 = tf.Variable(tf.truncated_normal([hidden_layer_nodes,1], stddev=0.1))
b2 = tf.Variable(0.)
z2 = tf.matmul(a1,W2) + b2
y = 1 / (1.0 + tf.exp(-z2))

loss =tf.reduce_mean(-(y_ * tf.log(y)+(1- y_)* tf.log (1-y)))

update = tf.train.AdamOptimizer(0.01).minimize(loss)

sess = tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(0,50):
        sess.run(update, feed_dict = {x:fvecs, y_:np_labels})
        print(sess.run(loss, feed_dict={x: fvecs, y_: np_labels}))

      #  sess.run(update, feed_dict = {x:data_x, y_:data_y})
     #   print(sess.run(loss, feed_dict={x: data_x, y_: data_y}))

print('prediction: ', y.eval(session=sess, feed_dict =  {x:[[493.9, 702.6, .....

我想打印损失

谢谢

【问题讨论】:

    标签: python tensorflow deep-learning loss-function


    【解决方案1】:

    不是一个 TensorFlow 问题。这是因为自己实现损失函数的想法非常糟糕。

    import tensorflow as tf
    
    z2 = tf.random_normal([8, 10]) * 20
    y_ = tf.random_uniform([8, 1], minval=0, maxval=10, dtype=tf.float32)
    
    y = 1 / (1.0 + tf.exp(-z2))
    
    loss = tf.reduce_mean(-(y_ * tf.log(y)+(1- y_)* tf.log (1-y)))
    
    with tf.Session() as sess:
        print sess.run(loss)  # will always fail with high prob
    

    将给出Inf 只是因为缺少 log-sum-exp 技巧,这会导致您的实现由于数值不稳定性而失败(一个产生溢出的民间传说示例)。只需多次运行此代码,您就会得到NaNInf

    解决办法是:

    1. y = tf.sigmoid(-z2) 替换为y = tf.identity(z2) 以获得未转换的logits
    2. loss = .. 替换为loss = tf.nn.sigmoid_cross_entropy_with_logits(...) 以使用数值稳定方式

    请参阅sigmoid_cross_entropy_with_logits 的文档,其中明确描述了此问题。

    【讨论】:

      猜你喜欢
      • 2018-12-07
      • 1970-01-01
      • 2018-02-03
      • 1970-01-01
      • 2020-11-21
      • 2017-09-02
      • 2021-11-20
      • 2019-02-11
      • 2021-05-12
      相关资源
      最近更新 更多