簡體   English   中英

在 tf.keras 中定義 model 塊

[英]Defining model blocks in tf.keras

我正在試驗我的模型的架構,我想有幾個預定義的層塊,我可以隨意混合。 I thought that creating a different class for each of this block structure would make it easier, and I figured that subclassing the Model class in tf.keras was the way to go. 所以我做了以下(玩具示例,但很長。對不起。)。

class PoolingBlock(Model):
    def __init__(self, filters, stride, name):
        super(PoolingBlock, self).__init__(name=name)

        self.bn = BatchNormalization()
        self.conv1 = Conv1D(filters=filters, kernel_size=1, padding='same')
        self.mp1 = MaxPooling1D(stride, padding='same')

    def call(self, input_tensor, training=False, mask=None):
        x = self.bn(input_tensor)
        x = tf.nn.relu(x)
        x = self.conv1(x)
        x = self.mp1(x)
        return x

class ModelA(Model):
    def __init__(self, n_dense, filters, stride, name):
        super(ModelA, self).__init__(name=name)

        self.d1 = Dense(n_dense, "DenseLayer1")
        self.pb1 = PoolingBlock(filters, stride, name="PoolingBlock_1")
        self.d2 = Dense(n_dense, "DenseLayer2")

    def call(self, inputs, training=False, mask=None):
        x = inputs
        x = self.d1(x)
        x = self.pb1(x)
        x = self.d2(x)
        return x

model = ModelA(100, 10, 2, 'ModelA')
model.build(input_shape=x.shape)

然后我像往常一樣繼續使用model.compile(...)model.fit(...) 但是在訓練時,我收到了這個警告:

警告:tensorflow:實體 < 綁定方法 PoolingBlock.call 的 < model.PoolingBlock object 在 0x7fe09ca04208 不能按原樣執行 > 請將此報告給 autgograph 團隊。 提交錯誤時,將詳細程度設置為 10(在 Linux 上, export AUTOGRAPH_VERBOSITY=10 )並附加完整的 output。 原因:在 0x7fe09ca04208 處轉換 < model.PoolingBlock object 的綁定方法 PoolingBlock.call > >:AttributeError:模塊“gast”沒有屬性“Num”

我不明白那是什么意思。 我想知道我的訓練是否按計划進行,這種子類化方式是否正確可靠,是否可以以某種方式抑制此警告。

請嘗試降級gast的版本

pip 安裝gast==0.2.2

然后重新訓練網絡

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM