forked from rksltnl/RNTN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
184 lines (133 loc) · 5.31 KB
/
utils.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
import numpy as np
from ComputeCostAndGrad import ComputeCostAndGrad
from ComputeCostAndGradMiniBatch import ComputeCostAndGradMiniBatch
import copy
from RNTNModel import RNTNModel
def checkGradient_MiniBatch(dictionary, trees):
model = RNTNModel(dictionary)
theta_init = model.getTheta()
# compute analytical gradient
costObj = ComputeCostAndGradMiniBatch()
cost, grad = costObj.compute(theta_init, dictionary, trees)
eps = 1E-4
numgrad = np.zeros(grad.shape)
# compute numerical gradient
for i in range(model.num_parameters):
if i % 10 == 0:
print '%d/%d' % (i, model.num_parameters)
indicator = np.zeros(model.num_parameters)
indicator[i] = 1
theta_plus = theta_init + eps*indicator
cost_plus, grad_plus = costObj.compute(theta_plus, dictionary, trees)
theta_minus = theta_init - eps*indicator
cost_minus, grad_minus = costObj.compute(theta_minus, dictionary, trees)
numgrad[i] = (cost_plus - cost_minus)/(2*eps)
print 'analytical gradient: ', grad
print 'numerical gradient: ', numgrad
normdiff = np.linalg.norm(numgrad - grad) / np.linalg.norm(numgrad + grad)
print 'Norm difference: ', normdiff
return normdiff
def checkGradientClean(dictionary, trees):
# Code adopted from UFLDL gradientChecker
# http://ufldl.stanford.edu/wiki/index.php/Gradient_checking_and_advanced_optimization
model = RNTNModel(dictionary)
theta_init = model.getTheta()
# compute analytical gradient
costObj = ComputeCostAndGrad(dictionary, trees)
cost, grad = costObj.compute(theta_init)
eps = 1E-4
numgrad = np.zeros(grad.shape)
# compute numerical gradient
for i in range(model.num_parameters):
if i % 10 == 0:
print '%d/%d' % (i, model.num_parameters)
indicator = np.zeros(model.num_parameters)
indicator[i] = 1
theta_plus = theta_init + eps*indicator
cost_plus, grad_plus = costObj.compute(theta_plus)
theta_minus = theta_init - eps*indicator
cost_minus, grad_minus = costObj.compute(theta_minus)
numgrad[i] = (cost_plus - cost_minus)/(2*eps)
print 'analytical gradient: ', grad
print 'numerical gradient: ', numgrad
normdiff = np.linalg.norm(numgrad - grad) / np.linalg.norm(numgrad + grad)
print 'Norm difference: ', normdiff
return normdiff
def checkGradient(model, trees):
# Code adopted from UFLDL gradientChecker
# http://ufldl.stanford.edu/wiki/index.php/Gradient_checking_and_advanced_optimization
computer = ComputeCostAndGrad(model, trees)
cost, grad = computer.compute()
eps = 1E-4
numgrad = np.zeros(grad.shape)
theta = model.getTheta()
for i in range(model.num_parameters):
if i % 10 == 0:
print '%d/%d' % (i, model.num_parameters)
indicator = np.zeros(model.num_parameters)
indicator[i] = 1
theta_plus = theta + eps*indicator
model_plus = copy.deepcopy(model)
model_plus.updateParamsGivenTheta(theta_plus)
computer_plus = ComputeCostAndGrad(model_plus, trees)
cost_plus, grad_plus = computer_plus.compute()
theta_minus = theta - eps*indicator
model_minus = copy.deepcopy(model)
model_minus.updateParamsGivenTheta(theta_minus)
computer_minus = ComputeCostAndGrad(model_minus, trees)
cost_minus, grad_minus = computer_minus.compute()
numgrad[i] = (cost_plus - cost_minus)/(2*eps)
print 'analytical gradient: ', grad
print 'numerical gradient: ', numgrad
print 'Norm difference: '
normdiff = np.linalg.norm(numgrad - grad) / np.linalg.norm(numgrad + grad)
return normdiff
def softmax(x):
e = np.exp(x - np.max(x))
return e / sum(e)
def vectorizeParams(*args):
vect = np.array([])
for matrix in args:
vect = np.hstack([vect, matrix.ravel()])
return vect
def constructCompactDictionary(trees):
# Take a tree lists and union all words
# note: have a "*UNK*" to deal with previously unseen words
dictionary = set()
dictionary = dictionary.union(['*UNK*'])
for tree in trees:
dictionary = dictionary.union(tree.word_yield().split(' '))
return dictionary
def constructDictionary(*args):
# Take a list of tree lists and union all words
dictionary = set()
for tree_split in args:
for trees in tree_split:
for tree in trees:
dictionary = dictionary.union(tree.word_yield().split(' '))
return dictionary
'''
def simpleQuadraticFunction(x):
cost = x[0]**2 + 3*x[0]*x[1]
grad = np.zeros(2)
grad[0] = 2*x[0] + 3*x[1]
grad[1] = 3*x[0]
return cost, grad
def checkQuadraticGradient():
model = np.array([4,10])
[cost, grad] = simpleQuadraticFunction(model)
eps = 1E-4
numgrad = np.zeros(model.shape)
model_perturbed = model
for i in range(2):
indicator = np.zeros(2)
indicator[i] = 1;
model_perturbed = model + eps*indicator
cost_plus, grad_plus = simpleQuadraticFunction(model_perturbed)
model_perturbed = model - eps*indicator
cost_minus, grad_minus = simpleQuadraticFunction(model_perturbed)
numgrad[i] = (cost_plus - cost_minus)/(2*eps)
print grad
print numgrad
print np.linalg.norm(grad - numgrad) / np.linalg.norm(grad + numgrad)
'''