-
Notifications
You must be signed in to change notification settings - Fork 1
/
prioritized_memory.py
55 lines (41 loc) · 1.46 KB
/
prioritized_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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Code from https://github.com/rlcode/per
import random
import numpy as np
from SumTree import SumTree
class Memory: # stored as ( s, a, r, s_ ) in SumTree
e = 0.01
a = 0.6
beta = 0.4
beta_increment_per_sampling = 0.001
def __init__(self, capacity):
self.tree = SumTree(capacity)
self.capacity = capacity
def _get_priority(self, weight):
return (np.abs(weight) + self.e) ** self.a
def add_per(self, weight, sample):
p = self._get_priority(weight)
self.tree.add(p, sample)
def add(self, weight, sample):
p = weight + 0.0000001 if weight%10==0 else weight
self.tree.add(p, sample)
def sample(self, n):
batch = []
idxs = []
segment = self.tree.total() / n
priorities = []
self.beta = np.min([1., self.beta + self.beta_increment_per_sampling])
for i in range(n):
a = segment * i
b = segment * (i + 1)
s = random.uniform(a, b)
(idx, p, data) = self.tree.get(s)
priorities.append(p)
batch.append(data)
idxs.append(idx)
sampling_probabilities = priorities / self.tree.total()
weights = np.power(self.tree.n_entries * sampling_probabilities, -self.beta)
weights /= weights.max()
return batch, idxs, weights, self.tree.total()
def update(self, idx, weight):
p = self._get_priority(weight)
self.tree.update(idx, p)