proximal policy optimization (ppo)speech.ee.ntu.edu.tw/~tlkagk/courses/mlds_2018/lecture...basic...
TRANSCRIPT
Proximal Policy Optimization (PPO)
default reinforcement learning algorithm at OpenAI
Policy Gradient
On-policy âOff-policy
Add constraint
DeepMind https://youtu.be/gn4nRCC9TwQ
OpenAI https://blog.openai.com/openai-baselines-ppo/
Policy Gradient (Review)
Basic Components
EnvActorReward
Function
VideoGame
Go
Get 20 scores when killing a monster
The rule of GO
You cannot control
âą Policy đ is a network with parameter đ
âą Input: the observation of machine represented as a vector or a matrix
âą Output: each action corresponds to a neuron in output layer
âŠâŠ
pixelsfire
right
left
Score of an action
0.7
0.2
0.1
Take the action based on the probability.
Policy of Actor
Start with observation đ 1 Observation đ 2 Observation đ 3
Example: Playing Video Game
Action đ1: ârightâ
Obtain reward đ1 = 0
Action đ2 : âfireâ
(kill an alien)
Obtain reward đ2 = 5
Start with observation đ 1 Observation đ 2 Observation đ 3
Example: Playing Video Game
After many turns
Action đđ
Obtain reward đđ
Game Over(spaceship destroyed)
This is an episode.
We want the total reward be maximized.
Total reward:
đ =
đĄ=1
đ
đđĄ
Actor, Environment, Reward
đ = đ 1, đ1, đ 2, đ2, ⯠, đ đ , đđTrajectory
Actor
đ 1
đ1
Env
đ 2
Env
đ 1
đ1
Actor
đ 2
đ2
Env
đ 3
đ2
âŠâŠ
đđ đ
= đ đ 1 đđ đ1|đ 1 đ đ 2|đ 1, đ1 đđ đ2|đ 2 đ đ 3|đ 2, đ2 âŻ
= đ đ 1 à·
đĄ=1
đ
đđ đđĄ|đ đĄ đ đ đĄ+1|đ đĄ , đđĄ
Actor, Environment, Reward
Actor
đ 1
đ1
Env
đ 2
Env
đ 1
đ1
Actor
đ 2
đ2
Env
đ 3
đ2
âŠâŠ
đ đ =
đĄ=1
đ
đđĄ
Reward
đ1
Reward
đ2
updatedupdated
àŽ€đ đ =
đ
đ đ đđ đ
Expected Reward
= đžđ~đđ đ đ đ
Policy Gradient àŽ€đ đ =
đ
đ đ đđ đ đ» àŽ€đ đ =?
đ» àŽ€đ đ =
đ
đ đ đ»đđ đ =
đ
đ đ đđ đđ»đđ đ
đđ đ
=
đ
đ đ đđ đ đ»đđđđđ đ
â1
đ
đ=1
đ
đ đđ đ»đđđđđ đđ
đ đ do not have to be differentiable
It can even be a black box.
đ đ„ đ»đđđđ đ„
đ»đ đ„ =
= đžđ~đđ đ đ đ đ»đđđđđ đ
=1
đ
đ=1
đ
đĄ=1
đđ
đ đđ đ»đđđđđ đđĄđ|đ đĄ
đ
Policy Gradient
đ1: đ 11, đ1
1 đ đ1
đ 21, đ2
1
âŠâŠ
đ đ1
âŠâŠ
đ2: đ 12, đ1
2 đ đ2
đ 22, đ2
2
âŠâŠ
đ đ2âŠâŠ
Given policy đđ
đ â đ + đđ» àŽ€đ đ
đ» àŽ€đ đ =
1
đ
đ=1
đ
đĄ=1
đđ
đ đđ đ»đđđđđ đđĄđ|đ đĄ
đ
UpdateModel
DataCollection
đ» àŽ€đ đ = đžđ~đđ đ đ đ đ»đđđđđ đ
only used once
đ â đ + đđ» àŽ€đ đ
đ» àŽ€đ đ =1
đ
đ=1
đ
đĄ=1
đđ
đ đđ đ»đđđđđ đđĄđ|đ đĄ
đ
Implementation
âŠâŠ
fire
right
left 1
0
0
Consider as classification problem
đđĄđ
đ đĄđ
1
đ
đ=1
đ
đĄ=1
đđ
đđđđđ đđĄđ|đ đĄ
đ 1
đ
đ=1
đ
đĄ=1
đđ
đ»đđđđđ đđĄđ|đ đĄ
đ
1
đ
đ=1
đ
đĄ=1
đđ
đ đđ đđđđđ đđĄđ|đ đĄ
đ1
đ
đ=1
đ
đĄ=1
đđ
đ đđ đ»đđđđđ đđĄđ|đ đĄ
đ
TF, pyTorch âŠ
đđĄđđ đĄ
đ đ đđ
Tip 1: Add a Baselineđ â đ + đđ» àŽ€đ đ
đ» àŽ€đ đ â1
đ
đ=1
đ
đĄ=1
đđ
đ đđ đ»đđđđđ đđĄđ|đ đĄ
đ
Ideal case
SamplingâŠâŠ
a b c a b c
It is probability âŠ
a b c
Not sampled
a b c
The probability of the actions not sampled will decrease.
1
đ
đ=1
đ
đĄ=1
đđ
đ đđ â đ đ»đđđđđ đđĄđ|đ đĄ
đ
It is possible that đ đđ is always positive.
đ â đž đ đ
Tip 2: Assign Suitable Credit
đ đ , đ1 đ đ , đ2 đ đ , đ3
+0+5 -2
đ = +3
Ă 3 Ă 3 Ă 3
đ» àŽ€đ đ â1
đ
đ=1
đ
đĄ=1
đđ
đ đđ â đ đ»đđđđđ đđĄđ|đ đĄ
đ
đĄâČ=đĄ
đđđđĄâČđ
đ đ , đ2 đ đ , đ2 đ đ , đ3
+0-5 -2
đ = â7
Ă â7 Ă â7 Ă â7Ă â2 Ă â2 Ă â2 Ă â2
Tip 2: Assign Suitable Credit
đ» àŽ€đ đ â1
đ
đ=1
đ
đĄ=1
đđ
đ đđ â đ đ»đđđđđ đđĄđ|đ đĄ
đ
đĄâČ=đĄ
đđđđĄâČđ
đĄâČ=đĄ
đđđŸđĄ
âČâđĄđđĄâČđ
đŸ < 1Add discount factor
Can be state-dependent
Advantage Function
đŽđ đ đĄ , đđĄ
How good it is if we take đđĄother than other actions at đ đĄ.
Estimated by âcriticâ (later)
From on-policy to off-policy
Using the experience more than once
On-policy v.s. Off-policy
âą On-policy: The agent learned and the agent interacting with the environment is the same.
âą Off-policy: The agent learned and the agent interacting with the environment is different.
éżć äžæŁ äœçșäžæŁăéżć ćšæéç
On-policy â Off-policy
đ» àŽ€đ đ = đžđ~đđ đ đ đ đ»đđđđđ đ
đžđ„~đ[đ đ„ ]
âą Use đđ to collect data. When đ is updated, we have to sample training data again.
âą Goal: Using the sample from đđâČ to train đ. đâČ is fixed, so we can re-use the sample data.
Importance Sampling
â1
đ
đ=1
đ
đ đ„đ
đ„đ is sampled from đ đ„
We only have đ„đ sampled from đ đ„
= නđ đ„ đ đ„ đđ„ = නđ đ„đ đ„
đ đ„đ đ„ đđ„ = đžđ„~đ[đ đ„
đ đ„
đ đ„]
Importance weight
Issue of Importance Sampling
đžđ„~đ[đ đ„ ] = đžđ„~đ[đ đ„đ đ„
đ đ„]
Vđđđ„~đ[đ đ„ ] â Vđđđ„~đ[đ đ„đ đ„
đ đ„]
đđŽđ đ
= đž đ2 â đž đ 2
Vđđđ„~đ đ đ„ = đžđ„~đ đ đ„ 2 â đžđ„~đ đ đ„2
Vđđđ„~đ[đ đ„đ đ„
đ đ„] = đžđ„~đ đ đ„
đ đ„
đ đ„
2
â đžđ„~đ đ đ„đ đ„
đ đ„
2
= đžđ„~đ đ đ„ 2đ đ„
đ đ„â đžđ„~đ đ đ„
2
Issue of Importance Sampling
đžđ„~đ[đ đ„ ] = đžđ„~đ[đ đ„đ đ„
đ đ„]
đ đ„
đ đ„ đ đ„
Very large weight
đžđ„~đ đ đ„ is negative
đžđ„~đ đ đ„ is positive?negative
On-policy â Off-policy
đ» àŽ€đ đ = đžđ~đđ đ đ đ đ»đđđđđ đ
âą Use đđ to collect data. When đ is updated, we have to sample training data again.
âą Goal: Using the sample from đđâČ to train đ. đâČ is fixed, so we can re-use the sample data.
đ» àŽ€đ đ = đžđ~đđâČ
đ
đđ đ
đđâČ đđ đ đ»đđđđđ đ
Importance Sampling
đžđ„~đ[đ đ„ ] = đžđ„~đ[đ đ„đ đ„
đ đ„]
âą Sample the data from đâČ.
âą Use the data to train đ many times.
On-policy â Off-policy
= đž đ đĄ,đđĄ ~đđ[đŽđ đ đĄ , đđĄ đ»đđđđđ đđĄ
đ|đ đĄđ ]
= đž đ đĄ,đđĄ ~đđâČ[đđ đ đĄ , đđĄđđâČ đ đĄ , đđĄ
đŽđ đ đĄ , đđĄ đ»đđđđđ đđĄđ|đ đĄ
đ ]
đ đ„ đ»đđđđ đ„đ»đ đ„ =
đŽđâČđ đĄ , đđĄ
This term is from sampled data.
đœđâČđ = đž đ đĄ,đđĄ ~đđâČ
đđ đđĄ|đ đĄđđâČ đđĄ|đ đĄ
đŽđâČđ đĄ , đđĄ
= đž đ đĄ,đđĄ ~đđâČ[đđ đđĄ|đ đĄđđâČ đđĄ|đ đĄ
đđ đ đĄđđâČ đ đĄ
đŽđ đ đĄ , đđĄ đ»đđđđđ đđĄđ|đ đĄ
đ ]
Gradient for update
When to stop?
Add Constraint穩玟穩æïŒæ„æ„çșç
PPO / TRPO
đœđđđđâČ đ = đœđ
âČđ â đœđŸđż đ, đâČ
đœđâČđ = đž đ đĄ,đđĄ ~đđâČ
đđ đđĄ|đ đĄđđâČ đđĄ|đ đĄ
đŽđâČđ đĄ , đđĄ
đœđđ đđđâČ đ = đž đ đĄ,đđĄ ~đđâČ
đđ đđĄ|đ đĄđđâČ đđĄ|đ đĄ
đŽđâČđ đĄ , đđĄ
đŸđż đ, đâČ < đż
TRPO (Trust Region Policy Optimization)
Proximal Policy Optimization (PPO)
đ cannot be very different from đâČ
Constraint on behavior not parameters
đ đ„ đ»đđđđ đ„đ»đ đ„ =
PPO algorithm
âą Initial policy parameters đ0
âą In each iteration
âą Using đđ to interact with the environment to collect
đ đĄ , đđĄ and compute advantage đŽđđđ đĄ , đđĄ
âą Find đ optimizing đœđđđ đ
âą If đŸđż đ, đđ > đŸđżđđđ„, increase đœ
âą If đŸđż đ, đđ < đŸđżđđđ, decrease đœ
đœđđđđđ đ = đœđ
đđ â đœđŸđż đ, đđ
Update parameters several times
Adaptive KL Penalty
đœđđđ â
đ đĄ,đđĄ
đđ đđĄ|đ đĄđđđ đđĄ|đ đĄ
đŽđđđ đĄ , đđĄ
đœđđđđđ đ = đœđ
đđ â đœđŸđż đ, đđ
đœđđđ â
đ đĄ,đđĄ
đđ đđĄ|đ đĄđđđ đđĄ|đ đĄ
đŽđđđ đĄ , đđĄ
PPO algorithm
PPO2 algorithm
đœđđđ2đđ đ â
đ đĄ,đđĄ
đđđđđ đđĄ|đ đĄđđđ đđĄ|đ đĄ
đŽđđđ đĄ , đđĄ , đđđđ
đđ đđĄ|đ đĄđđđ đđĄ|đ đĄ
, 1 â í
â
đ đĄ,đđĄ
đđđ đđđđđđ đđĄ|đ đĄđđđ đđĄ|đ đĄ
, 1 â í, 1 + í đŽđđđ đĄ , đđĄ
đđ đđĄ|đ đĄđđđ đđĄ|đ đĄ1 1 + í1 â í
1
1 + í
1 â í
đœđđđđđ đ = đœđ
đđ â đœđŸđż đ, đđ
đœđđđ â
đ đĄ,đđĄ
đđ đđĄ|đ đĄđđđ đđĄ|đ đĄ
đŽđđđ đĄ , đđĄ
PPO algorithm
PPO2 algorithm
đœđđđ2đđ đ â
đ đĄ,đđĄ
đđđđđ đđĄ|đ đĄđđđ đđĄ|đ đĄ
đŽđđđ đĄ , đđĄ , đđđđ
đđ đđĄ|đ đĄđđđ đđĄ|đ đĄ
, 1 â í
â
đ đĄ,đđĄ
đđđ đđđđđđ đđĄ|đ đĄđđđ đđĄ|đ đĄ
, 1 â í, 1 + í đŽđđđ đĄ , đđĄ
1 1 + í1 â í
1
1 + í
1 â í
1 1 + í1 â í
1
1 + í
1 â í
đŽ > 0 đŽ < 0
Experimental Results
https://arxiv.org/abs/1707.06347