张量流预测包含全零

时间:2016-11-05 05:25:36

标签: python tensorflow

我最近做过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个输出通道,每个通道都有合理的结果。

1 个答案:

答案 0 :(得分:2)

从代码中可以看出,您尝试学习从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是不同的:它是目标数字的单热编码,即对于0,它将是1,0,0,0,.... ..;对于1,它将是0,1,0,0,......;对于2 - 0,0,1,......等等。