Simon Zeng
Simon Zeng

Reputation: 45

Shapes of logits and labels are incompatible

The full error message is like this:

ValueError: Shapes (2, 1) and (50, 1) are incompatible

It occurs when my model is trained. The mistake either is in my input_fn:

train_input_fn = tf.estimator.inputs.numpy_input_fn(
    x = {"x" : training_data},
    y = training_labels,
    batch_size = 50,
    num_epochs = None,
    shuffle = True)

in my logits and loss function:

dense = tf.layers.dense(inputs = pool2_flat, units = 1024, activation = tf.nn.relu)
dropout = tf.layers.dropout(inputs = dense, rate = 0.4, training = mode == tf.estimator.ModeKeys.TRAIN)
logits = tf.layers.dense(inputs = dropout, units = 1)

loss = tf.losses.softmax_cross_entropy(labels = labels, logits = logits)

or in my dataset. I can only print out the shape of my dataset for you to take a look at it.

#shape of the dataset
train_data.shape
(1196,2,1)
train_data[0].shape
(2,1)

#this is the data
train_data[0][0].shape
(1,)
train_data[0][0][0].shape
(20,50,50)

#this is the labels
train_data[0][1].shape
(1,)

The problem seems to be the shape of the logits. They are supposed to be [batch_size, num_classes] in this case [50,1] but are [2,1]. The shape of the labels is correctly [50,1]

I have made a github gist if you want to take a look at the whole code. https://gist.github.com/hjkhjk1999/38f358a53da84a94bf5a59f44050aad5

Upvotes: 1

Views: 2639

Answers (1)

Diego Aguado
Diego Aguado

Reputation: 1606

In your code, you are stating that the inputs to your model will be feed in batches of 50 samples per batch with one variable. But it looks like your are feeding actually a batch of 2 samples with 1 variable (shape=[2, 1]) despite feeding labels with shape [50, 1].

That's the problem, you are giving 50 'questions' and two 'answers'.

Also, your dataset is shaped in a really weird way. I see you named your github gist 3D Conv. If you are indeed trying to do a 3D convolution you might want to reshape your dataset into a tensor (numpy array) of this shape shape = [samples, width, height, deepth]

Upvotes: 1

Related Questions