简体   繁体   中英

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

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 :

x_train = np.expand_dims(x_train, -1)

This would transform shape (60000, 28, 28) to (60000, 28, 28, 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM