Shy*_*tak 1 python machine-learning prediction tensorflow
我正在为TensorFlow运行以下代码,所有概率都是NaN,所有预测都是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)
Run Code Online (Sandbox Code Playgroud)
问题源于您的代码中的这一行:
W = tf.Variable(tf.zeros([22, 5]))
Run Code Online (Sandbox Code Playgroud)
在定义神经网络时,将权重初始化为零是一个常见错误.本文解释了它背后的原因(非常近似,所有神经元都具有相同的值,因此网络不会学习).相反,您应该将权重初始化为小的随机数,并且典型的方案是使用tf.truncated_normal()与输入单位数量相反的标准偏差:
W = tf.Variable(tf.truncated_normal([22, 5], stddev=1./22.))
Run Code Online (Sandbox Code Playgroud)
rrao建议添加一个偏差项,并转换为更多数值稳定的tf.nn.softmax_cross_entropy_with_logits()op用于你的损失函数也是好主意,这些可能是获得合理准确性的必要步骤.
| 归档时间: |
|
| 查看次数: |
1747 次 |
| 最近记录: |