MCPcopy Create free account
hub / github.com/lazyprogrammer/machine_learning_examples / __init__

Method __init__

rl2/cartpole/pg_theano.py:118–164  ·  view source on GitHub ↗
(self, D, hidden_layer_sizes)

Source from the content-addressed store, hash-verified

116# approximates V(s)
117class ValueModel:
118 def __init__(self, D, hidden_layer_sizes):
119 # constant learning rate is fine
120 lr = 1e-4
121
122 # create the graph
123 self.layers = []
124 M1 = D
125 for M2 in hidden_layer_sizes:
126 layer = HiddenLayer(M1, M2)
127 self.layers.append(layer)
128 M1 = M2
129
130 # final layer
131 layer = HiddenLayer(M1, 1, lambda x: x)
132 self.layers.append(layer)
133
134 # get all params for gradient later
135 params = []
136 for layer in self.layers:
137 params += layer.params
138
139 # inputs and targets
140 X = T.matrix('X')
141 Y = T.vector('Y')
142
143 # calculate output and cost
144 Z = X
145 for layer in self.layers:
146 Z = layer.forward(Z)
147 Y_hat = T.flatten(Z)
148 cost = T.sum((Y - Y_hat)**2)
149
150 # specify update rule
151 grads = T.grad(cost, params)
152 updates = [(p, p - lr*g) for p, g in zip(params, grads)]
153
154 # compile functions
155 self.train_op = theano.function(
156 inputs=[X, Y],
157 updates=updates,
158 allow_input_downcast=True
159 )
160 self.predict_op = theano.function(
161 inputs=[X],
162 outputs=Y_hat,
163 allow_input_downcast=True
164 )
165
166 def partial_fit(self, X, Y):
167 X = np.atleast_2d(X)

Callers

nothing calls this directly

Calls 3

forwardMethod · 0.95
HiddenLayerClass · 0.70
gradMethod · 0.45

Tested by

no test coverage detected