-
Notifications
You must be signed in to change notification settings - Fork 1
/
experience_replay.py
38 lines (28 loc) · 1.64 KB
/
experience_replay.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import torch
import random
import numpy as np
from collections import namedtuple, deque
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class ReplayBuffer:
"""Fixed-size experience buffer"""
def __init__(self, config):
"""Initialize a ReplayBuffer object."""
self.memory = deque(maxlen=config["buffer"]["size"])
self.experience = namedtuple("Experience", field_names=["state", "action", "reward", "next_state", "done"])
self.seed = random.seed(config["general"]["seed"])
def add(self, state, action, reward, next_state, done):
"""Add a new experience to memory."""
e = self.experience(state, action, reward, next_state, done)
self.memory.append(e)
def sample(self, batch_size):
"""Randomly sample a batch of experiences from memory."""
experiences = random.sample(self.memory, k=batch_size)
states = torch.from_numpy(np.vstack([e.state for e in experiences if e is not None])).float().to(DEVICE)
actions = torch.from_numpy(np.vstack([e.action for e in experiences if e is not None])).long().to(DEVICE)
rewards = torch.from_numpy(np.vstack([e.reward for e in experiences if e is not None])).float().to(DEVICE)
next_states = torch.from_numpy(np.vstack([e.next_state for e in experiences if e is not None])).float().to(DEVICE)
dones = torch.from_numpy(np.vstack([e.done for e in experiences if e is not None]).astype(np.uint8)).float().to(DEVICE)
return states, actions, rewards, next_states, dones
def __len__(self):
"""Return the current size of internal memory."""
return len(self.memory)