suku
suku

Reputation: 10929

Illegal argument error: logits and labels must be same size

I am getting an error when calculating the cost function using softmax. It says that the shape of my logits and labels are not match

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

Even if I do reshape or transpose the shape cannot match as logits is [1000,2] size and labels is [1000,1]. How do I tackle this issue?

n_nodes_hl1 = 250
n_nodes_hl2 = 250
n_classes = 2
batch_size = 1000

with open("xdf.pickle", 'rb') as f:
  features = pickle.load(f)
with open("ydf.pickle", 'rb') as f:
  labels = pickle.load(f)


def neural_network_model(data, feature_count):
  hidden_layer_1 = {'weights': tf.Variable(tf.random_normal([feature_count, n_nodes_hl1])),
                  'biases': tf.Variable(tf.random_normal([n_nodes_hl1]))}
  hidden_layer_2 = {'weights': tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
              'biases': tf.Variable(tf.random_normal([n_nodes_hl2]))}
  output_layer = {'weights': tf.Variable(tf.random_normal([n_nodes_hl2, n_classes])),
                'biases': tf.Variable(tf.random_normal([n_classes])), }

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

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


def train_neural_network(x, y, features, labels):
  X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.2)

  prediction = neural_network_model(x, len(features.columns))
  cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y))
  optimizer = tf.train.AdamOptimizer().minimize(cost)
  hm_epochs = 1

  with tf.Session() as sess:
    sess.run(tf.initialize_all_variables())
    for epoch in range(hm_epochs):
        epoch_loss = 0

        for i in range(int(len(X_train) / batch_size)):
            epoch_x = X_train[i*batch_size: min((i + 1)*batch_size, len(X_train))]
            epoch_y = y_train[i*batch_size: min((i + 1)*batch_size, len(y_train))]
            i, c = sess.run([optimizer, cost], feed_dict = {x:epoch_x, y:epoch_y})
            epoch_loss += c

        print('Epoch', epoch, ' completed out of ', hm_epochs, ' loss: ', epoch_loss)

    correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
    accuracy = tf.reduce_mean(tf.cast(correct, 'float'))

    print('Accuracy: ', accuracy.eval({x: X_test, y: y_test}))


 x = tf.placeholder('float', [None, len(features.columns)])
 y = tf.placeholder('float')
 train_neural_network(x, y, features, labels)

Upvotes: 0

Views: 538

Answers (1)

ml4294
ml4294

Reputation: 2629

Since I do not know what data you have, I can only guess. Your network has n_classes output neurons (2 in your case), but I assume that your labels are binary (0 or 1). You will either have to reduce the number of output neurons to 1 (this should work since you only have two classes), or convert your labels into one-hot labels ([1,0] for a label 0 and [0,1] for a label 1).

You could also try using tf.nn.sparse_softmax_cross_entropy_with_logits(), maybe it will work, so that you don't have to change the rest of the network...

Upvotes: 1

Related Questions