(self, batch_size=32)
| 267 | return np.argmax(act_values[0]) # returns action |
| 268 | |
| 269 | def replay(self, batch_size=32): |
| 270 | # first check if replay buffer contains enough data |
| 271 | if self.memory.size < batch_size: |
| 272 | return |
| 273 | |
| 274 | # sample a batch of data from the replay memory |
| 275 | minibatch = self.memory.sample_batch(batch_size) |
| 276 | states = minibatch['s'] |
| 277 | actions = minibatch['a'] |
| 278 | rewards = minibatch['r'] |
| 279 | next_states = minibatch['s2'] |
| 280 | done = minibatch['d'] |
| 281 | |
| 282 | # Calculate the tentative target: Q(s',a) |
| 283 | target = rewards + (1 - done) * self.gamma * np.amax(self.model.predict(next_states), axis=1) |
| 284 | |
| 285 | # With the Keras API, the target (usually) must have the same |
| 286 | # shape as the predictions. |
| 287 | # However, we only need to update the network for the actions |
| 288 | # which were actually taken. |
| 289 | # We can accomplish this by setting the target to be equal to |
| 290 | # the prediction for all values. |
| 291 | # Then, only change the targets for the actions taken. |
| 292 | # Q(s,a) |
| 293 | target_full = self.model.predict(states) |
| 294 | target_full[np.arange(batch_size), actions] = target |
| 295 | |
| 296 | # Run one training step |
| 297 | self.model.partial_fit(states, target_full) |
| 298 | |
| 299 | if self.epsilon > self.epsilon_min: |
| 300 | self.epsilon *= self.epsilon_decay |
| 301 | |
| 302 | |
| 303 | def load(self, name): |
no test coverage detected