TypeError:__init __()缺少1个必需的位置参数:“ kernel_size”

时间:2019-09-30 13:24:03

标签: python python-3.x image-processing keras

这是我运行的代码:

def fish_model():
    model = Sequential()
    from keras.layers import Activation, Dense
    from keras.layers.convolutional import Convolution2D
    from keras.layers.convolutional import MaxPooling2D
    from keras.layers import Dropout
    model.add(Convolution2D(filters=(6,3,3),input_shape=(256,768,1),activation='relu'))
    model.add(MaxPooling2D(pool_size=(2,2),strides=2))
    model.add(Convolution2D(filters=6, nb_row=3, nb_col=3,subsample=(2,2),
    input_shape=(256, 768, 1,), activation='relu', border_mode='same'))
    model.add(Dropout(0.1))
    model.add(Flatten())
    model.add(Dense(8, activation='softmax'))
    epochs = 5
    lrate = 0.1
    decay = lrate/epochs
    #sgd = SGD(lr=lrate, momentum=0.5, decay=decay, nesterov=False)
    model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['categorical_accuracy'])
    return model

model= fish_model()
print(model.summary())
history = model.fit(X_train, Y_train, validation_data=(x_validation, y_validation), nb_epoch=6, batch_size=4)

我遇到以下错误:

Traceback (most recent call last)
<ipython-input-28-bf6be1e3ca4c> in <module>
----> 1 model= fish_model()
      2 print(model.summary())
      3 history = model.fit(X_train, Y_train, validation_data=(x_validation, y_validation), nb_epoch=6, batch_size=4)

<ipython-input-26-634ceb657d80> in fish_model()
     23     from keras.layers.convolutional import MaxPooling2D
     24     from keras.layers import Dropout
---> 25     model.add(Convolution2D(filters=(6,3,3),input_shape=(256,768,1),activation='relu'))
     26     model.add(MaxPooling2D(pool_size=(2,2),strides=2))
     27     model.add(Convolution2D(filters=6, nb_row=3, nb_col=3,subsample=(2,2),

~\Anaconda3\lib\site-packages\keras\legacy\interfaces.py in wrapper(*args, **kwargs)
     89                 warnings.warn('Update your `' + object_name + '` call to the ' +
     90                               'Keras 2 API: ' + signature, stacklevel=2)
---> 91             return func(*args, **kwargs)
     92         wrapper._original_function = func
     93         return wrapper

 TypeError: __init__() missing 1 required positional argument: 'kernel_size'

为什么我在Convolution2D中遇到错误?我该如何修复代码?

在上面的代码中,我添加了2个Convolution2D调用。当我尝试单行显示时,出现错误。

1 个答案:

答案 0 :(得分:0)

您正在尝试使用与Conv2D相同的参数。您应该将行更改为
model.add(Convolution2D(6,3,3,input_shape=(256,768,1),activation='relu'))