import torch
import numpy as np
PyTorch에서는 Tensor type이 Variable과 Constant를 포함
기존에는 Variable 타입이 있었지만, 이는 자동미분을 위한 것이었는데, 이 기능을 Tensor 타입에 포함시킴
기존의 데이터를 Torch의 Tensor
특정한 값의 Tensor 생성하기로
난수 생성하기
데이터 타입
GPU 사용하기
GPU 를 사용하기 위해 Cuda에서 사용하는 데이터타입으로 바꾸어줘야 한다.
방법 세가지!
대부분 이렇게 사용
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(device)
a = torch.rand(2)
print(a)
a = a.to(device)
print(a)
import torch
x = torch.arange(0, 5)
y = torch.arange(1, 6)
print(x + y)
print(torch.add(x, y))
print(x.add(y))
print(x - y)
print(torch.sub(x, y))
print(x.sub(y))
print(x * y)
print(torch.mul(x, y))
print(x.mul(y))
print(x / y)
print(torch.div(x, y))
print(x.div(y))
print(x**y)
print(torch.pow(x, y))
print(x.pow(y))
print(-x)
print(torch.negative(x))
print(x.negative())
torch.abs
: 절대값torch.sign
: 부호torch.round
: 반올림torch.ceil
: 올림torch.floor
: 내림torch.square
: 제곱torch.sqrt
: 제곱근torch.maximum
: 두 텐서의 각 원소에서 최댓값만 반환.torch.minimum
: 두 텐서의 각 원소에서 최솟값만 반환.torch.cumsum
: 누적합torch.cumprod
: 누적곱print(torch.abs)
print(torch.sign)
print(torch.round)
print(torch.ceil)
print(torch.floor)
print(torch.square)
print(torch.sqrt)
print(torch.maximum)
print(torch.minimum)
print(torch.cumsum)
print(torch.cumprod)
차원 축소 연산
x = torch.randint(10, size=(5, 2, 4))
print(x.shape)
print(x)
print(torch.sum(x))
print(torch.sum(x).shape)
print(torch.sum(x, dim=1))
print(torch.sum(x, dim=1).shape)
print()
print(torch.sum(x, dim=2))
print(torch.sum(x, dim=2).shape)
행렬 연산
a = torch.tensor([[2, 0], [0, 1]], dtype=torch.float32)
b = torch.tensor([[1, 1], [1, 1]], dtype=torch.float32)
a, b
torch.matmul(a, b), torch.mm(a, b)
torch.linalg.inv(a)
크기와 차원을 바꾸는 명령
print(a.view(4, -1), a.view((4, -1)), sep="\n")
print(a.view(4, -1).shape, a.view((4, -1)).shape)
torch.reshape(a, (4, -1))
# view함수가 있어 expand_dims 같은 함수가 따로 필요없다.
a.view((1, 4, 1)), a.view((1, 4, 1)).shape
print(torch.squeeze(a.view((1, 4, 1)), dim=2))
print(torch.squeeze(a.view((1, 4, 1)), dim=2).shape)
print(torch.unsqueeze(a.view((1, 4)), dim=2))
print(torch.unsqueeze(a.view((1, 4)), dim=2).shape)
a = torch.arange(10).view(5, 2)
a
a.transpose(1, 0)
torch.transpose(a, 1, 0)
a
print(a)
print(a.transpose_(0, 1))
print(a)
a.shape
형상 연산 중 유용한 것
a
b = torch.arange(10).view(2, 5)
b
# a = a.contiguous()
print(a.view_as(b))
print(a.reshape_as(b))
indexing, slicing
a[0]
a[0, 1]
a[4, 1]
a[:2]
텐서를 나누거나 두 개 이상의 텐서를 합치는 명령
c = torch.rand(3, 6)
c
c1, c2, c3 = torch.chunk(c, 3, dim=1)
print(c1)
print(c2)
print(c3)
c1, c2, c3 = torch.chunk(c, 3, dim=0)
print(c1)
print(c2)
print(c3)
torch.split(c, 1, dim=0)
torch.split(c, 2, dim=1)
torch.split(c, 3, dim=1)
a = torch.ones(2, 3)
b = torch.zeros(3, 3)
a, b
torch.cat([a, b], dim=0)
torch.cat([a, b], dim=1)
a = torch.ones_like(b)
a, a.size()
b, b.size()
torch.stack([a, b], dim=1), torch.stack([a, b], dim=1).size()
torch.stack([a, b], dim=0), torch.stack([a, b], dim=0).size()
torch.stack([a, b], dim=2), torch.stack([a, b], dim=2).size()
torch.tile(a, (3, 1))
torch.tile(a, (1, 2))
어렵다...
💻 출처 : 제로베이스 데이터 취업 스쿨