如何将 2 维张量与 3 维张量连接

How to concat a 2-d tensor with a 3-d tensor

在AE-lstm中, 这是一个形状为 [batch, timestamp, diml](视为 [b, t, dl])的 lstm 输出,方面向量为 [batch, dima](视为 [b, da]

How to concat two variables to make the shape be [b, t, dl+da]?

这意味着对于每个批次,将方面向量连接到每个时间戳行。

我不是很确定,但我想你想要的是

C = tf.concat([A, tf.tile(tf.expand_dims(B, axis=1), [1, t, 1])], axis=-1)

其中 A 是你的 lstm,B 是方面向量。我通过简单地检查尺寸来验证它,这似乎是正确的。让我们看看这是否是您真正需要的。

编辑:明确地说,这是我用来测试的完整代码:

import tensorflow as tf

b = 5
t = 10
dl = 15
da = 12

A = tf.ones(shape=(b, t, dl))
B = tf.ones(shape=(b, da))

C = tf.concat([A, tf.tile(tf.expand_dims(B, axis=1), [1, t, 1])], axis=-1)
print(C)

这给出了预期的输出:

Tensor("concat:0", shape=(5, 10, 27), dtype=float32)