如何将张量中的每个向量乘以向量的每个元素
How do I multiply each vector in tensor by each element of vector
嗨,所以我真正想要的是,如果我们有矩阵 W 和向量 V,例如:
V=[1,2,3,4]
W=[[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]]
我们应该得到结果:
result=[[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]]
我在网站上找到了这个方法:
V = tf.constant([1,2,4], dtype=tf.float32)
W = tf.constant([[1,2,3,4],[1,2,3,4],[1,2,3,4]], dtype=tf.float32)
tf.multiply(tf.expand_dims(V,1),W)
## produce: [[1,2,3,4],[2,4,6,8],[4,8,12,16]]
这正是我想要的,但是当我在我的模型上实现它时,它还包括导致错误的向量的批量大小
with input shapes: [?,1,297], [?,297,300].
我认为这可能会产生相同的错误
V = tf.constant([[1,2,4]], dtype=tf.float32)
W = tf.constant([[[1,2,3,4],[1,2,3,4],[1,2,3,4]]], dtype=tf.float32)
tf.multiply(tf.expand_dims(V,1),W)
我想知道从 softmax 输出向量中获取每个元素并将它们乘以特征张量中每个向量的权重的标准程序是什么
我发现通过使用
V = tf.constant([[1,2,4]], dtype=tf.float32)
W = tf.constant([[[1,2,3,4],[1,2,3,4],[1,2,3,4]]], dtype=tf.float32)
h2=tf.keras.layers.multiply([W,tf.expand_dims(V,2)])
keras 层会为我们忽略 batch size 部分,但我们必须更改 expand dim 的参数,因为我们在馈入层之前仍然需要考虑 V 的 batch size。
嗨,所以我真正想要的是,如果我们有矩阵 W 和向量 V,例如:
V=[1,2,3,4]
W=[[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]]
我们应该得到结果:
result=[[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]]
我在网站上找到了这个方法:
V = tf.constant([1,2,4], dtype=tf.float32)
W = tf.constant([[1,2,3,4],[1,2,3,4],[1,2,3,4]], dtype=tf.float32)
tf.multiply(tf.expand_dims(V,1),W)
## produce: [[1,2,3,4],[2,4,6,8],[4,8,12,16]]
这正是我想要的,但是当我在我的模型上实现它时,它还包括导致错误的向量的批量大小
with input shapes: [?,1,297], [?,297,300].
我认为这可能会产生相同的错误
V = tf.constant([[1,2,4]], dtype=tf.float32)
W = tf.constant([[[1,2,3,4],[1,2,3,4],[1,2,3,4]]], dtype=tf.float32)
tf.multiply(tf.expand_dims(V,1),W)
我想知道从 softmax 输出向量中获取每个元素并将它们乘以特征张量中每个向量的权重的标准程序是什么
我发现通过使用
V = tf.constant([[1,2,4]], dtype=tf.float32)
W = tf.constant([[[1,2,3,4],[1,2,3,4],[1,2,3,4]]], dtype=tf.float32)
h2=tf.keras.layers.multiply([W,tf.expand_dims(V,2)])
keras 层会为我们忽略 batch size 部分,但我们必须更改 expand dim 的参数,因为我们在馈入层之前仍然需要考虑 V 的 batch size。