Keras throws `'Tensor' object has no attribute '_keras_shape'` when splitting a layer output

巧了我就是萌 提交于 2019-12-02 06:57:17

问题


I have sentence embedding output X of a sentence pair of dimension 2*1*300. I want to split this output into two vectors of shape 1*300 to calculate its absolute difference and product.

x = MaxPooling2D(pool_size=(1,MAX_SEQUENCE_LENGTH),strides=(1,1))(x)
x_A = Reshape((1,EMBEDDING_DIM))(x[:,0])
x_B = Reshape((1,EMBEDDING_DIM))(x[:,1])

diff = keras.layers.Subtract()([x_A, x_B])
prod = keras.layers.Multiply()([x_A, x_B])


nn = keras.layers.Concatenate()([diff, prod])

Currently, when I do x[:,0] it throws an error saying AttributeError: 'Tensor' object has no attribute '_keras_shape'. I assume the result of splitting of tensor object is a tensor object that doesn't have _keras_shape.

Can someone help me solve this? Thanks.


回答1:


Keras adds some info to tensors when they're processed in layers. Since you're splitting the tensor outside layers, it loses that info.

The solution involves returning the split tensors from Lambda layers:

x_A = Lambda(lambda x: x[:,0], output_shape=notNecessaryWithTensorflow)(x)
x_B = Lambda(lambda x: x[:,1], output_shape=notNecessaryWithTensorflow)(x)
x_A = Reshape((1,EMBEDDING_DIM))(x_A)
x_B = Reshape((1,EMBEDDING_DIM))(x_B)


来源:https://stackoverflow.com/questions/47616588/keras-throws-tensor-object-has-no-attribute-keras-shape-when-splitting-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!