-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathnn_models.py
125 lines (93 loc) · 3.41 KB
/
nn_models.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
118
119
120
121
122
123
124
125
import torch
import torch.nn as nn
from torch.nn import Parameter
from torch.nn import functional as F
import torch.optim
from torch.autograd import Variable
from quaternion_models import QuaternionLinearAutograd, QuaternionLinear
class QRNN(nn.Module):
def __init__(self, feat_size, hidden_size, nb_hidden):
super(QRNN, self).__init__()
# Reading options:
self.input_dim=feat_size
self.hidden_dim=hidden_size
self.N_hid=nb_hidden
self.num_classes=feat_size
# List initialization
self.wx = nn.ModuleList([])
self.uh = nn.ModuleList([])
# Activation
self.act=nn.Sigmoid()
curr_dim=self.input_dim
for i in range(self.N_hid):
self.wx.append(nn.Linear(curr_dim, self.hidden_dim))
# uh initialization
self.uh.append(QuaternionLinearAutograd(self.hidden_dim, self.hidden_dim))
curr_dim=self.hidden_dim
# output layer initialization
self.fco = nn.Linear(curr_dim, self.num_classes)
self.adam = torch.optim.Adam(self.parameters(), lr=0.0002)
def forward(self, x):
h_init = Variable(torch.zeros(x.shape[1],self. hidden_dim))
x = x.cuda()
h_init = h_init.cuda()
wx_out=self.wx[0](x)
hiddens = []
pre_act = []
h=h_init
for k in range(x.shape[0]):
at=wx_out[k]+self.uh[0](h)
h=at
# Delimiter, time to generate !
out = []
hiddens = []
pre_act = []
for k in range(x.shape[0]):
at=self.uh[0](h)
h=at
output = self.act(self.fco(h))
out.append(output.unsqueeze(0))
return torch.cat(out,0)
class RNN(nn.Module):
def __init__(self, feat_size, hidden_size, nb_hidden):
super(RNN, self).__init__()
# Reading options:
self.input_dim=feat_size
self.hidden_dim=hidden_size
self.N_hid=nb_hidden
self.num_classes=feat_size
# List initialization
self.wx = nn.ModuleList([])
self.uh = nn.ModuleList([])
# Activation
self.act=nn.Sigmoid()
curr_dim=self.input_dim
for i in range(self.N_hid):
self.wx.append(nn.Linear(curr_dim, self.hidden_dim))
# uh initialization
self.uh.append(nn.Linear(self.hidden_dim, self.hidden_dim))
curr_dim=self.hidden_dim
# output layer initialization
self.fco = nn.Linear(curr_dim, self.num_classes)
self.adam = torch.optim.Adam(self.parameters(), lr=0.0002)
def forward(self, x):
h_init = Variable(torch.zeros(x.shape[1],self. hidden_dim))
x = x.cuda()
h_init = h_init.cuda()
wx_out=self.wx[0](x)
hiddens = []
pre_act = []
h=h_init
for k in range(x.shape[0]):
at=wx_out[k]+self.uh[0](h)
h=at
# Delimiters, time to generate !
out = []
hiddens = []
pre_act = []
for k in range(x.shape[0]):
at=self.uh[0](h)
h=at
output = self.act(self.fco(h))
out.append(output.unsqueeze(0))
return torch.cat(out,0)