将两个具有不同形状的3D张量乘以(张量)

发布于 2025-02-01 11:42:47 字数 373 浏览 4 评论 0原文

在TensorFlow中,如果我有两个3维张量,一个尺寸之一(100、9、135),另一个具有尺寸(100、29、135):

x1:tensor(shape =(100、9、135),dtype = float64)

x2:张量(shape =(100,29,135),dtype = float64)

我需要乘以这两个张量,因此当我使用“ tf.multiply”时,我会出现错误,如下所示:

z = tf.tf.multiplyly (x1,x2)

print(“ z:”,z)

值:尺寸必须相等,但为9和29,带有输入形状:[100,9,135],[100,29,135]。

如何在TensorFlow中完成?提前致谢。

In TensorFlow, If I have two 3 dimensional tensors, one of dimension (100, 9, 135) and the other has dimension (100, 29, 135):

x1: Tensor(shape=(100, 9, 135), dtype=float64)

x2: Tensor(shape=(100, 29, 135), dtype=float64)

I need to multiply these two tensors, so when I was using "tf.multiply" I got an error as follows:

z = tf.multiply(x1,x2)

print("z:", z)

ValueError: Dimensions must be equal, but are 9 and 29, with input shapes: [100,9,135], [100,29,135].

How can this be done in TensorFlow? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉酒的小男人 2025-02-08 11:42:48

我不确定您期望的输出形状,但是您可以尝试使用tf.einsum进行矩阵乘法进行实验:

import tensorflow as tf

t1 = tf.random.normal((2, 9, 35))
t2 = tf.random.normal((2, 29, 35))

e = tf.einsum('bij,bkj->bik', t1, t2)
# or e =  tf.einsum('...ij,...kj->...ik', t1, t2)

print(e)

检查 docs 有关更多选项。

I am not sure what output shape you expect, but you can try experimenting with tf.einsum to do matrix multiplication:

import tensorflow as tf

t1 = tf.random.normal((2, 9, 35))
t2 = tf.random.normal((2, 29, 35))

e = tf.einsum('bij,bkj->bik', t1, t2)
# or e =  tf.einsum('...ij,...kj->...ik', t1, t2)

print(e)

Check the docs for more options.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文