【问题标题】:Tensorflow: multiply matrix columns by elements of vector and get matrix backTensorflow:将矩阵列乘以向量的元素并返回矩阵
【发布时间】:2018-09-10 16:10:49
【问题描述】:
这是我想在 Tensorflow 中完成的任务。
我有 2x2 矩阵(可训练)
x_1 x_2
x_3 x_4
我有输入向量
a
b
我想将矩阵的每一列乘以向量的元素并返回以下矩阵
ax_1 bx_2
ax_3 bx_4
我可以通过将矩阵的每一列声明为单独的变量来得到这个结果,但我想知道是否有更优雅的解决方案。
【问题讨论】:
标签:
tensorflow
vector
matrix-multiplication
【解决方案1】:
感谢broadcasting,您应该可以使用正则乘法运算符:
import tensorflow as tf
x = tf.constant([[3, 5], [7, 11]], dtype=tf.int32)
a = tf.constant([4, 8], dtype=tf.int32)
y = x * a
with tf.Session() as sess:
print(sess.run(y)) # Result: [[12, 40], [28, 88]]