行张量乘法

2024-04-24 02:47:38 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我们有两个二维张量

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

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

提前谢谢你的帮助


Tags: dfchcgceagaeahbf
1条回答
网友
1楼 · 发布于 2024-04-24 02:47:38
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)也给出了相同的结果。你知道吗

相关问题 更多 >