Keras Lambda layer error: did not return a tensor
Keras Lambda layer error: did not return a tensor
我正在使用 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]])
我正在使用 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]])