Keras Lambda层错误:未返回张量



我正在使用Lambda创建一个自注意层,但它引发了一个错误,即Lambda层的输出不是张量。

我的代码:

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

我调用Lambda函数如下:

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

使用lambda函数包装K.stack,如下所示将解决问题。

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

最新更新