PyTorch torch.stack() 方法沿新維度連接(連接)一係列張量(兩個或更多張量)。它插入新的維度並沿該維度連接張量。此方法連接具有相同尺寸和形狀的張量。我們還可以使用 torch.cat() 來連接張量,但這裏我們討論 torch.stack() 方法。
用法: torch.stack(tensors, dim=0)
Arguments:
- 張量:它是具有相同形狀和維度的張量序列
- 暗淡:這是要插入的尺寸。它是 0 到輸入張量維數之間的整數。
返回: 它返回沿新維度的串聯張量。
讓我們借助一些 Python 3 示例來了解 torch.stack() 方法。
示例1:
在下麵的 Python 示例中,我們使用 torch.stack() 方法連接兩個一維張量。
Python3
# Python 3 program to demonstrate torch.stack() method
# for two one dimensional tensors
# importing torch
import torch
# creating tensors
x = torch.tensor([1.,3.,6.,10.])
y = torch.tensor([2.,7.,9.,13.])
# printing above created tensors
print("Tensor x:", x)
print("Tensor y:", y)
# join above tensor using "torch.stack()"
print("join tensors:")
t = torch.stack((x,y))
# print final tensor after join
print(t)
print("join tensors dimension 0:")
t = torch.stack((x,y), dim = 0)
print(t)
print("join tensors dimension 1:")
t = torch.stack((x,y), dim = 1)
print(t)
輸出:
Tensor x: tensor([ 1., 3., 6., 10.]) Tensor y: tensor([ 2., 7., 9., 13.]) join tensors: tensor([[ 1., 3., 6., 10.], [ 2., 7., 9., 13.]]) join tensors dimension 0: tensor([[ 1., 3., 6., 10.], [ 2., 7., 9., 13.]]) join tensors dimension 1: tensor([[ 1., 2.], [ 3., 7.], [ 6., 9.], [10., 13.]])
解釋:在上麵的代碼中,張量 x 和 y 是一維的,每個都有四個元素。最終的級聯張量是二維張量。由於維度為 1,我們可以堆疊維度為 0 和 1 的張量。當 dim =0 時,張量會堆疊,增加行數。當dim = 1 時,張量會轉置並沿列堆疊。
示例 2:
在下麵的 Python 示例中,我們使用 torch.stack() 方法連接兩個一維張量。
Python3
# Python 3 program to demonstrate torch.stack() method
# for two 2D tensors.
# importing torch
import torch
# creating tensors
x = torch.tensor([[1., 3., 6.], [10., 13., 20.]])
y = torch.tensor([[2., 7., 9.], [14., 21., 34.]])
# printing above created tensors
print("Tensor x:\n", x)
print("Tensor y:\n", y)
# join above tensor using "torch.stack()"
print("join tensors")
t = torch.stack((x, y))
# print final tensor after join
print(t)
print("join tensors in dimension 0:")
t = torch.stack((x, y), 0)
print(t)
print("join tensors in dimension 1:")
t = torch.stack((x, y), 1)
print(t)
print("join tensors in dimension 2:")
t = torch.stack((x, y), 2)
print(t)
輸出:
Tensor x: tensor([[ 1., 3., 6.], [10., 13., 20.]]) Tensor y: tensor([[ 2., 7., 9.], [14., 21., 34.]]) join tensors tensor([[[ 1., 3., 6.], [10., 13., 20.]], [[ 2., 7., 9.], [14., 21., 34.]]]) join tensors in dimension 0: tensor([[[ 1., 3., 6.], [10., 13., 20.]], [[ 2., 7., 9.], [14., 21., 34.]]]) join tensors in dimension 1: tensor([[[ 1., 3., 6.], [ 2., 7., 9.]], [[10., 13., 20.], [14., 21., 34.]]]) join tensors in dimension 2: tensor([[[ 1., 2.], [ 3., 7.], [ 6., 9.]], [[10., 14.], [13., 21.], [20., 34.]]])
解釋:在上麵的代碼中,x和y是二維張量。請注意,最終張量是 3-D 張量。由於每個輸入張量的維度為 2,因此我們可以將維度為 0 和 2 的張量堆疊起來。通過 dim = 0、1 和 2 查看最終輸出張量之間的差異。
示例 3:
在此示例中,我們連接兩個以上的張量。我們可以連接任意數量的張量。
Python3
# Python 3 program to demonstrate torch.stack() method
# for three one-dimensional tensors
# importing torch
import torch
# creating tensors
x = torch.tensor([1., 3., 6., 10.])
y = torch.tensor([2., 7., 9., 13.])
z = torch.tensor([4., 5., 8., 11.])
# printing above created tensors
print("Tensor x:", x)
print("Tensor y:", y)
print("Tensor z:", z)
# join above tensor using "torch.stack()"
print("join tensors:")
t = torch.stack((x, y, z))
# print final tensor after join
print(t)
print("join tensors dimension 0:")
t = torch.stack((x, y, z), dim=0)
print(t)
print("join tensors dimension 1:")
t = torch.stack((x, y, z), dim=1)
print(t)
輸出:
Tensor x: tensor([ 1., 3., 6., 10.]) Tensor y: tensor([ 2., 7., 9., 13.]) Tensor z: tensor([ 4., 5., 8., 11.]) join tensors: tensor([[ 1., 3., 6., 10.], [ 2., 7., 9., 13.], [ 4., 5., 8., 11.]]) join tensors dimension 0: tensor([[ 1., 3., 6., 10.], [ 2., 7., 9., 13.], [ 4., 5., 8., 11.]]) join tensors dimension 1: tensor([[ 1., 2., 4.], [ 3., 7., 5.], [ 6., 9., 8.], [10., 13., 11.]])
示例 4:演示錯誤
在下麵的示例中,我們顯示了輸入張量形狀不同時的錯誤。
Python3
# Python 3 program to demonstrate torch.stack() method
# for one-dimensional tensors
# importing torch
import torch
# creating tensors
x = torch.tensor([1., 3., 6., 10.])
y = torch.tensor([2., 7., 9.])
# printing above created tensors
print("Tensor x:", x)
print("Tensor y:", y)
# join above tensor using "torch.stack()"
print("join tensors:")
t = torch.stack((x, y))
# print final tensor after join
print(t)
print("join tensors dimension 0:")
t = torch.stack((x, y), dim=0)
print(t)
print("join tensors dimension 1:")
t = torch.stack((x, y), dim=1)
print(t)
輸出:
Shape of x: torch.Size([4])
Shape of y: torch.Size([3])
RuntimeError: stack expects each tensor to be equal size, but got [4] at entry 0 and [3] at entry 1
請注意,兩個張量的形狀並不相同。它會引發運行時錯誤。同樣,當張量的維度不同時,會引發運行時錯誤。自己嘗試不同維度的張量,看看輸出如何。
相關用法
- Python PyTorch sin()用法及代碼示例
- Python PyTorch sinh()用法及代碼示例
- Python PyTorch acos()用法及代碼示例
- Python PyTorch asin()用法及代碼示例
- Python PyTorch atan()用法及代碼示例
- Python PyTorch cos()用法及代碼示例
- Python PyTorch cosh()用法及代碼示例
- Python PyTorch tan()用法及代碼示例
- Python PyTorch tanh()用法及代碼示例
- Python PyTorch from_numpy()用法及代碼示例
- Python PyTorch div()用法及代碼示例
- Python PyTorch clamp()用法及代碼示例
- Python PyTorch ceil()用法及代碼示例
- Python PyTorch add()用法及代碼示例
- Python PyTorch abs()用法及代碼示例
- Python PyTorch exp()用法及代碼示例
- Python PyTorch numel()用法及代碼示例
- Python PyTorch is_storage()用法及代碼示例
- Python PyTorch is_tensor()用法及代碼示例
- Python PyTorch trunc()用法及代碼示例
- Python PyTorch frac()用法及代碼示例
- Python PyTorch log()用法及代碼示例
- Python PyTorch fmod()用法及代碼示例
- Python PyTorch floor()用法及代碼示例
- Python PyTorch zeros()用法及代碼示例
注:本文由純淨天空篩選整理自shahidedu7大神的英文原創作品 Python PyTorch stack() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。