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

Class ValueModel

rl2/mountaincar/pg_tf.py:122–168  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

120
121# approximates V(s)
122class ValueModel:
123 def __init__(self, D, ft, hidden_layer_sizes=[]):
124 self.ft = ft
125 self.costs = []
126
127 # create the graph
128 self.layers = []
129 M1 = D
130 for M2 in hidden_layer_sizes:
131 layer = HiddenLayer(M1, M2)
132 self.layers.append(layer)
133 M1 = M2
134
135 # final layer
136 layer = HiddenLayer(M1, 1, lambda x: x)
137 self.layers.append(layer)
138
139 # inputs and targets
140 self.X = tf.placeholder(tf.float32, shape=(None, D), name='X')
141 self.Y = tf.placeholder(tf.float32, shape=(None,), name='Y')
142
143 # calculate output and cost
144 Z = self.X
145 for layer in self.layers:
146 Z = layer.forward(Z)
147 Y_hat = tf.reshape(Z, [-1]) # the output
148 self.predict_op = Y_hat
149
150 cost = tf.reduce_sum(tf.square(self.Y - Y_hat))
151 self.cost = cost
152 self.train_op = tf.train.AdamOptimizer(1e-1).minimize(cost)
153
154 def set_session(self, session):
155 self.session = session
156
157 def partial_fit(self, X, Y):
158 X = np.atleast_2d(X)
159 X = self.ft.transform(X)
160 Y = np.atleast_1d(Y)
161 self.session.run(self.train_op, feed_dict={self.X: X, self.Y: Y})
162 cost = self.session.run(self.cost, feed_dict={self.X: X, self.Y: Y})
163 self.costs.append(cost)
164
165 def predict(self, X):
166 X = np.atleast_2d(X)
167 X = self.ft.transform(X)
168 return self.session.run(self.predict_op, feed_dict={self.X: X})
169
170
171def play_one_td(env, pmodel, vmodel, gamma):

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected