【问题标题】: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]]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-04
      • 2020-01-16
      • 2018-11-06
      • 2011-12-13
      • 1970-01-01
      相关资源
      最近更新 更多