Tensor Manipulation II

매일 공부(ML)·2022년 1월 20일
0

Pytorch

목록 보기
5/8

차례

  • View

  • Squeeze

  • Unsqueeze

  • Type Casting

  • Concatenate

  • Stacking

  • In-place Operation


View(Reshape)
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])


Squeeze
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])

Unsqueeze
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])

Type Casting
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.])


Concatenate
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.]])

Stacking
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.]])
        

Ones and Zeros
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.]])

In-place Operation
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.]])
profile
성장을 도울 아카이빙 블로그

0개의 댓글