自定义模型中遇到的报错日志及解决方法(TensorFlow2.1.0 )

1. ValueError: Layer dense expects 1 inputs, but it received 2 input tensors.

问题代码:

model.build(input_shape=[None, 50])

问题出在参数input_shape的传入上,不能传入列表形式的shape,只能是tuple类型的。
修改后正确代码为

model.build(input_shape=(None, 50))

2.ValueError: You tried to call count_params on ***, but the layer isn’t built. You can build it manually via: ****

问题代码:

import tensorflow as tf
from tensorflow.keras import layers, models


class MyModel(tf.keras.models.Model):
    def __init__(self):
        super(MyModel, self).__init__()

    def build(self, input_shape):
        # self.dense = layers.Dense(4)
        # self.dense2 = layers.Dense(2)
        # self.built = True
        super(MyModel, self).build(input_shape)

    def call(self, inputs):

        # x = self.dense(inputs)
        # x = self.dense2(x)
        x = layers.Dense(4)(inputs)
        x = layers.Dense(2)(x)

        return x


def main():

    model = MyModel()
    model.build(input_shape=(None, 50))
    model.summary()


if __name__ == "__main__":
    main()

问题出在call函数内部,不可以直接通过layers.Dense()的方式构建网络层,
应该在build函数内部先建立对应的层

正确代码应为:

import tensorflow as tf
from tensorflow.keras import layers, models


class MyModel(tf.keras.models.Model):
    def __init__(self):
        super(MyModel, self).__init__()

    def build(self, input_shape):
        self.dense = layers.Dense(4)
        self.dense2 = layers.Dense(2)
        # self.built = True
        super(MyModel, self).build(input_shape)

    def call(self, inputs):

        x = self.dense(inputs)
        x = self.dense2(x)
        # x = layers.Dense(4)(inputs)
        # x = layers.Dense(2)(x)

        return x


def main():

    model = MyModel()
    model.build(input_shape=(None, 50))
    model.summary()


if __name__ == "__main__":
    main()

同时如果只是通过self.built = True的方式,同样会出现此错误,建议通过super(MyModel, self).build(input_shape)的方式,使得build生效。

3. summary输出时output shape 为multiple的方法

  1. 在model.build()后手动调用call方法,并输入shape大小。
    eg:
   model = MyModel()
   model.build(input_shape=(None, 50))
   model.call(tf.keras.Input(shape=(50, )))
   model.summary()

最终summary输出:

Model: "my_model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 4)                 204       
_________________________________________________________________
dense_1 (Dense)              (None, 2)                 10        
=================================================================
Total params: 214
Trainable params: 214
Non-trainable params: 0
_________________________________________________________________

Process finished with exit code 0
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值