以功能样式链接自定义 Keras 图层

我想使用'功能API构建一个模型。我的模型非常大,因此我想通过继承来自 来创建自定义图层。以下是我的尝试,灵感来自TensorFlow的文档。tf.kerastf.keras.layers.Layer


import tensorflow as tf


class Conv2D(tf.keras.layers.Layer):

    def __init__(self):

        super().__init__()


        input_layer = tf.keras.layers.Input(

            shape=(256, 256, 3)

        )

        self.conv = tf.keras.layers.Conv2D(

            filters=16,

            kernel_size=3,

            strides=(1, 1),

            padding="same"

        )(input_layer)


    def call(self, inputs):

        return self.conv(inputs)


outer_input_layer = tf.keras.layers.Input(

    shape=(256, 256, 3)

)

x = Conv2D()(outer_input_layer)

此代码崩溃,出现以下错误。


Traceback (most recent call last):

  File "c:\Users\user\.vscode\extensions\ms-python.python-2020.2.64397\pythonFiles\ptvsd_launcher.py", line 48, in <module>

    main(ptvsdArgs)

  File "c:\Users\user\.vscode\extensions\ms-python.python-2020.2.64397\pythonFiles\lib\python\old_ptvsd\ptvsd\__main__.py", line 432, in main

    run()

  File "c:\Users\user\.vscode\extensions\ms-python.python-2020.2.64397\pythonFiles\lib\python\old_ptvsd\ptvsd\__main__.py", line 316, in run_file

    runpy.run_path(target, run_name='__main__')

  File "C:\Users\user\code\.env\lib\runpy.py", line 263, in run_path

    pkg_name=pkg_name, script_name=fname)

  File "C:\Users\user\code\.env\lib\runpy.py", line 96, in _run_module_code

    mod_name, mod_spec, pkg_name, script_name)

  File "C:\Users\user\code\.env\lib\runpy.py", line 85, in _run_code

    exec(code, run_globals)

  File "c:\Users\user\code\tests.py", line 23, in <module>

    x = Conv2D()(outer_input_layer)

  File "C:\Users\user\code\.env\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 773, in __call__        

    outputs = call_fn(cast_inputs, *args, **kwargs)

  File "C:\Users\user\code\.env\lib\site-packages\tensorflow_core\python\autograph\impl\api.py", line 237, in wrapper

    raise e.ag_error_metadata.to_exception(e)

TypeError: in converted code:

我的方法有什么问题?


哈士奇WWW
浏览 86回答 1
1回答

慕桂英3389331

没有自定义图层具有“输入”图层。这没有多大意义。输入是您在调用图层时传递给图层的内容。Vo_import tensorflow as tfclass ConvBN(tf.keras.layers.Layer):&nbsp; &nbsp; def __init__(self, activation, name):&nbsp; &nbsp; &nbsp; &nbsp; super().__init__()&nbsp; &nbsp; &nbsp; &nbsp; #here you just "store" the layers, you don't use them&nbsp; &nbsp; &nbsp; &nbsp; #you also store any other property you find necessary for the call&nbsp; &nbsp; &nbsp; &nbsp; self.conv = tf.keras.layers.Conv2D(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filters=16,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kernel_size=3,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strides=(1, 1),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; padding="same",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name = name+'_conv'&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp;self.bn = tf.keras.layers.BatchNormalization(name = name + "_bn")&nbsp; &nbsp; &nbsp; &nbsp;self.activation = tf.keras.layers.Activation(activation, name = name + "_act")&nbsp; &nbsp; def call(self, inputs):&nbsp; &nbsp; &nbsp; &nbsp; #here you "use" the layers with the given input to produce an output&nbsp; &nbsp; &nbsp; &nbsp; out = self.conv(inputs)&nbsp; &nbsp; &nbsp; &nbsp; out = self.bn(out)&nbsp; &nbsp; &nbsp; &nbsp; out = self.activation(out)&nbsp; &nbsp; &nbsp; &nbsp; return out如果您不打算多次使用“同一层”,也可以创建更简单的 blok:def convGroup(input_tensor, activation, name):&nbsp; &nbsp; out = tf.keras.layers.Conv2D(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filters=16,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kernel_size=3,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strides=(1, 1),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; padding="same",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name = name+'_conv'&nbsp; &nbsp; &nbsp; &nbsp; )(input_tensor)&nbsp; &nbsp; out = tf.keras.layers.BatchNormalization(name = name + "_bn")(out)&nbsp; &nbsp; out = tf.keras.layers.Activation(activation, name = name + "_act")(out)&nbsp; &nbsp; return out
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python