-
Notifications
You must be signed in to change notification settings - Fork 0
/
phase_model.py
84 lines (64 loc) · 2.91 KB
/
phase_model.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
import torch
import torch.nn as nn
# Different architectures for phase recognition
class LSTM(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim, softmax, device):
super(LSTM, self).__init__()
self.hidden_dim = hidden_dim
self.softmax = softmax
self.output_dim = output_dim
self.device = device
self.model = nn.LSTM(input_dim, hidden_dim, 1)
self.hidden2out = nn.Linear(hidden_dim, output_dim)
self.hidden = self.init_hidden()
def init_hidden(self):
return (torch.randn(1, 1, self.hidden_dim).type(torch.FloatTensor).to(self.device),
torch.randn(1, 1, self.hidden_dim).type(torch.FloatTensor).to(self.device))
def forward(self, input):
hidden_output, self.hidden = self.model(input, self.hidden)
output = self.hidden2out(hidden_output)
output_resized = torch.randn(len(output), self.output_dim)
for i in range(len(output)):
output_resized[i] = output[i][0]
output_resized = torch.FloatTensor(output_resized)
return output_resized
class GRU(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim, softmax, device):
super(GRU, self).__init__()
self.hidden_dim = hidden_dim
self.softmax = softmax
self.output_dim = output_dim
self.device = device
self.model = nn.GRU(input_dim, hidden_dim, 1)
self.hidden2out = nn.Linear(hidden_dim, output_dim)
self.hidden = self.init_hidden()
def init_hidden(self):
return torch.randn(1, 1, self.hidden_dim).type(torch.FloatTensor).to(self.device)
def forward(self, input):
hidden_output, self.hidden = self.model(input, self.hidden)
output = self.hidden2out(hidden_output)
output_resized = torch.randn(len(output), self.output_dim)
for i in range(len(output)):
output_resized[i] = output[i][0]
output_resized = torch.FloatTensor(output_resized)
return output_resized
class RNN(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim, softmax, device):
super(RNN, self).__init__()
self.hidden_dim = hidden_dim
self.softmax = softmax
self.output_dim = output_dim
self.device = device
self.model = nn.RNN(input_dim, hidden_dim, 1)
self.hidden2out = nn.Linear(hidden_dim, output_dim)
self.hidden = self.init_hidden()
def init_hidden(self):
return torch.randn(1, 1, self.hidden_dim).type(torch.FloatTensor).to(self.device)
def forward(self, input):
hidden_output, self.hidden = self.model(input, self.hidden)
output = self.hidden2out(hidden_output)
output_resized = torch.randn(len(output), self.output_dim)
for i in range(len(output)):
output_resized[i] = output[i][0]
output_resized = torch.FloatTensor(output_resized)
return output_resized