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.
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