| 12 | self.buffer.append(exp) |
| 13 | |
| 14 | def sample(self, batch_size): |
| 15 | mini_batch = random.sample(self.buffer, batch_size) |
| 16 | batch_state, batch_action, batch_reward, batch_next_state, batch_done = [], [], [], [], [] |
| 17 | |
| 18 | for experience in mini_batch: |
| 19 | s, a, r, s_p, done = experience |
| 20 | batch_state.append(s) |
| 21 | batch_action.append(a) |
| 22 | batch_reward.append(r) |
| 23 | batch_next_state.append(s_p) |
| 24 | batch_done.append(done) |
| 25 | batch_state = paddle.to_tensor(batch_state, dtype='float32') |
| 26 | batch_action = paddle.to_tensor(batch_action, dtype='float32') |
| 27 | batch_reward = paddle.to_tensor(batch_reward, dtype='float32') |
| 28 | batch_next_state = paddle.to_tensor(batch_next_state, dtype='float32') |
| 29 | batch_done = paddle.to_tensor(batch_done, dtype='float32') |
| 30 | |
| 31 | return batch_state, batch_action, batch_reward, batch_next_state, batch_done |
| 32 | |
| 33 | def __len__(self): |
| 34 | return len(self.buffer) |