Trying to understand "Cannot batch tensors with different shapes in component 0"

1,865 views
Skip to first unread message

Amrish

unread,
Nov 18, 2019, 12:52:58 PM11/18/19
to Discuss

I am getting the following error while using a data generator for my Keras model. There are many articles on StackOverflow on this specific error, but I think none of them apply to my case (or at least I couldn't translate the root cause to this one)


tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot batch tensors with different shapes in component 0. First element had shape [10] and element 21 had shape [7]


Everything works fine when I use a batch size of 1, but things start breaking for a batch size > 1. It will be nice to understand what exactly is going on here?


Following is a simple Keras model to duplicate the issue


def fetch_model():
  new_model = Sequential()
  new_model.add(BatchNormalization(input_shape=(10, 128)))  # The input shape excludes batch
  new_model.add(Flatten())
  new_model.add(Dense(2048, activation="relu"))
  new_model.add(Dense(audioset_label_count, activation="sigmoid"))
  new_model.compile("adam", loss="binary_crossentropy", metrics=["accuracy"])
  return new_model


and I am training the model on AudioSet data, and I have added a sample of that to https://github.com/i2infinity/python-demo


For reproducing this issue, please run python train.py on https://github.com/i2infinity/python-demo.git

Kailash Ahirwar

unread,
Nov 20, 2019, 3:34:00 AM11/20/19
to Discuss
This is a very common error. All of your samples are in different dimensions. You need to look at the dimensions.

Amrish Ravidas

unread,
Nov 22, 2019, 3:03:45 AM11/22/19
to Kailash Ahirwar, Discuss
Hey Kailash - Thanks for getting back. I double-checked the dataset and I verified that all the audio features within the *.tfrecord (AudioSet) are of the same size. Here is a colab workspace to quickly duplicate the issue https://colab.research.google.com/drive/1zo2OlADeFR7AodY2D7gq9aBinDelLgd4 I could also reproduce this on a training set containing just 2 tfrecord's in it. I think I am missing something small, but unable to find the root cause!

--
You received this message because you are subscribed to the Google Groups "Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss+u...@tensorflow.org.
To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/discuss/6ac3ecf5-b826-437d-ac39-7142a9a13425%40tensorflow.org.


--
Thanks,
Amrish
Reply all
Reply to author
Forward
0 new messages