keras模型训练报错AttributeError: ‘NoneType‘ object has no attribute ‘_inbound_nodes‘
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了keras模型训练报错AttributeError: ‘NoneType‘ object has no attribute ‘_inbound_nodes‘,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1983字,纯文字阅读大概需要3分钟。
内容图文
问题描述
最近正在复现BiSenetv2网络。由于目前BiSenetv2的代码基本都是pytorch,所以自己根据论文模型结构完成了模型的keras版本,但是在进行训练时一直出现如下报错
百度了很多发现应该是由于我在使用keras搭建模型时,需要使用TensorFlow夹杂搭建,但是keras无论使用函数式还是序列式,每一层的输出类型都是layer类型 ,所以当使用非keras函数时,得到的变量类型不是layer类型,因此出现以上报错。
解决方案
(1)由于代码中使用了concatenate以及reshape这两个方法,具体使用例如:
from keras import backend as K
from keras.layers import Dense, Input
inpt = Input(shape=(224, 224, 3))
x = Conv2d(63, (3,3), padding='same', activation='relu')
...
x = K.concatenate([branch1, branch2, branch3], axis=3)
x = K.reshpe(x, (1000,))
# 上面两行代码并不是连续出现,这里写出来,主要描述使用了“连接”“reshape”两种操作;
将其修改为:
from keras.layers import Concatenate, Resahpe
x = Concatenate(axis=3)([branch1, branch2, branch3])
x = Resahpe((1000,))(x)
可以想到,直接使用concatenate或者reshape不是作为一层,而Concatenate或者Reshape是一个layer;
我按照上述方法修改后仍然报错
(2)将自定义层的输出变为keras layer类型,实现也很简单,就是调用keras中的Lambda函数。举例如下:
from tensorflow import squeeze
from keras.layers import Lambda
def squeeze_dim(input):
output = squeeze(input, axis = [1,2])
def my_model(inputs):
x = Conv2d(inputs)
...
# 只需要使用Lambda调用预定义好的函数即可
x = Lambda(squeeze_dim, name='sqe_dim')(x)
model = Model(input = inputs, output = x)
...
(3)激活函数的使用**(我的代码报错的原因)**
left = Multiply()([left1,keras.activations.sigmoid(right1)])
right = Multiply()([left2,keras.activations.sigmoid(right2)])
将其修改为:
left = Multiply()([left1, Activation('sigmoid')(right1)])
right = Multiply()([left2, Activation('sigmoid')(right2)])
修改后问题解决,没有报错
参考博客:
https://www.cnblogs.com/chenzhen0530/p/10893803.html
https://blog.csdn.net/jorg_zhao/article/details/89929230
https://blog.csdn.net/wdh315172/article/details/105437494
https://blog.csdn.net/weixin_43786241/article/details/106834761
内容总结
以上是互联网集市为您收集整理的keras模型训练报错AttributeError: ‘NoneType‘ object has no attribute ‘_inbound_nodes‘全部内容,希望文章能够帮你解决keras模型训练报错AttributeError: ‘NoneType‘ object has no attribute ‘_inbound_nodes‘所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。