使用Lambda解决_inbound_nodes错误

Keras出现了下面的错误:

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

原因是使用了Keras backend的reshape操作:

x = K.reshape(x, (num_pictures, 32, 32, 512))

但是Keras backend并不是一个Layer,于是出现了上面的错误.解决的方法是使用Lambda,Lambda用于定义一个Layer,其中没有需要学习的变量,只是对Tensor进行一些操作.先定义一个reshape的函数:

def reshape_tensor(x, shape):
    return K.reshape(x, shape);

然后再调用这个函数:

x = Lambda(reshape_tensor, arguments={'shape': (num_pictures, 32, 32, 512)})(x)

这样就不会出错了.

posted @ 2019-06-12 12:33  MSTK  阅读(2166)  评论(0编辑  收藏  举报