【发布时间】: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
假设我们有两个二维张量
A = [[a, b], [c, d]] 和 B=[[e, f], [g, h]]
我需要一个值为[ae + bf + ce + df, ag + ah + cg + ch]的一维张量
提前感谢您的帮助
【问题讨论】:
标签: python python-3.x tensorflow tensor
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) 也给出了相同的结果。
【讨论】: