Keras中的矩阵大小不兼容问题

时间:2018-12-20 02:09:56

标签: keras neural-network

最近,我刚刚开始学习使用Keras来实现神经网络,并且我尝试根据以下结构实现针对MNIST问题的Le-Net5 NN。
enter image description here

代码:

# Load the data 
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# Parameter set up
input_shape = (32,32,1)
batch_size = 128

# Format the image info
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
x_train = np.pad(x_train, ((0,0),(2,2),(2,2),(0,0)), 'constant')
x_test = np.pad(x_test, ((0,0),(2,2),(2,2),(0,0)), 'constant')

# Encode label
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)

print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# Construct the model
model = keras.Sequential()
conv_stride = 1
pooling_stride = 2
model.add(layers.Conv2D(filters=6, kernel_size=[5,5], input_shape=input_shape, padding="valid", strides=[conv_stride,conv_stride], activation='tanh'))
model.add(layers.AveragePooling2D(pool_size=[2,2], padding="valid", strides=pooling_stride))
model.add(layers.Conv2D(filters=16, kernel_size=[5,5], padding="valid", strides=[conv_stride,conv_stride],activation='tanh'))
model.add(layers.AveragePooling2D(pool_size=[2,2], padding="valid", strides=pooling_stride))
model.add(layers.Conv2D(filters=120, kernel_size=[5,5], padding="valid", strides=[conv_stride,conv_stride], activation='tanh'))
model.add(layers.Flatten())
model.add(layers.Dense(84, activation='tanh'))
model.add(layers.Dense(10, activation=tf.nn.softmax))
print(model.summary())
model.compile(optimizer="sgd", loss='categorical_crossentropy', metrics=['accuracy'])

model.fit(x_train, y_train, batch_size=batch_size, verbose=1, epochs=12)
score = model.evaluate(x_test, y_test, verbose=1)
print("Test Accuracy: ", score[1])

但是,当我运行程序时出现错误,错误消息是:

Matrix size-incompatible: In[0]: [128,1536], In[1]: [1176,200]
     [[{{node dense/MatMul}} = MatMul[T=DT_FLOAT, _class=["loc:@training/Adam/gradients/dense/MatMul_grad/MatMul"], transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](flatten/Reshape, dense/MatMul/ReadVariableOp)]]

我已经使用model.summary()检查了结构,看来神经网络的结构是正确的。谁能告诉我导致错误的原因?

0 个答案:

没有答案