【问题标题】:tensorflow prediction contains all zeros张量流预测包含全零
【发布时间】:2016-11-05 16:36:31
【问题描述】:

我最近做了 mnist tensorflow 教程,想尝试改变一下。在这个例子中,我试图获得一个 28*28*3 的输入(r、g、b 为 3)并返回完全相同的输出。为方便起见,我只是在进出纯白色。

#!/usr/bin/env python
import tensorflow as tf

input_layer_size = 2352 # number of pixels * number of color channels (rgb)

white = [255] * input_layer_size # white is a square of white pixels
white = [white]

sess = tf.InteractiveSession()

x = tf.placeholder(tf.float32, shape=[None, input_layer_size])
y_ = tf.placeholder(tf.float32, shape=[None, input_layer_size])

W = tf.Variable(tf.truncated_normal([input_layer_size,input_layer_size], stddev=0.1))
b = tf.Variable(tf.truncated_normal([input_layer_size], stddev=0.1))

sess.run(tf.initialize_all_variables())

y = tf.nn.softmax(tf.matmul(x,W) + b)
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(tf.clip_by_value(y, 1e-10, 1.0)), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

for i in range(100):
    train_step.run(feed_dict={x: white, y_: white})

feed_dict = {x:white}
classification = sess.run(y, feed_dict)
print ("Output:", classification[0])

由于某种原因,它的输出是[ 0., 0., 0., ..., 0., 0., 0.]。为什么不是预期的结果([ 255., 255., ... ])?

我用 mnist 数据尝试了完全相同的代码,它工作正常,给了我 10 个输出通道,每个通道都有合理的结果。

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    从代码看来,您似乎尝试学习从 x 到 y 的线性变换,其中 x 和 y 都是表示两个图像的(行)向量:y = x * W + b。这是一个回归问题。解决方案将是 W - 单位矩阵,并且 b 是零向量。下面的代码通过最小化 |y - (x * W + b)| 来解决这个问题:

    #!/usr/bin/env python
    import tensorflow as tf
    tf.reset_default_graph()
    
    input_layer_size = 2352 # number of pixels * number of color channels (rgb)
    
    white = [255] * input_layer_size # white is a square of white pixels
    white = [white]
    
    sess = tf.InteractiveSession()
    
    x = tf.placeholder(tf.float32, shape=[None, input_layer_size])
    y_ = tf.placeholder(tf.float32, shape=[None, input_layer_size])
    
    W = tf.Variable(tf.truncated_normal([input_layer_size,input_layer_size], stddev=0.1))
    b = tf.Variable(tf.truncated_normal([input_layer_size], stddev=0.1))
    
    y = tf.matmul(x,W) + b
    loss = tf.reduce_sum(tf.abs(y - y_))
    train_step = tf.train.AdagradOptimizer(0.001).minimize(loss)
    
    sess.run(tf.initialize_all_variables())
    
    for i in range(1000):
      loss_, _ = sess.run([loss, train_step], feed_dict={x: white, y_: white})
    
    print loss_
    
    feed_dict = {x:white}
    classification = sess.run(y, feed_dict)
    print ("Output:", classification[0])
    

    当您使用 mnist 数据尝试相同的代码时,它起作用了,因为 y 不同:它是目标数字的 one-hot 编码,即对于 0,它将是 1、0、0、0,...;对于 1,它将是 0, 1, 0, 0, ...;对于 2 - 0, 0, 1, ... 等等。

    【讨论】:

    • 请忽略我上面的意外评论。有几件事是错误的:y 被限制在 (1e-10, 1) 范围内,而它应该是大约 255 以最小化损失,损失本身(softmax 的交叉熵)是针对多类分类问题而不是回归问题。
    • 我明白了,主要问题是使用了错误的损失,谢谢。
    • 谢谢,效果很好。您能否为我解释一下线性变换和回归之间的区别,以及为什么线性变换在这里不起作用?
    • 我建议的代码通过解决回归问题找到线性变换的参数(矩阵W和向量b)。您可以在此处找到有关回归问题的更多详细信息:en.wikipedia.org/wiki/Regression_analysis
    猜你喜欢
    • 2019-07-20
    • 2017-11-22
    • 2017-08-16
    • 2017-04-28
    • 2017-04-13
    • 1970-01-01
    • 2020-10-03
    • 2018-01-10
    • 1970-01-01
    相关资源
    最近更新 更多