简体   繁体   English

示例中的错字“使用Keras在Tensorflow中定制层”

[英]Typo in the example “custom layer in Tensorflow using Keras”

I think there is a typo in the Tensorflow example for building a custom layer using Keras. 我认为Tensorflow 示例中有一个错字,用于使用Keras构建自定义图层。 The tutorial is on using Eager mode. 本教程是关于使用“急切”模式的。 The only missing part is 唯一缺少的部分是

super(MySimpleLayer, self).__init__() 

in the init method: init方法中:

class MySimpleLayer(tf.keras.layers.Layer):
  def __init__(self, output_units):
    **super(MySimpleLayer, self).__init__()**
    self.output_units = output_units

  def build(self, input):
    # The build method gets called the first time your layer is used.
    # Creating variables on build() allows you to make their shape depend
    # on the input shape and hence remove the need for the user to specify
    # full shapes. It is possible to create variables during __init__() if
    # you already know their full shapes.
    self.kernel = self.add_variable(
      "kernel", [input.shape[-1], self.output_units])

  ...

The init method just needs: 初始化方法只需要:

super(MySimpleLayer, self).__init__()

Without this line, errors of missing attributes will be shown, such as: 如果没有此行,将显示缺少属性的错误,例如:

AttributeError: 'MySimpleLayer' object has no attribute '_scope'

, that are parts of the parent class. ,它们是父类的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM