2017-05-04 26 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") 

マイモデルは エポックを取得する取得します、損失: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

関連する問題