简体   繁体   English

在 tf.keras 中定义 model 块

[英]Defining model blocks in tf.keras

I'm experimenting with my model's architecture and I would like to have several predefined blocks of layers that I could mix at will.我正在试验我的模型的架构,我想有几个预定义的层块,我可以随意混合。 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. 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. So I have done the following (Toy example, yet long. Sorry.).所以我做了以下(玩具示例,但很长。对不起。)。

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)

Then I continue with model.compile(...) and model.fit(...) as usual.然后我像往常一样继续使用model.compile(...)model.fit(...) But when training, I receive this warning:但是在训练时,我收到了这个警告:

WARNING:tensorflow:Entity < bound method PoolingBlock.call of < model.PoolingBlock object at 0x7fe09ca04208 > > could not be transformed and will be executed as-is.警告:tensorflow:实体 < 绑定方法 PoolingBlock.call 的 < model.PoolingBlock object 在 0x7fe09ca04208 不能按原样执行 > Please report this to the AutgoGraph team.请将此报告给 autgograph 团队。 When filing the bug, set the verbosity to 10 (on Linux, export AUTOGRAPH_VERBOSITY=10 ) and attach the full output.提交错误时,将详细程度设置为 10(在 Linux 上, export AUTOGRAPH_VERBOSITY=10 )并附加完整的 output。 Cause: converting < bound method PoolingBlock.call of < model.PoolingBlock object at 0x7fe09ca04208 > >: AttributeError: module 'gast' has no attribute 'Num'原因:在 0x7fe09ca04208 处转换 < model.PoolingBlock object 的绑定方法 PoolingBlock.call > >:AttributeError:模块“gast”没有属性“Num”

I don't understand what that means.我不明白那是什么意思。 I am wondering if my training is going as I have planned, if this way of subclassing is correct and solid, if I can suppress this warning somehow.我想知道我的训练是否按计划进行,这种子类化方式是否正确可靠,是否可以以某种方式抑制此警告。

Kindly try to downgrade the version of gast请尝试降级gast的版本

pip install gast==0.2.2 pip 安装gast==0.2.2

And then re-train the network然后重新训练网络

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM