如何在 PyTorch 中加入张量?
我们可以使用和连接两个或多个张量。用于连接两个或多个张量,而用于堆叠张量。我们可以加入不同维度的张量,例如0维、-1维。torch.cat()torch.stack()torch.cat()torch.stack()
双方并用于加盟张量。那么,这两种方法的基本区别是什么?torch.cat()torch.stack()
torch.cat()沿现有维度连接一系列张量,因此不会改变张量的维度。
torch.stack()沿着新的维度堆叠张量,结果,它增加了维度。
脚步
导入所需的库。在以下所有示例中,所需的Python库是torch。确保您已经安装了它。
创建两个或多个PyTorch张量并打印它们。
使用或加入上面创建的张量。提供维度,即0,-1,以连接特定维度的张量torch.cat()torch.stack()
最后,打印连接或堆叠的张量。
示例1
# Python program to join tensors in PyTorch
# import necessary library
import torch
# create tensors
T1 = torch.Tensor([1,2,3,4])
T2 = torch.Tensor([0,3,4,1])
T3 = torch.Tensor([4,3,2,5])
# print above created tensors
print("T1:", T1)
print("T2:", T2)
print("T3:", T3)
# join (concatenate) above tensors using torch.cat()
T = torch.cat((T1,T2,T3))
# print final tensor after concatenation
print("T:",T)输出结果当您运行上述Python3代码时,它将产生以下输出
T1: tensor([1., 2., 3., 4.]) T2: tensor([0., 3., 4., 1.]) T3: tensor([4., 3., 2., 5.]) T: tensor([1., 2., 3., 4., 0., 3., 4., 1., 4., 3., 2., 5.])
示例2
# import necessary library
import torch
# create tensors
T1 = torch.Tensor([[1,2],[3,4]])
T2 = torch.Tensor([[0,3],[4,1]])
T3 = torch.Tensor([[4,3],[2,5]])
# print above created tensors
print("T1:\n", T1)
print("T2:\n", T2)
print("T3:\n", T3)
print("join(concatenate) tensors in the 0 dimension")
T = torch.cat((T1,T2,T3), 0)
print("T:\n", T)
print("join(concatenate) tensors in the -1 dimension")
T = torch.cat((T1,T2,T3), -1)
print("T:\n", T)输出结果当您运行上述Python3代码时,它将产生以下输出
T1:
tensor([[1., 2.],
[3., 4.]])
T2:
tensor([[0., 3.],
[4., 1.]])
T3:
tensor([[4., 3.],
[2., 5.]])
join(concatenate) tensors in the 0 dimension
T:
tensor([[1., 2.],
[3., 4.],
[0., 3.],
[4., 1.],
[4., 3.],
[2., 5.]])
join(concatenate) tensors in the -1 dimension
T:
tensor([[1., 2., 0., 3., 4., 3.],
[3., 4., 4., 1., 2., 5.]])在上面的示例中,二维张量沿0维和-1维连接。在0维中连接会增加行数,而保持列数不变。
示例3
# Python program to join tensors in PyTorch
# import necessary library
import torch
# create tensors
T1 = torch.Tensor([1,2,3,4])
T2 = torch.Tensor([0,3,4,1])
T3 = torch.Tensor([4,3,2,5])
# print above created tensors
print("T1:", T1)
print("T2:", T2)
print("T3:", T3)
# join above tensor using "torch.stack()"
print("join(stack) tensors")
T = torch.stack((T1,T2,T3))
# print final tensor after join
print("T:\n",T)
print("join(stack) tensors in the 0 dimension")
T = torch.stack((T1,T2,T3), 0)
print("T:\n", T)
print("join(stack) tensors in the -1 dimension")
T = torch.stack((T1,T2,T3), -1)
print("T:\n", T)输出结果当您运行上述Python3代码时,它将产生以下输出
T1: tensor([1., 2., 3., 4.])
T2: tensor([0., 3., 4., 1.])
T3: tensor([4., 3., 2., 5.])
join(stack) tensors
T:
tensor([[1., 2., 3., 4.],
[0., 3., 4., 1.],
[4., 3., 2., 5.]])
join(stack) tensors in the 0 dimension
T:
tensor([[1., 2., 3., 4.],
[0., 3., 4., 1.],
[4., 3., 2., 5.]])
join(stack) tensors in the -1 dimension
T:
tensor([[1., 0., 4.],
[2., 3., 3.],
[3., 4., 2.],
[4., 1., 5.]])在上面的例子中,你可以注意到一维张量是堆叠的,最终的张量是一个二维张量。
示例4
# import necessary library
import torch
# create tensors
T1 = torch.Tensor([[1,2],[3,4]])
T2 = torch.Tensor([[0,3],[4,1]])
T3 = torch.Tensor([[4,3],[2,5]])
# print above created tensors
print("T1:\n", T1)
print("T2:\n", T2)
print("T3:\n", T3)
print("Join (stack)tensors in the 0 dimension")
T = torch.stack((T1,T2,T3), 0)
print("T:\n", T)
print("Join(stack) tensors in the -1 dimension")
T = torch.stack((T1,T2,T3), -1)
print("T:\n", T)输出结果当您运行上述Python3代码时,它将产生以下输出。
T1:
tensor([[1., 2.],
[3., 4.]])
T2:
tensor([[0., 3.],
[4., 1.]])
T3:
tensor([[4., 3.],
[2., 5.]])
Join (stack)tensors in the 0 dimension
T:
tensor([[[1., 2.],
[3., 4.]],
[[0., 3.],
[4., 1.]],
[[4., 3.],
[2., 5.]]])
Join(stack) tensors in the -1 dimension
T:
tensor([[[1., 0., 4.],
[2., 3., 3.]],
[[3., 4., 2.],
[4., 1., 5.]]])在上面的示例中,您可以注意到2D张量被连接(堆叠)以创建3D张量。