이번 내용은 본인이 pytorch에 대해서 생소하기도 하고 모듈 자체를 만들어서 하나의 프로그램 구조로 만드는 것에 저어어어엉말 안 익숙하기 때문에 힘들었다,,, 그래도 수업 두 번씩 들으니 그때서야 이해가 되더라ㅎㅎㅎㅎ,,, 배운 내용 중에 포스트로 올리면 좋을 만한 것을 추려보았다.
view: reshape과 동일하게 tensor의 shape을 변환 (reshape과 거의 유사)
squeeze: 차원의 개수가 1인 차원을 삭제 (압축)
unsqueeze: 차원의 개수가 1인 차원을 추가
# # #
# # #
tensor([[[0.7466, 0.5440], [0.7145, 0.2119],
[0.8279, 0.0697]],
[[0.8323, 0.2671], [0.2484, 0.8983], [0.3228, 0.2254]]])
tensor_ex.view([-1, 6])
# tensor([[0.7466, 0.5440, 0.7145, 0.2119, 0.8279, 0.0697],
# [0.8323, 0.2671, 0.2484, 0.8983, 0.3228, 0.2254]])
tensor_ex.reshape([-1,6])
# tensor([[0.7466, 0.5440, 0.7145, 0.2119, 0.8279, 0.0697],
# [0.8323, 0.2671, 0.2484, 0.8983, 0.3228, 0.2254]])
a = torch.zeros(3, 2)
b = a.view(2, 3)
a.fill_(1)
# b에도 1로 가득찬 모습을 보인다
a = torch.zeros(3, 2)
b = a.t().reshape(6)
a.fill_(1)
# 이 경우에는 b가 1로 가득차있지 않다
tensor_ex = torch.rand(size=(2, 1, 2))
tensor_ex.squeeze()
# tensor([[0.8510, 0.8263],
# [0.7602, 0.1309]])
tensor_ex = torch.rand(size=(2, 2))
tensor_ex.unsqueeze(0).shape
# torch.Size([1, 2, 2])
tensor_ex.unsqueeze(1).shape
# torch.Size([2, 1, 2])
tensor_ex.unsqueeze(2).shape
# torch.Size([2, 2, 1])
n1 = np.arange(10).reshape(2,5)
n2 = np.arange(10).reshape(5,2)
t1 = torch.FloatTensor(n1)
t2 = torch.FloatTensor(n2)
t1.mm(t2)
# tensor([[ 60., 70.],
# [160., 195.]])
t1.dot(t2)
# RuntimeError
t1.matmul(t2)
# tensor([[ 60., 70.], # [160., 195.]])
a = torch.rand(10)
b = torch.rand(10)
a.dot(b)
a = torch.rand(10)
b = torch.rand(10)
a.mm(b)
a = torch.rand(5, 2, 3)
b = torch.rand(5)
a.mm(b)
# 5, 2, 3 와 5의 연산이므로 연산이 불가능
a = torch.rand(5, 2, 3)
b = torch.rand(3)
a.matmul(b)
# 위와 아래는 같은 것
a[0].mm(torch.unsqueeze(b,1)).squeeze()
a[1].mm(torch.unsqueeze(b,1)).squeeze()
a[2].mm(torch.unsqueeze(b,1)).squeeze()
a[3].mm(torch.unsqueeze(b,1)).squeeze()
a[4].mm(torch.unsqueeze(b,1)).squeeze()
import torch
import torch.nn.functional as F
tensor = torch.FloatTensor([0.5, 0.7, 0.1])
h_tensor = F.softmax(tensor, dim=0)
h_tensor
# tensor([0.3458, 0.4224, 0.2318])
y = torch.randint(5, (10,5))
# 최대 4인 rand int를 10by5 tensor로 구현
y_label = y.argmax(dim=1)
torch.nn.functional.one_hot(y_label)
w = torch.tensor(2.0, requires_grad=True)
# 미분이 되는 대상
y = w**2
z = 10*y + 25
z.backward()
w.grad
a = torch.tensor([2., 3.], requires_grad=True)
b = torch.tensor([6., 4.], requires_grad=True)
Q = 3*a**3 - b**2
external_grad = torch.tensor([1., 1.])
Q.backward(gradient=external_grad)
a.grad
# a.grad ([36, 81])
b.grad
# tensor([-12., -8.])
import torch
import numpy as np
t = torch.tensor([[1, 2, 3],
[4, 5, 6]])
print(torch.chunk(t, 2, 0))
print(torch.chunk(t, 2, 1))
> (tensor([[1, 2, 3]]), tensor([[4, 5, 6]]))
(tensor([[1, 2],
[4, 5]]), tensor([[3],[6]]))
x = torch.tensor([[[0,1],[2,3]],[[4,5],[6,7]]])
> tensor([[[0, 1],
[2, 3]],
[[4, 5],
[6, 7]]])
torch.swapdims(x, 0, 1)
> tensor([[[0, 1],
[4, 5]],
[[2, 3],
[6, 7]]])
torch.swapdims(x, 0, 2)
> tensor([[[0, 4],
[2, 6]],
[[1, 5],
[3, 7]]])
self[index[i][j][k]][j][k] = src[i][j][k] # if dim == 0
self[i][index[i][j][k]][k] = src[i][j][k] # if dim == 1
self[i][j][index[i][j][k]] = src[i][j][k] # if dim == 2
src = torch.arange(1, 11).reshape((2, 5))
src
index = torch.tensor([[0, 1, 2, 0]])
torch.zeros(3, 5, dtype=src.dtype).scatter_(0, index, src)
index = torch.tensor([[0, 1, 2], [0, 1, 4]])
torch.zeros(3, 5, dtype=src.dtype).scatter_(1, index, src)
torch.full((2, 4), 2.).scatter_(1, torch.tensor([[2], [3]]),
1.23, reduce='multiply')
torch.full((2, 4), 2.).scatter_(1, torch.tensor([[2], [3]]),
1.23, reduce='add')
참고 : https://pytorch.org/docs/stable/generated/torch.Tensor.scatter_.html?highlight=scatter#torch.Tensor.scatter_
네이버 부스트캠프 자료