【问题标题】:TensorFlow Cost Value return NANTensorFlow 成本值返回 NAN
【发布时间】:2018-11-06 18:53:19
【问题描述】:

我正在使用 Tensorflow 制作一个简单的逻辑回归模型。但是成本值总是返回 nan。

我的数据集分为x_data和y_data。 x_data 是图像中的坐标,y_data 是 1 或 0,因为我的图像是黑白的。我试图找到白色和黑色之间的分界线。

def train(input,iterations):
import tensorflow as tf
tf.set_random_seed(777)  # for reproducibility

x_data = []
y_data = []

i_dim = input.shape[0]
j_dim = input.shape[1]

for i in range(i_dim):
    for j in range(j_dim):
        x_data.append([j,i_dim-i-1])
        y_data.append([int(input[i,j])])

# placeholders for a tensor that will be always fed.
X = tf.placeholder(tf.float32, shape=[None, 2])
Y = tf.placeholder(tf.float32, shape=[None, 1])

W = tf.Variable(tf.random_normal([2, 1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')

# Hypothesis using sigmoid: tf.div(1., 1. + tf.exp(tf.matmul(X, W)))
hypothesis = tf.sigmoid(tf.matmul(X, W) + b)

# cost/loss function
cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) *
                       tf.log(1 - hypothesis))

train = tf.train.AdamOptimizer(1e-4).minimize(cost)

# Launch graph
with tf.Session() as sess:
    # Initialize TensorFlow variables
    sess.run(tf.global_variables_initializer())

    for step in range(iterations):
        cost_val, _ = sess.run([cost, train], feed_dict={X: x_data, Y: y_data})
        print(step, cost_val)

这是我的日志 (0, nan) (1, nan) (2, nan) (3, nan) (4, nan) (5, nan) (6, nan) (7, nan) (8, nan) (9, nan) (10, nan) (11, nan) (12, nan) (13, nan) (14, nan) (15, nan) (16, nan) (17, nan) (18, nan) (19, nan) (20, nan)

等等

【问题讨论】:

  • hypothesis 可以是 1 还是 0?
  • 这是可能的
  • 那么你试图计算一个零的对数,这显然是有问题的。
  • 是的,它成功了

标签: python tensorflow nan


【解决方案1】:

当您的假设等于 1 时,您的损失的第二部分变为 Y * log(0),因此输出为 nan。我建议您在对数内添加一个小常数,它应该可以工作。试试这个

cost = -tf.reduce_mean(Y*(tf.log(hypothesis+1e-4))+(1-Y)*(tf.log(1-hypothesis+1e-4)))

【讨论】:

  • 我试过这个。成本 = -tf.reduce_mean(Y *(​​ tf.log(hypothesis)+1e-4) + (1 - Y) * (tf.log(1 - hypothesis)+1e-4)) 仍然无法正常工作
  • 试试这个成本 = -tf.reduce_mean(Y *(​​ tf.log(hypothesis + 1e-4)) + (1 - Y) * (tf.log(1 - hypothesis+1e-4 )))
  • 很好,很高兴能帮上忙。请考虑将此作为已接受的答案:)
猜你喜欢
  • 1970-01-01
  • 2021-03-06
  • 1970-01-01
  • 1970-01-01
  • 2019-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-14
相关资源
最近更新 更多