首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >TensorFlow成本值返回NAN

TensorFlow成本值返回NAN
EN

Stack Overflow用户
提问于 2018-05-28 16:19:34
回答 1查看 1.1K关注 0票数 0

我正在使用Tensorflow创建一个简单的Logistic回归模型。但是成本值总是返回nan。

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

代码语言:javascript
复制
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)

诸若此类

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-28 17:07:24

当您的假设等于1时,损失的第二部分变成Y* log(0),因此是nan输出。我建议你在对数中添加一个小常量,它应该会起作用。尝尝这个

代码语言:javascript
复制
cost = -tf.reduce_mean(Y*(tf.log(hypothesis+1e-4))+(1-Y)*(tf.log(1-hypothesis+1e-4)))
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50561959

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档