【发布时间】:2021-01-16 16:17:52
【问题描述】:
我在使用 Keras 训练 MNIST 模型(CNN)时出现以下错误。顺便说一句,机器学习的完整初学者,如果有明显的问题,请道歉。任何帮助表示赞赏
from keras.layers import Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras import backend as K
K.common.set_image_dim_ordering('th')
导致错误模块 'keras.backend' 没有属性 'common'。当用 image_data_format 替换时,它仍然没有修复任何错误
# fix random seed for reproducibility
seed = 3
np.random.seed(seed)
# load data
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(X_train.shape[0], 1, 28, 28).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28).astype('float32')
print (X_train.shape, y_train.shape)
print (X_test.shape, y_test.shape)
# normalize inputs
X_train = X_train / 255
X_test = X_test / 255
# one hot encoding
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
num_op = y_test.shape[1]
print (num_op)
def cnn_model():
# create model
model = Sequential()
model.add(Convolution2D(32, 5, 5, padding='valid', input_shape=(1, 28, 28), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(num_op, activation='softmax'))
# Compile Model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
# Build the model
model = cnn_model()
# Fit the model
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=200, verbose=2)
这会导致错误 ValueError: Negative dimension size 由于从 1 中减去 5 导致 '{{node conv2d_9/Conv2D}} = Conv2D[T=DT_FLOAT, data_format="NHWC", dilations=[1, 1, 1 , 1], explicit_paddings=[], padding="VALID", strides=[1, 5, 5, 1], use_cudnn_on_gpu=true](conv2d_9_input, conv2d_9/Conv2D/ReadVariableOp)' 输入形状:[?,1, 28,28],[5,5,28,32]。
每次我尝试修复某些东西时,我都会忽略什么总是出错,我可能会错过一些明显的东西,或者在尝试修复它时可能犯了一两个错误
【问题讨论】:
标签: python tensorflow machine-learning keras deep-learning