own_data_cnn_implementation_keras icon indicating copy to clipboard operation
own_data_cnn_implementation_keras copied to clipboard

ValueError: x and y must have same first dimension, but have shapes (20,) and (2,)

Open Coralma opened this issue 7 years ago • 10 comments

Hi Friend,

Can you help me to check this issue?

Traceback (most recent call last): File "D:/projects-python/keras-sample/cnn/custom_data_cnn.py", line 217, in plt.plot(xc,train_loss) File "C:\Users\ccc\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\pyplot.py", line 3317, in plot ret = ax.plot(*args, **kwargs) File "C:\Users\ccc\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib_init_.py", line 1898, in inner return func(ax, *args, **kwargs) File "C:\Users\ccc\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\axes_axes.py", line 1406, in plot for line in self._get_lines(*args, **kwargs): File "C:\Users\ccc\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\axes_base.py", line 407, in _grab_next_args for seg in self._plot_args(remaining, kwargs): File "C:\Users\ccc\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\axes_base.py", line 385, in _plot_args x, y = self._xy_from_xy(x, y) File "C:\Users\ccc\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\axes_base.py", line 244, in _xy_from_xy "have shapes {} and {}".format(x.shape, y.shape)) ValueError: x and y must have same first dimension, but have shapes (20,) and (2,)

Coralma avatar Dec 02 '17 13:12 Coralma

same error I have replaced with 5 but its same error

ipa-nhg-dd avatar Feb 14 '18 15:02 ipa-nhg-dd

Did anyone succeeded to handle this problem?

Esaada avatar Apr 05 '18 14:04 Esaada

Even i encountered the same error.Can anyone able to solve the error??

jaggi avatar May 14 '18 08:05 jaggi

Your error will disappear if you don't run the training with callbacks part. Just comment out

from keras import callbacks

filename='model_train_new.csv'
csv_log=callbacks.CSVLogger(filename, separator=',', append=False)

early_stopping=callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=0, verbose=0, mode='min')

filepath="Best-weights-my_model-{epoch:03d}-{loss:.4f}-{acc:.4f}.hdf5"

checkpoint = callbacks.ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='min')

callbacks_list = [csv_log,early_stopping,checkpoint]

hist = model.fit(X_train, y_train, batch_size=16, nb_epoch=num_epoch, verbose=1, validation_data=(X_test, y_test),callbacks=callbacks_list)

The reason for your error is your original training has 20 epochs, but you trained again with callbacks which only trained 2 epochs. Of course there will be error.

tensor-flower avatar May 30 '18 06:05 tensor-flower

@Coralma use numpy and if x and y are values put them in an array like np.array([])

mainadwitiya avatar Jan 22 '20 09:01 mainadwitiya

Your error will disappear if you don't run the training with callbacks part. Just comment out

from keras import callbacks

filename='model_train_new.csv'
csv_log=callbacks.CSVLogger(filename, separator=',', append=False)

early_stopping=callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=0, verbose=0, mode='min')

filepath="Best-weights-my_model-{epoch:03d}-{loss:.4f}-{acc:.4f}.hdf5"

checkpoint = callbacks.ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='min')

callbacks_list = [csv_log,early_stopping,checkpoint]

hist = model.fit(X_train, y_train, batch_size=16, nb_epoch=num_epoch, verbose=1, validation_data=(X_test, y_test),callbacks=callbacks_list)

The reason for your error is your original training has 20 epochs, but you trained again with callbacks which only trained 2 epochs. Of course there will be error.

Thanks for your answer!- -

forge1yc avatar Jun 13 '20 02:06 forge1yc

@tensor-flower Thanks for your answer!- -

forge1yc avatar Jun 13 '20 02:06 forge1yc

Your error will disappear if you don't run the training with callbacks part. Just comment out

from keras import callbacks

filename='model_train_new.csv'
csv_log=callbacks.CSVLogger(filename, separator=',', append=False)

early_stopping=callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=0, verbose=0, mode='min')

filepath="Best-weights-my_model-{epoch:03d}-{loss:.4f}-{acc:.4f}.hdf5"

checkpoint = callbacks.ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='min')

callbacks_list = [csv_log,early_stopping,checkpoint]

hist = model.fit(X_train, y_train, batch_size=16, nb_epoch=num_epoch, verbose=1, validation_data=(X_test, y_test),callbacks=callbacks_list)

The reason for your error is your original training has 20 epochs, but you trained again with callbacks which only trained 2 epochs. Of course there will be error.

please i have a same error but i can't find the callbacks part ! where can i find the callbacks part ??

my error is :
raise ValueError(f"x and y must have same first dimension, but " ValueError: x and y must have same first dimension, but have shapes (10,) and (11,)

Haninu avatar Jan 13 '21 20:01 Haninu

I meet same error message,the reason is i transform two table to arrays,but i ignore that table1 has 51 rows,table2 has 50 rows. But I use same x_value for table1/2, so there is an error. It work well after i transform table1 to 50 rows.

seuwins avatar Jun 02 '21 03:06 seuwins

Hi, i have the same issue, but i dont understand how to properly use callbacks.

czenon1 avatar Dec 03 '21 08:12 czenon1