This repository has been archived by the owner on Nov 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
replay_memory.py
37 lines (30 loc) · 1.59 KB
/
replay_memory.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
import numpy as np
import random
from collections import namedtuple, deque
import torch
class ReplayBuffer:
def __init__(self, buffer_size, batch_size, device):
"""Initialize a ReplayBuffer object.
Params
======
buffer_size (int): maximum size of buffer
batch_size (int): size of each training batch
device (string): GPU or CPU
"""
self.memory = deque(maxlen=buffer_size)
self.batch_size = batch_size
self.experience = namedtuple("Experience", field_names=["state", "action", "reward", "next_state", "done"])
self.device = device
def add(self, state, action, reward, next_state, done):
e = self.experience(state, action, reward, next_state, done)
self.memory.append(e)
def sample(self):
experiences = random.sample(self.memory, k=self.batch_size)
states = torch.from_numpy(np.array([e.state for e in experiences if e is not None])).float().to(self.device)
actions = torch.from_numpy(np.array([e.action for e in experiences if e is not None])).long().to(self.device)
rewards = torch.from_numpy(np.array([e.reward for e in experiences if e is not None])).float().to(self.device)
next_states = torch.from_numpy(np.array([e.next_state for e in experiences if e is not None])).float().to(self.device)
dones = torch.from_numpy(np.array([e.done for e in experiences if e is not None]).astype(np.uint8)).float().to(self.device)
return (states, actions, rewards, next_states, dones)
def __len__(self):
return len(self.memory)