tensorflow layers层源码解读,卷积篇

    layers是tensorflow封装好的高层api,变量会由layers自己创建,计算方式也由layer层自动执行,相对于tensorflow的nn层更加的方便实用,可以直接当黑盒来使用。

    Layers是所有layers的基类,基类中有一个build变量,来控制本层是否被创建,初始时为false。两个重要的方法build和call方法,build中存放本层需要初始化的变量,call中存放本层的计算逻辑,这两个方法都需要子类进行重写。基类中重写了__call__方法,当调用__call__时,类会首先检查build变量是否为false,若为false则先执行build()方法初始化变量,并将build变量设为True,这样在第二次调用__call__时将不再有变量被初始化。创建完变量后,__call__继续执行call()方法。

    上面是layers中Conv模块的类图。每个类对应的参数说明如下:

tf.layers.Conv1D()
参数:
    filters:卷积核数目
    kernel_size:一维卷积长度
    strides:一维卷积strides
    padding:'valid' or 'same'
    data_format:'channels_last' or 'channels_first' 如果为first会先将其转为last
    dilation_rate:1 ??
    activation:None 激活函数 如果为None则没有激活函数
    use_bias:True 是否使用bias
    kernel_initializer:kernel变量的初始化方法,迁移学习中会用到
    bias_initializer:bias初始化方法,默认全为0
    kernel_regularizer:kernel正则化系数
    bias_regularizer:bias正则化系数
    trainable:kernel和bias是否可以被训练
方法:
    __call__(inputs):
        inputs [bs,length,in_channel]
        return [bs,卷积后长度,filters]
变量:
        self.kernel [kernel_wight,in_channel,filters]
        self,bais   [filters] 也就是一个卷积核共用一个bias

tf.layers.Conv2D()
参数:
    filters:卷积核数目
    kernel_size:(卷积核高度,卷积核宽度)
    strides:(平移高度,平移宽度)
方法:
    __call__(inputs):
        inputs [bs,height,length,in_channel]
        return [bs,卷积后高度,卷积后宽度,filters]
变量:
    self.kernel [kernel_height,kernel_wight,in_channel,filters]
    self,bais   [filters] 也就是一个卷积核共用一个bias


tf.layers.Conv3D()
参数:
    filters:卷积核数目
    kernel_size:(卷积核帧数,卷积核高度,卷积核宽度)
    strides:(平移帧数,平移高度,平移宽度)
方法:
    __call__(inputs):
        inputs [bs,depth,height,length,in_channel]
        return [bs,卷积后帧数,卷积后高度,卷积后宽度,filters]
变量:
    self.kernel [kernel_depth,kernel_height,kernel_wight,in_channel,filters]
    self,bais   [filters] 也就是一个卷积核共用一个bias

猜你喜欢

转载自blog.csdn.net/qq_34001907/article/details/80015747