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 incompatible shapes when using batch size >1 #13369

Closed
NBasty opened this issue Sep 27, 2019 · 1 comment
Closed

Error incompatible shapes when using batch size >1 #13369

NBasty opened this issue Sep 27, 2019 · 1 comment
Labels
type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited.

Comments

@NBasty
Copy link

NBasty commented Sep 27, 2019

  • Have I written custom code: yes and it works fine for batch size 1
  • OS Platform and Distribution: Ubuntu 18.04
  • TensorFlow backend: yes
  • TensorFlow version: 1.13.1 (also tried on others 1.14.0)
  • Keras version: tried all versions from 2.2.1 to 2.3,
  • Python version: 3.6.8
  • CUDA/cuDNN version: 10.0
  • GPU model and memory: (tried on) titan V and GeForce 12GB

When I try to train my model with batch size greater than 1 it always gives me an error such as this one below, where he incompatible shape on the left is the product of the batch size and the shape on the right. There is a similar question #11749 but the proposed fix of using another version does not work for me. I have tried increasing the batch size to 2 with all keras versions from 2.2.1 to 2.3 and that does not solve the problem for me. Is anybody else still experiencing this issue? Could anybody help?

InvalidArgumentError Traceback (most recent call last)
in
12
13 history = model.fit(x_train, y_train, batch_size = 2, epochs = 200,
---> 14 validation_data = (x_val, y_val), callbacks = callbacks_list)

...

InvalidArgumentError: Incompatible shapes: [7077888] vs. [3538944]
[[{{node training/Adam/gradients/loss_1/conv3d_54_loss/mul_grad/BroadcastGradientArgs}}]]
[[{{node loss_1/mul}}]]

It works quite well as it is right now with batch size 1 but I want to increase the batch size for more stability in my training.

Thanks.

@naiveHobo
Copy link

Could you post the output shapes of your model and the shape of the tensor containing the labels? I faced a similar issue recently.

The output shape for my model is [batch_size, seq_len, num_classes] where each row in each batch is a probability distribution over num_classes.

The shape for the labels in my dataset object was [batch_size, seq_len]. This threw an incompatible shape error. After searching for a bit, I found out that my metric fucntion (sparse_categorical_accuracy), tries to squeeze the last dimension of the labels tensor and throws an error is the last dimension is not 1.

Changing my labels tensor to the shape [batch_size, seq_len, 1] fixed the problem.

@gowthamkpr gowthamkpr added backend:tensorflow type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited. labels Sep 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited.
Projects
None yet
Development

No branches or pull requests

4 participants