Tensorflow准确性问题

时间:2017-04-12 12:38:44

标签: python tensorflow neural-network

我已经编写了我的第一个Tensorflow程序(使用我自己的数据)。它运作良好,至少它不会崩溃!但是我的有线精度值是0 0还是1?

................................. 
the previous part of the code, is only about handeling csv file an getting Data in correct format / shapes
......................................................
# Tensoflow
x = tf.placeholder(tf.float32,[None,len(Training_Data[0])],name='Train_data')# each input has a 457 lenght

y_ = tf.placeholder(tf.float32,[None, numberOFClasses],name='Labels')#

#w = tf.Variable(tf.zeros([len(Training_Data[0]),numberOFClasses]),name='Weights')
w = tf.Variable(tf.truncated_normal([len(Training_Data[0]),numberOFClasses],stddev=1./10),name='Weights')

b = tf.Variable(tf.zeros([numberOFClasses]),name='Biases')
model = tf.add(tf.matmul(x,w),b)

y =  tf.nn.softmax(model)
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
#cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
sess = tf.Session()
sess.run(tf.global_variables_initializer())

for j in range(len(train_data)):
    if(np.shape(train_data) == (batchSize,numberOFClasses)):
        sess.run(train_step,feed_dict={x:train_data[j],y_:np.reshape(train_labels[j],(batchSize,numberOFClasses)) })

correct_prediction = tf.equal(tf.arg_max(y,1),tf.arg_max(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float"))

accuracy_vector= []
current_class =[] 
for i in range(len(Testing_Data)):
    if( np.shape(Testing_Labels[i]) == (numberOFClasses,)):
        accuracy_vector.append(sess.run(accuracy,feed_dict={x:np.reshape(Testing_Data[i],(1,457)),y_:np.reshape(Testing_Labels[i],(1,19))}))#,i)#,Test_Labels[i])
        current_class.append(int(Test_Raw[i][-1]))

绘制accuracy_vector,提供以下内容:

[enter image description here]

知道我在这里失踪了吗?

非常感谢任何暗示!

2 个答案:

答案 0 :(得分:2)

cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))

tf.nn.softmax_cross_entropy_with_logits想要未缩放的日志。 来自doc:

  

警告:此操作需要未缩放的日志,因为它会在内部执行logmax以提高效率。不要使用softmax的输出调用此op,因为它会产生不正确的结果。

这意味着行y = tf.nn.softmax(model)错误。

相反,您希望将未缩放的日志传递给该函数,因此:

y = model

此外,一旦解决了这个问题,如果网络无法正常工作,请尝试将学习率从0.01降低到大约1e-3或1e-4。 (我告诉你这个,因为1e-2通常是一个"高"学习率)

答案 1 :(得分:1)

您正在测试1号批次,因此预测是好的还是假的,所以你只能获得0或1的准确度:

accuracy_vector.append(sess.run(accuracy,feed_dict={x:np.reshape(Testing_Data[i],(1,457)),y_:np.reshape(Testing_Labels[i],(1,19))}))#,i)#,Test_Labels[i])

只需使用更大的批量大小:  accuracy_vector.append(sess.run(accuracy,feed_dict={x:np.reshape(Testing_Data[i:i+batch_size],(batch_size,457)),y_:np.reshape(Testing_Labels[i:i+batch_size],(batch_size,19))}))