如何在不共享权重的情况下使用“ Keras图层定义使用嵌套函数”?

时间:2019-07-20 13:58:22

标签: keras keras-layer tf.keras keras-2

在我的模型中,我创建了许多具有相同架构的CNN图层,因此我创建了如下函数:

Slow Blow eLiquid 1443 27686 A1 7 4 Bottle Size: 10ml, Nicotine:20mg

我创建了一个创建网络的函数。例如:

def conv_opt(dilation_rate=(1, 1), padding='same', activation=None,
             kernel_initializer='glorot_uniform',
             use_bias=False, bias_initializer='zeros', trainable=True):

    def my_conv2d(inputs, n_filters, filter_size=(3, 3), strides=(1, 1), name=None):
        if name is None:
            outputs = Conv2D(filters=n_filters, kernel_size=filter_size, kernel_initializer=kernel_initializer,
                             strides=strides, dilation_rate=dilation_rate, padding=padding,
                             use_bias=use_bias, bias_initializer=bias_initializer, trainable=trainable)(inputs)

        else:
            outputs = Conv2D(filters=n_filters, kernel_size=filter_size, kernel_initializer=kernel_initializer,
                             strides=strides, dilation_rate=dilation_rate, padding=padding,
                             use_bias=use_bias, bias_initializer=bias_initializer,
                             name=name, trainable=trainable)(inputs)

        if activation is None:
            return outputs
        elif activation == 'relu':
            outputs = ReLU()(outputs)

        elif activation == 'sigmoid':
            outputs = Lambda(K.sigmoid)(outputs)

        elif activation == 'leakyrelu':
            outputs = LeakyReLU()(outputs)
        else:
            raise Exception('Code doesn\'t support the activation function')

        return outputs

    return my_conv2d

我有模特。例如:

def my_net(my_conv):
    e1 = my_conv(inputs, kernel_size)
    e1 = my_conv(e1, kernel_size)
    e1 = my_conv(e1, kernel_size)
    return e1

我知道这类似于post
但就我而言,我向 my_net 函数输入了 my_conv my_conv 是否在{strong> my_net 中是shared weight
我不想在 my_net 中共享 my_conv

0 个答案:

没有答案