-
Notifications
You must be signed in to change notification settings - Fork 14
/
rnncells.py
138 lines (98 loc) · 4.12 KB
/
rnncells.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
126
127
128
129
130
131
132
133
134
135
136
137
138
import torch
import torch.nn as nn
from torch.autograd import Variable
import numpy as np
class LSTMCell(nn.Module):
def __init__(self, input_size, hidden_size, bias=True):
super(LSTMCell, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.bias = bias
self.xh = nn.Linear(input_size, hidden_size * 4, bias=bias)
self.hh = nn.Linear(hidden_size, hidden_size * 4, bias=bias)
self.reset_parameters()
def reset_parameters(self):
std = 1.0 / np.sqrt(self.hidden_size)
for w in self.parameters():
w.data.uniform_(-std, std)
def forward(self, input, hx=None):
# Inputs:
# input: of shape (batch_size, input_size)
# hx: of shape (batch_size, hidden_size)
# Outputs:
# hy: of shape (batch_size, hidden_size)
# cy: of shape (batch_size, hidden_size)
if hx is None:
hx = Variable(input.new_zeros(input.size(0), self.hidden_size))
hx = (hx, hx)
hx, cx = hx
gates = self.xh(input) + self.hh(hx)
# Get gates (i_t, f_t, g_t, o_t)
input_gate, forget_gate, cell_gate, output_gate = gates.chunk(4, 1)
i_t = torch.sigmoid(input_gate)
f_t = torch.sigmoid(forget_gate)
g_t = torch.tanh(cell_gate)
o_t = torch.sigmoid(output_gate)
cy = cx * f_t + i_t * g_t
hy = o_t * torch.tanh(cy)
return (hy, cy)
class RNNCell(nn.Module):
def __init__(self, input_size, hidden_size, bias=True, nonlinearity="tanh"):
super(RNNCell, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.bias = bias
self.nonlinearity = nonlinearity
if self.nonlinearity not in ["tanh", "relu"]:
raise ValueError("Invalid nonlinearity selected for RNN.")
self.x2h = nn.Linear(input_size, hidden_size, bias=bias)
self.h2h = nn.Linear(hidden_size, hidden_size, bias=bias)
self.reset_parameters()
def reset_parameters(self):
std = 1.0 / np.sqrt(self.hidden_size)
for w in self.parameters():
w.data.uniform_(-std, std)
def forward(self, input, hx=None):
# Inputs:
# input: of shape (batch_size, input_size)
# hx: of shape (batch_size, hidden_size)
# Output:
# hy: of shape (batch_size, hidden_size)
if hx is None:
hx = Variable(input.new_zeros(input.size(0), self.hidden_size))
hy = (self.x2h(input) + self.h2h(hx))
if self.nonlinearity == "tanh":
hy = torch.tanh(hy)
else:
hy = torch.relu(hy)
return hy
class GRUCell(nn.Module):
def __init__(self, input_size, hidden_size, bias=True):
super(GRUCell, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.bias = bias
self.x2h = nn.Linear(input_size, 3 * hidden_size, bias=bias)
self.h2h = nn.Linear(hidden_size, 3 * hidden_size, bias=bias)
self.reset_parameters()
def reset_parameters(self):
std = 1.0 / np.sqrt(self.hidden_size)
for w in self.parameters():
w.data.uniform_(-std, std)
def forward(self, input, hx=None):
# Inputs:
# input: of shape (batch_size, input_size)
# hx: of shape (batch_size, hidden_size)
# Output:
# hy: of shape (batch_size, hidden_size)
if hx is None:
hx = Variable(input.new_zeros(input.size(0), self.hidden_size))
x_t = self.x2h(input)
h_t = self.h2h(hx)
x_reset, x_upd, x_new = x_t.chunk(3, 1)
h_reset, h_upd, h_new = h_t.chunk(3, 1)
reset_gate = torch.sigmoid(x_reset + h_reset)
update_gate = torch.sigmoid(x_upd + h_upd)
new_gate = torch.tanh(x_new + (reset_gate * h_new))
hy = update_gate * hx + (1 - update_gate) * new_gate
return hy