PyTorch:如何将包含整数和张量元组的列表中的元素相乘
PyTorch: How to multiply elements in a list containing tuples of integer and tensor
我有以下列表 ABC_lst
,其中包含一个整数和一个张量的元组。
代码:
import numpy as np
import torch
A_int = 40
A_tsr = 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]]))
A_tpl = (A_int, A_tsr)
B_int = 42
B_tsr = torch.tensor(np.array([[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]]))
B_tpl = (B_int, B_tsr)
C_int = 38
C_tsr = torch.tensor(np.array([[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]]))
C_tpl = (C_int, C_tsr)
ABC_lst = [A_tpl, B_tpl, C_tpl]
ABC_lst
输出:
[(40, 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]])),
(42, tensor([[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]])),
(38, tensor([[ 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]]))]
如何将整数与相应的张量相乘,例如。将 40
乘以
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]])
将42
乘以
tensor([[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]])
等等。
返回的对象应该是张量,如下所示:
tensor([[[ 40., 80., 120., 160., 200.],
[ 40., 80., 120., 160., 200.],
[ 40., 80., 120., 160., 200.],
[ 40., 80., 120., 160., 200.],
[ 40., 80., 120., 160., 200.]],
[[168., 210., 252., 294., 336.],
[168., 210., 252., 294., 336.],
[168., 210., 252., 294., 336.],
[168., 210., 252., 294., 336.],
[168., 210., 252., 294., 336.]],
[[266., 304., 342., 380., 418.],
[266., 304., 342., 380., 418.],
[266., 304., 342., 380., 418.],
[266., 304., 342., 380., 418.],
[266., 304., 342., 380., 418.]]])
在上面的例子中,我有 3 组整数和张量。我如何为整数和张量的任意“集合”概括上述乘法的代码?
如果有人能提供帮助,将不胜感激。
编辑:我需要在 GPU 中完成以上所有操作,因此需要使用张量。
从两个列表开始:I
整数列表和 X
张量列表:
I = [torch.tensor(40), torch.tensor(42), torch.tensor(38)]
X = [
torch.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]]),
torch.tensor([[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]]),
torch.tensor([[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]]),
]
您可以压缩两者并创建一个包含所有乘法结果的列表。然后将这个列表堆叠成一个张量,如下所示:
torch.stack([i*x for i, x in zip(I, X)])
当然,您可以向列表中添加更多元素。
我有以下列表 ABC_lst
,其中包含一个整数和一个张量的元组。
代码:
import numpy as np
import torch
A_int = 40
A_tsr = 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]]))
A_tpl = (A_int, A_tsr)
B_int = 42
B_tsr = torch.tensor(np.array([[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]]))
B_tpl = (B_int, B_tsr)
C_int = 38
C_tsr = torch.tensor(np.array([[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]]))
C_tpl = (C_int, C_tsr)
ABC_lst = [A_tpl, B_tpl, C_tpl]
ABC_lst
输出:
[(40, 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]])),
(42, tensor([[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]])),
(38, tensor([[ 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]]))]
如何将整数与相应的张量相乘,例如。将 40
乘以
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]])
将42
乘以
tensor([[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]])
等等。
返回的对象应该是张量,如下所示:
tensor([[[ 40., 80., 120., 160., 200.],
[ 40., 80., 120., 160., 200.],
[ 40., 80., 120., 160., 200.],
[ 40., 80., 120., 160., 200.],
[ 40., 80., 120., 160., 200.]],
[[168., 210., 252., 294., 336.],
[168., 210., 252., 294., 336.],
[168., 210., 252., 294., 336.],
[168., 210., 252., 294., 336.],
[168., 210., 252., 294., 336.]],
[[266., 304., 342., 380., 418.],
[266., 304., 342., 380., 418.],
[266., 304., 342., 380., 418.],
[266., 304., 342., 380., 418.],
[266., 304., 342., 380., 418.]]])
在上面的例子中,我有 3 组整数和张量。我如何为整数和张量的任意“集合”概括上述乘法的代码?
如果有人能提供帮助,将不胜感激。
编辑:我需要在 GPU 中完成以上所有操作,因此需要使用张量。
从两个列表开始:I
整数列表和 X
张量列表:
I = [torch.tensor(40), torch.tensor(42), torch.tensor(38)]
X = [
torch.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]]),
torch.tensor([[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]]),
torch.tensor([[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]]),
]
您可以压缩两者并创建一个包含所有乘法结果的列表。然后将这个列表堆叠成一个张量,如下所示:
torch.stack([i*x for i, x in zip(I, X)])
当然,您可以向列表中添加更多元素。