# (NETWORK) WEEK9 TIME SERIES ###### tags: `Computer Science` `uscc` `SMART SENSOR` ```python= class LSTM(nn.Module): def __init__(self, input_dim, hidden_dim, num_layers, output_dim): super(LSTM, self).__init__() self.hidden_dim = hidden_dim self.num_layers = num_layers self.lstm = nn.LSTM(input_dim, hidden_dim, num_layers, batch_first=True) self.fc = nn.Linear(hidden_dim, output_dim) def forward(self, x): h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim).requires_grad_() c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim).requires_grad_() out, (hn, cn) = self.lstm(x, (h0.detach(), c0.detach())) out = self.fc(out[:, -1, :]) return out ``` ## lstm2 ```python= device = 'cpu' class LSTM(nn.Module): def __init__(self, input_dim, hidden_dim, num_layers, output_dim): super(LSTM, self).__init__() self.hidden_dim = hidden_dim self.num_layers = num_layers self.lstm = nn.LSTM(input_dim, hidden_dim, num_layers, batch_first=True) self.fc = nn.Linear(hidden_dim, output_dim) def forward(self, x): h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim).to(device) c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim).to(device) out, (hn, cn) = self.lstm(x, (h0, c0)) out = self.fc(out[:, -1, :]) return out ``` ## GRU ```python= class GRU(nn.Module): def __init__(self, input_dim, hidden_dim, num_layers, output_dim): super(GRU, self).__init__() self.hidden_dim = hidden_dim self.num_layers = num_layers self.gru = nn.GRU(input_dim, hidden_dim, num_layers, batch_first=True) self.fc = nn.Linear(hidden_dim, output_dim) def forward(self, x): h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim).requires_grad_() out, (hn) = self.gru(x, (h0.detach())) out = self.fc(out[:, -1, :]) return out ``` ## lstm example ```python= >>> rnn = nn.LSTM(input_size=10, hidden_size=20, num_layers=2) >>> # input=(sequence length, batch_size, input_size) >>> input = torch.randn(5, 3, 10) >>> # h0=(D * num_layers, batch_size, H_out) input_size) >>> h0 = torch.randn(2, 3, 20) >>> # c0=(D * num_layers, batch_size, H_out) input_size) >>> c0 = torch.randn(2, 3, 20) >>> output, (hn, cn) = rnn(input, (h0, c0)) ``` ## gru example ```python= >>> rnn = nn.GRU(input_size=10, hidden_size=20, num_layers=2) >>> # input=(sequence length, batch_size, input_size) >>> input = torch.randn(5, 3, 10) >>> # h0=(D * num_layers, batch_size, H_out) >>> h0 = torch.randn(2, 3, 20) >>> output, hn = rnn(input, h0) ```