【问题标题】:Keras Lambda layer error: did not return a tensorKeras Lambda 层错误:没有返回张量
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-26
    • 2019-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多