-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
75 lines (61 loc) · 2.76 KB
/
Copy pathmodel.py
File metadata and controls
75 lines (61 loc) · 2.76 KB
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
import torch
import torch.nn as nn
from torch.autograd import Variable
use_cuda = torch.cuda.is_available()
class RNN_1(nn.Module):
def __init__(self, input_size=12, hidden_size=2, num_layers=1, bidirectional=False):
super(RNN_1, self).__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers
self.bidirectional = bidirectional
self.rnn = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)
def forward(self, x):
# Set initial states
if self.bidirectional == True:
h0 = Variable(torch.randn(2 * self.num_layers, x.size(0), self.hidden_size))
c0 = Variable(torch.randn(2 * self.num_layers, x.size(0), self.hidden_size))
else:
h0 = Variable(torch.randn(self.num_layers, x.size(0), self.hidden_size))
c0 = Variable(torch.randn(self.num_layers, x.size(0), self.hidden_size))
# Forward propagate
out, _ = self.rnn(x, (h0, c0))
return out
class RNN_2(nn.Module):
def __init__(self, input_size=2, hidden_size=10, num_layers=1, bidirectional=False):
super(RNN_2, self).__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers
self.bidirectional = bidirectional
self.rnn = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)
self.fc = nn.Linear(hidden_size, 2) # coversong & noncoversong
def forward(self, x):
# Set initial states
if self.bidirectional == True:
h0 = Variable(torch.randn(2 * self.num_layers, x.size(0), self.hidden_size))
c0 = Variable(torch.randn(2 * self.num_layers, x.size(0), self.hidden_size))
else:
h0 = Variable(torch.randn(self.num_layers, x.size(0), self.hidden_size))
c0 = Variable(torch.randn(self.num_layers, x.size(0), self.hidden_size))
# Forward propagate
out, _ = self.rnn(x, (h0, c0))
# MLP with last step from RNN
out = self.fc(out[:, -1, :])
return out
# In the case of controlling inital input size to the minimum length of all inputs
# Directly apply a MLP network with the output of first RNN
class MLP(nn.Module):
def __init__(self, input_size, output_size=2, hidden_dims=[50, 10]):
super(MLP, self).__init__()
self.layers = []
prev_dim = input_size
for hidden_dim in hidden_dims:
self.layers.append(nn.Linear(prev_dim, hidden_dim))
self.layers.append(nn.ReLU(True))
prev_dim = hidden_dim
self.layers.append(nn.Linear(prev_dim, output_size))
self.layer_module = nn.ModuleList(self.layers)
def forward(self, x):
out = x
for layer in self.layer_module:
out = layer(out)
return out