custom_layers

通过扩展tf.keras.Layer类实现自定义层。初始化应在build方法中完成,forward计算则在call方法中执行。变量可以在__init__中创建,但延迟在build中创建允许根据输入形状动态创建。尽可能使用标准层以提高代码可读性和维护性。若需要新层,考虑提交GitHub问题或贡献代码。
摘要由CSDN通过智能技术生成

Implementing custom layers

The best way to implement your own layer is extending the tf.keras.Layer class and implementing:

  • __init__ , where you can do all input-independent initialization
  • build, where you know the shapes of the input tensors and can do the rest of the initialization
  • call, where you do the forward computation

Note that you don't have to wait until build is called to create your variables, you can also create them in __init__. However, the advantage of creating them in build is that it enables late variable creation based on the shape of the inputs the layer will operate on. On the other hand, creating variables in __init__ would mean that shapes required to create the variables will need to be explicitly specified.

class MyDenseLayer(tf.keras.layers.Layer):
  def __init
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值