【发布时间】:2020-09-05 03:00:38
【问题描述】:
我使用 Tensorflow 2.0 图层子类化的图层制作了这个自定义图层。我正在尝试制作一层残块。但是当我通过顺序 API 在我的模型中添加这个自定义层时,我得到了 下面的错误。
class ResidualBlock(Layer):
def __init__(self, **kwargs):
super(ResidualBlock, self).__init__(**kwargs)
def build(self, input_shape):
"""
This method should build the layers according to the above specification. Make sure
to use the input_shape argument to get the correct number of filters, and to set the
input_shape of the first layer in the block.
"""
self.bn_1 = BatchNormalization(input_shape=input_shape)
self.conv_1 = Conv2D( input_shape[0],(3,3), padding='SAME')
self.bn_2 = BatchNormalization()
self.conv_2 = Conv2D( input_shape[0],(3,3), padding='SAME')
def call(self, inputs, training=False):
"""
This method should contain the code for calling the layer according to the above
specification, using the layer objects set up in the build method.
"""
h = self.bn_1(training=True)(inputs)
h = tf.nn.relu(h)
h = self.conv_1(h)
h = self.bn_2(training=True)(h)
h = tf.nn.relu(h)
h = self.conv_2(h)
return Add(inputs, h)
但是当我初始化这个层时,我得到了错误。
test_model = tf.keras.Sequential([ResidualBlock(input_shape=(28, 28, 1), name="residual_block")])
test_model.summary()
我的错误日志:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-13-991ed1d78e4b> in <module>()
1 # Test your custom layer - the following should create a model using your layer
2
----> 3 test_model = tf.keras.Sequential([ResidualBlock(input_shape=(28, 28, 1), name="residual_block")])
4 test_model.summary()
5 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/autograph/impl/api.py in wrapper(*args, **kwargs)
263 except Exception as e: # pylint:disable=broad-except
264 if hasattr(e, 'ag_error_metadata'):
--> 265 raise e.ag_error_metadata.to_exception(e)
266 else:
267 raise
ValueError: in user code:
<ipython-input-12-3beea3ca10b0>:32 call *
h = self.bn_1(training=True)(inputs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:800 __call__ **
'The first argument to `Layer.call` must always be passed.')
ValueError: The first argument to `Layer.call` must always be passed.
【问题讨论】:
标签: python tensorflow tensorflow2.0 keras-layer