-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutils_memory.py
71 lines (61 loc) · 2.04 KB
/
utils_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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from typing import (
Tuple,
)
import torch
from utils_types import (
BatchAction,
BatchDone,
BatchNext,
BatchReward,
BatchState,
TensorStack5,
TorchDevice,
)
class ReplayMemory(object):
def __init__(
self,
channels: int,
capacity: int,
device: TorchDevice,
full_sink: bool = True,
) -> None:
self.__device = device
self.__capacity = capacity
self.__size = 0
self.__pos = 0
sink = lambda x: x.to(device) if full_sink else x
self.__m_states = sink(torch.zeros(
(capacity, channels, 84, 84), dtype=torch.uint8))
self.__m_actions = sink(torch.zeros((capacity, 1), dtype=torch.long))
self.__m_rewards = sink(torch.zeros((capacity, 1), dtype=torch.int8))
self.__m_dones = sink(torch.zeros((capacity, 1), dtype=torch.bool))
def push(
self,
folded_state: TensorStack5,
action: int,
reward: int,
done: bool,
) -> None:
self.__m_states[self.__pos] = folded_state
self.__m_actions[self.__pos, 0] = action
self.__m_rewards[self.__pos, 0] = reward
self.__m_dones[self.__pos, 0] = done
self.__pos += 1
self.__size = max(self.__size, self.__pos)
self.__pos %= self.__capacity
def sample(self, batch_size: int) -> Tuple[
BatchState,
BatchAction,
BatchReward,
BatchNext,
BatchDone,
]:
indices = torch.randint(0, high=self.__size, size=(batch_size,))
b_state = self.__m_states[indices, :4].to(self.__device).float()
b_next = self.__m_states[indices, 1:].to(self.__device).float()
b_action = self.__m_actions[indices].to(self.__device)
b_reward = self.__m_rewards[indices].to(self.__device).float()
b_done = self.__m_dones[indices].to(self.__device).float()
return b_state, b_action, b_reward, b_next, b_done
def __len__(self) -> int:
return self.__size