-
Notifications
You must be signed in to change notification settings - Fork 19.4k
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
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.
|
Changing my metric from |
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.
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.
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. The problem seems to be with the keras callback function while implementing the metric. |
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 |
Using TensorFlow backend. Working. |
@kaushib11 |
Using TensorFlow backend. 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. |
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.
The model:
Training of the model: Epoch 1/50 For predictions:
Try this, let me know if you come across any erros? |
I'm not sure, but I'd say that using 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] |
Could you point out where is that happening? |
Based in the error that I showed in my first comment
I'm just guessing it's either in the |
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).
|
Thank you! |
It fixes the issue. |
Or you can define customized accuracy:
and then use |
@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! |
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.
insuring that sparse_categorical_accuracy is imported from
|
@pabloppp I was getting the same error and my issue got resolved when I removed the
|
I have the similar issue and checked the keras version was 2.2.4 |
I solved this problem.
Original, I used |
If you are using a RNN careful with When you are insert If you delete this layer you will probably get through that error cleanly. Hope this helps to you. |
I have the same problem. the code: print(embedded_sequences.shape)l_lstm = Bidirectional(GRU(100, return_sequences=True))(embedded_sequences) a = Lambda(concated,output_shape=(300,))(l_att) #concated is concat function. preds = Dense(3, activation='softmax')(a) |
who can help me ? thanks ! |
Hi, |
Hi,
I'm very sorry. I haven't solved this problem. I'm still learning.
…------------------ 原始邮件 ------------------
发件人: "Arjun-Arvindakshan"<notifications@github.com>;
发送时间: 2019年10月31日(星期四) 晚上7:58
收件人: "keras-team/keras"<keras@noreply.github.com>;
抄送: "如若初见"<903258755@qq.com>;"Comment"<comment@noreply.github.com>;
主题: 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. 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: Versions:
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:
|
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: |
I have the same problem, but it happened for a reason different than all above. the issue happened in my case when I specified |
hi, my code is:
error of my code is: `
code of maxpooling and unpooling execusion from a part of SegNet code:
|
hi.... can you check my code? : -- coding: utf-8 --""" @author: FaraDars from keras import layers from keras import optimizers, losses #=============================================================================== Prepare datafrom keras.datasets import mnist (x_train, _), (x_test, _) = mnist.load_data() x_train = x_train.astype('float32') / 255. pool_size=(2,2) #------------------- 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 + Modelinput_img = layers.Input(shape=(28, 28, 1)) # adapt this if using x1 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(input_img) pool_1, mask_1 = MaxPoolingWithArgmax2D(pool_size=(2, 2))(x2) #x = layers.MaxPooling2D((2, 2), padding='same')(x) pool_2, mask_2 = MaxPoolingWithArgmax2D(pool_size=(2, 2), padding="same")(x4) #x = layers.MaxPooling2D((2, 2), padding='same')(x) #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-dimensionalunpool_1 = MaxUnpooling2D(pool_size)([pool_2, mask_2]) x5 = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(unpool_1) unpool_2 = MaxUnpooling2D(pool_size)([x6, mask_1]) x7 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(unpool_2) #unpool_2 = MaxUnpooling2D(pool_size)([x4, mask_2]) #x5 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(unpool_2) decoded = layers.Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x8) autoencoder = Model(input_img, decoded) print(autoencoder.summary()) #=============================================================================== Train the Modelautoencoder.fit(x_train, x_train, #=============================================================================== Predict + Visualizationimport matplotlib.pyplot as plt decoded_imgs = autoencoder.predict(x_test) n = 10 # how many digits we will display
plt.show() error of this code: InvalidArgumentError: 2 root error(s) found. code of layers1: from keras import backend as K class MaxPoolingWithArgmax2D(Layer):
class MaxUnpooling2D(Layer):
|
tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [2,16] vs. [8,16] 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 |
I'm having the same problem. Changing loss from |
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 andmetrics=["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.
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
It doesn't really matter what kind of model I use, the importat thing is that this 4 things are true:
sparse_categorical_crossentropy
as its loss functionaccuracy
as one of its metricsThen, I just fit the model.
After the first batch is processed, when tring to calculate the accuracy I get the following error:
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.
The text was updated successfully, but these errors were encountered: