Error During Python Train Data so how to train data

5.5k Views Asked by At

Epoch 1/100

ValueError Traceback (most recent call last) in <cell line: 1>() ----> 1 model_history=classifier.fit(X_train,Y_train,batch_size=100,validation_split=0.2,epochs = 100)

1 frames /usr/local/lib/python3.9/dist-packages/keras/engine/training.py in tf__train_function(iterator) 13 try: 14 do_return = True ---> 15 retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope) 16 except: 17 do_return = False

ValueError: in user code:

File "/usr/local/lib/python3.9/dist-packages/keras/engine/training.py", line 1284, in train_function  *
    return step_function(self, iterator)
File "/usr/local/lib/python3.9/dist-packages/keras/engine/training.py", line 1268, in step_function  **
    outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.9/dist-packages/keras/engine/training.py", line 1249, in run_step  **
    outputs = model.train_step(data)
File "/usr/local/lib/python3.9/dist-packages/keras/engine/training.py", line 1051, in train_step
    loss = self.compute_loss(x, y, y_pred, sample_weight)
File "/usr/local/lib/python3.9/dist-packages/keras/engine/training.py", line 1109, in compute_loss
    return self.compiled_loss(
File "/usr/local/lib/python3.9/dist-packages/keras/engine/compile_utils.py", line 265, in __call__
    loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/lib/python3.9/dist-packages/keras/losses.py", line 142, in __call__
    losses = call_fn(y_true, y_pred)
File "/usr/local/lib/python3.9/dist-packages/keras/losses.py", line 268, in call  **
    return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/usr/local/lib/python3.9/dist-packages/keras/losses.py", line 2156, in binary_crossentropy
    backend.binary_crossentropy(y_true, y_pred, from_logits=from_logits),
File "/usr/local/lib/python3.9/dist-packages/keras/backend.py", line 5707, in binary_crossentropy
    return tf.nn.sigmoid_cross_entropy_with_logits(

ValueError: `logits` and `labels` must have the same shape, received ((None, 10) vs (None, 1)).

ValueError: logits and labels must have the same shape, received ((None, 10) vs (None, 1)).

Please Resolve this error as early as possible

4

There are 4 best solutions below

0
pavan chand parakala On

I think you are starting to learn tensorflow and implementing only one feature variable and one label; I ran into the same issue. Try using this, which resolved mine, and hopefully yours too:

model.fit(tf.expand_dims(x,axis=-1),y,epochs=100)
0
Md. Abdullah Al Mamun On

ValueError: logits and labels must have the same shape, received ((None, 10) vs (None, 1)).

This error comes perhaps,

in your model architecture, in the last layer (also called the logit layer), you used 1 neuron; change it to 10, because you are classifying 10 different categories.

you might be using binaryCrossEntropy loss, use instead cross entropy loss.

the summary is, if you are creating a binary classifier then your labels must be binary values. Or if you are creating a multi-classifier and your architecture's last layer doesn't match with your labels category length then you will get this kind of error.

0
rakitten On

This worked for me I had 4 classes

model.add(layers.Dense(4, activation='softmax', kernel_regularizer=regularizers.l2(0.001)))

In your case, It will be

model.add(layers.Dense(10, activation='softmax', kernel_regularizer=regularizers.l2(0.001)))

0
MonkyJr On

I think you can try some of these:

  • Check that the number of labels correspond when extranting labels from data and when defining the last layer in the model implemented.

ValueError: logits and labels must have the same shape, received ((None, 10) vs (None, 1)). So for this error check in your model definition how the last(prediction) dense layer looks like

  • I got this error sometimes but its more focused on how i declare my labels, especifically the loss function implemented and if labels are categorical or not.

The error in this case will appear different but you can also have this to consider when training your models;)