-
Notifications
You must be signed in to change notification settings - Fork 12
/
controlRNN.py
85 lines (68 loc) · 3.42 KB
/
controlRNN.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
import graphAttack as ga
import numpy as np
import scipy.optimize
"""Control script"""
def run():
"""Run the model"""
N, T, D, H1, H2 = 2, 3, 4, 5, 4
trainData = np.linspace(- 0.1, 0.3, num=N * T * D).reshape(N, T, D)
trainLabels = np.random.random((N, T, D))
mainGraph = ga.Graph(False)
xop = mainGraph.addOperation(ga.Variable(trainData), feederOperation=True)
hactivations0, cStates0 = ga.addInitialLSTMLayer(mainGraph,
inputOperation=xop,
nHidden=H1)
hactivations1, cStates1 = ga.appendLSTMLayer(mainGraph,
previousActivations=hactivations0,
nHidden=H2)
# hactivations0 = ga.addInitialRNNLayer(mainGraph,
# inputOperation=xop,
# activation=ga.TanhActivation,
# nHidden=H1)
# hactivations1 = ga.appendRNNLayer(mainGraph,
# previousActivations=hactivations0,
# activation=ga.TanhActivation,
# nHidden=H2)
finalCost, costOperationsList = ga.addRNNCost(mainGraph,
hactivations1,
costActivation=ga.SoftmaxActivation,
costOperation=ga.CrossEntropyCostSoftmax,
nHidden=H2,
labelsShape=xop.shape,
labels=None)
def f(p, costOperationsList=costOperationsList, mainGraph=mainGraph):
data = trainData
labels = trainLabels
mainGraph.feederOperation.assignData(data)
mainGraph.resetAll()
for index, cop in enumerate(costOperationsList):
cop.assignLabels(labels[:, index, :])
mainGraph.attachParameters(p)
c = mainGraph.feedForward()
return c
hactivations = [hactivations0, hactivations1]
cStates = [cStates0, cStates1]
def fprime(p, data, labels, costOperationsList=costOperationsList, mainGraph=mainGraph):
mainGraph.feederOperation.assignData(data)
mainGraph.resetAll()
for index, cop in enumerate(costOperationsList):
cop.assignLabels(labels[:, index, :])
mainGraph.attachParameters(p)
c = mainGraph.feedForward()
mainGraph.feedBackward()
g = mainGraph.unrollGradients()
nLayers = len(hactivations)
for i in range(nLayers):
hactivations[i][0].assignData(hactivations[i][-1].getValue())
cStates[i][0].assignData(cStates[i][-1].getValue())
return c, g
params = mainGraph.unrollGradientParameters()
numGrad = scipy.optimize.approx_fprime(params, f, 1e-8)
analCostGraph, analGradientGraph = fprime(params, trainData, trainLabels)
return numGrad, analGradientGraph, analCostGraph, mainGraph
if (__name__ == "__main__"):
nGrad, aGrad, aCost, mainGraph = run()
params = mainGraph.unrollGradientParameters()
print(mainGraph)
print("\n%-16.16s %-16.16s" % ("Grad difference", "Total Gradient"))
print("%-16.8e %-16.8e" % (np.sum(np.abs(aGrad - nGrad)), np.sum(np.abs(aGrad))))