tf.keras.layers.Layer

介绍

Layer继承自 module.py中的Module calss Layer(module.Module):

他的作用是: 实现常见的神经网络操作的类,如卷积、批标准化等。这些操作需要管理权重、损失、更新和层间连接。

A layer is a class implementing common neural networks operations, such
as convolution, batch norm, etc. These operations require managing weights,
losses, updates, and inter-layer connectivity.

官方建议的继承实现方法

__ init__()

‘__ init__() ‘ : 在成员变量中保存配置

build()

‘ build() ‘ :每一层在知道输入的 shape 和 dtype 的时候才会知道怎样初始化参数,如 add_weights(),所以不能写在’ __init__() ‘ 中。又因为如 add_weights() 这些操作只需要调用一次, 在不断的传播中,call() 被多次调用,所以也不能写在call() ‘中。
__call__第一次调用的时候会调用 build() ,然后设置self.built = True,之后每次调用__call__的时候不再调用build()

`build()`: Called once from `__call__`, when we know the shapes of inputs
and `dtype`. Should have the calls to `add_weight()`, and then
call the super's `build()` (which sets `self.built = True`, which is
nice in case the user wants to call `build()` manually before the
first `__call__`).

call()

‘ call() ‘ : call()的调用必须在build() 之后,确切的说是在设置 self.built = True 之后。 它实际上执行将层的逻辑应用到输入张量上。

`call()`: Called in `__call__` after making sure `build()` has been called
once. Should actually perform the logic of applying the layer to the
input tensors (which should be passed in as the first argument).

总结

上一页 -
- 下一页