我试图在MNIST数据集上运行3DCNN。我的代码像这样抛出一个ValueError:重塑MNIST数据集上的3DCNN的目标数组大小 - ValueError
ValueError:输入数组应该具有与目标数组相同数量的样本。发现15000个输入样本和60000个目标样本。
这里是我的代码:
import tensorflow
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv3D, MaxPooling3D
from keras import backend as K
batch_size = 128
num_classes = 10
epochs = 12
img_rows, img_cols, img_dep = 28, 28, 4
(x_train, y_train), (x_test, y_test) = mnist.load_data()
print y_train.shape[0]
if K.image_data_format() == 'channels_first':
x_train = x_train.reshape(15000, 1, img_dep, img_rows, img_cols)
x_test = x_test.reshape(2500, 1, img_dep, img_rows, img_cols)
input_shape = (1, img_dep, img_rows, img_cols)
else:
x_train = x_train.reshape(15000, img_dep, img_rows, img_cols, 1)
x_test = x_test.reshape(2500, img_dep, img_rows, img_cols, 1)
input_shape = (img_dep, img_rows, img_cols, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
y_train.reshape(15000, img_dep)
y_test.reshape(2500, img_dep)
print('x_train shape', x_train.shape)
print('x_test shape', x_test.shape)
print('y_train shape', y_train.shape)
print('y_test shape', y_test.shape)
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
model = Sequential()
model.add(Conv3D(32, kernel_size=(3, 3, 3),
activation='relu',
input_shape=input_shape))
model.add(Conv3D(64, (2, 2, 2), activation='relu'))
#model.add(MaxPooling3D(pool_size=(2, 2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
任何想法,我要去哪里错了吗?我对ML很陌生,因此这可能是一个简单的错误,只需一个简单的修复。我在网上找不到任何人使用MNIST数据运行3DCNN。谢谢!
的回溯信息是:
Traceback (most recent call last):
File "3dconvnet.py", line 67, in <module>
validation_data=(x_test, y_test))
File "/home/jackson/anaconda2/lib/python2.7/site-packages/keras/models.py", line 853, in fit
initial_epoch=initial_epoch)
File "/home/jackson/anaconda2/lib/python2.7/site-packages/keras/engine/training.py", line 1406, in fit
batch_size=batch_size)
File "/home/jackson/anaconda2/lib/python2.7/site-packages/keras/engine/training.py", line 1308, in _standardize_user_data
_check_array_lengths(x, y, sample_weights)
File "/home/jackson/anaconda2/lib/python2.7/site-packages/keras/engine/training.py", line 229, in _check_array_lengths
'and ' + str(list(set_y)[0]) + ' target samples.')
ValueError: Input arrays should have the same number of samples as target arrays. Found 15000 input samples and 60000 target samples.
印刷形状的输出是:
('x_train shape', (15000, 4, 28, 28, 1))
('x_test shape', (2500, 4, 28, 28, 1))
('y_train shape', (60000,))
('y_test shape', (10000,))
编辑* 1:加入回溯
编辑** 2:加入打印输出形状
论哪一行你有错误信息? Post full traceback – kvorobiev
刚刚发布Traceback –
在完成__all__整形并将其添加到问题后,打印出x_train,y_train,x_test,y_test的形状。 – putonspectacles