简体   繁体   English

'{{node conv2d_3/Conv2D} 从 1 中减去 3 导致的负维度大小

[英]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_dimstf.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.

相关问题 'Conv2D' 从 1 中减去 3 导致的负尺寸大小 - Negative dimension size caused by subtracting 3 from 1 for 'Conv2D' “ Encoder / conv6 / Conv2D”的2减去3导致的负尺寸大小 - Negative dimension size caused by subtracting 3 from 2 for 'Encoder/conv6/Conv2D' 多个Conv1D图层:由于'conv1d_2 / convolution / Conv2D从1中减去8而导致的负尺寸大小 - Multiple Conv1D Layers: Negative dimension size caused by subtracting 8 from 1 for 'conv1d_2/convolution/Conv2D CNN Keras:ValueError:负数尺寸是由'conv2d的2减去3引起的 - CNN Keras: ValueError: Negative dimension size caused by subtracting 3 from 2 for 'conv2d 'conv2d_2/convolution' 从 1 中减去 3 导致的负维度大小 - Negative dimension size caused by subtracting 3 from 1 for 'conv2d_2/convolution' ValueError:由 1 为 'conv3d_3/convolution' 减去 22 引起的负尺寸大小(操作:'Conv3D') - ValueError: Negative dimension size caused by subtracting 22 from 1 for 'conv3d_3/convolution' (op: 'Conv3D') 输入形状为 [?,1,10000,80], [3,3,80,16] 的 'conv2d_1/convolution'(操作:'Conv2D')从 1 中减去 3 导致的负尺寸大小 - Negative dimension size caused by subtracting 3 from 1 for 'conv2d_1/convolution' (op: 'Conv2D') with input shapes: [?,1,10000,80], [3,3,80,16] 负维度大小由 2 减去 5 导致的 'conv2d_4/convolution' (op: 'Conv2D') 输入形状:[?,5,2,64], [5,5,64,64] - Negative dimension size caused by subtracting 5 from 2 for 'conv2d_4/convolution' (op: 'Conv2D') with input shapes: [?,5,2,64], [5,5,64,64] 输入形状为 [?,1,74,16], [3,3,16,32] 的“conv2d_2/convolution”(操作:“Conv2D”)从 1 中减去 3 导致的负尺寸大小 - Negative dimension size caused by subtracting 3 from 1 for 'conv2d_2/convolution' (op: 'Conv2D') with input shapes: [?,1,74,16], [3,3,16,32] Keras-输入尺寸为[?,4,80,64],[5,5,64,64]的'conv2d_5 / convolution'(op:'Conv2D')的4中减去5引起的负尺寸大小 - Keras - Negative dimension size caused by subtracting 5 from 4 for 'conv2d_5/convolution' (op: 'Conv2D') with input shapes: [?,4,80,64], [5,5,64,64]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM