tensorflow.keras.layers.Layer官网介绍、样例代码

@创建于:20210629
@修改于:20210629

1、Layer官网介绍

| A layer is a callable object that takes as input one or more tensors and that outputs one or more tensors. It involves computation, defined in the call() method, and a state (weight variables), defined either in the constructor __init__() or in the build() method.

层是一个可调用对象,它以一个或多个张量作为输入并输出一个或多个张量。 它涉及在call() 方法中定义的计算,以及在构造函数__init__()build() 方法中定义的state(权重变量)。

We recommend that descendants of Layer implement the following methods:
我们建议 Layer 的后代实现以下方法:

  • __init__(): Defines custom layer attributes, and creates layer state variables that do not depend on input shapes, using add_weight().

  • __init__():定义自定义层属性,并使用 add_weight() 创建不依赖于输入形状的层状态变量。

  • build(self, input_shape): This method can be used to create weights that depend on the shape(s) of the input(s), using add_weight(). __call__() will automatically build the layer (if it has not been built yet) by calling build().

  • build(self, input_shape):此方法可用于创建依赖于输入形状的权重,使用 add_weight()__call__() 将通过调用 build() 自动构建层(如果尚未构建)。

  • call(self, *args, **kwargs): Called in __call__ after making sure build() has been called. call() performs the logic of applying the layer to the input tensors (which should be passed in as argument).

  • Two reserved keyword arguments you can optionally use in call() are:

    • training (boolean, whether the call is in inference mode or training mode)
    • mask (boolean tensor encoding masked timesteps in the input, used in RNN layers)
  • call(self, *args, **kwargs):在确保调用了 build() 之后在 __call__ 中调用。 call() 执行将层应用到输入张量(应该作为参数传入)的逻辑。
    您可以选择在 call() 中使用的两个保留关键字参数是:

    • training(布尔值,无论调用是在推理模式还是训练模式)
    • mask(布尔张量编码输入中的屏蔽时间步长,用于 RNN 层)
  • get_config(self): Returns a dictionary containing the configuration used to initialize this layer. If the keys differ from the arguments in __init__, then override from_config(self) as well. This method is used when saving the layer or a model that contains this layer.

  • get_config(self):返回一个包含用于初始化该层的配置的字典。如果键与 __init__ 中的参数不同,那么也覆盖 from_config(self)。保存图层或包含该图层的模型时使用此方法。

2、使用样例代码

2.1 使用样例

import tensorflow as tf


class TestDenseLayer(tf.keras.layers.Layer):
    def __init__(self, num_outputs):
        print('In __init__')
        super(TestDenseLayer, self).__init__()
        self.num_outputs = num_outputs

    def build(self, input_shape):
        print('In build')
        self.kernel = self.add_variable("kernel",
            shape=[int(input_shape[-1]), self.num_outputs])
        print('self.kernel = ', self.kernel.shape)

    def call(self, input):
        print('In call')
        return tf.matmul(input, self.kernel)


layer = TestDenseLayer(10)
input = tf.ones((2, 3))
print('input shape = {}, value is\n{}'.format(input.shape, input))
# 此处显式调用类中的call方法,但是在调用call方法前,自动调用build的方法。
result = layer(input)
print('result =', result)

2.2 输出结果

In __init__

input shape = (2, 3), value is
[[1. 1. 1.]
 [1. 1. 1.]]
 
In build
self.kernel =  (3, 10)

In call
result = tf.Tensor(
[[-0.81507313 -0.070647    0.5845617   0.1208241  -0.07275787  1.3390236
   1.0638947  -1.4866217   0.42016876 -0.28244376]
 [-0.81507313 -0.070647    0.5845617   0.1208241  -0.07275787  1.3390236
   1.0638947  -1.4866217   0.42016876 -0.28244376]], shape=(2, 10), dtype=float32)

3、参考资料

tensorflow2.0中Layer的__init__(),build(), call()函数
官网:https://www.tensorflow.org/guide/keras/custom_layers_and_models

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值