View
Squeeze
Unsqueeze
Type Casting
Concatenate
Stacking
In-place Operation
t = np.array([[[0, 1, 2],
[3, 4, 5]],
[[6, 7, 8],
[9, 10, 11]]])
ft = torch.FloatTensor(t)
print(ft.shape) #torch.Size([2, 2, 3])
print(ft.view([-1,3]))
print(ft.view([-1,3]).shape)
#결과
tensor([[ 0., 1., 2.],
[ 3., 4., 5.],
[ 6., 7., 8.],
[ 9., 10., 11.]])
torch.Size([4, 3])
print(ft.view([-1, 1, 3]))
print(ft.view([-1, 1, 3]).shape)
#결과
tensor([[[ 0., 1., 2.]],
[[ 3., 4., 5.]],
[[ 6., 7., 8.]],
[[ 9., 10., 11.]]])
torch.Size([4, 1, 3])
import torch
import numpy as np
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])
FT = torch.Tensor([0,1,2])
print(FT.shape) # torch.Size([3])
print(FT.unsqueeze(0)) #tensor([[0., 1., 2.]])
print(FT.unsqueeze(0).shape) #torch.Size([1, 3])
print(FT.view(1, -1)) #tensor([[0., 1., 2.]])
print(FT.view(1, -1).shape) #torch.Size([1, 3])
print(FT.unsqueeze(1))
print(FT.view(1, -1).shape)
#결과
tensor([[0.],
[1.],
[2.]])
torch.Size([1, 3])
print(FT.unsqueeze(1))
print(FT.unsqueeze(1).shape)
#결과
tensor([[0.],
[1.],
[2.]])
torch.Size([3, 1])
print(FT.unsqueeze(-1))
print(FT.unsqueeze(-1).shape)
#결과
tensor([[0.],
[1.],
[2.]])
torch.Size([3, 1])
lt = torch.LongTensor([1,2,3,4])
print(lt) #tensor([1, 2, 3, 4])
print(lt.float()) #tensor([1., 2., 3., 4.])
bt = torch.ByteTensor([True, False, False, True])
print(bt) #tensor([1, 0, 0, 1], dtype=torch.uint8)
print(bt.long()) #tensor([1, 0, 0, 1])
print(bt.float()) #tensor([1., 0., 0., 1.])
x = torch.FloatTensor([[1,2], [3, 4]])
y = torch.FloatTensor([[5,6], [7,8]])
print(torch.cat([x, y], dim = 0))
print(torch.cat([x, y], dim = 1))
#결과
tensor([[1., 2.],
[3., 4.],
[5., 6.],
[7., 8.]])
tensor([[1., 2., 5., 6.],
[3., 4., 7., 8.]])
x1 = torch.FloatTensor([1, 4])
y1 = torch.FloatTensor([2, 5])
z1 = torch.FloatTensor([3, 6])
print(torch.stack([x1, y1, z1]))
print(torch.stack([x1, y1, z1], dim =1))
#결과
tensor([[1., 4.],
[2., 5.],
[3., 6.]])
tensor([[1., 2., 3.],
[4., 5., 6.]])
print(torch.cat([x1.unsqueeze(0),y1.unsqueeze(0), z1.unsqueeze(0)], dim = 0))
#결과
tensor([[1., 4.],
[2., 5.],
[3., 6.]])
x = torch.FloatTensor([[0, 1, 2], [2, 1, 0]])
print(x)
tensor([[0., 1., 2.],
[2., 1., 0.]])
print(torch.ones_like(x))
print(torch.zeros_like(x))
tensor([[1., 1., 1.],
[1., 1., 1.]])
tensor([[0., 0., 0.],
[0., 0., 0.]])
x2 = torch.FloatTensor([[1, 2], [3, 4]])
print(x2.mul(2.))
print(x2)
print(x2.mul_(2.))
print(x2)
tensor([[2., 4.],
[6., 8.]])
tensor([[1., 2.],
[3., 4.]])
tensor([[2., 4.],
[6., 8.]])
tensor([[2., 4.],
[6., 8.]])