简体   繁体   中英

ValueError: Shapes (None, 2) and (None, 3) are incompatible

So this is the code I'm trying to run

from sklearn.datasets import make_blobs
from tensorflow.keras.utils import to_categorical
from keras.layers import *
from keras import metrics 
from tensorflow.keras.optimizers import SGD
from keras.models import Sequential
from keras.layers import Dense
import numpy as np


def prepa ():
X, y = make_blobs(n_samples=1000,centers=3, n_features=2,random_state=2)

testX=X[:499]
trainX=X[500:999]
testy=y[:499]
trainy=y[500:999]

return trainX,trainy,testX,testy


trainX, testX, trainy,testy=prepa()


#define the model
model = Sequential()  # Création d'un réseau de neurones vide 
model.add(Dense(50,input_dim=2,activation="relu",kernel_initializer='he_uniform'))
model.add(Dense(3,activation="Softmax"))

#compile the model
opt = SGD(learning_rate=0.001)
model.compile(loss='categorical_crossentropy',optimizer=opt,metrics=['accuracy'])

fit the model 
history=model.fit(trainX, trainy, validation_data=(testX, testy), epochs=200, verbose=0)

But I receive this Error:

ValueError Traceback (most recent call last)

in () 1 #fit the model ----> 2 history=model.fit(trainX, trainy, validation_data=(testX, testy), epochs=200, verbose=0)

9 frames

/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs) 992 except Exception as e: # pylint:disable=broad-except 993 if hasattr(e, "ag_error_metadata"): --> 994 raise e.ag_error_metadata.to_exception(e) 995 else: 996 raise

ValueError: in user code:

/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:853 train_function  *
    return step_function(self, iterator)
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:842 step_function  **
    outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:1286 run
    return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:2849 call_for_each_replica
    return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:3632 _call_for_each_replica
    return fn(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:835 run_step  **
    outputs = model.train_step(data)
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:789 train_step
    y, y_pred, sample_weight, regularization_losses=self.losses)
/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py:201 __call__
    loss_value = loss_obj(y_t, y_p, sample_weight=sw)
/usr/local/lib/python3.7/dist-packages/keras/losses.py:141 __call__
    losses = call_fn(y_true, y_pred)
/usr/local/lib/python3.7/dist-packages/keras/losses.py:245 call  **
    return ag_fn(y_true, y_pred, **self._fn_kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:206 wrapper
    return target(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/keras/losses.py:1666 categorical_crossentropy
    y_true, y_pred, from_logits=from_logits, axis=axis)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:206 wrapper
    return target(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/keras/backend.py:4839 categorical_crossentropy
    target.shape.assert_is_compatible_with(output.shape)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py:1161 assert_is_compatible_with
    raise ValueError("Shapes %s and %s are incompatible" % (self, other))

ValueError: Shapes (None, 2) and (None, 3) are incompatible

As, your error depicts, there is something wrong with the shapes. You should always check your data shapes before training the model to avoid any inconsistency issues.

So, when I checked, the shape of testX is (501,) which should be (501,2) and this is happening because testX is getting assigned to trainy in your code.

Replace

trainX,testX,trainy,testy = prepa()

with

trainX,trainy,testX,testy = prepa()

because this is what you are returning from the prepa() function and unpacking should be in suitable/similar order to maintain the assignments.


Also, you should use following indexing in prepa() function to get the whole dataset.

testX = X[:499]
trainX = X[499:]
testy = y[:499]
trainy = y[499:]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM