-
Notifications
You must be signed in to change notification settings - Fork 15
/
rnn.py
228 lines (176 loc) · 6.6 KB
/
rnn.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
215
216
217
218
219
220
221
222
import numpy as np
import collections
class RNN:
def __init__(self,wvecDim,outputDim,numWords,mbSize=30,rho=1e-4):
self.wvecDim = wvecDim
self.outputDim = outputDim
self.numWords = numWords
self.mbSize = mbSize
self.defaultVec = lambda : np.zeros((wvecDim,))
self.rho = rho
def initParams(self):
# Word vectors
self.L = 0.01*np.random.randn(self.wvecDim,self.numWords)
# Hidden activation weights
self.W = 0.01*np.random.randn(self.wvecDim,2*self.wvecDim)
self.b = np.zeros((self.wvecDim))
# Softmax weights
self.Ws = 0.01*np.random.randn(self.outputDim,self.wvecDim)
self.bs = np.zeros((self.outputDim))
self.stack = [self.L, self.W, self.b, self.Ws, self.bs]
# Gradients
self.dW = np.empty(self.W.shape)
self.db = np.empty((self.wvecDim))
self.dWs = np.empty(self.Ws.shape)
self.dbs = np.empty((self.outputDim))
def costAndGrad(self,mbdata,test=False):
"""
Each datum in the minibatch is a tree.
Forward prop each tree.
Backprop each tree.
Returns
cost
Gradient w.r.t. W, Ws, b, bs
Gradient w.r.t. L in sparse form.
"""
cost = 0.0
correct = 0.0
total = 0.0
self.L,self.W,self.b,self.Ws,self.bs = self.stack
# Zero gradients
self.dW[:] = 0
self.db[:] = 0
self.dWs[:] = 0
self.dbs[:] = 0
self.dL = collections.defaultdict(self.defaultVec)
# Forward prop each tree in minibatch
for tree in mbdata:
c,corr,tot = self.forwardProp(tree.root)
cost += c
correct += corr
total += tot
if test:
return (1./len(mbdata))*cost,correct,total
# Back prop each tree in minibatch
for tree in mbdata:
self.backProp(tree.root)
# scale cost and grad by mb size
scale = (1./self.mbSize)
for v in self.dL.itervalues():
v *=scale
# Add L2 Regularization
cost += (self.rho/2)*np.sum(self.W**2)
cost += (self.rho/2)*np.sum(self.Ws**2)
return scale*cost,[self.dL,scale*(self.dW + self.rho*self.W),scale*self.db,
scale*(self.dWs+self.rho*self.Ws),scale*self.dbs]
def forwardProp(self,node):
cost = correct = total = 0.0
if node.isLeaf:
node.hActs = self.L[:,node.word]
node.fprop = True
else:
if not node.left.fprop:
c,corr,tot = self.forwardProp(node.left)
cost += c
correct += corr
total += tot
if not node.right.fprop:
c,corr,tot = self.forwardProp(node.right)
cost += c
correct += corr
total += tot
# Affine
node.hActs = np.dot(self.W,
np.hstack([node.left.hActs, node.right.hActs])) + self.b
# Relu
node.hActs[node.hActs<0] = 0
# Softmax
node.probs = np.dot(self.Ws,node.hActs) + self.bs
node.probs -= np.max(node.probs)
node.probs = np.exp(node.probs)
node.probs = node.probs/np.sum(node.probs)
node.fprop = True
return cost - np.log(node.probs[node.label]), correct + (np.argmax(node.probs)==node.label),total + 1
def backProp(self,node,error=None):
# Clear nodes
node.fprop = False
# Softmax grad
deltas = node.probs
deltas[node.label] -= 1.0
self.dWs += np.outer(deltas,node.hActs)
self.dbs += deltas
deltas = np.dot(self.Ws.T,deltas)
if error is not None:
deltas += error
deltas *= (node.hActs != 0)
# Leaf nodes update word vecs
if node.isLeaf:
self.dL[node.word] += deltas
return
# Hidden grad
if not node.isLeaf:
self.dW += np.outer(deltas,
np.hstack([node.left.hActs, node.right.hActs]))
self.db += deltas
# Error signal to children
deltas = np.dot(self.W.T, deltas)
self.backProp(node.left, deltas[:self.wvecDim])
self.backProp(node.right, deltas[self.wvecDim:])
def updateParams(self,scale,update,log=False):
"""
Updates parameters as
p := p - scale * update.
If log is true, prints root mean square of parameter
and update.
"""
if log:
for P,dP in zip(self.stack[1:],update[1:]):
pRMS = np.sqrt(np.mean(P**2))
dpRMS = np.sqrt(np.mean((scale*dP)**2))
print "weight rms=%f -- update rms=%f"%(pRMS,dpRMS)
self.stack[1:] = [P+scale*dP for P,dP in zip(self.stack[1:],update[1:])]
# handle dictionary update sparsely
dL = update[0]
for j in dL.iterkeys():
self.L[:,j] += scale*dL[j]
def toFile(self,fid):
import cPickle as pickle
pickle.dump(self.stack,fid)
def fromFile(self,fid):
import cPickle as pickle
self.stack = pickle.load(fid)
def check_grad(self,data,epsilon=1e-6):
cost, grad = self.costAndGrad(data)
for W,dW in zip(self.stack[1:],grad[1:]):
W = W[...,None] # add dimension since bias is flat
dW = dW[...,None]
for i in xrange(W.shape[0]):
for j in xrange(W.shape[1]):
W[i,j] += epsilon
costP,_ = self.costAndGrad(data)
W[i,j] -= epsilon
numGrad = (costP - cost)/epsilon
err = np.abs(dW[i,j] - numGrad)
print "Analytic %.9f, Numerical %.9f, Relative Error %.9f"%(dW[i,j],numGrad,err)
# check dL separately since dict
dL = grad[0]
L = self.stack[0]
for j in dL.iterkeys():
for i in xrange(L.shape[0]):
L[i,j] += epsilon
costP,_ = self.costAndGrad(data)
L[i,j] -= epsilon
numGrad = (costP - cost)/epsilon
err = np.abs(dL[j][i] - numGrad)
print "Analytic %.9f, Numerical %.9f, Relative Error %.9f"%(dL[j][i],numGrad,err)
if __name__ == '__main__':
import tree as treeM
train = treeM.loadTrees()
numW = len(treeM.loadWordMap())
wvecDim = 10
outputDim = 5
rnn = RNN(wvecDim,outputDim,numW,mbSize=4)
rnn.initParams()
mbData = train[:4]
print "Numerical gradient check..."
rnn.check_grad(mbData)