Updates global policy and value networks using the local networks' gradients
(self, steps, sess)
| 217 | return steps, global_step |
| 218 | |
| 219 | def update(self, steps, sess): |
| 220 | """ |
| 221 | Updates global policy and value networks using the local networks' gradients |
| 222 | """ |
| 223 | |
| 224 | # In order to accumulate the total return |
| 225 | # We will use V_hat(s') to predict the future returns |
| 226 | # But we will use the actual rewards if we have them |
| 227 | # Ex. if we have s1, s2, s3 with rewards r1, r2, r3 |
| 228 | # Then G(s3) = r3 + V(s4) |
| 229 | # G(s2) = r2 + r3 + V(s4) |
| 230 | # G(s1) = r1 + r2 + r3 + V(s4) |
| 231 | reward = 0.0 |
| 232 | if not steps[-1].done: |
| 233 | reward = self.get_value_prediction(steps[-1].next_state, sess) |
| 234 | |
| 235 | # Accumulate minibatch samples |
| 236 | states = [] |
| 237 | advantages = [] |
| 238 | value_targets = [] |
| 239 | actions = [] |
| 240 | |
| 241 | # loop through steps in reverse order |
| 242 | for step in reversed(steps): |
| 243 | reward = step.reward + self.discount_factor * reward |
| 244 | advantage = reward - self.get_value_prediction(step.state, sess) |
| 245 | # Accumulate updates |
| 246 | states.append(step.state) |
| 247 | actions.append(step.action) |
| 248 | advantages.append(advantage) |
| 249 | value_targets.append(reward) |
| 250 | |
| 251 | feed_dict = { |
| 252 | self.policy_net.states: np.array(states), |
| 253 | self.policy_net.advantage: advantages, |
| 254 | self.policy_net.actions: actions, |
| 255 | self.value_net.states: np.array(states), |
| 256 | self.value_net.targets: value_targets, |
| 257 | } |
| 258 | |
| 259 | # Train the global estimators using local gradients |
| 260 | global_step, pnet_loss, vnet_loss, _, _ = sess.run([ |
| 261 | self.global_step, |
| 262 | self.policy_net.loss, |
| 263 | self.value_net.loss, |
| 264 | self.pnet_train_op, |
| 265 | self.vnet_train_op, |
| 266 | ], feed_dict) |
| 267 | |
| 268 | # Theoretically could plot these later |
| 269 | return pnet_loss, vnet_loss |
no test coverage detected