tf.nn.softmax_cross_entropy_with_logits()错误:logits和标签必须大小相同

时间:2016-12-31 23:22:53

标签: python tensorflow

我是TensorFlow的新手,我正在尝试编写一种算法来对CIFAR-10数据集中的图像进行分类。我收到了这个错误:

%ALLUSERSPROFILE%\Application Data

这是我的代码:

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)]]

我很确定这意味着第48行(如上所示)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) prediction形状不一样,但我不太了解TensorFlow,知道如何解决它。我甚至不太了解y的设置位置,我从教程中获取了大部分代码,并将其应用于不同的数据集。我该如何解决这个错误?

2 个答案:

答案 0 :(得分: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()的有效参数。

答案 1 :(得分: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())
相关问题