forked from speechbrain/speechbrain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_RNN.py
More file actions
168 lines (147 loc) · 4.99 KB
/
Copy pathtest_RNN.py
File metadata and controls
168 lines (147 loc) · 4.99 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from collections import OrderedDict
import torch
import torch.nn
def test_RNN(device):
from speechbrain.nnet.RNN import GRU, LSTM, RNN, LiGRU, QuasiRNN, RNNCell
# Check RNN
inputs = torch.randn(4, 2, 7, device=device)
net = RNN(
hidden_size=5,
input_shape=inputs.shape,
num_layers=2,
bidirectional=False,
).to(device)
output, hn = net(inputs)
output_l = []
hn_t = None
for t in range(inputs.shape[1]):
out_t, hn_t = net(inputs[:, t, :].unsqueeze(1), hn_t)
output_l.append(out_t.squeeze(1))
out_steps = torch.stack(output_l, dim=1)
assert torch.all(torch.lt(torch.add(out_steps, -output), 1e-3)), (
"GRU output mismatch"
)
assert torch.all(torch.lt(torch.add(hn_t, -hn), 1e-3)), (
"GRU hidden states mismatch"
)
assert torch.jit.trace(net, inputs)
# Check GRU
inputs = torch.randn(4, 2, 7, device=device)
net = GRU(
hidden_size=5,
input_shape=inputs.shape,
num_layers=2,
bidirectional=False,
).to(device)
output, hn = net(inputs)
output_l = []
hn_t = None
for t in range(inputs.shape[1]):
out_t, hn_t = net(inputs[:, t, :].unsqueeze(1), hn_t)
output_l.append(out_t.squeeze(1))
out_steps = torch.stack(output_l, dim=1)
assert torch.all(torch.lt(torch.add(out_steps, -output), 1e-3)), (
"GRU output mismatch"
)
assert torch.all(torch.lt(torch.add(hn_t, -hn), 1e-3)), (
"GRU hidden states mismatch"
)
assert torch.jit.trace(net, inputs)
# Check LSTM
inputs = torch.randn(4, 2, 7, device=device)
net = LSTM(
hidden_size=5,
input_shape=inputs.shape,
num_layers=2,
bidirectional=False,
).to(device)
output, hn = net(inputs)
output_l = []
hn_t = None
for t in range(inputs.shape[1]):
out_t, hn_t = net(inputs[:, t, :].unsqueeze(1), hn_t)
output_l.append(out_t.squeeze(1))
out_steps = torch.stack(output_l, dim=1)
assert torch.all(torch.lt(torch.add(out_steps, -output), 1e-3)), (
"LSTM output mismatch"
)
assert torch.all(torch.lt(torch.add(hn_t[0], -hn[0]), 1e-3)) and torch.all(
torch.lt(torch.add(hn_t[1], -hn[1]), 1e-3)
), "LSTM hidden states mismatch"
assert torch.jit.trace(net, inputs)
# Check LiGRU
inputs = torch.randn(1, 2, 2, device=device)
net = LiGRU(
hidden_size=5,
input_shape=inputs.shape,
num_layers=2,
bidirectional=False,
normalization="layernorm",
).to(device)
output, hn = net(inputs)
output_l = []
hn_t = None
for t in range(inputs.shape[1]):
out_t, hn_t = net(inputs[:, t, :].unsqueeze(1), hn_t)
output_l.append(out_t.squeeze(1))
out_steps = torch.stack(output_l, dim=1)
assert torch.all(torch.lt(torch.add(out_steps, -output), 1e-3)), (
"LiGRU output mismatch"
)
assert torch.all(torch.lt(torch.add(hn_t[0], -hn[0]), 1e-3)) and torch.all(
torch.lt(torch.add(hn_t[1], -hn[1]), 1e-3)
), "LiGRU hidden states mismatch"
# Check QuasiRNN
inputs = torch.randn(1, 2, 2, device=device)
net = QuasiRNN(
hidden_size=5,
input_shape=inputs.shape,
num_layers=2,
bidirectional=False,
).to(device)
output, hn = net(inputs)
output_l = []
hn_t = None
for t in range(inputs.shape[1]):
out_t, hn_t = net(inputs[:, t, :].unsqueeze(1), hn_t)
output_l.append(out_t.squeeze(1))
out_steps = torch.stack(output_l, dim=1)
assert torch.all(torch.lt(torch.add(out_steps, -output), 1e-3)), (
"QuasiRNN output mismatch"
)
assert torch.all(
torch.lt(torch.add(hn_t[0], -hn[0][1]), 1e-3)
) and torch.all(torch.lt(torch.add(hn_t[1], -hn[1][1]), 1e-3)), (
"QuasiRNN hidden states mismatch"
)
assert torch.jit.trace(net, inputs)
# Check RNNCell
inputs = torch.randn(4, 2, 7, device=device)
net = RNNCell(hidden_size=5, input_size=7, num_layers=2, dropout=0.0).to(
device
)
hn_t = None
output_lst = []
for t in range(inputs.shape[1]):
output, hn_t = net(inputs[:, t], hn_t)
output_lst.append(output)
out_steps = torch.stack(output_lst, dim=1)
rnn = torch.nn.RNN(
input_size=7, hidden_size=5, num_layers=2, batch_first=True
).to(device)
# rename the state_dict
state = net.state_dict()
new_state = []
for name, tensor in state.items():
index, weight_id = name[len("rnn_cells.")], name[len("rnn_cells.0.") :]
new_state.append((f"{weight_id}_l{index}", tensor))
new_state = OrderedDict(new_state)
rnn.load_state_dict(new_state)
output, hn = rnn(inputs)
assert torch.all(torch.lt(torch.add(out_steps, -output), 1e-3)), (
"RNNCell output mismatch"
)
assert torch.all(torch.lt(torch.add(hn_t[0], -hn[0]), 1e-3)) and torch.all(
torch.lt(torch.add(hn_t[1], -hn[1]), 1e-3)
), "RNNCell hidden states mismatch"
assert torch.jit.trace(rnn, inputs)