ValueError: This model has not yet been built. Build the model first by calling build() or calling f

ValueError: This model has not yet been built. Build the model first by calling build() or calling fit() with some data. Or specify input_shape or batch_input_shape in the first layer for automatic build.  报错原因

 

1、keras建模有3种实现方式——序列模型、函数模型、子类模型。

 序列模型:即通过Sequential方式,初始创建Sequential对象,然后一层一层add添加层结构

函数模型:在函数内部,创建一层一层的layers,然后通过Sequential封装返回,Sequential此时会自动调用build函数,构建模型,后期我们不需要在调用model.build(input_shape)。这里的函数模型其实更强调是layer,而不是Model,这种方式也可以通过类的方式来写,只不过此时要继承layer.Layer,然后再调用时,创建完类对象后,要用Sequential封装一下。

子类模型:通过类的方式定义神经网络,此时一般继承自tf.keras.Model,然后在其他地方通过创建类创建神经网络

 

2、为何会报错

这种错误一般会出现在子类模型中,通过子类方式创建并不会默认调用build函数,需要我们自己调用build函数进行构建

model.build(input_shape) # `input_shape` is the shape of the input data, e.g. input_shape = (None, 32, 32, 3)

当然,也可将model.summary() 放在model.fit() 后面,此时不用调用model.buile() 也不会报错

 

3、参考

https://blog.csdn.net/weixin_39190382/article/details/109322296

https://blog.csdn.net/sisiel/article/details/103163016

https://stackoverflow.com/questions/55908188/this-model-has-not-yet-been-built-error-on-model-summary/64564990#64564990

https://blog.csdn.net/m0_37602827/article/details/104740696?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param

 

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值