-
Notifications
You must be signed in to change notification settings - Fork 0
/
rnn.py
executable file
·220 lines (167 loc) · 6.68 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
import numpy as np
import collections
# This is a simple Recursive Neural Netowrk with one ReLU Layer and a softmax layer
# TODO: You must update the forward and backward propogation functions of this file.
# You can run this file via 'python rnn.py' to perform a gradient check!
# tip: insert pdb.set_trace() in places where you are unsure whats going on
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):
np.random.seed(12341)
# Word vectors
self.L = 0.01*np.random.randn(self.wvecDim,self.numWords)
# Hidden layer parameters
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) # note this is " U " in the notes and the handout.. there is a reason for the change in notation
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.
or if in test mode
Returns
cost, correctArray, guessArray, total
"""
cost = 0.0
correct = []
guess = []
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,tot = self.forwardProp(tree.root,correct,guess)
cost += c
total += tot
if test:
return (1./len(mbdata))*cost,correct,guess,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,correct=[], guess=[]):
cost = total = 0.0 # cost should be a running number and total is the total examples we have seen used in accuracy reporting later
################
# TODO: Implement the recursive forwardProp function
# - you should update node.probs, node.hActs1, node.fprop, and cost
# - node: your current node in the parse tree
# - correct: this is a running list of truth labels
# - guess: this is a running list of guess that our model makes
# (we will use both correct and guess to make our confusion matrix)
################
return cost, total + 1
def backProp(self,node,error=None):
# Clear nodes
node.fprop = False
################
# TODO: Implement the recursive backProp function
# - you should update self.dWs, self.dbs, self.dW, self.db, and self.dL[node.word] accordingly
# - node: your current node in the parse tree
# - error: error that has been passed down from a previous iteration
################
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)
err1 = 0.0
count = 0.0
print "Checking dW..."
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)
err1+=err
count+=1
if 0.001 > err1/count:
print "Grad Check Passed for dW"
else:
print "Grad Check Failed for dW: Sum of Error = %.9f" % (err1/count)
# check dL separately since dict
dL = grad[0]
L = self.stack[0]
err2 = 0.0
count = 0.0
print "Checking dL..."
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)
err2+=err
count+=1
if 0.001 > err2/count:
print "Grad Check Passed for dL"
else:
print "Grad Check Failed for dL: Sum of Error = %.9f" % (err2/count)
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)