forked from cgpotts/cs224u
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_np_model_gradients.py
214 lines (191 loc) · 6.98 KB
/
test_np_model_gradients.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
from nltk.tree import Tree
from np_shallow_neural_classifier import ShallowNeuralClassifier
from np_rnn_classifier import RNNClassifier
from np_autoencoder import Autoencoder
from np_tree_nn import TreeNN
import numpy as np
import pytest
import utils
__author__ = "Christopher Potts"
__version__ = "CS224u, Stanford, Spring 2021"
utils.fix_random_seeds()
class GradientCheckError(Exception):
"""Raised if a gradient check fails."""
@pytest.mark.parametrize("hidden_activation, d_hidden_activation", [
[np.tanh, utils.d_tanh],
[utils.relu, utils.d_relu]
])
def test_np_shallow_neural_classifier_gradients(hidden_activation, d_hidden_activation):
model = ShallowNeuralClassifier(
max_iter=10,
hidden_activation=hidden_activation,
d_hidden_activation=d_hidden_activation)
# A tiny dataset so that we can run `fit` and set all the model
# parameters:
X = utils.randmatrix(5, 2)
y = np.random.choice((0,1), 5)
model.fit(X, y)
# Use the first example for the check:
ex = X[0]
label = model._onehot_encode([y[0]])[0]
# Forward and backward to get the gradients:
hidden, pred = model.forward_propagation(ex)
d_W_hy, d_b_hy, d_W_xh, d_b_xh = model.backward_propagation(
hidden, pred, ex, label)
# Model parameters to check:
param_pairs = (
('W_hy', d_W_hy),
('b_hy', d_b_hy),
('W_xh', d_W_xh),
('b_xh', d_b_xh)
)
gradient_check(param_pairs, model, ex, label)
@pytest.mark.parametrize("hidden_activation, d_hidden_activation", [
[np.tanh, utils.d_tanh],
[utils.relu, utils.d_relu]
])
def test_np_rnn_classifier(hidden_activation, d_hidden_activation):
# A tiny dataset so that we can run `fit` and set all the model
# parameters:
vocab = ['a', 'b', '$UNK']
data = [
[list('ab'), 'good'],
[list('aab'), 'good'],
[list('abb'), 'good']]
model = RNNClassifier(
vocab,
max_iter=10,
hidden_dim=2,
hidden_activation=hidden_activation,
d_hidden_activation=d_hidden_activation)
X, y = zip(*data)
model.fit(X, y)
# Use the first example for the check:
ex = X[0]
label = model._onehot_encode([y[0]])[0]
# Forward and backward to get the gradients:
hidden, pred = model.forward_propagation(ex)
d_W_hy, d_b, d_W_hh, d_W_xh = model.backward_propagation(
hidden, pred, ex, label)
# Model parameters to check:
param_pairs = (
('W_xh', d_W_xh),
('W_hh', d_W_hh),
('W_hy', d_W_hy),
('b', d_b)
)
gradient_check(param_pairs, model, ex, label)
@pytest.mark.parametrize("hidden_activation, d_hidden_activation", [
[np.tanh, utils.d_tanh],
[utils.relu, utils.d_relu]
])
def test_np_autoencoder(hidden_activation, d_hidden_activation):
model = Autoencoder(
max_iter=10,
hidden_dim=2,
hidden_activation=hidden_activation,
d_hidden_activation=d_hidden_activation)
# A tiny dataset so that we can run `fit` and set all the model
# parameters:
X = utils.randmatrix(5, 5)
model.fit(X)
# Use the first example for the check:
ex = X[0]
label = X[0]
# Forward and backward to get the gradients:
hidden, pred = model.forward_propagation(ex)
d_W_hy, d_b_hy, d_W_xh, d_b_xh = model.backward_propagation(
hidden, pred, ex, label)
# Model parameters to check:
param_pairs = (
('W_hy', d_W_hy),
('b_hy', d_b_hy),
('W_xh', d_W_xh),
('b_xh', d_b_xh)
)
gradient_check(param_pairs, model, ex, label)
@pytest.mark.parametrize("hidden_activation, d_hidden_activation", [
[np.tanh, utils.d_tanh],
[utils.relu, utils.d_relu]
])
def test_np_tree_nn(hidden_activation, d_hidden_activation):
# A tiny dataset so that we can run `fit` and set all the model
# parameters:
vocab = ["1", "+", "2"]
X = [
"(even (odd 1) (neutral (neutral +) (odd 1)))",
"(odd (odd 1) (neutral (neutral +) (even 2)))"]
X = [Tree.fromstring(ex) for ex in X]
y = [tree.label() for tree in X]
model = TreeNN(
vocab,
max_iter=10,
hidden_dim=5,
hidden_activation=hidden_activation,
d_hidden_activation=d_hidden_activation)
model.fit(X, y)
# Use the first example for the check:
ex = X[0]
label = model._onehot_encode([ex.label()])[0]
# Forward and backward to get the gradients:
hidden, pred = model.forward_propagation(ex)
d_W_hy, d_b_y, d_W, d_b = model.backward_propagation(
hidden, pred, ex, label)
# Model parameters to check:
param_pairs = (
('W_hy', d_W_hy),
('b_y', d_b_y),
('W', d_W),
('b', d_b)
)
gradient_check(param_pairs, model, ex, label)
def gradient_check(param_pairs, model, ex, label, epsilon=0.0001, threshold=0.001):
"""
Numerical gradient check following the method described here:
http://ufldl.stanford.edu/wiki/index.php/Gradient_checking_and_advanced_optimization
Parameters
----------
param_pairs : list of str, np.aray pairs
In each pair, the first is the name of the parameter to check,
and the second is its purported derivatives. We use the name
as the first pair so that we can raise an informative error
message in the case of a failure.
model : trained model instance
This should have attributes for all of the parameters named in
`param_pairs`, and it must have methods `forward_propagation`,
and `get_error`.
ex : an example that `model` can process
label : a label vector that `model` can learn from directly
epsilon : float
The small constant by which the parameter values are changed.
threshold : float
Tolerance for raising an error.
Raises
------
GradientCheckError
"""
for param_name, d_params in param_pairs:
params = getattr(model, param_name)
# This iterator will allow is to cycle over all the values for
# arrays of any dimension:
iterator = np.nditer(params, flags=['multi_index'], op_flags=['readwrite'])
while not iterator.finished:
idx = iterator.multi_index
actual = params[idx]
params[idx] = actual + epsilon
_, pred = model.forward_propagation(ex)
grad_pos = model.get_error(pred, label)
params[idx] = actual - epsilon
_, pred = model.forward_propagation(ex)
grad_neg = model.get_error(pred, label)
grad_est = (grad_pos - grad_neg) / (epsilon * 2.0)
params[idx] = actual
grad_bp = d_params[idx]
# Relative error to control for differences in proportion
# across parameter values:
err = np.abs(grad_bp - grad_est) / (np.abs(grad_bp) + np.abs(grad_est))
if err >= threshold:
raise GradientCheckError(
"Gradient check error for {} at {}: error is {}".format(
param_name, idx, err))
iterator.iternext()