!pip3 install torch torchvision
import torch
Requirement already satisfied: torch in /usr/local/lib/python3.6/dist-packages (1.1.0) Requirement already satisfied: torchvision in /usr/local/lib/python3.6/dist-packages (0.3.0) Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from torch) (1.16.4) Requirement already satisfied: pillow>=4.1.1 in /usr/local/lib/python3.6/dist-packages (from torchvision) (4.3.0) Requirement already satisfied: six in /usr/local/lib/python3.6/dist-packages (from torchvision) (1.12.0) Requirement already satisfied: olefile in /usr/local/lib/python3.6/dist-packages (from pillow>=4.1.1->torchvision) (0.46)
# https://pytorch.org/docs/stable/torch.html?highlight=rand#torch.rand
# torch.rand(sizes) -> [0,1)
# 0에서 1사이의 랜덤한 숫자
x = torch.rand(2,3)
x
tensor([[0.9312, 0.4167, 0.9262],
[0.2511, 0.3974, 0.0930]])
# https://pytorch.org/docs/stable/torch.html?highlight=randn#torch.randn
# Random Normal
# 정규분포에서 샘플링한 값
x = torch.randn(2,3)
x
tensor([[ 2.1308, -0.3603, -0.4545],
[-0.0652, 0.6919, 0.4636]])
# https://pytorch.org/docs/stable/torch.html?highlight=rand#torch.randint
# Random Integer
# 시작과 끝 사이의 랜덤한 자연수
x = torch.randint(2,5,size=(2,3))
x
tensor([[3, 2, 3],
[3, 3, 3]])
# https://pytorch.org/docs/stable/torch.html?highlight=zeros#torch.zeros
# torch.zeros(2,3) -> [[0,0,0],[0,0,0]]
# 0으로 채워진 텐서
x = torch.zeros(2,3)
x
tensor([[0., 0., 0.],
[0., 0., 0.]])
# https://pytorch.org/docs/stable/torch.html?highlight=zeros#torch.zeros_like
# same shape tensor filled with zeros
# 인자로 들어오는 텐서와 형태가 같은 0으로 채워진 텐서
ref = torch.rand(4,5)
x = torch.zeros_like(ref)
x
tensor([[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]])
# https://pytorch.org/docs/stable/torch.html?highlight=ones#torch.ones
# torch.ones(2,3) -> [[1,1,1],[1,1,]]
# 1로 채워진 텐서
x = torch.ones(2,3)
x
tensor([[1., 1., 1.],
[1., 1., 1.]])
# https://pytorch.org/docs/stable/torch.html?highlight=ones_like#torch.ones_like
# same shape tensor filled with ones
# 인자로 들어오는 텐서와 형태가 같은 1로 채워진 텐서
ref = torch.rand(4,5)
x = torch.ones_like(ref)
x
tensor([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
# https://pytorch.org/docs/stable/tensors.html?highlight=type#torch.Tensor.type
# without dtype, returns type of tensor
# tensor.type()은 해당 텐서의 타입을 리턴하고 type(tensor)는 토치의 텐서 클래스라는 것을 리턴함
x = torch.rand(2,3)
print(x.type())
print(type(x))
torch.FloatTensor <class 'torch.Tensor'>
# with dtype, cast the object to the dtype
# tensor.type()을 dtype과 함께 사용하면 텐서의 데이터 타입을 dtype에 넣어준 데이터 타입으로 바꿔줍니다.
double_x = x.type(dtype=torch.DoubleTensor)
print(double_x.type())
torch.DoubleTensor
# https://pytorch.org/docs/stable/tensors.html?highlight=type#torch.Tensor.type_as
# 위의 type 함수와 유사하게 type_as라는 함수를 사용해 데이터타입을 바꿀 수 있습니다.
int_x = x.type_as(torch.IntTensor())
print(int_x.type())
torch.IntTensor
# https://pytorch.org/docs/stable/torch.html?highlight=from_numpy#torch.from_numpy
# torch.from_numpy(ndarray) -> tensor
# from_numpy 함수를 이용해 넘파이 배열을 토치텐서로 바꿀 수 있습니다.
import numpy as np
x1 = np.ndarray(shape=(2,3), dtype=int,buffer=np.array([1,2,3,4,5,6]))
x2 = torch.from_numpy(x1)
x2,x2.type()
(tensor([[1, 2, 3],
[4, 5, 6]]), 'torch.LongTensor')
# https://pytorch.org/docs/stable/tensors.html?highlight=numpy#torch.Tensor.numpy
# tensor.numpy() -> ndarray
# 반대로 토치 텐서를 .numpy() 를 통해 넘파이 배열로 바꿀 수 있습니다.
x3 = x2.numpy()
x3
array([[1, 2, 3],
[4, 5, 6]])
# 배열을 인자로 넣어 토치 텐서를 생성할 수 있습니다.
x = torch.FloatTensor([[1,2,3],[4,5,6]])
x
tensor([[1., 2., 3.],
[4., 5., 6.]])
# https://pytorch.org/docs/stable/cuda.html?highlight=device#torch.cuda.device
# device 함수를 사용해 텐서를 원하는 장치로 이동시킬 수 있습니다.
cpu = torch.device('cpu')
gpu = torch.device('cuda')
# https://pytorch.org/docs/stable/cuda.html?highlight=available#torch.cuda.is_available
# gpu가 사용 가능한지 체크해줍니다.
if torch.cuda.is_available():
# https://pytorch.org/docs/stable/tensors.html?highlight=#torch.Tensor.to
# .to 함수를 이용해 지정한 장치로 이동시켜줍니다.
x_gpu = x.to(gpu)
print(x_gpu)
x_cpu = x_gpu.to(cpu)
print(x_cpu)
tensor([[1., 2., 3.],
[4., 5., 6.]], device='cuda:0')
tensor([[1., 2., 3.],
[4., 5., 6.]])
# https://pytorch.org/docs/stable/tensors.html?highlight=size#torch.Tensor.size
# size 함수를 이용해 텐서의 형태를 알 수 있습니다.
x = torch.FloatTensor(10,12,3,3)
x.size(), x.size()[1:2]
(torch.Size([10, 12, 3, 3]), torch.Size([12]))
# 임의의 텐서를 생성합니다.
x = torch.randn(4,3)
print(x)
tensor([[-1.1821, 0.9164, -0.2556],
[-0.5557, 1.4337, 1.5086],
[ 0.7669, -0.1645, 0.2401],
[-0.7632, 1.3450, 0.0454]])
# middle 2 rows
# 토치 텐서 역시 기존 파이썬 인덱싱을 똑같이 쓸 수 있습니다.
x[1:3,:]
tensor([[-0.5557, 1.4337, 1.5086],
[ 0.7669, -0.1645, 0.2401]])
# https://pytorch.org/docs/stable/torch.html?highlight=index_select#torch.index_select
# index select along dimension 1
# index_select 함수를 사용해 지정한 차원 기준으로 원하는 값들을 뽑아낼 수 있습니다.
selected = torch.index_select(x,dim=1,index=torch.LongTensor([0,2]))
selected
tensor([[-1.1821, -0.2556],
[-0.5557, 1.5086],
[ 0.7669, 0.2401],
[-0.7632, 0.0454]])
# https://pytorch.org/docs/stable/torch.html?highlight=masked_select#torch.masked_select
# torch.masked_select(input, mask)
# masked_select를 통해 뽑고자 하는 값들을 마스킹해서 선택할 수 있습니다.
x = torch.randn(2,3)
mask = torch.ByteTensor([[0,0,1],[0,1,0]])
out = torch.masked_select(x,mask)
print(x, mask, out, sep="\n\n")
tensor([[-0.0036, 0.7174, -0.0733],
[-0.9133, -0.6957, -0.7995]])
tensor([[0, 0, 1],
[0, 1, 0]], dtype=torch.uint8)
tensor([-0.0733, -0.6957])
# https://pytorch.org/docs/stable/torch.html?highlight=torch%20cat#torch.cat
# torch.cat(seq, dim=0) -> concatenate tensor along dim
# cat 함수를 이용해 텐서를 원하는대로 붙일 수 있습니다.
# cat은 concatenate의 약자입니다.
x = torch.FloatTensor([[1,2,3],[4,5,6]])
y = torch.FloatTensor([[-1,-2,-3],[-4,-5,-6]])
z1 = torch.cat([x,y],dim=0)
z2 = torch.cat([x,y],dim=1)
print(x,y,z1,z2,sep="\n\n")
tensor([[1., 2., 3.],
[4., 5., 6.]])
tensor([[-1., -2., -3.],
[-4., -5., -6.]])
tensor([[ 1., 2., 3.],
[ 4., 5., 6.],
[-1., -2., -3.],
[-4., -5., -6.]])
tensor([[ 1., 2., 3., -1., -2., -3.],
[ 4., 5., 6., -4., -5., -6.]])
# https://pytorch.org/docs/stable/torch.html?highlight=torch%20stack#torch.stack
# torch.stack(sequence,dim=0) -> stack along new dim
# stack 함수를 통해 텐서를 붙일수도 있습니다.
x = torch.FloatTensor([[1,2,3],[4,5,6]])
x_stack = torch.stack([x,x,x,x],dim=0)
print(x_stack,x_stack.size(),sep="\n\n")
tensor([[[1., 2., 3.],
[4., 5., 6.]],
[[1., 2., 3.],
[4., 5., 6.]],
[[1., 2., 3.],
[4., 5., 6.]],
[[1., 2., 3.],
[4., 5., 6.]]])
torch.Size([4, 2, 3])
# https://pytorch.org/docs/stable/torch.html?highlight=chunk#torch.chunk
# torch.chunk(tensor, chunks, dim=0) -> tensor into number of chunks
# chunk 함수를 통해 텐서를 원하는 chunk 개수만큼으로 분리할 수 있습니다.
x_1, x_2 = torch.chunk(z1,2,dim=0)
y_1, y_2, y_3 = torch.chunk(z1,3,dim=1)
print(z1,x_1,x_2,z1,y_1,y_2,y_3,sep="\n")
tensor([[ 1., 2., 3.],
[ 4., 5., 6.],
[-1., -2., -3.],
[-4., -5., -6.]])
tensor([[1., 2., 3.],
[4., 5., 6.]])
tensor([[-1., -2., -3.],
[-4., -5., -6.]])
tensor([[ 1., 2., 3.],
[ 4., 5., 6.],
[-1., -2., -3.],
[-4., -5., -6.]])
tensor([[ 1.],
[ 4.],
[-1.],
[-4.]])
tensor([[ 2.],
[ 5.],
[-2.],
[-5.]])
tensor([[ 3.],
[ 6.],
[-3.],
[-6.]])
# https://pytorch.org/docs/stable/torch.html?highlight=split#torch.split
# torch.split(tensor,split_size,dim=0) -> split into specific size
# split 함수를 통해 원하는 사이즈로 텐서를 자를 수 있습니다.
x1,x2 = torch.split(z1,2,dim=0)
y1 = torch.split(z1,2,dim=1)
print(z1,x1,x2,sep="\n")
print("\nThis is y1:")
for i in y1:
print(i)
tensor([[ 1., 2., 3.],
[ 4., 5., 6.],
[-1., -2., -3.],
[-4., -5., -6.]])
tensor([[1., 2., 3.],
[4., 5., 6.]])
tensor([[-1., -2., -3.],
[-4., -5., -6.]])
This is y1:
tensor([[ 1., 2.],
[ 4., 5.],
[-1., -2.],
[-4., -5.]])
tensor([[ 3.],
[ 6.],
[-3.],
[-6.]])
# https://pytorch.org/docs/stable/torch.html?highlight=squeeze#torch.squeeze
# torch.squeeze(input,dim=None) -> reduce dim by 1
# squeeze 함수를 통해 길이가 1인 차원들을 압축시킬 수 있습니다.
x1 = torch.FloatTensor(10,1,3,1,4)
x2 = torch.squeeze(x1)
print(x1.size(),x2.size(),sep="\n")
torch.Size([10, 1, 3, 1, 4]) torch.Size([10, 3, 4])
# https://pytorch.org/docs/stable/torch.html#torch.unsqueeze
# torch.unsqueeze(input,dim=None) -> add dim by 1
# squeeze와 반대로 unsqueeze를 통해 차원을 늘릴수 있습니다.
x1 = torch.FloatTensor(10,3,4)
x2 = torch.unsqueeze(x1,dim=0)
print(x1.size(),x2.size(),sep="\n")
torch.Size([10, 3, 4]) torch.Size([1, 10, 3, 4])
# _(underbar) refers to in-place operation -> https://discuss.pytorch.org/t/what-is-in-place-operation/16244
import torch.nn.init as init
# https://pytorch.org/docs/stable/nn.html?highlight=init%20uniform#torch.nn.init.uniform_
# uniform 분포를 따라 텐서를 초기화 할 수 있습니다.
x1 = init.uniform_(torch.FloatTensor(3,4),a=0,b=9)
# https://pytorch.org/docs/stable/nn.html?highlight=init%20uniform#torch.nn.init.normal_
# 정규 분포를 따라 텐서를 초기화 할 수 있습니다.
x2 = init.normal_(torch.FloatTensor(3,4),std=0.2)
# https://pytorch.org/docs/stable/nn.html?highlight=init%20uniform#torch.nn.init.constant_
# 지정한 값으로 텐서를 초기화 할 수 있습니다.
x3 = init.constant_(torch.FloatTensor(3,4),3.1415)
x1,x2,x3
(tensor([[2.7964, 0.5791, 7.9576, 3.1357],
[5.0798, 3.0238, 4.4316, 2.2166],
[0.1232, 0.1668, 5.4055, 7.5189]]),
tensor([[-0.0223, -0.1276, -0.1218, -0.3168],
[ 0.2625, 0.1122, 0.2551, 0.2158],
[-0.2849, 0.1506, -0.0209, 0.1337]]),
tensor([[3.1415, 3.1415, 3.1415, 3.1415],
[3.1415, 3.1415, 3.1415, 3.1415],
[3.1415, 3.1415, 3.1415, 3.1415]]))
# https://pytorch.org/docs/stable/torch.html?highlight=add#torch.add
# torch.add()
# 더하기 연산
x1 = torch.FloatTensor([[1,2,3],[4,5,6]])
x2 = torch.FloatTensor([[1,2,3],[4,5,6]])
add = torch.add(x1,x2)
print(x1,x2,add,x1+x2,x1-x2,sep="\n")
tensor([[1., 2., 3.],
[4., 5., 6.]])
tensor([[1., 2., 3.],
[4., 5., 6.]])
tensor([[ 2., 4., 6.],
[ 8., 10., 12.]])
tensor([[ 2., 4., 6.],
[ 8., 10., 12.]])
tensor([[0., 0., 0.],
[0., 0., 0.]])
# torch.add() broadcasting
# 더하기 연산의 브로드캐스팅
x1 = torch.FloatTensor([[1,2,3],[4,5,6]])
x2 = torch.add(x1,10)
print(x1,x2,x1+10,x2-10,sep="\n")
tensor([[1., 2., 3.],
[4., 5., 6.]])
tensor([[11., 12., 13.],
[14., 15., 16.]])
tensor([[11., 12., 13.],
[14., 15., 16.]])
tensor([[1., 2., 3.],
[4., 5., 6.]])
# https://pytorch.org/docs/stable/torch.html?highlight=add#torch.mul
# torch.mul() -> element-wise multiplication
# size better match
# 곱하기 연산입니다. 각각의 요소간 곱이기 때문에 사이즈가 일치해야 연산이 가능합니다.
x1 = torch.FloatTensor([[1,2,3],[4,5,6]])
x2 = torch.FloatTensor([[1,2,3],[4,5,6]])
x3 = torch.mul(x1,x2)
x3
tensor([[ 1., 4., 9.],
[16., 25., 36.]])
# torch.mul() -> broadcasting
# 곱하기 연산의 브로드캐스팅
x1 = torch.FloatTensor([[1,2,3],[4,5,6]])
x2 = x1*10
x2
tensor([[10., 20., 30.],
[40., 50., 60.]])
# https://pytorch.org/docs/stable/torch.html?highlight=add#torch.div
# torch.div() -> size better match
# 나누기 연산. 각각의 요소간 나누기이기 때문에 사이즈가 일치해야 연산이 가능합니다.
x1 = torch.FloatTensor([[1,2,3],[4,5,6]])
x2 = torch.FloatTensor([[1,2,3],[4,5,6]])
x3 = torch.div(x1,x2)
x3
tensor([[1., 1., 1.],
[1., 1., 1.]])
# torch.div() -> broadcasting
# 나누기 연산의 브로드캐스팅
x1 = torch.FloatTensor([[1,2,3],[4,5,6]])
x1/5
tensor([[0.2000, 0.4000, 0.6000],
[0.8000, 1.0000, 1.2000]])
# https://pytorch.org/docs/stable/torch.html?highlight=add#torch.pow
# torch.pow(input,exponent)
# 파워 연산(x의 n승)
x1 = torch.FloatTensor(3,4)
torch.pow(x1,2),x1**2
(tensor([[0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00],
[ nan, 0.0000e+00, 1.6868e-15, 1.0853e-17],
[2.8730e-15, inf, 4.5243e-14, inf]]),
tensor([[0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00],
[ nan, 0.0000e+00, 1.6868e-15, 1.0853e-17],
[2.8730e-15, inf, 4.5243e-14, inf]]))
# https://pytorch.org/docs/stable/torch.html?highlight=add#torch.exp
# torch.exp(tensor,out=None)
# exponential 연산
x1 = torch.FloatTensor(3,4)
torch.exp(x1)
tensor([[1.0000, 1.0000, 1.0000, 1.0000],
[ nan, 1.0000, 1.0000, 1.0000],
[1.0000, inf, 1.0000, inf]])
# https://pytorch.org/docs/stable/torch.html?highlight=add#torch.log
# torch.log(input, out=None) -> natural logarithm
# 로그 연산
x1 = torch.FloatTensor(3,4)
torch.log(x1)
tensor([[ nan, -inf, -99.8132, -inf],
[ nan, -inf, -17.0080, -19.5310],
[-16.7417, 48.1868, -15.3634, 48.4053]])
# https://pytorch.org/docs/stable/torch.html?highlight=add#torch.mm
# torch.mm(mat1, mat2) -> matrix multiplication
# 행렬곱 연산
x1 = torch.FloatTensor(3,4)
x2 = torch.FloatTensor(4,5)
torch.mm(x1,x2)
tensor([[ 7.1466e-44, 0.0000e+00, nan, 0.0000e+00, 0.0000e+00],
[ nan, nan, nan, nan, nan],
[ 3.8561e-20, 0.0000e+00, nan, 0.0000e+00, -3.5468e-34]])
# https://pytorch.org/docs/stable/torch.html?highlight=add#torch.bmm
# torch.bmm(batch1, batch2) -> batch matrix multiplication
# 배치 행렬곱 연산. 맨 앞에 batch 차원은 무시하고 뒤에 요소들로 행렬곱을 합니다.
x1 = torch.FloatTensor(10,3,4)
x2 = torch.FloatTensor(10,4,5)
torch.bmm(x1,x2).size()
torch.Size([10, 3, 5])
# https://pytorch.org/docs/stable/torch.html?highlight=add#torch.dot
# torch.dot(tensor1,tensor2) -> dot product of two tensor
# 두 텐서간의 프로덕트 연산
x1 = torch.tensor([2, 3])
x2 = torch.tensor([2, 1])
torch.dot(x1,x2)
tensor(7)
# https://pytorch.org/docs/stable/torch.html?highlight=add#torch.t
# torch.t(matrix) -> transposed matrix
# 행렬의 전치
x1 = torch.tensor([[1,2],[3,4]])
print(x1,x1.t(),sep="\n")
tensor([[1, 2],
[3, 4]])
tensor([[1, 3],
[2, 4]])
# https://pytorch.org/docs/stable/torch.html?highlight=add#torch.transpose
# torch.transpose(input,dim0,dim1) -> transposed matrix
# 차원을 지정할 수 있는 행렬의 전치 연산
x1 = torch.FloatTensor(10,3,4)
print(x1.size(), torch.transpose(x1,1,2).size(), x1.transpose(1,2).size(),sep="\n")
torch.Size([10, 3, 4]) torch.Size([10, 4, 3]) torch.Size([10, 4, 3])