2016-06-26 3 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])) 

신경망을 정의 할 때 흔히 저지르는 실수입니다. This article은 그 뒤에있는 추론을 설명합니다 (매우 대략 모든 뉴런은 동일한 값을 가지므로 네트워크는 배우지 않습니다). 대신 작은 임의의 숫자에 가중치를 초기화해야하고, 전형적인 방식은 입력 장치의 수와 표준 편차와 반비례 proporational을 tf.truncated_normal()을 사용하는 것입니다

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

rrao's suggestions는 바이어스 용어를 추가하고 전환 더 많은 수치 적으로 안정적인 손실 함수를위한 tf.nn.softmax_cross_entropy_with_logits() op도 좋은 아이디어이며, 이는 아마도 합리적인 정확성을 얻는 데 필요한 단계 일 것입니다.

+0

@rrao 나는 여러분의 제안을 모두 포함했지만, 여전히 운이 없다 :/나는 디버깅을했는데 훈련 단계가 끝나면 가중치가 모두 NaN이어서 미래의 계산이 엉망이된다. 트레이닝 단계를 실행 한 후에 가중치가 NaN으로 바뀌는 원인이 무엇인지 생각해보십시오. –

+1

아마도 학습 속도를 줄이려고합니다. – natschz

+0

첫 번째 시도 :/행운. 나는 이것이 무엇인지 전혀 모른다. 데이터가 될 수 있을까요? 내가 가지고있는 것은 allTrainingArray에서 22 개의 다른 속성을 가진 283 개의 플레이어 행이며 [0,0,0,0,1] (또는 그 중 어느 위치에서나 1)을 가진 283 명의 플레이어와 일치하며 어떤 종류의 플레이어인지 나타냅니다 . TF 데이터 예제를보고 데이터 배열을 만들었으므로 문제가 될 것이라고 생각하지 않았습니다. –