第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

以功能樣式鏈接自定義 Keras 圖層

以功能樣式鏈接自定義 Keras 圖層

哈士奇WWW 2022-09-06 21:07:05
我想使用'功能API構(gòu)建一個(gè)模型。我的模型非常大,因此我想通過繼承來自 來創(chuàng)建自定義圖層。以下是我的嘗試,靈感來自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)此代碼崩潰,出現(xiàn)以下錯(cuò)誤。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貢獻(xiàn)2036條經(jīng)驗(yàn) 獲得超8個(gè)贊

沒有自定義圖層具有“輸入”圖層。這沒有多大意義。輸入是您在調(diào)用圖層時(shí)傳遞給圖層的內(nèi)容。


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

如果您不打算多次使用“同一層”,也可以創(chuàng)建更簡單的 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


查看完整回答
反對 回復(fù) 2022-09-06
  • 1 回答
  • 0 關(guān)注
  • 95 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號