【问题标题】:Row wise Tensor multiplication逐行张量乘法
【发布时间】:2019-03-05 12:49:26
【问题描述】:

假设我们有两个二维张量

A = [[a, b], [c, d]]B=[[e, f], [g, h]]

我需要一个值为[ae + bf + ce + df, ag + ah + cg + ch]的一维张量

提前感谢您的帮助

【问题讨论】:

  • 看看here的逻辑。

标签: python python-3.x tensorflow tensor


【解决方案1】:
import tensorflow as tf

A=[[1, 2], [3, 4]]

B=[[5, 6], [7, 8]]

Ax = tf.Variable(initial_value=A)

Bx = tf.Variable(initial_value=B)

with tf.Session() as sess :
    sess.run( tf.global_variables_initializer() )
    ABx = tf.tensordot(Ax, Bx, axes=[[1], [1]])
    print(sess.run( tf.reduce_sum(ABx, 0) ))

ABx = tf.tensordot(Ax, Bx, axes=[[1], [1]]) 给你这个。

[[17 23]
 [39 53]]

tf.reduce_sum(ABx, 0) 给你这个。

[56 76]

代码tf.reduce_sum(tf.matmul(Ax, Bx,transpose_b=True),0) 也给出了相同的结果。

【讨论】:

    猜你喜欢
    • 2018-10-23
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    • 2018-10-15
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    相关资源
    最近更新 更多