Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PPO #28

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
24 changes: 12 additions & 12 deletions Aprendizado por Reforço Profundo/Actor-Critic/A2C/A2C.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@
"id": "1ZiSoaCSnX-3"
},
"source": [
"## Experience Replay"
"## Memory Buffer"
]
},
{
Expand All @@ -242,8 +242,8 @@
"source": [
"import numpy as np\n",
"\n",
"class ExperienceReplay:\n",
" \"\"\"Experience Replay Buffer para A2C.\"\"\"\n",
"class MemoryBuffer:\n",
" \"\"\"Memory Buffer Buffer para A2C.\"\"\"\n",
" def __init__(self, max_length, observation_space):\n",
" \"\"\"Cria um Replay Buffer.\n",
"\n",
Expand Down Expand Up @@ -286,7 +286,7 @@
" self.dones[self.length] = dones\n",
" self.length += 1\n",
"\n",
" def sample(self):\n",
" def get_batch(self):\n",
" \"\"\"Retorna um batch de experiências.\n",
" \n",
" Parâmetros\n",
Expand Down Expand Up @@ -340,7 +340,7 @@
" self.entropy_coef = entropy_coef\n",
"\n",
" self.n_steps = n_steps\n",
" self.memory = ExperienceReplay(n_steps, observation_space.shape[0])\n",
" self.memory = MemoryBuffer(n_steps, observation_space.shape[0])\n",
"\n",
" self.actor = Actor(observation_space.shape[0], action_space.n).to(self.device)\n",
" self.critic = Critic(observation_space.shape[0]).to(self.device)\n",
Expand Down Expand Up @@ -380,7 +380,7 @@
" if self.memory.length < self.n_steps:\n",
" return\n",
"\n",
" (states, actions, rewards, next_states, dones) = self.memory.sample()\n",
" (states, actions, rewards, next_states, dones) = self.memory.get_batch()\n",
"\n",
" states = torch.FloatTensor(states).to(self.device)\n",
" actions = torch.FloatTensor(actions).to(self.device)\n",
Expand Down Expand Up @@ -664,7 +664,7 @@
" self.entropy_coef = entropy_coef\n",
"\n",
" self.n_steps = n_steps\n",
" self.memory = ExperienceReplay(n_steps, observation_space.shape[0])\n",
" self.memory = MemoryBuffer(n_steps, observation_space.shape[0])\n",
"\n",
" self.actorcritic = ActorCritic(observation_space.shape[0], action_space.n).to(self.device)\n",
" self.actorcritic_optimizer = optim.Adam(self.actorcritic.parameters(), lr=lr)\n",
Expand Down Expand Up @@ -702,7 +702,7 @@
" if self.memory.length < self.n_steps:\n",
" return\n",
"\n",
" (states, actions, rewards, next_states, dones) = self.memory.sample()\n",
" (states, actions, rewards, next_states, dones) = self.memory.get_batch()\n",
"\n",
" states = torch.FloatTensor(states).to(self.device)\n",
" actions = torch.FloatTensor(actions).to(self.device)\n",
Expand Down Expand Up @@ -869,7 +869,7 @@
"source": [
"import numpy as np\n",
"\n",
"class MultipleExperienceReplay:\n",
"class MultipleMemoryBuffer:\n",
" def __init__(self, max_length, env_num, observation_space):\n",
" self.length = 0\n",
" self.max_length = max_length\n",
Expand All @@ -888,7 +888,7 @@
" self.dones[self.length] = dones\n",
" self.length += 1\n",
"\n",
" def sample(self):\n",
" def get_batch(self):\n",
" self.length = 0\n",
"\n",
" return (self.states, self.actions, self.rewards, self.next_states, self.dones)"
Expand Down Expand Up @@ -944,7 +944,7 @@
" self.entropy_coef = entropy_coef\n",
"\n",
" self.n_steps = n_steps\n",
" self.memory = MultipleExperienceReplay(n_steps, env_num, observation_space.shape[0])\n",
" self.memory = MultipleMemoryBuffer(n_steps, env_num, observation_space.shape[0])\n",
"\n",
" self.actorcritic = ActorCritic(observation_space.shape[0], action_space.n).to(self.device)\n",
" self.actorcritic_optimizer = optim.Adam(self.actorcritic.parameters(), lr=lr)\n",
Expand Down Expand Up @@ -982,7 +982,7 @@
" if self.memory.length < self.n_steps:\n",
" return\n",
"\n",
" (states, actions, rewards, next_states, dones) = self.memory.sample()\n",
" (states, actions, rewards, next_states, dones) = self.memory.get_batch()\n",
"\n",
" states = torch.FloatTensor(states).to(self.device)\n",
" actions = torch.FloatTensor(actions).to(self.device)\n",
Expand Down
Loading