What does this error mean: "got logits shape [3,3] and labels shape [33]"?

Issue

I created two random arrays in NumPy and then I used x and y in model.fit() but I got this error:

Node:
‘sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits’
logits and labels must have the same first dimension, got logits shape
[3,3] and labels shape [33] [[{{node
sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}}]]
[Op:__inference_train_function_6412]

What does this error mean?

Code:

import numpy as np
import pandas as pd 
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import numpy

x=np.random.randint(40,100,33).reshape(3, 11)
y=np.random.randint(44,66,33).reshape(3, 11)

model = keras.Sequential(
    [
        layers.Dense(3, activation="relu", name="layer1"),
        layers.Dense(3, activation="relu", name="layer2"),
        layers.Dense(3, name="layer3"),
    ]
)
# Call model on a test input

model.compile(optimizer=keras.optimizers.Adam(learning_rate=.0001),loss='sparse_categorical_crossentropy',metrics=['accuracy'])

model.fit(x,y,batch_size=10,epochs=5,verbose=2)

enter image description here

enter image description here

Solution

You need to consider multiple steps:

  1. The shape of x, y should be equal in the first dimension. you have error here.
  2. Read Doc numpy.random.randint. you write np.random.randint(44,66,33) so you have 66-44 = 20 different classes for y, but at the last layer, you write : layers.Dense(3). you have an error here.
  3. Add layers.Input(shape=(...,)) to your network base shape of x.
  4. For generating random numbers between [0,1) you can use numpy.random.rand

Try like below:

import numpy as np
import pandas as pd 
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import numpy

x=np.random.rand(40,100, 3).reshape(40,-1)
y=np.random.randint(0,3, size=40)

model = keras.Sequential(
    [
        layers.Input(shape=(300,)),
        layers.Dense(3, activation="relu", name="layer1"),
        layers.Dense(3, activation="relu", name="layer2"),
        layers.Dense(3, name="layer3"),
    ]
)

model.compile(optimizer=keras.optimizers.Adam(learning_rate=.0001),
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
model.fit(x,y,batch_size=10,epochs=2)  

Output:

Epoch 1/2
4/4 [==============================] - 1s 5ms/step - loss: 5.9796 - accuracy: 0.2500
Epoch 2/2
4/4 [==============================] - 0s 6ms/step - loss: 5.9774 - accuracy: 0.2500

Answered By – I'mahdi

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published