【发布时间】:2018-09-25 16:45:19
【问题描述】:
我正在尝试在 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:添加打印形状的输出
【问题讨论】:
-
您在哪一行收到错误消息?发布完整的回溯
-
刚刚发布了 Traceback
-
在 all 重塑完成后打印出 x_train、y_train、x_test、y_test 的形状并将其添加到问题中。
-
刚刚添加了形状的打印输出
-
这个问题解决了吗?
标签: python machine-learning keras mnist