加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
utils_memory.py 1.96 KB
一键复制 编辑 原始数据 按行查看 历史
zeng798473532 提交于 2021-11-09 15:43 . add to.(device)
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).to(device)
self.__m_actions = torch.zeros((capacity, 1), dtype=torch.long).to(device)
self.__m_rewards = torch.zeros((capacity, 1), dtype=torch.int8).to(device)
self.__m_dones = torch.zeros((capacity, 1), dtype=torch.bool).to(device)
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()
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
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化