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)
CodePudding user response:
You need to consider multiple steps:
- The shape of x, y should be equal in the first dimension. you have error here.
- Read Doc
numpy.random.randint
. you writenp.random.randint(44,66,33)
so you have66-44 = 20
different classes for y, but at the last layer, you write :layers.Dense(3)
. you have an error here. - Add
layers.Input(shape=(...,))
to your network base shape ofx
. - For generating random numbers between
[0,1)
you can usenumpy.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