import numpy as np
import torch
t = np.array([[[0, 1, 2],
[3, 4, 5]],
[[ 6, 7, 8],
[9, 10, 11]]])
ft = torch.FloatTensor(t)
print(ft)
print(ft.shape)
tensor([[[ 0., 1., 2.], [ 3., 4., 5.]], [[ 6., 7., 8.], [ 9., 10., 11.]]]) 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])
print(ft.view([-1, 4, 3]))
print(ft.view([-1, 4, 3]).shape)
tensor([[[ 0., 1., 2.], [ 3., 4., 5.], [ 6., 7., 8.], [ 9., 10., 11.]]]) torch.Size([1, 4, 3])
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) # [3, 1] -> [3]
print()
print(ft.squeeze(dim=0))
print(ft.squeeze(dim=0).shape) # dimension 0의 element count가 1이 아니므로 변화가 없다.
tensor([0., 1., 2.]) torch.Size([3]) tensor([[0.], [1.], [2.]]) torch.Size([3, 1])
ft = torch.Tensor([0, 1, 2])
print(ft.shape)
torch.Size([3])
print(ft.unsqueeze(0))
print(ft.unsqueeze(0).shape)
tensor([[0., 1., 2.]]) torch.Size([1, 3])
print(ft.view(1, -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])
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]) # T -> 1 / F -> 0
print(bt)
print()
bt = (lt == 3) # [1, 2, 3, 4] -> [F, F, T, F]
print(bt)
tensor([1, 0, 0, 1], dtype=torch.uint8) tensor([False, False, True, False])
print(bt.long())
print(bt.float())
tensor([0, 0, 1, 0]) tensor([0., 0., 1., 0.])
x = torch.FloatTensor([[1, 2], [3, 4]]) # 2 x 2
y = torch.FloatTensor([[5, 6], [7, 8]]) # 2 x 2
print(torch.cat([x, y], dim=0)) # 4 x 2
print(torch.cat([x, y], dim=0).shape)
print(torch.cat([x, y], dim=1)) # 2 x 4
print(torch.cat([x, y], dim=1).shape)
tensor([[1., 2.], [3., 4.], [5., 6.], [7., 8.]]) torch.Size([4, 2]) tensor([[1., 2., 5., 6.], [3., 4., 7., 8.]]) torch.Size([2, 4])
x = torch.FloatTensor([1, 4]) # 2
y = torch.FloatTensor([2, 5])
z = torch.FloatTensor([3, 6])
print(torch.stack([x, y, z])) # 3 x 2
print(torch.stack([x, y, z], dim=1)) # 2 x 3
tensor([[1., 4.], [2., 5.], [3., 6.]]) tensor([[1., 2., 3.], [4., 5., 6.]])
print(torch.cat([x.unsqueeze(0), y.unsqueeze(0), z.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.]])
x = torch.FloatTensor([[1, 2], [3, 4]])
print(x.mul(2.))
print(x)
print(x.mul_(2.)) # memory를 새로 선언하지 않고 기존의 memory주소에 넣기
print(x) # 다시 동일한 변수를 print 했을 시 변경된 값이 출력됨.
tensor([[2., 4.], [6., 8.]]) tensor([[1., 2.], [3., 4.]]) tensor([[2., 4.], [6., 8.]]) tensor([[2., 4.], [6., 8.]])