Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
neurips2020-flatland-starter-kit
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
adrian_egli
neurips2020-flatland-starter-kit
Commits
8d6304b3
Commit
8d6304b3
authored
4 years ago
by
Egli Adrian (IT-SCI-API-PFI)
Browse files
Options
Downloads
Patches
Plain Diff
. doc
parent
41d4b483
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
reinforcement_learning/ppo_agent.py
+21
-14
21 additions, 14 deletions
reinforcement_learning/ppo_agent.py
with
21 additions
and
14 deletions
reinforcement_learning/ppo_agent.py
+
21
−
14
View file @
8d6304b3
...
...
@@ -165,38 +165,45 @@ class PPOAgent(Policy):
return
states
,
actions
,
rewards
,
states_next
,
dones
,
prob_actions
def
train_net
(
self
):
for
handle
in
range
(
len
(
self
.
memory
)):
agent_episode_history
=
self
.
memory
.
get_transitions
(
handle
)
if
len
(
agent_episode_history
)
>
0
:
# convert the replay buffer to torch tensors (arrays)
states
,
actions
,
rewards
,
states_next
,
dones
,
probs_action
=
\
self
.
_convert_transitions_to_torch_tensors
(
agent_episode_history
)
# Optimize policy for K epochs:
for
_
in
range
(
self
.
K_epoch
):
# evaluating actions (actor) and values (critic)
# Optimize policy for K epochs:
for
_
in
range
(
self
.
K_epoch
):
# All agents have to propagate their experiences made during past episode
for
handle
in
range
(
len
(
self
.
memory
)):
# Extract agent's episode history (list of all transitions)
agent_episode_history
=
self
.
memory
.
get_transitions
(
handle
)
if
len
(
agent_episode_history
)
>
0
:
# Convert the replay buffer to torch tensors (arrays)
states
,
actions
,
rewards
,
states_next
,
dones
,
probs_action
=
\
self
.
_convert_transitions_to_torch_tensors
(
agent_episode_history
)
# Evaluating actions (actor) and values (critic)
logprobs
,
state_values
,
dist_entropy
=
self
.
actor_critic_model
.
evaluate
(
states
,
actions
)
#
f
inding the ratios (pi_thetas / pi_thetas_replayed):
#
F
inding the ratios (pi_thetas / pi_thetas_replayed):
ratios
=
torch
.
exp
(
logprobs
-
probs_action
.
detach
())
#
f
inding Surrogate Los
s:
#
F
inding Surrogate Lo
o
s
advantages
=
rewards
-
state_values
.
detach
()
surr1
=
ratios
*
advantages
surr2
=
torch
.
clamp
(
ratios
,
1.
-
self
.
surrogate_eps_clip
,
1.
+
self
.
surrogate_eps_clip
)
*
advantages
# The loss function is used to estimate the gardient and use the entropy function based
# heuristic to penalize the gradient function when the policy becomes deterministic this would let
# the gardient to become very flat and so the gradient is no longer useful.
loss
=
\
-
torch
.
min
(
surr1
,
surr2
)
\
+
self
.
weight_loss
*
self
.
loss_function
(
state_values
,
rewards
)
\
-
self
.
weight_entropy
*
dist_entropy
#
m
ake a gradient step
#
M
ake a gradient step
self
.
optimizer
.
zero_grad
()
loss
.
mean
().
backward
()
self
.
optimizer
.
step
()
#
stor
e current loss to the agent
#
Transfer th
e current loss to the agent
s loss (information) for debug purpose only
self
.
loss
=
loss
.
mean
().
detach
().
numpy
()
# Reset all collect transition data
self
.
memory
.
reset
()
def
end_episode
(
self
,
train
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment