【发布时间】:2018-11-12 04:52:01
【问题描述】:
我正在使用 Lambda 创建一个 self attention 层,但它引发了一个错误,即 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的输出是type:
,下一层需要输入是tensor -
已解决!使用 lambda wrap 修复 K.stack
标签: python tensorflow keras lstm keras-layer