Keras Lambda层错误:未返回张量

时间:2018-11-12 04:52:02

标签: python tensorflow keras lstm keras-layer

我正在使用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)

1 个答案:

答案 0 :(得分:0)

使用lambda函数包装K.stack可以解决此问题。

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