Pytorch - soup1997/Study-Alone GitHub Wiki
class CustomDataset(torch.utils.data.Dataset): 
  def __init__(self):
  ๋ฐ์ดํฐ์
์ ์ ์ฒ๋ฆฌ๋ฅผ ํด์ฃผ๋ ๋ถ๋ถ
  def __len__(self):
  ๋ฐ์ดํฐ์
์ ๊ธธ์ด. ์ฆ, ์ด ์ํ์ ์๋ฅผ ์ ์ด์ฃผ๋ ๋ถ๋ถ
  def __getitem__(self, idx): 
  ๋ฐ์ดํฐ์
์์ ํน์  1๊ฐ์ ์ํ์ ๊ฐ์ ธ์ค๋ ํจ์์ด๋ฏธ์ง๋ฅผ ๋ณํ(transform)์ ์ ์ฉํด์ ๋ฐ์ดํฐ๋ฅผ ์กฐ์ํ๊ณ ํ์ต์ ์ ํฉํ๊ฒ ๋ง๋ ๋ค๋ ๊ฒ์ด ํฐ ๊ฐ๋ ์ด๋ค.
import torchvision.transforms as T
# ์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ ์์
์ ์ ์
preprocess = T.Compose([
    T.Resize((256, 256)), # resize to (256, 256)
    T.RandomHorizontalFlip(), # 
    T.ToTensor(), # normalization with range [0, 1]
    T.Normalize((0.5), (0.5)) # ๊ฐ ์ฑ๋๋ณ ํ๊ท ์ ๋บ๋ค ํ์คํธ์ฐจ๋ก ๋๋์ด ํ์คํ๋ฅผ ์งํ, standarazation์ ๊ฐ๊น์ 
])
# ์ด๋ฏธ์ง์ ์ ์ฒ๋ฆฌ ์์
 ์ ์ฉ
image = Image.open('image.jpg')
imge = preprocess(image)class Net(nn.Module):
    def __init__(self, input_dim, hidden_dim, seq_len, output_dim, layers):
        super(Net, self).__init__()
        self.hidden_dim = hidden_dim
        self.seq_len = seq_len
        self.output_dim = output_dim
        self.layers = layers
        self.lstm = nn.LSTM(input_size=input_dim, 
                            hidden_size=hidden_dim, 
                            num_layers=layers,
                            batch_first=True)
        
        self.fc = nn.Linear(in_features=hidden_dim, 
                            out_features=output_dim, 
                            bias=True)
    def reset_hidden_state(self, x):
        h_0 = torch.zeros(self.layers, x.size(0), self.hidden_dim)
        c_0 = torch.zeros(self.layers, x.size(0), self.hidden_dim)
        return h_0, c_0
    def forward(self, x):
        h_0, c_0 = self.reset_hidden_state(x)
        output, (h_t, c_t) = self.lstm(x, (h_0, c_0))
        x = self.fc(output[:, -1])
        return xtorch.nn.LSTM(input_size, hidden_size, num_layers, bias=True, batch_first=True, dropout, bidirectional)
- 
input_size:input์ feature dimension์ ๋ฃ์ด์ฃผ์ด์ผ ํ๋ค. time step์ด ์๋ input feature dimension
- 
hidden_size: ๋ด๋ถ์์ ์ด๋ค feature dimension์ผ๋ก ๋ฐ๊ฟ์ฃผ๊ณ ์ถ์์ง๋ฅผ ๋ํ๋
- 
num_layers: ์ฌ๊ท ์ธต์ ๊ฐฏ์

- 
bias: bias term์ ๋ ๊ฒ์ธ๊ฐ (Default: True)
- 
batch_first: batch_first=True๋ผ๋ฉด, (Batch_size, Time_step, Input_feature_dimension) ์์ (Default: False)
- 
dropout: Dropout์ ๋น์จ, generalization ์ ์๋๋ฉด ์กฐ์ 
- 
bidirectional: ์๋ฐฉํฅ ์ฌ๋ถ (bidirectional ํ๋ฉด [forward, backword]๊ฐ ๋์ด feature dimension 2๋ฐฐ)
output, (h_t, c_t) = self.lstm(x, (h_0, c_0))Input์ ์
๋ ฅ sequence์ ์ด๊ธฐ state (LSTM์ด๋ฉด {hidden state, cell state} ๊ทธ ์ธ๋ {hidden state})๋ก ๊ตฌ์ฑ
์ด๊ธฐ state๊ฐ ์๋ค๋ฉด ๋ฃ์ด์ฃผ์ง ์์๋ ๋๊ณ  ์๋์ผ๋ก zero๋ก ์ค์  
์
๋ ฅ sequence์ dimension์ (Batch, Time_step, Feature dimension)์ (batch_first=True ๊ธฐ์ค)def LSTMCell(input, hidden, w_ih, w_hh, b_ih=None, b_hh=None):
    if input.is_cuda:
        ...
    hx, cx = hidden
    gates = F.linear(input, w_ih, b_ih) + F.linear(hx, w_hh, b_hh)
    ingate, forgetgate, cellgate, outgate = gates.chunk(4, 1)
    ingate     = F.sigmoid(ingate)
    forgetgate = F.sigmoid(forgetgate)
    cellgate   = F.tanh(cellgate)
    outgate    = F.sigmoid(outgate)
    cy = (forgetgate * cx) + (ingate * cellgate)
    hy = outgate * F.tanh(cy)
    return hy, cy
torch.nn.LSTMCell(input_size, hidden_size, bias=True, device=None, dtype=None)
- 
input_size: ์ ๋ ฅ์ผ๋ก ์ฃผ์ด์ง๋ ๋ฐ์ดํฐ์ feature์ ๊ฐฏ์
- 
hidden_size: hidden state$h$ ์ ๋ฒกํฐ ํฌ๊ธฐ