为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

哈士奇WWW 2022-09-06 21:07:05
我想使用'功能API构建一个模型。我的模型非常大,因此我想通过继承来自 来创建自定义图层。以下是我的尝试,灵感来自TensorFlow的文档。tf.kerastf.keras.layers.Layerimport tensorflow as tfclass 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:我的方法有什么问题?
查看完整描述

1 回答

?
慕桂英3389331

TA贡献2036条经验 获得超8个赞

没有自定义图层具有“输入”图层。这没有多大意义。输入是您在调用图层时传递给图层的内容。


Vo_


import tensorflow as tf


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

    def __init__(self, activation, name):

        super().__init__()


        #here you just "store" the layers, you don't use them

        #you also store any other property you find necessary for the call

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

            filters=16,

            kernel_size=3,

            strides=(1, 1),

            padding="same",

            name = name+'_conv'

        )

       self.bn = tf.keras.layers.BatchNormalization(name = name + "_bn")

       self.activation = tf.keras.layers.Activation(activation, name = name + "_act")


    def call(self, inputs):

        #here you "use" the layers with the given input to produce an output

        out = self.conv(inputs)

        out = self.bn(out)

        out = self.activation(out)


        return out

如果您不打算多次使用“同一层”,也可以创建更简单的 blok:


def convGroup(input_tensor, activation, name):

    out = tf.keras.layers.Conv2D(

            filters=16,

            kernel_size=3,

            strides=(1, 1),

            padding="same",

            name = name+'_conv'

        )(input_tensor)

    out = tf.keras.layers.BatchNormalization(name = name + "_bn")(out)

    out = tf.keras.layers.Activation(activation, name = name + "_act")(out)


    return out


查看完整回答
反对 回复 2022-09-06
  • 1 回答
  • 0 关注
  • 85 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号