-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer_test.py
More file actions
67 lines (59 loc) · 1.64 KB
/
Copy pathbuffer_test.py
File metadata and controls
67 lines (59 loc) · 1.64 KB
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
"""
I want to test the buffer and see if it is reconstructing states properly.
I'll try a simplified version here
"""
from dqn import Buffer
import torch
buf = Buffer(max_size=10, im_dim=(2,), state_depth=3, max_ep_len=2)
f = 0
print(f"count before: {buf.count()}")
# adding a sample transition to the buffer
t = 0
s = torch.tensor([[0.,1.], [0.,1.], [0.,1.]])
a = torch.tensor(0)
r = torch.tensor(1)
sp = torch.tensor([[0.,1.], [0.,1.], [2.,3.]])
done = False
start = True
buf.add((s, a, r, sp, done, t, f))
# adding another sample transition
f += 1
t = 1
s = torch.tensor([[0.,1.], [0.,1.], [2.,3.]])
a = torch.tensor(1)
r = torch.tensor(2)
sp = torch.tensor([[0.,1.], [2.,3.], [5.,5.]])
done = True
start = False
buf.add((s, a, r, sp, done, t, f))
print(f"count after: {buf.count()}")
# filling the buffer
# NOTE: the buffer relies on states being sequential
# so has to be a valid trajectory
for i in range(301):
f += 1
t = 0
# ADDING A START STATE
s = torch.tensor([[0.,1.], [0.,1.], [0.,1.]])
a = torch.tensor(0)
r = torch.tensor(1)
sp = torch.tensor([[0.,1.], [0.,1.], [2.,3.]])
done = False
buf.add((s, a, r, sp, done, t, f))
# ADDING AN END STATE
f += 1
t = 1
s = torch.tensor([[0.,1.], [0.,1.], [2.,3.]])
a = torch.tensor(1)
r = torch.tensor(2)
sp = torch.tensor([[0.,1.], [2.,3.], [5.,5.]])
done = True
buf.add((s, a, r, sp, done, t, f))
print(f"count after: {buf.count()}")
print(buf.frame_tensor)
# sampling from the buffer
n = 10
s, a, r, sp, d = buf.sample(n)
print(len(buf.frame_tensor))
for i in range(n):
print(f"Sample {i}: s={s[i]}, a={a[i]}, r={r[i]}, sp={sp[i]}, d={d[i]}")