-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_lstm.py
56 lines (44 loc) · 1.78 KB
/
model_lstm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from argparse import ArgumentParser
import os
import torch
from torch import nn
class MyLSTM(nn.Module):
def __init__(self,
num_inputs: int = 9,
num_outputs: int = 8,
num_layers: int = 1,
hidden_size: int = 512
):
super(MyLSTM, self).__init__()
# Save args
self.num_inputs = num_inputs
self.num_outputs = num_outputs
self.num_layers = num_layers
self.hidden_size = hidden_size
self.lstm = nn.LSTM(input_size=self.num_inputs,
hidden_size=self.hidden_size,
num_layers=self.num_layers)
self.h0 = torch.randn(self.num_layers, 1, self.hidden_size)
self.c0 = torch.randn(self.num_layers, 1, self.hidden_size)
self.fc = nn.Linear(self.hidden_size, num_outputs)
def init_sequence(self, batch_size, device):
"""Initializing the state."""
self.device = device
self.batch_size = batch_size
# reset state
h = self.h0.clone().repeat(1, self.batch_size, 1).to(self.device)
c = self.c0.clone().repeat(1, self.batch_size, 1).to(self.device)
self.state = (h, c)
def forward(self, x=None):
if x is None:
x = torch.zeros(self.batch_size, self.num_inputs).to(self.device)
x = x.unsqueeze(0)
outp, self.state = self.lstm(x, self.state)
o = torch.sigmoid(self.fc(outp))
return o, self.state
@staticmethod
def add_model_specific_args(parent_parser):
parser = ArgumentParser(parents=[parent_parser], add_help=False)
parser.add_argument('--lstm_num_layers', type=int, default=1)
parser.add_argument('--lstm_hidden_size', type=int, default=512)
return parser