错误信息:
Keras AttributeError 'NoneType' object has no attribute '_inbound_nodes'
原因:
只要使用Model,就必须保证该函数内全为layer而不能有其他函数,如果有其他函数必须用Lambda封装为layer。
比如现在用到concat这个很基本的操作:
model = Model(inputs=input, outputs=output) # 提示报错
def model()
……
concat_sc = tf.concat([sc1, sc2], axis=-1) # 意图使用concat拼接sc1和sc2
……
会提示报错,这是因为tf.concat是函数而不是layer,点进去以后的在array_ops.py文件中的定义如下
@tf_export("concat")
@dispatch.add_dispatch_support
def concat(values, axis, name="concat"):
"""Concatenates tensors along one dimension.
又因为model函数中不允许除了layer以外的其他函数出现,所以报错。以concat为例,可改为如下形式
concat_sc = Concatenate(axis=-1)([sc1, sc2])
成功运行,这是因为Concatenate本身是tf定义的layer,点进去以后merge.py文件中如下:
class Concatenate(_Merge):
"""Layer that concatenates a list of inputs.
It takes as input a list of tensors,
all of the same shape except for the concatenation axis,
and returns a single tensor, the concatenation of all inputs.
# Arguments
axis: Axis along which to concatenate.
**kwargs: standard layer keyword arguments.
"""
参考文献: