錯誤信息:
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.
"""
參考文獻: