简体   繁体   中英

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. The tutorial is on using Eager mode. The only missing part is

super(MySimpleLayer, self).__init__() 

in the init method:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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