-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_memory.py
68 lines (58 loc) · 2.09 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
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,
) -> None:#初始化
self.__device = device
self.__capacity = capacity
self.__size = 0
self.__pos = 0
self.__m_states = torch.zeros(
(capacity, channels, 84, 84), dtype=torch.uint8)
self.__m_actions = torch.zeros((capacity, 1), dtype=torch.long)
self.__m_rewards = torch.zeros((capacity, 1), dtype=torch.int8)
self.__m_dones = 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 = (self.__pos + 1) % self.__capacity # 先进先出,应该是队列,循环队列
self.__size = max(self.__size, self.__pos) # 循环队列中现在的元素个数
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() # 一共5个,前四个作为state
b_next = self.__m_states[indices, 1:].to(self.__device).float() # 后四个作为next_state
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