x_data.device
: ํ์ฌ ๋ฐ์ดํฐ๊ฐ ์ด๋์ ์ฌ๋ผ์์๋์ง ํ์ธmm
or matmul
ํจ์ ์ฌ์ฉ (ํ๋ ฌ ๊ฐ์ ์ฐ์ฐ)dot
ํจ์ ์ฌ์ฉ (๋ฒกํฐ ๊ฐ์ ์ฐ์ฐ)matmul
๋ฅผ ์ฌ์ฉํ๋ฉด ์๋์ผ๋ก broadcasting
์ด ์ผ์ด๋๊ธฐ ๋๋ฌธ์ b๋ฅผ ์๋์ผ๋ก (3,1)๋ก ๋ง์ถฐ ๊ณ์ฐํ๊ฒ ๋๋ค. matmul
์ ์ด๋ฐ ๊ผด๊ณผ ๊ฐ๋ค.import torch
import torch.nn.functional as F
tensor = torch.FloatTensor([0.5, 0.7, 0.1])
h_tensor = F.softmax(tensor, dim=0)
๊ตฌ๊ธ ์ฝ๋ฉ .ipynb ํ์ผ ; https://drive.google.com/open?id=1nlT2Fq-vURe5aywOZ0VHuqa5D0tydL2u
x = torch.randn(3, 4)
# tensor([[ 0.1427, 0.0231, -0.5414, -1.0009],
# [-0.4664, 0.2647, -0.1228, -1.1068],
# [-1.1734, -0.6571, 0.7230, -0.6004]])
indices = torch.tensor([0, 2])
torch.index_select(x, 0, indices)
# tensor([[ 0.1427, 0.0231, -0.5414, -1.0009],
# [-1.1734, -0.6571, 0.7230, -0.6004]])
import torch
A = torch.Tensor([[1, 2],
[3, 4]])
indices = torch.tensor([[0], # A์ 0ํ, 0์ด
[1]]) # A์ 1ํ 1์ด
output = torch.gather(A, 1, indices).flatten()
>>> tensor([1., 4.])
torch.numel
: Returns the total number of elements in the input tensortorch.chunk
: ์ฃผ์ด์ง ํ
์๋ฅผ ์ํ๋ ์กฐ๊ฐ(chunk)๋งํผ ๋๋๊ธฐ
x = torch.randn(5, 7) # (5, 7) ํ๋ ฌ์ ๋์๊ฐ ํ
์
# ๋ฐ์ดํฐ๊ฐ 5๊ฐ, feature๊ฐ 7๊ฐ๋ผ๋ ๋ป
layer = MyLiner(7, 12) # output์ size๋ (5, 12)๊ฐ ๋๋ค.
in_features
: 7out_features
: 5return x @ self.weights + self.bias
๋ for epoch in range(epochs):
# clear gradient buffers because we don't want any gradient from previous epoch to carry forward
optimizer.zero_grad()
# get output from the model, given the outputs
outputs = model(inputs)
# get loss for the predicted output
loss = criterion(outputs, labels) # ์์ธก๊ฐ๊ณผ ์ค์ ๊ฐ์ loss๋ฅผ ๊ตฌํจ
print(loss)
# get gradients w, r, t to parameters
# (w, r, t) -> MSE loss function's parameters
loss.backward() # loss๋ฅผ w์ ๋ํด ๋ฏธ๋ถ, r์ ๋ํด ๋ฏธ๋ถ, ...
# update parameters
optimizer.step()
์ด๋ ค์ ๋ ๋ถ๋ถ:
- dimension (์ฐจ์) ์ ๋ํ ๊ฐ๋ ์ด ํท๊ฐ๋ ธ๊ณ ์ด๋ ต๊ฒ ๋ค๊ฐ์๋ค.
torch.swapdims
์gather
ํจ์,scatter_
ํจ์๊ฐ ์ฝ๊ฒ ์ดํด๋์ง ์์๋ค.๋์ ๋ ธ๋ ฅ!
- ์ฌ๋ฌ ๋ธ๋ก๊ทธ ์ ๋ฆฌ๋ฅผ ๋ณด๋ฉด์ ๋ด ๋๋ฆ๋๋ก์ ์ดํด๋ฅผ ํ๊ณ ๊ทธ๋ฆผ ๊ทธ๋ ค๊ฐ๋ฉฐ ์ ๋ฆฌ๋ฅผ ํ๋ค. ์๊ฐ์ด ๊ฝค ์ค๋ ๊ฑธ๋ ธ์ง๋ง ์ดํด๋ ํ์คํ๊ฒ ํ ๊ฒ ๊ฐ๋ค.