Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
pabloppp opened this issue Nov 28, 2018 · 41 comments
Labels
To investigate Looks like a bug. It needs someone to investigate.

Comments

@pabloppp
Copy link

pabloppp commented Nov 28, 2018

⚠️⚠️⚠️
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.

@pabloppp pabloppp changed the title Error InvalidArgumentError: Incompatible shapes when using accuracy metric Error InvalidArgumentError: Incompatible shapes when using accuracy metric, sparse_categorical_crossentropy, and batch size > 1 Nov 28, 2018
@gabrieldemarmiesse gabrieldemarmiesse added To investigate Looks like a bug. It needs someone to investigate. type:tensorFlow labels Nov 29, 2018
@WatanabeSmith
Copy link

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

@WatanabeSmith
Copy link

Changing my metric from sparse_categorical_accuracy to categorical_accuracy avoids the error

@kaushikb11
Copy link

kaushikb11 commented Dec 4, 2018

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.

@kaushikb11
Copy link

kaushikb11 commented Dec 5, 2018

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.

@pabloppp
Copy link
Author

pabloppp commented Dec 5, 2018

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)

@kaushikb11
Copy link

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.

@pabloppp
Copy link
Author

pabloppp commented Dec 5, 2018

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

@kaushikb11
Copy link

@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.

@pabloppp
Copy link
Author

pabloppp commented Dec 5, 2018

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

@kaushikb11
Copy link

kaushikb11 commented Dec 5, 2018

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?

@pabloppp
Copy link
Author

pabloppp commented Dec 5, 2018

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.

@kaushikb11
Copy link

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?

@pabloppp
Copy link
Author

pabloppp commented Dec 5, 2018

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]

@kaushikb11
Copy link

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?

@pabloppp
Copy link
Author

pabloppp commented Dec 5, 2018

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

@pabloppp
Copy link
Author

pabloppp commented Dec 5, 2018

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

@pabloppp pabloppp closed this as completed Dec 5, 2018
@subashp
Copy link

subashp commented Jan 30, 2019

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!

@aman-crest
Copy link

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.

@dilshatu
Copy link

dilshatu commented Jun 4, 2019

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'

@ghost
Copy link

ghost commented Jun 30, 2019

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

@ghost
Copy link

ghost commented Jun 30, 2019

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

@touhi99
Copy link

touhi99 commented Jul 2, 2019

I had the similar problem on Keras 2.2.4.

Downgrading to 2.2.2 version somehow solves the problem for me!

@husi253
Copy link

husi253 commented Jul 2, 2019

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.

@theisjendal
Copy link

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

@imrrahul
Copy link

@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!!!

@hafezmg48
Copy link

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

@sharkdeng
Copy link

sharkdeng commented Aug 15, 2019

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!

@Spawnfile
Copy link

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.

@xiaojiangwangjiang
Copy link

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

@xiaojiangwangjiang
Copy link

who can help me ? thanks !

@Arjun-Arvindakshan
Copy link

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.

@xiaojiangwangjiang
Copy link

xiaojiangwangjiang commented Nov 8, 2019 via email

@thiago1080
Copy link

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

@Bovey0809
Copy link

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

@theroyakash
Copy link

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:

	 [[node metrics/sparse_categorical_accuracy/Equal (defined at <ipython-input-6-9c043135f786>:3) ]] [Op:__inference_distributed_function_1074]

Function call stack:
distributed_function```

@Nestak2
Copy link

Nestak2 commented Jun 26, 2020

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

@OmniaZayed
Copy link

OmniaZayed commented Jun 29, 2020

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?!

@fatemeh190
Copy link

hi,
my problem is like the title...
please help me to solve it...

my code is:


from keras import layers
from keras.models import Model

from keras import optimizers, losses
from layers1 import MaxPoolingWithArgmax2D, MaxUnpooling2D
#import tensorflow as tf

#===============================================================================
# Prepare data

from keras.datasets import mnist
import numpy as np

(x_train, _), (x_test, _) = mnist.load_data()

x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1))  # adapt this if using `channels_first` image data format
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1))  # adapt this if using `channels_first` image data format


pool_size=(2,2)


#-------------------
#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())
#===============================================================================
# Create Layers + Model

input_img = layers.Input(shape=(28, 28, 1))  # adapt this if using `channels_first` image data format

x1 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x2 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x1)


pool_1, mask_1 = MaxPoolingWithArgmax2D(pool_size=(2, 2))(x2)

#x = layers.MaxPooling2D((2, 2), padding='same')(x)
x3 = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(pool_1)
x4 = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x3)

pool_2, mask_2 = MaxPoolingWithArgmax2D(pool_size=(2, 2))(x4)


#x = layers.MaxPooling2D((2, 2), padding='same')(x)
#x3 = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(pool_2)

#pool_3, mask_3 = MaxPoolingWithArgmax2D(pool_size=(2, 2))(x3)


#encoded = layers.MaxPooling2D((2, 2), padding='same')(x)

# at this point the representation is (4, 4, 8) i.e. 128-dimensional

unpool_1 = MaxUnpooling2D(pool_size)([pool_2, mask_2])

x5 = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(unpool_1)
x6 = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x5)

unpool_2 = MaxUnpooling2D(pool_size)([x6, mask_1])

x7 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(unpool_2)
x8 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x7)

#unpool_2 = MaxUnpooling2D(pool_size)([x4, mask_2])

#x5 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(unpool_2)
#x5 = layers.UpSampling2D((2, 2))(x5)
#unpool_3 = MaxUnpooling2D(pool_size)([x5, mask_1])
#x6 = layers.Conv2D(16, (3, 3), activation='relu')(unpool_3)
#x = layers.UpSampling2D((2, 2))(x)

decoded = layers.Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x8)
#decoded = np.reshape(x8,input_img)
#decoded = tf.reshape(decoded, [28*28*1])
#decoded= layers.resize((28, 28, None))(decoded)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer=optimizers.Adam(), loss=losses.sparse_categorical_crossentropy)

print(autoencoder.summary())

#===============================================================================
# Train the Model

autoencoder.fit(x_train, x_train,
                epochs=10,
                batch_size=128,
                shuffle=(True),
                validation_data=(x_test, x_test))

error of my code is:

`


InvalidArgumentError: 2 root error(s) found.
  (0) Invalid argument: Incompatible shapes: [128,14,14,16] vs. [8]
	 [[{{node max_unpooling2d_2/max_unpooling2d_2/mul_4}}]]
	 [[Mean/_185]]
  (1) Invalid argument: Incompatible shapes: [128,14,14,16] vs. [8]
	 [[{{node max_unpooling2d_2/max_unpooling2d_2/mul_4}}]]
0 successful operations.
0 derived errors ignored.

code of maxpooling and unpooling execusion from a part of SegNet code:


from keras import backend as K
from keras.layers import Layer
import tensorflow as tf



class MaxPoolingWithArgmax2D(Layer):
    def __init__(self, pool_size=(2, 2), strides=(2, 2), padding="same", **kwargs):
        super(MaxPoolingWithArgmax2D, self).__init__(**kwargs)
        self.padding = padding
        self.pool_size = pool_size
        self.strides = strides

    def call(self, inputs, **kwargs):
        padding = self.padding
        pool_size = self.pool_size
        strides = self.strides
        if K.backend() == "tensorflow":
            ksize = [1, pool_size[0], pool_size[1], 1]
            padding = padding.upper()
            strides = [1, strides[0], strides[1], 1]
            output, argmax = tf.nn.max_pool_with_argmax(
                inputs, ksize=ksize, strides=strides, padding=padding
            )
        else:
            errmsg = "{} backend is not supported for layer {}".format(
                K.backend(), type(self).__name__
            )
            raise NotImplementedError(errmsg)
        argmax = K.cast(argmax, K.floatx())
        return [output, argmax]

    def compute_output_shape(self, input_shape):
        ratio = (1, 2, 2, 1)
        output_shape = [
            dim // ratio[idx] if dim is not None else None
            for idx, dim in enumerate(input_shape)
        ]
        output_shape = tuple(output_shape)
        return [output_shape, output_shape]

    def compute_mask(self, inputs, mask=None):
        return 2 * [None]


class MaxUnpooling2D(Layer):
    def __init__(self, size=(2, 2), **kwargs):
        super(MaxUnpooling2D, self).__init__(**kwargs)
        self.size = size

    def call(self, inputs, output_shape=None):
        updates, mask = inputs[0], inputs[1]
        with tf.variable_scope(self.name):
            mask = tf.cast(mask, "int32")
            input_shape = tf.shape(updates, out_type="int32")
            #  calculation new shape
            if output_shape is None:
                output_shape = (
                    input_shape[0],
                    input_shape[1] * self.size[0],
                    input_shape[2] * self.size[1],
                    input_shape[3],
                )
            self.output_shape1 = output_shape

            # calculation indices for batch, height, width and feature maps
            one_like_mask = tf.ones_like(mask, dtype="int32")
            batch_shape = tf.concat([[input_shape[0]], [1], [1], [1]], axis=0)
            batch_range = tf.reshape(
                tf.range(output_shape[0], dtype="int32"), shape=batch_shape
            )
            b = one_like_mask * batch_range
            y = mask // (output_shape[2] * output_shape[3])
            x = (mask // output_shape[3]) % output_shape[2]
            feature_range = tf.range(output_shape[3], dtype="int32")
            f = one_like_mask * feature_range

            # transpose indices & reshape update values to one dimension
            updates_size = tf.size(updates)
            indices = tf.transpose(tf.reshape(tf.stack([b, y, x, f]), [4, updates_size]))
            values = tf.reshape(updates, [updates_size])
            ret = tf.scatter_nd(indices, values, output_shape)
            return ret

    def compute_output_shape(self, input_shape):
        mask_shape = input_shape[1]
        return (
            mask_shape[0],
            mask_shape[1] * self.size[0],
            mask_shape[2] * self.size[1],
            mask_shape[3],
        )

@fatemeh190
Copy link

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.

hi.... can you check my code? :

-- coding: utf-8 --

"""
Created on Sat Jun 24 00:20:29 2017

@author: FaraDars
"""

from keras import layers
from keras.models import Model

from keras import optimizers, losses
from layers1 import MaxPoolingWithArgmax2D, MaxUnpooling2D
#import tensorflow as tf

#===============================================================================

Prepare data

from keras.datasets import mnist
import numpy as np

(x_train, _), (x_test, _) = mnist.load_data()

x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1)) # adapt this if using channels_first image data format
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1)) # adapt this if using channels_first image data format

pool_size=(2,2)

#-------------------
#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())

#===============================================================================

Create Layers + Model

input_img = layers.Input(shape=(28, 28, 1)) # adapt this if using channels_first image data format

x1 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x2 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x1)

pool_1, mask_1 = MaxPoolingWithArgmax2D(pool_size=(2, 2))(x2)

#x = layers.MaxPooling2D((2, 2), padding='same')(x)
x3 = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(pool_1)
x4 = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x3)

pool_2, mask_2 = MaxPoolingWithArgmax2D(pool_size=(2, 2), padding="same")(x4)

#x = layers.MaxPooling2D((2, 2), padding='same')(x)
#x3 = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(pool_2)

#pool_3, mask_3 = MaxPoolingWithArgmax2D(pool_size=(2, 2))(x3)

#encoded = layers.MaxPooling2D((2, 2), padding='same')(x)

at this point the representation is (4, 4, 8) i.e. 128-dimensional

unpool_1 = MaxUnpooling2D(pool_size)([pool_2, mask_2])

x5 = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(unpool_1)
x6 = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x5)

unpool_2 = MaxUnpooling2D(pool_size)([x6, mask_1])

x7 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(unpool_2)
x8 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x7)

#unpool_2 = MaxUnpooling2D(pool_size)([x4, mask_2])

#x5 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(unpool_2)
#x5 = layers.UpSampling2D((2, 2))(x5)
#unpool_3 = MaxUnpooling2D(pool_size)([x5, mask_1])
#x6 = layers.Conv2D(16, (3, 3), activation='relu')(unpool_3)
#decoded = layers.UpSampling2D((2, 2))(x8)

decoded = layers.Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x8)

autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer=optimizers.Adam(), loss=losses.SparseCategoricalCrossentropy())

print(autoencoder.summary())

#===============================================================================

Train the Model

autoencoder.fit(x_train, x_train,
epochs=10,
batch_size=185,
shuffle=(True),
validation_data=(x_test, x_test))

#===============================================================================

Predict + Visualization

import matplotlib.pyplot as plt

decoded_imgs = autoencoder.predict(x_test)

n = 10 # how many digits we will display
plt.figure(figsize=(20, 4))
for i in range(n):
# display original
ax = plt.subplot(2, n, i+1)
plt.imshow(x_test[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)

# display reconstruction
ax = plt.subplot(2, n, i + 1 + n)
plt.imshow(decoded_imgs[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)

plt.show()

error of this code: InvalidArgumentError: 2 root error(s) found.
(0) Invalid argument: Incompatible shapes: [185,14,14,16] vs. [8]
[[{{node max_unpooling2d_4/max_unpooling2d_4/mul_4}}]]
[[Mean_1/_371]]
(1) Invalid argument: Incompatible shapes: [185,14,14,16] vs. [8]
[[{{node max_unpooling2d_4/max_unpooling2d_4/mul_4}}]]
0 successful operations.
0 derived errors ignored.

code of layers1:

from keras import backend as K
from keras.layers import Layer
import tensorflow as tf

class MaxPoolingWithArgmax2D(Layer):
def init(self, pool_size=(2, 2), strides=(2, 2), padding="same", **kwargs):
super(MaxPoolingWithArgmax2D, self).init(**kwargs)
self.padding = padding
self.pool_size = pool_size
self.strides = strides

def call(self, inputs, **kwargs):
    padding = self.padding
    pool_size = self.pool_size
    strides = self.strides
    if K.backend() == "tensorflow":
        ksize = [1, pool_size[0], pool_size[1], 1]
        padding = padding.upper()
        strides = [1, strides[0], strides[1], 1]
        output, argmax = tf.nn.max_pool_with_argmax(
            inputs, ksize=ksize, strides=strides, padding=padding
        )
    else:
        errmsg = "{} backend is not supported for layer {}".format(
            K.backend(), type(self).__name__
        )
        raise NotImplementedError(errmsg)
    argmax = K.cast(argmax, K.floatx())
    return [output, argmax]

def compute_output_shape(self, input_shape):
    ratio = (1, 2, 2, 1)
    output_shape = [
        dim // ratio[idx] if dim is not None else None
        for idx, dim in enumerate(input_shape)
    ]
    output_shape = tuple(output_shape)
    return [output_shape, output_shape]

def compute_mask(self, inputs, mask=None):
    return 2 * [None]

class MaxUnpooling2D(Layer):
def init(self, size=(2, 2), **kwargs):
super(MaxUnpooling2D, self).init(**kwargs)
self.size = size

def call(self, inputs, output_shape=None):
    updates, mask = inputs[0], inputs[1]
    with tf.compat.v1.variable_scope(self.name):
        mask = tf.cast(mask, "int32")
        input_shape = tf.shape(updates, out_type="int32")
        #  calculation new shape
        if output_shape is None:
            output_shape = (
                input_shape[0],
                input_shape[1] * self.size[0],
                input_shape[2] * self.size[1],
                input_shape[3],
            )
        self.output_shape1 = output_shape

        # calculation indices for batch, height, width and feature maps
        one_like_mask = tf.ones_like(mask, dtype="int32")
        batch_shape = tf.concat([[input_shape[0]], [1], [1], [1]], axis=0)
        batch_range = tf.reshape(
            tf.range(output_shape[0], dtype="int32"), shape=batch_shape
        )
        b = one_like_mask * batch_range
        y = mask // (output_shape[2] * output_shape[3])
        x = (mask // output_shape[3]) % output_shape[2]
        feature_range = tf.range(output_shape[3], dtype="int32")
        f = one_like_mask * feature_range

        # transpose indices & reshape update values to one dimension
        updates_size = tf.size(updates)
        indices = tf.transpose(tf.reshape(tf.stack([b, y, x, f]), [4, updates_size]))
        values = tf.reshape(updates, [updates_size])
        ret = tf.scatter_nd(indices, values, output_shape)
        return ret

def compute_output_shape(self, input_shape):
    mask_shape = input_shape[1]
    return (
        mask_shape[0],
        mask_shape[1] * self.size[0],
        mask_shape[2] * self.size[1],
        mask_shape[3],
    )

@sandeep22-v
Copy link

tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [2,16] vs. [8,16]
[[node gradient_tape/binary_crossentropy/logistic_loss/mul/BroadcastGradientArgs (defined at trainnet.py:36) ]] [Op:__inference_train_function_1380]

I guess you got the above error.

[2,16] means batch_size = 2 and number of classes = 16 .The required parameter for your model is [2,16] but your model is trained with [8,16].What can you do??Very simple,in the training part,set the batch size to 2 instead of 8.As for validation part,be it 2 or 8 or any value,it doesn't affect this.

So just change the training batch size to 2 from 8

@Xu-Justin
Copy link

I'm having the same problem. Changing loss from sparse_categorical_crossentropy to categorical_crossentropy (remember to change the sparse label format to one hot label format) solved the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
To investigate Looks like a bug. It needs someone to investigate.
Projects
None yet
Development

No branches or pull requests