简体   繁体   English

PyTorch:如何通过广播两个不同形状的张量相乘

[英]PyTorch: How to multiply via broadcasting of two tensors with different shapes

I have the following two PyTorch tensors A and B.我有以下两个 PyTorch 张量 A 和 B。

A = torch.tensor(np.array([40, 42, 38]), dtype = torch.float64)

tensor([40., 42., 38.], dtype=torch.float64)
B = torch.tensor(np.array([[[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]], [[4,5,6,7,8],[4,5,6,7,8],[4,5,6,7,8],[4,5,6,7,8],[4,5,6,7,8]], [[7,8,9,10,11],[7,8,9,10,11],[7,8,9,10,11],[7,8,9,10,11],[7,8,9,10,11]]]), dtype = torch.float64)

tensor([[[ 1.,  2.,  3.,  4.,  5.],
         [ 1.,  2.,  3.,  4.,  5.],
         [ 1.,  2.,  3.,  4.,  5.],
         [ 1.,  2.,  3.,  4.,  5.],
         [ 1.,  2.,  3.,  4.,  5.]],

        [[ 4.,  5.,  6.,  7.,  8.],
         [ 4.,  5.,  6.,  7.,  8.],
         [ 4.,  5.,  6.,  7.,  8.],
         [ 4.,  5.,  6.,  7.,  8.],
         [ 4.,  5.,  6.,  7.,  8.]],

        [[ 7.,  8.,  9., 10., 11.],
         [ 7.,  8.,  9., 10., 11.],
         [ 7.,  8.,  9., 10., 11.],
         [ 7.,  8.,  9., 10., 11.],
         [ 7.,  8.,  9., 10., 11.]]], dtype=torch.float64)

Tensor A is of shape:张量 A 的形状为:

torch.Size([3])

Tensor B is of shape:张量 B 的形状为:

torch.Size([3, 5, 5])

How do I multiply tensor A with tensor B (using broadcasting) in such a way for eg.我如何以这种方式将张量 A 与张量 B(使用广播)相乘,例如。 the first value in tensor A (ie. 40. ) is multiplied with all the values in the first 'nested' tensor in tensor B, ie.张量 A 中的第一个值(即40. )乘以张量 B 中第一个“嵌套”张量中的所有值,即。

tensor([[[ 1.,  2.,  3.,  4.,  5.],
         [ 1.,  2.,  3.,  4.,  5.],
         [ 1.,  2.,  3.,  4.,  5.],
         [ 1.,  2.,  3.,  4.,  5.],
         [ 1.,  2.,  3.,  4.,  5.]],

and so on for the other 2 values in tensor A and the other two nested tensors in tensor B, respectively.对于张量 A 中的其他 2 个值和张量 B 中的其他两个嵌套张量,依此类推。

I could do this multiplication (via broadcasting) with numpy arrays if A and B are arrays of both shape (3,) - ie.我可以用 numpy arrays 做这个乘法(通过广播),如果 A 和 B 是两个形状 (3,) 的 arrays - 即。 A*B - but I can't seem to figure out a counterpart of this with PyTorch tensors. A*B - 但我似乎无法用 PyTorch 张量找出与之对应的对象。 Any help would really be appreciated.任何帮助将不胜感激。

When applying broadcasting in pytorch (as well as in numpy) you need to start at the last dimension (check out https://pytorch.org/docs/stable/notes/broadcasting.html ).在 pytorch(以及 numpy)中应用广播时,您需要从最后一个维度开始(查看https://pytorch.org/docs/stable/notes/broadcasting.html )。 If they do not match you need to reshape your tensor.如果它们不匹配,您需要重塑张量。 In your case they can't directly be broadcasted:在您的情况下,它们不能直接广播:

      [3]  # the two values in the last dimensions are not one and do not match
[3, 5, 5]

Instead you can redefine A = A[:, None, None] before muliplying such that you get shapes相反,您可以在多重运算之前重新定义A = A[:, None, None]以获得形状

[3, 1, 1]
[3, 5, 5]

which satisfies the conditions for broadcasting.满足广播条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM