Search code examples
pythontensorflowkeraslstmkeras-layer

Keras Lambda layer error: did not return a tensor


I am using Lambda to create a self attention layer, but it raise an error that the output of lambda layer is not a tensor.

My code:

def selfAttention(x):
    # input shape [None, n_window_sizes, n_hidden]
    temp_transpose = K.transpose(x) 
    inputs_transpose = K.permute_dimensions(temp_transpose, [2, 0, 1]) # [None, n_hidden, n_window_sizes]
    temp_weights = tf.matmul(x, inputs_transpose)
    weights = tf.nn.softmax(temp_weights)
    output = tf.matmul(weights, x)
    return output

I call Lambda function as below:

attention_input = K.stack([lstm[0], lstm[1], lstm[2]], axis = 1)
l_attention = Lambda(selfAttention)(attention_input)

Solution

  • Use lambda function to wrap K.stack as follow will solve the problem.

     attention_input = Lambda(lambda x: K.stack([x[0], x[1], x[2]], axis = 1))([lstm[0], lstm[1], lstm[2]])