[英]Typo in the example “custom layer in Tensorflow using Keras”
我认为Tensorflow 示例中有一个错字,用于使用Keras构建自定义图层。 本教程是关于使用“急切”模式的。 唯一缺少的部分是
super(MySimpleLayer, self).__init__()
在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])
...
初始化方法只需要:
super(MySimpleLayer, self).__init__()
如果没有此行,将显示缺少属性的错误,例如:
AttributeError: 'MySimpleLayer' object has no attribute '_scope'
,它们是父类的一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.