-
Notifications
You must be signed in to change notification settings - Fork 0
/
lstm.py
119 lines (77 loc) · 3.23 KB
/
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import math
import torch, sys
import torch.nn as nn
import torch.nn.functional as F
import rn_model
from torch.autograd import Variable
class BrainLine(nn.Module):
def __init__(self, inputs, outputs):
super().__init__()
self.line = nn.Linear(inputs, outputs)
def forward(self, x):
y = torch.tanh(self.line(x))
return y
class LSTMStack(nn.Module):
def __init__(self, ch_in, ch_out, num_layers, div=0):
super().__init__()
self.ch_in = ch_in
self.ch_out = ch_out
self.div = div
self.num_layers = num_layers
self.conv = nn.Conv1d(self.ch_in, self.ch_out, kernel_size=1)
self.lstm = nn.LSTM(input_size=self.ch_out, hidden_size=self.ch_out,
num_layers=num_layers, batch_first=True)
#self.res1 = ResidualUnit(self.ch_out, self.kernel_size)
#self.res2 = ResidualUnit(self.ch_out, self.kernel_size)
def forward(self, x, h, c):
x = self.conv(x)
x = x.permute(0, 2, 1)
x, (h, c) = self.lstm(x)
x = x.permute(0, 2, 1)
#x = self.res2(x)
x = F.max_pool1d(torch.relu(x), self.div)
return x, h, c
class LSTM(nn.Module):
def __init__(self, input_size, num_layers):
super(LSTM, self).__init__()
self.num_layers = num_layers
self.input_size = input_size
#self.hidden_size = hidden_size
self.lstm_layers = nn.ModuleList()
self.line_layers = nn.ModuleList()
self.lstm_layers.append(LSTMStack(2, 4, num_layers, div=2))
for _ in range(3):
self.lstm_layers.append(LSTMStack(4, 4, num_layers, div=5))
self.lstm_layers.append(LSTMStack(4, 4, num_layers, div=2))
self.line_layers.append(BrainLine(40, 16))
self.line_layers.append(BrainLine(16, 16))
self.line_layers.append(BrainLine(16, 3))
#nn.LSTM(input_size=input_size, hidden_size=hidden_size,
#num_layers=num_layers, batch_first=True)
#self.lstm_layers.append(nn.LSTM(input_size=input_size, hidden_size=hidden_size,
#num_layers=num_layers, batch_first=True)) #lstm
def forward(self, x):
h_0 = Variable(torch.zeros(self.num_layers, x.size(0), self.input_size)) #hidden state
c_0 = Variable(torch.zeros(self.num_layers, x.size(0), self.input_size)) #internal state
for lstm_layer in self.lstm_layers:
x, h_0, c_0 = lstm_layer(x, h_0, c_0) #lstm with input, hidden, and internal state
x = x.view(-1, 40)
for layer in self.line_layers:
x = layer(x)
return x
class SequenceModel(nn.Module):
def __init__(self, n_features, n_classes, n_hidden=256, n_layers=3):
super().__init__()
self.lstm = nn.LSTM(input_size=n_features, hidden_size=n_hidden,
num_layers=n_layers, batch_first=True,
dropout=0.75)
self.classifier = nn.Linear(n_hidden, n_classes)
def forward(self, x):
self.lstm.flatten_parameters()
x = x.permute(0, 2, 1)
_, (hidden, _) = self.lstm(x)
out = hidden[-1]
print(out.shape)
return self.classifier(out)
x = torch.rand(1, 2, 20000)
model = SequenceModel(n_features=2, n_classes=3)(x)