@创建于: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, usingadd_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), usingadd_weight()
.__call__()
will automatically build the layer (if it has not been built yet) by callingbuild()
. -
build(self, input_shape)
:此方法可用于创建依赖于输入形状的权重,使用add_weight()
。__call__()
将通过调用build()
自动构建层(如果尚未构建)。 -
call(self, *args, **kwargs)
: Called in__call__
after making surebuild()
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 overridefrom_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