In 3-dimension tensor, count how many items are there.
0th dimension
[ [ [ , ] ] ]
[] = 3
1st dimension
[ [ [ , ] ] ]
[] = 1
2nd dimension
[ [ [ , ] ] ]
[] = 2
tensor([[[0, 1]],
[[1, 1]],
[[2, 1]]])
torch.Size([3, 1, 2])
- FloatTensor
Create matrix.
torch.FloatTensor([[1.,2.],[3.,4.],[5.,6.]])
>>>
tensor([[1., 2.],
[3., 4.],
[5., 6.]])
- view
Same function as reshape in numpy.
-1 : It means I am not sure so I will let pytorch to decide the dimension.
torch.FloatTensor([[[1.,2.],[3.,4.]],[[5.,6.],[7.,8.]]])
>>>
tensor([[[1., 2.],
[3., 4.]],
[[5., 6.],
[7., 8.]]])
t.view([-1,2])
>>>
tensor([[1., 2.],
[3., 4.],
[5., 6.],
[7., 8.]])
a=torch.randn(1,2,3,4)
a
>>>
tensor([[[[ 0.3861, -0.2761, -2.0326, 0.1494],
[ 0.4762, -0.3741, 1.5705, -0.7584],
[ 0.4935, -0.9561, -0.5429, 2.1391]],
[[ 0.1052, -0.4533, -1.4188, 1.7282],
[-0.1203, -1.3732, 0.4870, -1.5323],
[-0.6002, -0.1653, 2.3912, -2.3870]]]])
a.view(1,3,2,4)
>>>
tensor([[[[ 0.3861, -0.2761, -2.0326, 0.1494],
[ 0.4762, -0.3741, 1.5705, -0.7584]],
[[ 0.4935, -0.9561, -0.5429, 2.1391],
[ 0.1052, -0.4533, -1.4188, 1.7282]],
[[-0.1203, -1.3732, 0.4870, -1.5323],
[-0.6002, -0.1653, 2.3912, -2.3870]]]])
- squeeze
Remove 1-dimension from tensor.
ft=torch.FloatTensor([[0],[1],[2]])
print(ft)
print(ft.shape)
>>>
tensor([[0.],
[1.],
[2.]])
torch.Size([3, 1])
print(ft.squeeze())
print(ft.squeeze().shape)
>>>
tensor([0., 1., 2.])
torch.Size([3])
- unsqueeze
Add 1-dimension.
ft_1=ft.squeeze()
ft_1.unsqueeze(0)
>>>
tensor([[0., 1., 2.]])
- cat
Concatenate.
x=torch.LongTensor([[1,2],[3,4]]).float()
y=torch.LongTensor([[5,6],[7,8]]).float()
torch.cat([x,y])
>>>
tensor([[1., 2.],
[3., 4.],
[5., 6.],
[7., 8.]])
torch.cat([x,y],dim=0)
>>>
tensor([[1., 2.],
[3., 4.],
[5., 6.],
[7., 8.]])
torch.cat([x,y],dim=1)
>>>
tensor([[1., 2., 5., 6.],
[3., 4., 7., 8.]])
- stack
x = torch.FloatTensor([1, 4])
y = torch.FloatTensor([2, 5])
z = torch.FloatTensor([3, 6])
torch.stack([x, y, z])
>>>
tensor([[1., 4.],
[2., 5.],
[3., 6.]])
Same as below.
torch.cat([x.unsqueeze(0), y.unsqueeze(0)],dim=0)
>>>
tensor([[[1., 2.],
[3., 4.]],
[[5., 6.],
[7., 8.]]])
- size
t = torch.FloatTensor([0., 1., 2., 3., 4., 5., 6.])
print(t)
print(t.size(0))
print(t.size(dim=0))
>>>
tensor([0., 1., 2., 3., 4., 5., 6.])
7
7
'Deep Learning > PyTorch' 카테고리의 다른 글
PyTorch-permute vs transpose (0) | 2022.12.05 |
---|---|
RNN (0) | 2022.08.22 |
Word2Vec VS Neural networks Emedding (0) | 2022.08.21 |
PyTorch-randint, scatter_, log_softmax, nll_loss, cross_entropy (0) | 2022.08.10 |
Linear Regression-requires_grad, zero_grad, backward, step (0) | 2022.08.08 |