tensorflow2自定义Layer

介绍

tf.keras.layers.Layer是 所有层的父类
详见:https://www.malaoshi.top/show_1EF3otOHo8ZF.html

在自定义 Layer 时,官方建议继承tf.keras.layers.Layer类,并实现下面3个方法:

  1. __init__()
  2. build()
  3. call()

官方自定义layer:
https://tensorflow.google.cn/tutorials/customization/custom_layers

__init__()

初始化方法,实例化时调用,保存成员变量的设置

build()

在 第一次 调用 call() 方法时,会先调用 build() 方法。

如调用 add_weight() 方法设置权重

call()

底层实现了 __call__() 方法,让 Layer 实例像函数那样调用

感谢:
https://blog.csdn.net/qq_32623363/article/details/104128497?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task


原文出处:/show_1EF59hmYVihh.html