15 min total

Reinforcement Learning

Policies

Policies

Advantage functions

sometimes we dont need to describe how good an action is in absolute sense, but only how better is it compared to others on average, we wanna know relative advatage of that action. mathematically defined as:

where A outputs how much better it is to take a specific action a in state s

Generalized Advantage estimation (GAE)



Model-free vs Model-based RL

in model free, you don't have an internal simulator that tells you what will happen to the environment/model if you make a particular move. but in model based you know what would exactly happen when you take an action x. algoritihms that use model-free are called model free algorithms else called model based

two main approaches to represent and train agents with model free RL:
a. policy optimization (on-policy): these methods optimize params by gradient ascent on objective function
b. Q learning (off-policy): these methods make best approximations for action value functions, use objective function based on bellman equation. its part of off policy algorithms becuase each update can use data collected at any point during training.

for model based RL algorithms (non exhaustive):
a. pure planning, agent computes a trajectory wtr to model, agent re-decides trajectory at each step and picks the one with best returns
b. expert iteration, a continual learning algorithm, at each step the agent uses a policy to suggest actions, then use a planning algorithm to find the most optimal move, update weights of that policy and then uses the best move

learned environment: ai model that simulates an environment, it inputs action and outputs state.

for model free RL (non exhaustive):
a. augment past agent experiences + fictitious experiences
b. only fictitious experirences from a learned environemnt (used by world models ‘training in the dreams’)
c. planning loops into policies; planner simulates possible action sequences and provides a proposed plan, policy inputs state + plan and outputs final action (final action doesnt neccesarily has to be from plan)

Expected Grad-Log-Prob (EGLP) lemma

means derivative of j wtr to

Log derivative trick:

property:




Now differentiate P(θ) with respect to θ

this lets us represent derivative of a probability as gradient of log probability x probability

eq1

(log derivative technique)

(substituting eq 1)


Vanilla policy gradient (VPG)

objective function pushes up probabilities of actions that lead to higher return and pushes down probabilities of actions that lead to a lower return until we arrive at the optimal policy.

its an on policy algorithm. and can be used for environments with discrete or continuous action spaces

denotes a policy with params and denotes expected finite horizon undiscounted return of the policy. gradient of the is:

is a trajectory and is advantage function for current policy

algorithm is as follows:

  1. at each step collect a set( ) of trajectories by running policy in the environment
  2. compute rewards per trajectory
  3. compute advantage estimates based on current value function
  4. estimate policy gradient
  5. compute policy update using any gradient ascent method
  6. fit value function by mean squared error via some gradient descent method


Trust Region Policy Optimization (TRPO)

is an on policy algorithm, can be used for discrete or continuous action spaces

Large policy updates can drastically change the agent's behavior and reduce performance. A normal learning rate does not reliably control how much the policy changes.

Restrict each update so the new policy stays close to the old one, measured using KL divergence

Whats TRPO Update like?

Training the Policy network/mo

a. collect trajectories using the current policy network outputs
b. calculate discounted sum of future rewards
c. calculate the advantages for each timestep
d. we update the neural network params with surrogate objective

e. problem: if there is too much variance in trajectories, there is a training instability
solution: restrict how different the new policy can be from the old policy by adding a KL divergence term over the action space
f. we want to improve hte policy without changing its action distributions too much, TRPO uses Hessian of KL divergence and Conjugate gradient to approximately solve giving
g. now to update the policy network using gradients, it checks tow conditions
i. does the new policy improves surrogate objective?
ii. is its KL divergence from old policy within the limit
h. if an acceptable update is found,

Training the value network

a. from each sampled trajectories, take their states
b. value network estimates
c. lets assume to be actual reward values (calculated by taking actually observed rewards)
d. calculate loss using mean squared error (MSE)

Protip: use a surrogate/proxy objective for policy gradients

if youre training a policy network, going to every state repeatedly and calculating its reward is really hectic and time consuming, soo … we already have data from agent’s previous attempts. Like the actual objective is this:

calculating expected reward would require running the environment again and again. So we do this instead (approximation)

What the fuck is a Hessian Matrix

since gradient alone doesn’t tells us how much a policy update will change KL divergence, we use a hessian matrix

we want to minimise the surrogate objective:
at the same time we dont want new policy to differ too much from the old policy:
our original gradient doesnt tells how much KL divergence will result from moving in that direction.

we define second order derivative via taylor series for approximation
we assume equation A ~ ,
taylor series for second order derivatives B ~
since we are comparing distributions in (A), if we substitute for 0 because distribution compared to itself is 0, we end up with C ~
where the H term is assuming policy network has two parameters

Proximal Policy Optimization

PPO motivated by the same question as TRPO, how can we take the biggest possible improvement step on a policy during training, without stepping so far that we accidentally cause performance collapse? This method emperically seems to perform better than TRPO.

PPO is also an on-policy algorithm, and can be used in discrete or continuous spces.

there are two variants of PPO, each use the same training loop, they jsut have different policy network objective functions

overall training remains the same;
a. collect trajectories using current policy|
b. calculate rewards-to-go
c. calculate the advantages
d. update hte actor (policy network)
e. update the critic (value network)
f. repeat

PPO Variants

  1. PPO-Penalty: has a surrogate objective with KL penalty, penalizes policy changes

    controls how strongly policy changes are penalized
  2. PPO-Clip: has a cliiped surrogate objective, removes incentive for excessive policy changes

    controls the clipping range, commonly 0.2

Why on-policy algorithms are pain in the ass

on policy algortihms collects experiences using its current policy, trains on those experiences and then once the policy has changed, those experiences become outdated and it generally collects new ones. Interacting with the environment can be expensive especially in robotics and physical simulations