当我只提出一个论据时,为什么会有两个论据呢?

时间:2019-04-07 16:26:56

标签: python python-3.x machine-learning keras deep-learning

以下代码段用于在具有张量流后端的keras中定义CNN架构:

class DownBlock(object):
    def __init__(self, prev_layer, num_chann = 16, depthwise_initializer = 'glorot_uniform', kernel_initializer = 'glorot_uniform', bias_initializer = 'zeros', drop_rate = None, spdrop_rate = None, activation = 'relu', pool = True):

        self.prev_layer = prev_layer

        if pool == True:
            self.prev_layer = MaxPooling2D((2, 2)) (self.prev_layer)
            self.prev_layer = Conv2D(num_chann, (1, 1), kernel_initializer = kernel_initializer, bias_initializer = bias_initializer) (self.prev_layer)

        self.convo = Activation(activation) (self.prev_layer)
        self.convo = BatchNormalization() (self.convo)
        if not spdrop_rate == None:             
            self.convo = SpatialDropout2D(spdrop_rate) (self.convo)
        if not drop_rate == None:             
            self.convo = Dropout(drop_rate) (self.convo)

        self.convo = Conv2D(num_chann, (1, 1), kernel_initializer = kernel_initializer, bias_initializer = bias_initializer) (self.convo)
        self.convo = DepthwiseConv2D((3, 3), depthwise_initializer = depthwise_initializer, bias_initializer = bias_initializer, padding = 'same') (self.convo)
        self.convo = Conv2D(num_chann, (1, 1), kernel_initializer = kernel_initializer, bias_initializer = bias_initializer) (self.convo)

        self.convo = Activation(activation) (self.convo)
        self.convo = BatchNormalization() (self.convo)
        if not spdrop_rate == None:             
            self.convo = SpatialDropout2D(spdrop_rate) (self.convo)
        if not drop_rate == None:             
            self.convo = Dropout(drop_rate) (self.convo)

        self.convo = DepthwiseConv2D((3, 3), depthwise_initializer = depthwise_initializer, bias_initializer = bias_initializer, padding = 'same') (self.convo)
        self.convo = Conv2D(num_chann, (1, 1), kernel_initializer = kernel_initializer, bias_initializer = bias_initializer) (self.convo)

        self.convo = Add([self.prev_layer, self.convo])

    def get(self):
        return self.convo

class UpBlock(object):
    def __init__(self, prev_layer, bridge_layer, num_chann = 16, depthwise_initializer = 'glorot_uniform', kernel_initializer = 'glorot_uniform', bias_initializer = 'zeros', drop_rate = None, spdrop_rate = None, activation = 'relu', up = True):

        self.prev_layer = prev_layer
        self.bridge_layer = bridge_layer

        self.convo = Activation(activation) (self.prev_layer)
        self.convo = BatchNormalization() (self.convo)
        if not spdrop_rate == None:             
            self.convo = SpatialDropout2D(spdrop_rate) (self.convo)
        if not drop_rate == None:             
            self.convo = Dropout(drop_rate) (self.convo)

        self.convo = Conv2D(num_chann, (1, 1), kernel_initializer = kernel_initializer, bias_initializer = bias_initializer) (self.convo)
        self.convo = DepthwiseConv2D((3, 3), depthwise_initializer = depthwise_initializer, bias_initializer = bias_initializer, padding = 'same') (self.convo)
        self.convo = Conv2D(num_chann, (1, 1), kernel_initializer = kernel_initializer, bias_initializer = bias_initializer) (self.convo)

        self.convo = Activation(activation) (self.convo)
        self.convo = BatchNormalization() (self.convo)
        if not spdrop_rate == None:             
            self.convo = SpatialDropout2D(spdrop_rate) (self.convo)
        if not drop_rate == None:             
            self.convo = Dropout(drop_rate) (self.convo)

        self.convo = DepthwiseConv2D((3, 3), depthwise_initializer = depthwise_initializer, bias_initializer = bias_initializer, padding = 'same') (self.convo)
        self.convo = Conv2D(num_chann, (1, 1), kernel_initializer = kernel_initializer, bias_initializer = bias_initializer) (self.convo)

        self.convo = Add([self.prev_layer, self.convo])

        if up == True:
            self.convo = Conv2D(num_chann/2, (1, 1), kernel_initializer = kernel_initializer, bias_initializer = bias_initializer) (self.convo)
            self.convo = Conv2DTranspose(num_chann/2, (2, 2), strides = (2, 2), kernel_initializer = kernel_initializer, bias_initializer = bias_initializer, padding = 'same') (self.convo)

        self.convo = Add([self.bridge_layer, self.convo])

    def get(self):
        return self.convo



inputs = Input((IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS))
s = Lambda(lambda x: x / 255) (inputs)
s = Conv2D(8, (1, 1)) (s)

d1 = DownBlock(s, num_chann = 16, drop_rate = 0.1)

d2 = DownBlock(d1.get(), num_chann = 32, drop_rate = 0.1)

d3 = DownBlock(d2.get(), num_chann = 64, drop_rate = 0.1)

d4 = DownBlock(d3.get(), num_chann = 128, drop_rate = 0.1)

d5 = DownBlock(d4.get(), num_chann = 256, drop_rate = 0.1)

m = DownBlock(d5.get(), num_chann = 512, drop_rate = 0.1)

u5 = UpBlock(m.get(), d4.get(), num_chann = 256, drop_rate = 0.1)

u4 = UpBlock(u5.get(), d3.get(), num_chann = 128, drop_rate = 0.1)

u3 = UpBlock(u4.get(), d2.get(), num_chann = 64, drop_rate = 0.1)

u2 = UpBlock(u3.get(), d1.get(), num_chann = 32, drop_rate = 0.1)

u1 = UpBlock(u2.get(), s, num_chann = 16, drop_rate = 0.1)

final = Conv2D(1, (1, 1)) (u1.get())
# final = SpatialDropout2D(0.1) (final)
final = Dropout(0.1) (final)
final = BatchNormalization() (final)
outputs = Activation("sigmoid") (final)

model = Model(inputs = [inputs], outputs = [outputs])

在Jupyter笔记本中执行时,将产生以下堆栈跟踪:

TypeError                                 Traceback (most recent call last)
<ipython-input-31-f23b70d0be6d> in <module>()
    79 s = Conv2D(8, (1, 1)) (s)
    80 
---> 81 d1 = DownBlock(s, num_chann = 16, drop_rate = 0.1)
    82 
    83 d2 = DownBlock(d1.get(), num_chann = 32, drop_rate = 0.1)

<ipython-input-31-f23b70d0be6d> in __init__(self, prev_layer, num_chann, depthwise_initializer, kernel_initializer, bias_initializer, drop_rate, spdrop_rate, activation, pool)
    29         self.convo = Conv2D(num_chann, (1, 1), kernel_initializer = kernel_initializer, bias_initializer = bias_initializer) (self.convo)
    30 
---> 31         self.convo = Add([self.prev_layer, self.convo])
    32 
    33     def get(self):

TypeError: __init__() takes 1 positional argument but 2 were given

跟踪中的最后一行...

TypeError: __init__() takes 1 positional argument but 2 were given

...谈论将两个位置参数传递给第一个UpBlock()调用,而我显然已经传递了一个-

d1 = DownBlock(s, num_chann = 16, drop_rate = 0.1)

另一个位置参数在哪里?为什么没有这个错误,为什么会出现此错误?

1 个答案:

答案 0 :(得分:2)

尽管错误是由于您对DownBlock的构造函数的调用引起的,但是Python还注意到错误回溯是(most recent call last)。此错误是指将两个参数传递给Add构造函数。 Python告诉您,对Add()的调用包含太多参数。

这里的技巧是,您似乎只向Add()提供一个列表参数,但是Python类的构造函数全部都收到一个隐式self作为其第一个位置参数。参见the Python docs

来自评论:

在Keras中使用功能性API时,必须首先像a = Add()中那样创建图层对象,然后必须像下面这样通过调用生成的对象将图层添加到计算图中:

out = a([input1, input2, ...])

或在您的原始示例中:

self.convo = Add()([self.prev_layer, self.convo])