PyTorch로 배우는 Intermediate Deep Learning
Michal Oleszak
Machine Learning Engineer


xhyh
h와 y는 동일합니다입력과 출력 3개(숨김 상태 2개):
h: 단기 상태c: 장기 상태3개의 "게이트":
class Net(nn.Module): def __init__(self, input_size): super().__init__()self.lstm = nn.LSTM( input_size=1, hidden_size=32, num_layers=2, batch_first=True, ) self.fc = nn.Linear(32, 1)def forward(self, x): h0 = torch.zeros(2, x.size(0), 32) c0 = torch.zeros(2, x.size(0), 32)out, _ = self.lstm(x, (h0, c0))out = self.fc(out[:, -1, :]) return out
__init__():nn.RNN을 nn.LSTM으로 교체forward():c를 사용c와 h를 0으로 초기화lstm 레이어에 전달
class Net(nn.Module): def __init__(self, input_size): super().__init__()self.gru = nn.GRU( input_size=1, hidden_size=32, num_layers=2, batch_first=True, ) self.fc = nn.Linear(32, 1)def forward(self, x): h0 = torch.zeros(2, x.size(0), 32) out, _ = self.gru(x, h0) out = self.fc(out[:, -1, :]) return out
__init__():nn.RNN을 nn.GRU로 교체forward():gru 레이어 사용
PyTorch로 배우는 Intermediate Deep Learning