输入形状为[?,174,1,40],[2,2,40,16]的'conv2d_2 / convolution'(op:'Conv2D')从1中减去2导致的负尺寸

时间:2020-06-17 07:47:11

标签: python

%store -r x_train 
%store -r x_test 
%store -r y_train 
%store -r y_test 
%store -r yy 
%store -r le
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, Conv2D, MaxPooling2D,  
GlobalAveragePooling2D
from keras.optimizers import Adam
from keras.utils import np_utils
from sklearn import metric
num_rows = 40
num_columns = 174
num_channels = 1
x_train = x_train.reshape(x_train.shape[0],num_rows , num_columns, 
num_channels)
x_test = x_test.reshape(x_test.shape[0], num_rows, 
num_columns,num_channels )

num_labels = yy.shape[1]
filter_size = 2
model = Sequential()

model.add(Conv2D(filters=16, kernel_size=2, 
activation='relu',input_shape=(40,174,1)))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))

model.add(Conv2D(filters=32, kernel_size=2, activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))

model.add(Conv2D(filters=64, kernel_size=2, activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))

model.add(Conv2D(filters=128, kernel_size=2, activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))
model.add(GlobalAveragePooling2D())

model.add(Dense(num_labels, activation='softmax'))

这是完整的代码。 当我尝试创建CNN模型时,我陷入了该错误。

InvalidArgumentError                      Traceback (most recent call 
last)
~/arshin/home/arshin/envs/aiml/lib/python3.6/site- 
 packages/tensorflow/python/framework/ops.py in _create_c_op(graph, 
 node_def, inputs, control_inputs)
 1566   try:
 -> 1567     c_op = c_api.TF_FinishOperation(op_desc)
  1568   except errors.InvalidArgumentError as e:

 InvalidArgumentError: Negative dimension size caused by subtracting 2 
 from 1 for 'conv2d_1/convolution' (op: 'Conv2D') with input shapes: 
 [?,174,1,40], [2,2,40,16].

代码不包含错误。为什么发生这种情况.......是由于tensorflow版本或其他原因导致的..我尝试了很多事情,但无法纠正错误。这段代码有什么问题..

0 个答案:

没有答案