张量流预测包含所有零

2024-04-29 14:00:16 发布

您现在位置:Python中文网/ 问答频道 /正文

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

#!/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个输出通道,每个通道都有合理的结果。在


Tags: ofrunlayernumberinputsizetffeed
1条回答
网友
1楼 · 发布于 2024-04-29 14:00:16

从代码中,您似乎试图学习从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,…;对于1,它是0,1,0,0,…;对于2-0,0,1。。。等等。在

相关问题 更多 >