Implement experience replay in the paper `Human-level control through deep reinforcement learning `_. This implementation provides the interface as a :class:`DataFlow`. This DataFlow is __not__ fork-safe (th
| 255 | |
| 256 | |
| 257 | class ExpReplay(DataFlow, Callback): |
| 258 | """ |
| 259 | Implement experience replay in the paper |
| 260 | `Human-level control through deep reinforcement learning |
| 261 | <http://www.nature.com/nature/journal/v518/n7540/full/nature14236.html>`_. |
| 262 | |
| 263 | This implementation provides the interface as a :class:`DataFlow`. |
| 264 | This DataFlow is __not__ fork-safe (thus doesn't support multiprocess prefetching). |
| 265 | |
| 266 | It does the following: |
| 267 | * Spawn `num_parallel_players` environment thread, each running an instance |
| 268 | of the environment with epislon-greedy policy. |
| 269 | * All environment instances writes their experiences to a shared replay |
| 270 | memory buffer. |
| 271 | * Produces batched samples by sampling the replay buffer. After producing |
| 272 | each batch, it executes the environment instances by a total of |
| 273 | `update_frequency` steps. |
| 274 | |
| 275 | This implementation assumes that state is batch-able, and the network takes batched inputs. |
| 276 | """ |
| 277 | |
| 278 | def __init__(self, |
| 279 | predictor_io_names, |
| 280 | get_player, |
| 281 | num_parallel_players, |
| 282 | state_shape, |
| 283 | batch_size, |
| 284 | memory_size, init_memory_size, |
| 285 | update_frequency, history_len, |
| 286 | state_dtype='uint8'): |
| 287 | """ |
| 288 | Args: |
| 289 | predictor_io_names (tuple of list of str): input/output names to |
| 290 | predict Q value from state. |
| 291 | get_player (-> gym.Env): a callable which returns a player. |
| 292 | num_parallel_players (int): number of players to run in parallel. |
| 293 | Standard DQN uses 1. |
| 294 | Parallelism increases speed, but will affect the distribution of |
| 295 | experiences in the replay buffer. |
| 296 | state_shape (tuple): |
| 297 | batch_size (int): |
| 298 | memory_size (int): |
| 299 | init_memory_size (int): |
| 300 | update_frequency (int): number of new transitions to add to memory |
| 301 | after sampling a batch of transitions for training. |
| 302 | history_len (int): length of history frames to concat. Zero-filled |
| 303 | initial frames. |
| 304 | state_dtype (str): |
| 305 | """ |
| 306 | assert len(state_shape) in [1, 2, 3], state_shape |
| 307 | init_memory_size = int(init_memory_size) |
| 308 | |
| 309 | for k, v in locals().items(): |
| 310 | if k != 'self': |
| 311 | setattr(self, k, v) |
| 312 | self.exploration = 1.0 # default initial exploration |
| 313 | |
| 314 | self.rng = get_rng(self) |