-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.py
More file actions
27 lines (22 loc) · 908 Bytes
/
memory.py
File metadata and controls
27 lines (22 loc) · 908 Bytes
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
import random
import pickle
class Memory:
def __init__(self, max_memory):
self._max_memory = max_memory
self._samples = []
def add_sample(self, sample):
self._samples.append(sample)
if len(self._samples) > self._max_memory:
self._samples.pop(0)
def sample(self, no_samples):
if no_samples > len(self._samples):
return random.sample(self._samples, len(self._samples))
return random.sample(self._samples, no_samples)
def save(self, path, filename):
with open(f'{path}/{filename}.pkl', 'wb') as fp:
pickle.dump(self._samples, fp)
print(f'\nMemory saved in path: {path}/{filename}.pkl')
def load(self, path, filename):
with open(f'{path}/{filename}.pkl', 'rb') as fp:
self._samples = pickle.load(fp)
print(f'\nMemory loaded from path: {path}/{filename}.pkl')