Keras: Error InvalidArgumentError: Incompatible shapes when using accuracy metric, sparse_categorical_crossentropy, and batch size > 1

Created on 28 Nov 2018  ·  37Comments  ·  Source: keras-team/keras

⚠️⚠️⚠️
I found out that this issue only happens in Keras version > 2.2.2 (so in 2.2.3 and 2.2.4 up to now).

I downgraded to version 2.2.2 (and Tensorflow 1.10.0) and the error doesn't happen anymore. But still this should be fixed because I want to be able to use the latest Tensorflow T__T


I found an issue when trying to fit a RNN model with sparse_categorical_crossentropy loss and metrics=["accuracy"]. I created simple example in order to reproduce consistently this error.

This is the input data: a simple fibonacci series where given a 3 number sequence, the model will try to predict the following 3 numbers.

x = np.array([[1, 1, 2], [1, 2, 3], [2, 3, 5], [3, 5, 8], [5, 8, 13], [8, 13, 21]])
y = np.array([[3, 5, 8], [5, 8, 13], [8, 13, 21], [13, 21, 34], [21, 34, 55], [34, 55, 89]])
y = y.reshape((-1, y.shape[1], 1))

It's just a silly example so I treated the inputs as tokens, like if it was a text to text network.

Now, here's the model I used

input_layer = Input(shape=x.shape[1:])
rnn = Embedding(90, 200)(input_layer)
rnn = Bidirectional(GRU(64, return_sequences=True))(rnn)
rnn = TimeDistributed(Dense(90))(rnn)
rnn = Activation("softmax")(rnn)

model = Model(inputs=input_layer, outputs=rnn)
model.compile(loss=sparse_categorical_crossentropy, optimizer="adam", metrics=['accuracy'])

model.summary()

It doesn't really matter what kind of model I use, the importat thing is that this 4 things are true:

  • The model predicts a times series with shape: (BatchSize, SeriesLength, VocabSize) in this case, the shape is (3, 3, 90) as the numbers are treated as tokens so there are 90 possible values (0 to 89).
  • The model uses sparse_categorical_crossentropy as its loss function
  • The model uses accuracy as one of its metrics
  • The batch size is bigger than 1 (if it's 1 everything works 😮 )

Then, I just fit the model.

model.fit(x, y, epochs=1000, batch_size=3)

After the first batch is processed, when tring to calculate the accuracy I get the following error:

InvalidArgumentError: Incompatible shapes: [9] vs. [3,3]
     [[{{node metrics_16/acc/Equal}} = Equal[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](metrics_16/acc/Reshape, metrics_16/acc/Cast)]]

If I remove the accuracy metric, the model is able to train and predict without any issue (except that I have no feedback about how the model is performing).

I had just done an identical model in a Notebook from a Udacity Nanodegree and there was no such error, so it's probably something related with either the Keras version, the Tensorflow version (I'm using the last version of both) or something else in my installation, in which case maybe you won't be able to reproduce it in your machine.

Does anybody have any idea of why is this happening? Thank you.

To investigate tensorflow

Most helpful comment

It looks like the issue has been fixed in the latest master and it will be most likely included in the next release 2.2.5 (hopefully soon).
Until then, you can update to the HEAD of master from pip by doing:

pip3 install git+https://github.com/keras-team/keras.git -U

All 37 comments

I am running into a highly similar issue, with the error only occurring with batch sizes greater than 1. Running keras in Rstudio, adam optimizer, binary_crossentropy for loss, and sparse_categorical_accuracy metric.

The Incompatible shapes error is always [batchsize*final_layer_units] vs. [batchsize].

My code below is not reproducible, but I'm not sure if the resulting error message will be helpful.

> batch_size <- 2
> 
> model <- keras_model_sequential() %>% 
+   layer_embedding(input_dim = max_title_words_dict,
+                   output_dim = embedding_dim,
+                   input_length = maxlen_title_words) %>% 
+   layer_flatten() %>% 
+   #layer_dense(units = 256, activation = 'relu') %>% 
+   layer_dense(units = 1300, activation = 'sigmoid',
+               #batch_input_shape = c(1, embedding_dim * max_title_words_dict)
+               NULL
+               )
> 
> opt <- keras::optimizer_adam()
> 
> model %>% compile(
+   optimizer = opt,
+   loss = "binary_crossentropy",
+   metrics = 'sparse_categorical_accuracy'
+   )
> 
> model %>% fit(
+   x_train, y_train,
+   epochs = 5,
+   batch_size = batch_size
+   #validation_data = list(x_valid, y_valid)
+ )
Error in py_call_impl(callable, dots$args, dots$keywords) : 
  InvalidArgumentError: Incompatible shapes: [2600] vs. [2]
     [[{{node metrics_38/sparse_categorical_accuracy/Equal}} = Equal[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](metrics_38/sparse_categorical_accuracy/Reshape, metrics_38/sparse_categorical_accuracy/Cast)]]
     [[{{node metrics_38/sparse_categorical_accuracy/Mean/_2367}} = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_425_metrics_38/sparse_categorical_accuracy/Mean", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

Detailed traceback: 
  File "C:\Users\blaze\ANACON~1\envs\R-TENS~1\lib\site-packages\keras\engine\training.py", line 1039, in fit
    validation_steps=validation_steps)
  File "C:\Users\blaze\ANACON~1\envs\R-TENS~1\lib\site-packages\keras\engine\training_arrays.py", line 199, in fit_loop
    outs = f(ins_bat

Changing my metric from sparse_categorical_accuracy to categorical_accuracy avoids the error

Even when you set the batch size as 1, you would get the error "InvalidArgumentError: Incompatible shapes" while evaluating the model. It should have raised an error at the time of training process.

I am able to execute the code for python 3.x. It's the issue with python 2.7 version. You can work around the issue by creating your custom metric function to get the accuracy. I would recommend you to use the abstract Keras backend 'K' as it has a lot of helpful methods.

def get_predictions(x_test):
    preds = model.predict(x_test)
    y_pred = [np.argmax(i, axis=1) for i in preds]
    y_pred = np.array(y_pred)
    y_pred = y_pred.reshape((-1, y.shape[1], 1))
    return y_pred

Then, you can flatten the arrays of the true and predicted values for easy comparison for our metric function. You can use the accuracy score function of sklearn.

accuracy_score(y.flatten(), y_pred.flatten())

The issue is when we add the metrics in the model.compile method for the RNN model, there's an error while training the model i.e Incompatible shapes: [6] vs. [2,3] where the arrays need to be flattened for the accuracy metric.
There's one more issue that when we set the batch size as 1, it doesn't raise an error while training but it raises an error during evaluation.

The problem seems to be with the keras callback function while implementing the metric.

It's the issue with python 2.7 version

No it's not. I was using python 3.6.5 and had the issue. It dissapeared when downgrading to Keras 2.2.2 with Tensorflow 1.10.0

There shouldn't be a need to use K and perform the transformations by yourself, that's exactly what Keras should be doing properly when using the sparse_categorical_crossentropy loss & accuracy metric (and it's doing it until version 2.2.2)

It's the issue with python 2.7 version

No it's not. I was using python 3.6.5 and had the issue. It dissapeared when downgrading to Keras 2.2.2 with Tensorflow 1.10.0

There shouldn't be a need to use K and perform the transformations by yourself, that's exactly what Keras should be doing properly when using the sparse_categorical_crossentropy loss & accuracy metric (and it's doing it until version 2.2.2)

Using TensorFlow backend.
The Python version is 3.5.2
The Keras version is 2.2.4
The tf version is 1.11.0
Epoch 1/10
2018-12-05 14:11:18.585075: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2018-12-05 14:11:18.664968: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:964] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2018-12-05 14:11:18.665485: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1411] Found device 0 with properties:
name: GeForce MX150 major: 6 minor: 1 memoryClockRate(GHz): 1.5315
pciBusID: 0000:01:00.0
totalMemory: 1.96GiB freeMemory: 1.48GiB
2018-12-05 14:11:18.665502: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1490] Adding visible gpu devices: 0
2018-12-05 14:11:18.910011: I tensorflow/core/common_runtime/gpu/gpu_device.cc:971] Device interconnect StreamExecutor with strength 1 edge matrix:
2018-12-05 14:11:18.910050: I tensorflow/core/common_runtime/gpu/gpu_device.cc:977] 0
2018-12-05 14:11:18.910057: I tensorflow/core/common_runtime/gpu/gpu_device.cc:990] 0: N
2018-12-05 14:11:18.910202: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1103] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 1222 MB memory) -> physical GPU (device: 0, name: GeForce MX150, pci bus id: 0000:01:00.0, compute capability: 6.1)
6/6 [==============================] - 1s 218ms/step - loss: 4.4940 - acc: 0.0556
Epoch 2/10
6/6 [==============================] - 0s 3ms/step - loss: 4.4572 - acc: 0.3333
Epoch 3/10
6/6 [==============================] - 0s 3ms/step - loss: 4.4174 - acc: 0.7222
Epoch 4/10
6/6 [==============================] - 0s 3ms/step - loss: 4.3741 - acc: 0.8333
Epoch 5/10
6/6 [==============================] - 0s 3ms/step - loss: 4.3297 - acc: 0.8889
Epoch 6/10
6/6 [==============================] - 0s 3ms/step - loss: 4.2783 - acc: 0.9444
Epoch 7/10
6/6 [==============================] - 0s 3ms/step - loss: 4.2223 - acc: 0.9444
Epoch 8/10
6/6 [==============================] - 0s 3ms/step - loss: 4.1613 - acc: 0.9444
Epoch 9/10
6/6 [==============================] - 0s 3ms/step - loss: 4.0873 - acc: 0.9444
Epoch 10/10
6/6 [==============================] - 0s 3ms/step - loss: 4.0116 - acc: 0.8889

Working.

@kaushib11
Can you please share your whole model and config?
Also, please try it using CPU instead of GPU

@kaushib11
Can you please share your whole model and config?
Also, please try it using CPU instead of GPU

Using TensorFlow backend.
The Python version is 3.5.2
The Keras version is 2.2.4
The tf version is 1.12.0
Epoch 1/10
2018-12-05 14:23:33.632957: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
6/6 [==============================] - 1s 122ms/step - loss: 4.4978 - acc: 0.0000e+00
Epoch 2/10
6/6 [==============================] - 0s 937us/step - loss: 4.4506 - acc: 0.6667
Epoch 3/10
6/6 [==============================] - 0s 997us/step - loss: 4.4052 - acc: 0.9444
Epoch 4/10
6/6 [==============================] - 0s 894us/step - loss: 4.3587 - acc: 0.9444
Epoch 5/10
6/6 [==============================] - 0s 938us/step - loss: 4.3089 - acc: 0.9444
Epoch 6/10
6/6 [==============================] - 0s 993us/step - loss: 4.2551 - acc: 0.9444
Epoch 7/10
6/6 [==============================] - 0s 949us/step - loss: 4.1954 - acc: 0.9444
Epoch 8/10
6/6 [==============================] - 0s 915us/step - loss: 4.1309 - acc: 0.9444
Epoch 9/10
6/6 [==============================] - 0s 886us/step - loss: 4.0583 - acc: 0.9444
Epoch 10/10
6/6 [==============================] - 0s 880us/step - loss: 3.9760 - acc: 0.9444
[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 10622315461930727356
, name: "/device:XLA_CPU:0"
device_type: "XLA_CPU"
memory_limit: 17179869184
locality {
}
incarnation: 13226915753236685263
physical_device_desc: "device: XLA_CPU device"
]

Ran it on CPU. It's working fine. And I used the same model you have mentioned above.

Well, in that case, the Mistery is even bigger.
It's definitely not working for me using Python 3.X and those versions of keras & tensorflow, no matter how good it works on your computer

One more way to go around it is to convert the y labels into one hot vectors, then we can mention categorical_crossentropy loss and categorical_accuracy metrics for the model.

from keras.utils.np_utils import to_categorical

categorical_y_labels = to_categorical(y, num_classes=size_of_vocabulary)

The model:

input_layer = Input(shape=x.shape[1:])
rnn = Embedding(90, 200)(input_layer)
rnn = Bidirectional(GRU(64, return_sequences=True))(rnn)
rnn = TimeDistributed(Dense(90))(rnn)
rnn = Activation("softmax")(rnn)

model = Model(inputs=input_layer, outputs=rnn)
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=['categorical_accuracy'])

Training of the model:

Epoch 1/50
6/6 [==============================] - 1s 130ms/step - loss: 4.4943 - categorical_accuracy: 0.0000e+00
Epoch 2/50
6/6 [==============================] - 0s 2ms/step - loss: 4.4343 - categorical_accuracy: 0.7222
Epoch 3/50
6/6 [==============================] - 0s 3ms/step - loss: 4.3718 - categorical_accuracy: 0.8333
Epoch 4/50
6/6 [==============================] - 0s 3ms/step - loss: 4.3009 - categorical_accuracy: 0.8889
Epoch 5/50
6/6 [==============================] - 0s 3ms/step - loss: 4.2230 - categorical_accuracy: 0.9444
Epoch 6/50
6/6 [==============================] - 0s 3ms/step - loss: 4.1343 - categorical_accuracy: 0.9444
Epoch 7/50
6/6 [==============================] - 0s 3ms/step - loss: 4.0317 - categorical_accuracy: 0.9444

For predictions:

def get_predictions(x_test):
    preds = model.predict(x_test)
    y_pred = [np.argmax(i, axis=1) for i in preds]
    y_pred = np.array(y_pred)
    y_pred = to_categorical(y_pred, num_classes=90)
    return y_pred

Try this, let me know if you come across any erros?

I'm not sure, but I'd say that using sparse_categorical_crossentropy instead of categorical_crossentropy has other benefits apart from being able to use labels in their tokenized format. I might be wrong but it sounds like the sparse keyword would suggest that the loss is specifically made for cases where one-hot vectors are too big that the model has a very low gradient to learn with (like NLP models where your word dictionary contains maybe a couple hundred of thousand words).

Anyway, I already know those suggestions, I tried a lot of things before posting this issue, that's why I was so explicit about the conditions for the issue to happen, plus as I told multiple times, I got it working by downgrading keras & tensorflow.

I submitted this issue so it could get fixed, not so I could find a way to bypass it, please stop suggesting hacky or alternative solutions, the only real solution here is to fix the bug causing the issue.

Well, I just gave the solution so you could run your code. Anyway, I think the bug is in the callback function when keras calls during the training process for the calculating the metrics. What do you think of it?

Yes, it seems like the error is there, indeed. Specifically, it seems like a call to 'Reshape' that's working very weird because it's not able to reshape from [9] to [3, 3]

Yes, it seems like the error is there, indeed. Specifically, it seems like a call to 'Reshape' that's working very weird because it's not able to reshape from [9] to [3, 3]

Could you point out where is that happening?

Based in the error that I showed in my first comment

InvalidArgumentError: Incompatible shapes: [9] vs. [3,3]
     [[{{node metrics_16/acc/Equal}} = Equal[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](metrics_16/acc/Reshape, metrics_16/acc/Cast)]]

I'm just guessing it's either in the Reshape or in the Cast method of the accuracy function when performig the Equal opperation

It looks like the issue has been fixed in the latest master and it will be most likely included in the next release 2.2.5 (hopefully soon).
Until then, you can update to the HEAD of master from pip by doing:

pip3 install git+https://github.com/keras-team/keras.git -U

It looks like the issue has been fixed in the latest master and it will be most likely included in the next release 2.2.5 (hopefully soon).
Until then, you can update to the HEAD of master from pip by doing:

pip3 install git+https://github.com/keras-team/keras.git -U

Thank you!

It looks like the issue has been fixed in the latest master and it will be most likely included in the next release 2.2.5 (hopefully soon).
Until then, you can update to the HEAD of master from pip by doing:

**pip3 install git+https://github.com/keras-team/keras.git -U**

It fixes the issue.

Or you can define customized accuracy:

def custom_sparse_categorical_accuracy(y_true, y_pred):
    return K.cast(K.equal(K.max(y_true, axis=-1),
                          K.cast(K.argmax(y_pred, axis=-1), K.floatx())),
                  K.floatx())

and then use metrics = [custom_sparse_categorical_accuracy] along with loss='sparse_categorical_crossentropy'

@fchollet, I recommend we should always have a regression test for sparse_categorical_crossentropy that includes 3D output such as RNN predictions & transformer nets.

This broke for me too recently after an upgrade to 2.2.4 for a recurrent autoencoder.

I had the similar problem on Keras 2.2.4.

Downgrading to 2.2.2 version somehow solves the problem for me!

It looks like the issue has been fixed in the latest master and it will be most likely included in the next release 2.2.5 (hopefully soon).
Until then, you can update to the HEAD of master from pip by doing:

pip3 install git+https://github.com/keras-team/keras.git -U

Thank you. It fixes the issue.

I had a bug for both the sparse categorical loss and accuracy function. My solution is based on the comment made by @Hvass-Labs in #17150.
The model is defined as follows

decoder_target = tf.placeholder(dtype='int32', shape=(None, sequence_length+1))
model.compile(optimizer=opt, target_tensors=[decoder_target],
              loss=sparse_cross_entropy, metrics=[sparse_categorical_accuracy])

insuring that sparse_categorical_accuracy is imported from tensorflow.python.keras.metrics as keras.metrics does not work for me and defining the loss function as

def sparse_cross_entropy(y_true, y_pred):
    loss = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y_true,
                                                          logits=y_pred)
    loss_mean = tf.reduce_mean(loss)
    return loss_mean

@pabloppp I was getting the same error and my issue got resolved when I removed the return_sequences=True just write as -

rnn = Bidirectional(GRU(64))(rnn)
I guess you this will work if not do let me know!! Hope this helps!!!

I have the similar issue and checked the keras version was 2.2.4

I solved this problem.

from keras.layers import Flatten, Dense, Conv2D, AveragePooling2D

def build_model():

    model = Sequential()
    model.add(Conv2D(filters=6, kernel_size=(5, 5), strides=(1, 1), padding='valid', input_shape=(28, 28, 1))) # (24, 24, 6)
    model.add(AveragePooling2D(pool_size=(2, 2), strides=(2, 2))) # (12, 12, 6)
    model.add(Conv2D(filters=16, kernel_size=(5, 5), strides=(1, 1), padding='valid')) # (8, 8, 16)
    model.add(AveragePooling2D(pool_size=(2, 2), strides=(2, 2))) # (4, 4, 16)

    model.add(Flatten())
    model.add(Dense(84, activation='softmax'))

    model.add(Dense(10, activation='softmax'))

    model.compile(loss='categorical_crossentropy', 
                  optimizer='sgd', 
                  metrics=['accuracy'])

    return model


model = build_model()
# model.build(input_shape=x_train.shape)
model.summary()

Original, I used model.build(input_shape=x_train.shape) (the noted line), and keras think the batch_size should be what we put here.
So I commented this line and add input_shape in the first layer in model. Now when you see model.summary() the batch_size would be None, ready for you fill in model.fit(batch_size). Hope that helps!

If you are using a RNN careful with return_sequence of your layer arguments. Check you last layer of LSTM. You should not insert return_sequence argument to you last LSTM layer.

When you are insert return_sequence to your LSTM layer, this means your layer outputs will be the inputs of next LSTM layer. However you do not have a LSTM layer after return_sequence layer.

If you delete this layer you will probably get through that error cleanly.

Hope this helps to you.

I have the same problem.
tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [300,3] vs. [2700,3]
[[{{node training/RMSprop/gradients/loss/dense_1_loss/mul_grad/BroadcastGradientArgs}}]]

the code:
sentence_input = Input(shape=(1000,), dtype='int32')
embedded_sequences = embedding_layer(sentence_input)

print(embedded_sequences.shape)

l_lstm = Bidirectional(GRU(100, return_sequences=True))(embedded_sequences)
l_att = AttLayer(100)(l_lstm)

a = Lambda(concated,output_shape=(300,))(l_att) #concated is concat function.

preds = Dense(3, activation='softmax')(a)
model = Model(sentence_input, preds)
model.summary()
model.compile(loss='categorical_crossentropy',optimizer='rmsprop',metrics=['acc'])
print("model fitting - attention network")
h = model.fit(x_train, y_train, validation_data=(x_val, y_val), epochs=2, batch_size=300)
plt.plot(h.history["loss"], label="train_loss")
plt.plot(h.history["val_loss"], label="val_loss")
plt.plot(h.history["acc"], label="train_acc")
plt.plot(h.history["val_acc"], label="val_acc")
plt.legend()
plt.show()

who can help me ? thanks !

Even when you set the batch size as 1, you would get the error "InvalidArgumentError: Incompatible shapes" while evaluating the model. It should have raised an error at the time of training process.

Hi,
I seem to encounter the same problem. And I can't seem to figure it out. Could you make it a bit more clearer?
Thank you.

Hi,
    I'm very sorry. I haven't solved this problem. I'm still learning.

------------------ 原始邮件 ------------------
发件人: "Arjun-Arvindakshan"<[email protected]>;
发送时间: 2019年10月31日(星期四) 晚上7:58
收件人: "keras-team/keras"<[email protected]>;
抄送: "如若初见"<[email protected]>;"Comment"<[email protected]>;
主题: Re: [keras-team/keras] Error InvalidArgumentError: Incompatible shapes when using accuracy metric, sparse_categorical_crossentropy, and batch size > 1 (#11749)

Even when you set the batch size as 1, you would get the error "InvalidArgumentError: Incompatible shapes" while evaluating the model. It should have raised an error at the time of training process.

Hi,
I seem to encounter the same problem. And I can't seem to figure it out. Could you make it a bit more clearer?
Thank you.


You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or unsubscribe.

I was facing the same issue. I solved it by removing the batch_size from the model definition.
I went from :
model1.add(LSTM(neurons, batch_input_shape=(batch_size, max_len, n_feat), stateful=False, recurrent_activation='hard_sigmoid', activation='sigmoid'))

to:

model1.add(LSTM(neurons, input_shape=(max_len, n_feat), stateful=False, recurrent_activation='hard_sigmoid', activation='sigmoid'))

I came across the same issue, downgrading from 2.2.4 to 2.2.2 doesn't help.

Using train data flow from directory I get this: Found 166105 images belonging to 84 classes.. Passing into a neural network generating this error:

Versions:

tensorflow: '2.1.0'- cpu
keras: '2.2.4-tf'
Python: 3.7.6
train_datagen = ImageDataGenerator(
      rescale=1./255,
      rotation_range=40,
      width_shift_range=0.2,
      height_shift_range=0.2,
      shear_range=0.2,
      zoom_range=0.2,
      horizontal_flip=True,
      fill_mode='nearest'
)

train_generator = train_datagen.flow_from_directory(
        '/Users/royakash/Desktop/Images',
        target_size=(100, 100),
        batch_size=1,
        class_mode='categorical'
)

def create_model():
    model = tf.keras.models.Sequential([
#         tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(100, 100, 1)),
#         tf.keras.layers.MaxPooling2D(2, 2),
#         tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
#         tf.keras.layers.MaxPooling2D(2,2),
#         tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
#         tf.keras.layers.MaxPooling2D(2,2),
#         tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
#         tf.keras.layers.MaxPooling2D(2,2),
#         tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(512, activation='relu'),
        tf.keras.layers.Dense(128, activation='relu'),
        tf.keras.layers.Dense(84, activation='softmax')
    ])

    return model

model.compile(optimizer= Adam(), loss='sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy'])

history = model.fit(train_generator, epochs=30, verbose=2)

Generating error:

```InvalidArgumentError: Incompatible shapes: [1,84] vs. [1,100,100]
[[node metrics/sparse_categorical_accuracy/Equal (defined at :3) ]] [Op:__inference_distributed_function_1074]

Function call stack:
distributed_function```

I had a problem of this kind with tensorflow version 2.2.0. I solved the issue when I uninstalled tensorflow and installed an older version:
pip uninstall tensorflow
pip install tensorflow==1.15.0

I have the same problem, but it happened for a reason different than all above. the issue happened in my case when I specified "mask_zero= True" in the embeddings layer. My model is similar to @pabloppp model here. But I am using loss= sparse_catagorical_crossentropy and metrics= [accuracy, perplexity]. The error seems to happen because of the perplexity metric.
ValueError: Incompatible shapes: values () vs sample_weight (None, 90)
Any thoughts?!

Was this page helpful?
0 / 5 - 0 ratings