2016-06-26 64 views
1

我对TensorFlow运行以下代码,所有概率为NaN,所有预测为0。但是,准确性起作用。我不知道如何调试。任何和所有的帮助表示赞赏。Tensor Flow所有的预测都是0

x = tf.placeholder("float", shape=[None, 22]) 
W = tf.Variable(tf.zeros([22, 5])) 

y = tf.nn.softmax(tf.matmul(x, W)) 
y_ = tf.placeholder(tf.float32, [None, 5]) 

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1])) 
#cross_entropy = -tf.reduce_sum(tf_softmax_correct*tf.log(tf_softmax + 1e-50)) 
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) 
init = tf.initialize_all_variables() 

sess = tf.Session() 
sess.run(init) 

for i in range(100): 
    batch_xs, batch_ys = random.sample(allTrainingArray,100), random.sample(allTrainingSkillsArray,100) 
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys}) 

#test on itself 
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) 
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) 
print "accuracy", sess.run(accuracy, feed_dict={x: batch_xs, y_: batch_ys}) 

probabilities = y 
print "probabilities", probabilities.eval(feed_dict={x: allTrainingArray}, session=sess) 

prediction=tf.argmax(y,1) 
print "predictions", prediction.eval(feed_dict={x: allTrainingArray}, session = sess) 

回答

0

我认为你有一个计算损失的问题。如果您添加一个biases载体,它也可能有助于您的结果。

你应该试试这个:

W = tf.Variable(tf.zeros([22, 5])) # can try better initialization methods 
b = tf.Variable(tf.zeros([5])) # can try better initialization methods 
y = tf.matmul(x, W) + b 

loss = tf.reduce_mean(
    tf.nn.softmax_cross_entropy_with_logits(y, y_) 
) 

train_step = tf.train.GradientDescentOptimizer(0.5).minimize(loss) 

如果你想看看tf.nn.softmax_cross_entropy_with_logits

5

问题来自这行代码中的茎:

W = tf.Variable(tf.zeros([22, 5])) 

初始化的权重为零在定义神经网络时是常见的错误。 (非常近似地,所有神经元都具有相同的值,所以网络不会学习)。相反,你应该初始化权重小的随机数,典型的方案是使用tf.truncated_normal(),标准偏差成反比proporational到的输入单元的数量:

W = tf.Variable(tf.truncated_normal([22, 5], stddev=1./22.)) 

rrao's suggestions添加的偏项,并切换到更多数字稳定tf.nn.softmax_cross_entropy_with_logits()为您的损失函数也是好主意,并且这些可能是获得合理准确性的必要步骤。

+0

@rrao我整合了你的两个建议,但仍然没有运气:/我做了一些调试,似乎在我的训练步骤后,权重都是NaN,这会扰乱未来的计算。你有没有想过在训练步骤后可能会导致体重变成NaN? –

+1

也许尝试降低学习率。 – natschz

+0

我试过的第一件事:/没有运气。我不知道这是什么。它可能是数据吗?我所拥有的是283行玩家在所有TrainingArray中拥有22种不同的属性,并且匹配283个玩家与[0,0,0,0,1](或任意位置上的1个),表明他是何种玩家。我看了一下TF的例子来制作我的数据阵列,所以我不认为这会是一个问题。 –

相关问题