2016-12-31 54 views
0

我是TensorFlow的新手,我正在嘗試編寫一個算法來對CIFAR-10數據集中的圖像進行分類。我收到此錯誤:tf.nn.softmax_cross_entropy_with_logits()錯誤:logits和標籤必須大小相同

InvalidArgumentError (see above for traceback): logits and labels must be same size: logits_size=[10000,10] labels_size=[1,10000] 
    [[Node: SoftmaxCrossEntropyWithLogits = SoftmaxCrossEntropyWithLogits[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](Reshape, Reshape_1)]] 

這裏是我的代碼:

import tensorflow as tf 
from tensorflow.examples.tutorials.mnist import input_data 
import cPickle 

n_nodes_hl1 = 500 
n_nodes_hl2 = 500 
n_nodes_hl3 = 500 

n_classes = 10 
batch_size = 100 
image_size = 32*32*3 # because 3 channels 

x = tf.placeholder('float', shape=(None, image_size)) 
y = tf.placeholder('float') 

def neural_network_model(data): 
    hidden_1_layer = {'weights':tf.Variable(tf.random_normal([image_size, n_nodes_hl1])), 'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))} 
    hidden_2_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])), 'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))} 
    hidden_3_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])), 'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))} 
    output_layer = {'weights':tf.Variable(I am new to TensorFlow and tf.random_normal([n_nodes_hl3, n_classes])), 'biases':tf.Variable(tf.random_normal([n_classes]))} 
    # input_data * weights + biases 
    l1 = tf.add(tf.matmul(data, hidden_1_layer['weights']), hidden_1_layer['biases']) 
    # activation function 
    l1 = tf.nn.relu(l1) 

    l2 = tf.add(tf.matmul(l1, hidden_2_layer['weights']), hidden_2_layer['biases']) 
    l2 = tf.nn.relu(l2) 

    l3 = tf.add(tf.matmul(l2, hidden_3_layer['weights']), hidden_3_layer['biases']) 
    l3 = tf.nn.relu(l3) 

    output = tf.matmul(l3, output_layer['weights']) + output_layer['biases'] 
    return output 

def train_neural_network(x): 
    prediction = neural_network_model(x) 
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(prediction, y))//THIS IS LINE 48 WHERE THE ERROR OCCURS 
    #learning rate = 0.001 
    optimizer = tf.train.AdamOptimizer().minimize(cost) 
    hm_epochs = 10 
    with tf.Session() as sess: 
     sess.run(tf.initialize_all_variables()) 
     for epoch in range(hm_epochs): 
      epoch_loss = 0 
      for i in range(5): 
       with open('data_batch_'+str(i+1),'rb') as f: 
        train_data = cPickle.load(f) 
       print train_data 
       print prediction.get_shape() 
       #print len(y) 
       _, c = sess.run([optimizer, cost], feed_dict={x:train_data['data'],y:train_data['labels']}) 
       epoch_loss += c 
      print 'Epoch ' + str(epoch) + ' completed out of ' + str(hm_epochs) + ' loss: ' + str(epoch_loss) 
     correct = tf.equal(tf.argmax(prediction,1), tf.argmax(y,1)) 
     accuracy = tf.reduce_mean(tf.cast(correct, 'float')) 
     with open('test_batch','rb') as f: 
      test_data = cPickle.load(f) 
      accuracy = accuracy.eval({x:test_data['data'],y:test_data['labels']}) 
     print 'Accuracy: ' + str(accuracy) 

train_neural_network(x) 

我敢肯定,這意味着在48行(如上圖所示)predictiony是不一樣的形狀,但我不明白TensorFlow足夠知道如何解決它。我甚至不知道在哪裏設置了y,我從教程中獲得了大部分代碼,並將其用於應用於不同的數據集。我該如何解決這個錯誤?

回答

2

tf.nn.softmax_cross_entropy_with_logits(logits, labels) op預計它的參數logitslabels是具有相同形狀的張量。此外,參數logitslabels應爲二維張量(矩陣),行數爲batch_size,列數爲num_classes

從錯誤信息和logits,我猜batch_size爲10000大小,num_classes是10從labels的大小,我猜你的標籤編碼爲整數,其中的列表該整數表示相應輸入示例的類的索引。 (我會想到這是一個tf.int32值,而不是tf.float32,因爲它似乎是在你的程序,但也許有一些自動轉換回事。)

在TensorFlow,你可以使用tf.nn.sparse_softmax_cross_entropy_with_logits()計算交叉熵這種形式的數據。在你的程序中,你可以通過更換cost計算這樣做:

cost = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
    prediction, tf.squeeze(y))) 

注意,tf.squeeze()運算是需要y轉換成長度batch_size(向量以是tf.nn.sparse_softmax_cross_entropy_with_logits()有效的參數

+0

我接着說:tf.squeeze()'和我仍然得到同樣的錯誤 –

+0

你怎麼也SWI tch to'tf.nn.sparse_softmax_cross_entropy_with_logits()'? – mrry

+0

沒有看到,謝謝。現在我得到'TypeError:DataType float32 attr'Tlabels'不在允許值列表中:int32,int64'我認爲這與你所談論的有關。我可以將'y = tf.placeholder('float')'改成'y = tf.placeholder('int')'嗎? –

1

這裏有一些更新代碼來支持TensorFlow 1.0版:

def train_neural_network(x): <br> 
&emsp;prediction = neural_network_model(x) <br> 
&emsp;# OLD VERSION: <br> 
&emsp;cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(prediction,y)) <br> 
&emsp;# NEW:<br> 
&emsp;cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y))<br> 
&emsp;optimizer = tf.train.AdamOptimizer().minimize(cost) <br> 
&emsp;hm_epochs = 10 <br> 
&emsp;with tf.Session() as sess: <br> 
&emsp;&emsp;#OLD: #sess.run(tf.initialize_all_variables()) <br> 
&emsp;&emsp;#NEW:<br> 
&emsp;&emsp;sess.run(tf.global_variables_initializer()) 
相關問題