इंटरमीडिएट Deep Learning with PyTorch
Michal Oleszak
Machine Learning Engineer


xhyh
h और y एक जैसे हैंतीन इनपुट/आउटपुट (दो हिडन स्टेट):
h: शॉर्ट-टर्म स्टेटc: लॉन्ग-टर्म स्टेटतीन "गेट":
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 को ज़ीरो से इनिशियलाइज़ करें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 लेयर का उपयोग करें
इंटरमीडिएट Deep Learning with PyTorch