[英]Negative dimension size caused by subtracting 3 from 1 for '{{node conv2d_3/Conv2D}
hello this is my first time to make a model and although I wrote the code as exactly I saw in the course I'M fllowing I got this error and I don't know what to do你好,这是我第一次制作 model 虽然我写的代码和我在课程中看到的完全一样,但我收到了这个错误,我不知道该怎么做
here's the code:这是代码:
from __future__ import print_function
import keras
from keras.optimizers import Adadelta
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense,Dropout,Flatten
from keras.layers import Conv2D,MaxPool2D
from keras import backend as k
batch_size=128
num_classes=10
epochs=12
img_rows=28;img_cols=28
(x_train,y_train),(x_test,y_test)=mnist.load_data()
if k.image_data_format()=="channels_first":
x_train=x_train.reshape(x_train.shape[0],img_rows,img_cols)
x_test=x_test.reshape(x_test.shape[0],img_rows,img_cols)
input_shape=(1,img_rows,img_cols)
else:
x_train=x_train.reshape(x_train.shape[0],1,img_rows,img_cols)
x_test=x_test.reshape(x_test.shape[0],1,img_rows,img_cols)
input_shape=(1,img_rows,img_cols)
x_train=x_train/255.0
x_test=x_test/255.0
y_train=keras.utils.to_categorical(y_train,num_classes)
y_test=keras.utils.to_categorical(y_test,num_classes)
model=Sequential()
model.add(Conv2D(32,kernel_size=(3,3),activation="relu",input_shape=input_shape))
model.add(Conv2D(64,kernel_size=(3,3),activation="relu"))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(128,activation="relu"))
model.add(Dense(10,activation="softmax"))
#model.build()
model.summary()
model.compile(loss=keras.losses.CategoricalCrossentropy,optimizer="Adadelta",metrics=["accuracy"])
model.fit(x_train,y_train,batch_size=batch_size,epochs=epochs,verbose=1,validation_data=(x_test,y_test))
ValueError: Negative dimension size caused by subtracting 3 from 1 for '{{node conv2d_3/Conv2D}} = Conv2D[T=DT_FLOAT, data_format="NHWC", dilations=[1, 1, 1, 1], explicit_paddings=[], padding="VALID", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true](Placeholder, conv2d_3/Conv2D/ReadVariableOp)' with input shapes: [?,1,28,28], [3,3,28,32].
You passed the wrong input shape: you put the channels dimension before height and width.您传递了错误的输入形状:您将通道尺寸放在高度和宽度之前。 This is what you passed:
这是你通过的:
n_samples, channels, height, width
You should instead use:您应该改为使用:
n_samples, height, width, channels
This is where you made the mistake:这是你犯错误的地方:
x_train=x_train.reshape(x_train.shape[0],1,img_rows,img_cols)
x_test=x_test.reshape(x_test.shape[0],1,img_rows,img_cols)
input_shape=(1,img_rows,img_cols)
Change these to put the channels last.更改这些以将频道放在最后。
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
An even better method would be to use np.expand_dims
or tf.expand_dims
:更好的方法是使用
np.expand_dims
或tf.expand_dims
:
x_train = np.expand_dims(x_train, -1)
This would transform shape (60000, 28, 28)
to (60000, 28, 28, 1)
这会将形状
(60000, 28, 28)
转换为(60000, 28, 28, 1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.