{
  "id": 326457,
  "title": "InvalidArgumentError:  required broadcastable shapes",
  "url": "/competitions/digit-recognizer/discussion/326457",
  "author_name": "Laurin Schaller",
  "post_date": "2022-05-22T14:47:29.027000",
  "votes": 0,
  "comment_count": 1,
  "views": null,
  "content": "<p>Hi, I have the images as numpy array with shape (42000, 28, 28, 1) and the labels also as a numpy array with shape (42000,). Then in the first layer I define this input shape:</p>\n<p><code>model = models.Sequential()\nmodel.add(layers.Conv2D(36, 3, activation='relu', input_shape=(28,28,1)))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(64, 3, activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(128, 3, activation='relu'))\nmodel.add(layers.Dense(10))\nmodel.summary()</code></p>\n<p>I then compile the model with sparse categorical crossentropy because the labels are integers from 0-9 and fit with the training data.</p>\n<p><code>model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy'])\nhistory = model.fit(x=trainImages, y=trainLabels, epochs=10, shuffle=False, validation_split=0.25)</code></p>\n<p>But then I get the error \"InvalidArgumentError:  required broadcastable shapes\". I don't understand why, I thought I provided the data with the right shape.</p>\n<p>Does anybody know what the problem is? Any help would be appreciated, thanks.</p>",
  "messages": [
    {
      "id": 1798210,
      "postDate": "2022-05-22T19:13:21.857Z",
      "content": "<p>I had simply forgotten to add a Flatten layer.</p>",
      "rawMarkdown": "I had simply forgotten to add a Flatten layer."
    },
    {
      "id": 1797937,
      "postDate": "2022-05-22T14:47:29.027Z",
      "content": "<p>Hi, I have the images as numpy array with shape (42000, 28, 28, 1) and the labels also as a numpy array with shape (42000,). Then in the first layer I define this input shape:</p>\n<p><code>model = models.Sequential()\nmodel.add(layers.Conv2D(36, 3, activation='relu', input_shape=(28,28,1)))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(64, 3, activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(128, 3, activation='relu'))\nmodel.add(layers.Dense(10))\nmodel.summary()</code></p>\n<p>I then compile the model with sparse categorical crossentropy because the labels are integers from 0-9 and fit with the training data.</p>\n<p><code>model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy'])\nhistory = model.fit(x=trainImages, y=trainLabels, epochs=10, shuffle=False, validation_split=0.25)</code></p>\n<p>But then I get the error \"InvalidArgumentError:  required broadcastable shapes\". I don't understand why, I thought I provided the data with the right shape.</p>\n<p>Does anybody know what the problem is? Any help would be appreciated, thanks.</p>",
      "rawMarkdown": "Hi, I have the images as numpy array with shape (42000, 28, 28, 1) and the labels also as a numpy array with shape (42000,). Then in the first layer I define this input shape:\n\n`model = models.Sequential()\nmodel.add(layers.Conv2D(36, 3, activation='relu', input_shape=(28,28,1)))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(64, 3, activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(128, 3, activation='relu'))\nmodel.add(layers.Dense(10))\nmodel.summary()`\n\nI then compile the model with sparse categorical crossentropy because the labels are integers from 0-9 and fit with the training data.\n\n`model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy'])\nhistory = model.fit(x=trainImages, y=trainLabels, epochs=10, shuffle=False, validation_split=0.25)`\n\nBut then I get the error \"InvalidArgumentError:  required broadcastable shapes\". I don't understand why, I thought I provided the data with the right shape.\n\nDoes anybody know what the problem is? Any help would be appreciated, thanks."
    }
  ],
  "comments": [
    {
      "id": 1798210,
      "author_name": "Laurin Schaller",
      "author_url": "",
      "post_date": "2022-05-22T19:13:21.857000",
      "content": "<p>I had simply forgotten to add a Flatten layer.</p>",
      "votes": 0,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "1798210": "I had simply forgotten to add a Flatten layer.",
    "1797937": "Hi, I have the images as numpy array with shape (42000, 28, 28, 1) and the labels also as a numpy array with shape (42000,). Then in the first layer I define this input shape:\n\n`model = models.Sequential()\nmodel.add(layers.Conv2D(36, 3, activation='relu', input_shape=(28,28,1)))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(64, 3, activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(128, 3, activation='relu'))\nmodel.add(layers.Dense(10))\nmodel.summary()`\n\nI then compile the model with sparse categorical crossentropy because the labels are integers from 0-9 and fit with the training data.\n\n`model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy'])\nhistory = model.fit(x=trainImages, y=trainLabels, epochs=10, shuffle=False, validation_split=0.25)`\n\nBut then I get the error \"InvalidArgumentError:  required broadcastable shapes\". I don't understand why, I thought I provided the data with the right shape.\n\nDoes anybody know what the problem is? Any help would be appreciated, thanks."
  }
}