解决Keras报错AttributeError: 'NoneType' object has no attribute 'inbound_nodes'

571 阅读1分钟

背景

在用Keras的时候遇到了这个报错

AttributeError: 'NoneType' object has no attribute 'inbound_nodes'

原因

原来是对Tensor操作的方法用错了,做了一个扩展操作,将一个2D的张量扩展成一个3D的张量 使用了K.repeat()的方法,这个方法返回的是一个Tensor,而不是一个Layer,当然没有入节点inbound_nodes这个属性

正确使用

这里应该用RepeatVector这个层,具体用法是

RepeatVector层 keras.layers.core.RepeatVector(n) RepeatVector层将输入重复n次

参数 n:整数,重复的次数 输入shape 形如(nb_samples, features)的2D张量

输出shape 形如(nb_samples, n, features)的3D张量

扩展

同样如果要对Tensor的某一个维做sum操作,也不能用K.sum,而应该用一个Lambda层