2017-05-04 3 views
4
import tensorflow as tf 
import numpy as np 
def weight(shape): 
return tf.Variable(tf.truncated_normal(shape, stddev=0.1)) 
def bias(shape): 
return tf.Variable(tf.constant(0.1, shape=shape)) 
def output(input,w,b): 
return tf.matmul(input,w)+b 
x_columns = 33 
y_columns = 1 
layer1_num = 7 
layer2_num = 7 
epoch_num = 10 
train_num = 1000 
batch_size = 100 
display_size = 1 
x = tf.placeholder(tf.float32,[None,x_columns]) 
y = tf.placeholder(tf.float32,[None,y_columns]) 

layer1 = 
tf.nn.relu(output(x,weight([x_columns,layer1_num]),bias([layer1_num]))) 
layer2=tf.nn.relu 
(output(layer1,weight([layer1_num,layer2_num]),bias([layer2_num]))) 
prediction = output(layer2,weight([layer2_num,y_columns]),bias([y_columns])) 

loss=tf.reduce_mean 
(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction)) 
train_step = tf.train.AdamOptimizer().minimize(loss) 

sess = tf.InteractiveSession() 
sess.run(tf.global_variables_initializer()) 
for epoch in range(epoch_num): 
    avg_loss = 0. 
    for i in range(train_num): 
     index = np.random.choice(len(x_train),batch_size) 
     x_train_batch = x_train[index] 
     y_train_batch = y_train[index] 
     _,c = sess.run([train_step,loss],feed_dict= 
{x:x_train_batch,y:y_train_batch}) 
     avg_loss += c/train_num 
    if epoch % display_size == 0: 
     print("Epoch:{0},Loss:{1}".format(epoch+1,avg_loss)) 
print("Training Finished") 

내 모델 에포크를 얻는다을 얻는다 6 , 손실 : 0.0 신기원 : 7, 손실 : 0.0 신기원 : 8, 손실 : 0.0 신기원 : 9, 손실 : 0.0 신기원 : 10, 손실 : 0.0 교육이TensorFlow 모델 손실 0

가 어떻게이 문제를 해결할 수있는 완료 ?

답변

2

softmax_cross_entropy_with_logits은 한 핫 양식, 즉 모양이 [batch_size, num_classes] 인 것으로 가정합니다. 여기에 y_columns = 1이 있습니다. 이는 항상 1 개의 클래스를 의미하며, 이는 항상 예측 된 것이고 네트워크의 관점에서 볼 때 '지상 진실'입니다. 따라서 가중치가 무엇이든 관계없이 출력이 항상 정확합니다. 따라서 loss=0.

다른 클래스가있는 것 같습니다. y_train에는 라벨의 ID가 포함되어 있습니다. 그런 다음 predictions 모양이 [batch_size, num_classes]이어야하며 대신 softmax_cross_entropy_with_logits을 사용해야합니다. tf.nn.sparse_softmax_cross_entropy_with_logits

+0

대단히 감사합니다. 당신의 대답은 내가 입력 과목에 관해 실수를했다는 것을 가르쳐주었습니다. 나는 예측할 수 있었다! – yoshi