forked from ermongroup/dail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
replaymemory.py
81 lines (63 loc) · 1.95 KB
/
replaymemory.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import random
from collections import deque
# Initialize replay buffers
def create_replay_memory(env, params):
'''
Creates separate replay memories for each domain
Args:
env : environments for each domain : dict
params : experiment parameters : dict
memtype : type of replay memory to create : str
Returns:
replay_memory : replay memories for each domain : dict
'''
replay_memory = {}
for d_ in env.keys():
if params['train']['memtype'] == 'vanilla':
replay_memory[d_] = FIFOdeque(memsize=params['train']['memsize'])
else:
print("[replaymemory.py] Unrecognized replay buffer type: {}".format(memtype))
replay_memory['model'] = FIFOdeque(memsize=params['train']['memsize'])
return replay_memory
class FIFOdeque():
'''
Vanilla replay memory with FIFO structure implemented with a deque
'''
def __init__(self, memsize):
self.memory = deque(maxlen=memsize)
def len(self):
'''
Number of entries in the replay memory
Args:
Returns:
len : number of entries in the memory : int
'''
return len(self.memory)
def add_to_memory(self, data):
'''
Add data to the end of the deque
Args:
data : data entry to add : tuple
Returns:
None
'''
self.memory.append(data)
def sample_from_memory(self, batchsize):
'''
Sample a batch from memory
Args:
batchsize : size of the batch to sample : int
Returns:
sample : sampled batch : list of
'''
sample = random.sample(self.memory, batchsize)
return sample
def set_memory(self, loaded_deque):
'''
Sample a batch from memory
Args:
loaded_deque : deque loaded from a saved file
Returns:
None
'''
self.memory = loaded_deque