Starting from:
$30

$24

[Solved]Assignment 2: Policy Gradients

    • Introduction

The goal of this assignment is to experiment with policy gradient and its variants, including variance reduction tricks such as implementing reward-to-go and neural network baselines. The startercode can be found at

https://github.com/berkeleydeeprlcourse/homework_fall2021/tree/master/hw2

    • Review

2.1    Policy gradient

Recall that the reinforcement learning objective is to learn a    that maximizes the objective function:

J( ) = E   ( ) [r( )]
(1)
where each rollout  is of length T , as follows:

T
1
tY
( ) = p(s0; a0; :::; sT  1; aT  1) = p(s0)  (a0js0)
p(stjst  1; at  1)  (atjst)
=1
and

T
1
Xt
r( ) = r(s0; a0; :::; sT  1; aT  1) =
r(st; at):
=0

The policy gradient approach is to directly take the gradient of this objective:

r J( ) = r Z
( )r( )d
(2)
= Z
( )r log  ( )r( )d :
(3)
= E   ( ) [r log  ( )r( )]
(4)



(5)

In practice, the expectation over trajectories    can be approximated from a batch of N sampled trajectories:

1
N




r J( )

Xi
r log  ( i)r( i)


(6)

N
=1




= N

r log  (aitjsit)!
r(sit; ait)!
:
(7)






1
N
T  1
T  1




Xi
X
X











=1
t=0
t=0



Here we see that the policy is a probability distribution over the action space, conditioned on the state. In the agent-environment loop, the agent samples an action at from ( jst) and the environment responds with a reward r(st; at).

2.2    Variance Reduction

2.2.1    Reward-to-go

One way to reduce the variance of the policy gradient is to exploit causality: the notion that the policy cannot a ect rewards in the past. This yields the following modi ed objective, where the sum of rewards here does
Berkeley CS 285    Deep Reinforcement Learning, Decision Making, and Control    Fall 2021



not include the rewards achieved prior to the time step at which the policy is being queried. This sum of rewards is a sample estimate of the Q function, and is referred to as the \reward-to-go."
r J( )   N

r log  (aitjsit)
r(sit0
; ait0)!
:
(8)
1
N  T  1
T
1





X X
tX0












i=1 t=0

=t




2.2.2    Discounting

Multiplying a discount factor to the rewards can be interpreted as encouraging the agent to focus more on the rewards that are closer in time, and less on the rewards that are further in the future. This can also be thought of as a means for reducing variance (because there is more variance possible when considering futures that are further into the future). We saw in lecture that the discount factor can be incorporated in two ways, as shown below.

The    rst way applies the discount on the rewards from full trajectory:
r J( )   N




r log  (aitjsit)!
t0  1r(sit0; ait0)!
(9)
1


N
T  1

T  1






X  X

tX0















i=1
t=0

=0


and the second way applies the discount on the \reward-to-go:"

tr(sit0; ait0)! :

r J( )   N


r log  (aitjsit)
t0

(10)



1
N
T  1
T
1







X X
tX0
















i=1 t=0

=t


.

2.2.3    Baseline

Another variance reduction method is to subtract a baseline (that is a constant with respect to ) from the sum of rewards:

r J( ) = r E   ( ) [r( )  b] :
(11)

This leaves the policy gradient unbiased because

r E    ( ) [b] = E    ( ) [r    log    ( )  b] = 0:

In this assignment, we will implement a value function V which acts as a state-dependent baseline. This value function will be trained to approximate the sum of future rewards starting from a particular state:

    • 1
V  (st)
tX0
[r(st0; at0)jst] ;


E

(12)
=t

so the approximate policy gradient now looks like this:
r J( )   N

r log  (aitjsit)
t

tr(sit0
; ait0)!
V  (sit)!
:
(13)
1
N  T  1
T  1

0







X X
tX0




















i=1 t=0
=t








2.2.4    Generalized Advantage Estimation



0




The quantity
T  1
t

tr(st0
; at0)

(st) from the previous policy gradient expression (removing the i

Pt0=t




V

index for clarity) can be interpreted as an estimate of the advantage function:

A (st; at) = Q (st; at)  V  (st);
(14)
Berkeley CS 285    Deep Reinforcement Learning, Decision Making, and Control    Fall 2021



where Q (st; at) is estimated using Monte Carlo returns and V (st) is estimated using the learned value function V . We can further reduce variance by also using V in place of the Monte Carlo returns to estimate the advantage function as:

A (st; at)   t = r(st; at) +  V  (st+1)  V  (st);
(15)

with the edge case T 1 = r(sT 1; aT 1) V (sT 1). However, this comes at the cost of introducing bias to our policy gradient estimate, due to modeling errors in V . We can instead use a combination of n-step Monte Carlo returns and V to estimate the advantage function as:


t+n

An(st; at) =
t0 tr(st0; at0) +  nV  (st+n+1)  V  (st):
(16)

tX0


=t


Increasing n incorporates the Monte Carlo returns more heavily in the advantage estimate, which lowers bias and increases variance, while decreasing n does the opposite. Note that n = T t 1 recovers the unbiased but higher variance Monte Carlo advantage estimate used in (13), while n = 0 recovers the lower variance but higher bias advantage estimate t.

We can combine multiple n-step advantage estimates as an exponentially weighted sum, which is known as the generalized advantage estimator (GAE). Let 2 [0; 1]. Then we de ne:



1   T
t  1 T  t  1




A
(s ; a
) =


X
n  1A
(s
; a );
(17)










GAE
t  t
1


n
t
t





n=1















where 1    T    t  1  is a normalizing constant. Note that a higher    emphasizes advantage estimates with higher
    • 
values of n, and a lower does the opposite. Thus, serves as a control for the bias-variance tradeo , where increasing decreases bias and increases variance. In the in nite horizon case (T = 1), we can show:



1
1










X



A
(s
; a ) =




n  1A
(s
; a )
(18)











GAE
t
t




n
t
t




1

n=1






1








=
tX0
(  )t0 t t0;


(19)














=t







where we have omitted the derivation for brevity (see the GAE paper https://arxiv.org/pdf/1506.02438.pdf for details). In the nite horizon case, we can write:

T
1

AGAE(st; at) =
(  )t0 t t0;
(20)
tX0


=t


which serves as a way we can e ciently implement the generalized advantage estimator, since we can recursively compute:

AGAE(st; at) =  t +   AGAE(st+1; at+1)
(21)
Berkeley CS 285    Deep Reinforcement Learning, Decision Making, and Control    Fall 2021


    • Overview of Implementation

3.1    Files

To implement policy gradients, we will be building up the code that we started in homework 1. All les needed to run your code are in the hw2 folder, but there will be some blanks you will ll with your solutions from homework 1. These locations are marked with # TODO: get this from hw1 and are found in the following les:

    • infrastructure/rl trainer.py

    • infrastructure/utils.py

    • policies/MLP policy.py


After bringing in the required components from the previous homework, you can begin work on the new policy gradient code. These placeholders are marked with TODO, located in the following les:

    • agents/pg agent.py

    • policies/MLP policy.py


The script to run the experiments is found in scripts/run hw2.py (for the local option) or scripts/run hw2.ipynb (for the Colab option).


3.2    Overview

As in the previous homework, the main training loop is implemented in infrastructure/rl trainer.py.


The policy gradient algorithm uses the following 3 steps:

    1. Sample trajectories by generating rollouts under your current policy.

    2. Estimate returns and compute advantages. This is executed in the train function of pg agent.py

    3. Train/Update parameters. The computational graph for the policy and the baseline, as well as the update functions, are implemented in policies/MLP policy.py.


    • Implementing Policy Gradients

You will be implementing two di erent return estimators within pg agent.py. The rst (\Case 1" within calculate q vals) uses the discounted cumulative return of the full trajectory and corresponds to the \vanilla" form of the policy gradient (Equation 9):


T
1


r( i) =
t0
r(sit0; ait0):
(22)
tX0



=0


The second (\Case 2") uses the \reward-to-go" formulation from Equation 10:

T
1


r( i) =
t0
tr(sit0; ait0):
(23)
X
t0=t

Note that these di er only by the starting point of the summation.

Implement these return estimators as well as the remaining sections marked TODO in the code. For the small-scale experiments, you may skip those sections that are run only if nn baseline is True; we will return to baselines in Section 6. (These sections are in MLPPolicyPG:update and PGAgent:estimate advantage.)

Berkeley CS 285    Deep Reinforcement Learning, Decision Making, and Control    Fall 2021


    • Small-Scale Experiments

After you have implemented all non-baseline code from Section 4, you will run two small-scale experiments to get a feel for how di erent settings impact the performance of policy gradient methods.

Experiment 1 (CartPole). Run multiple experiments with the PG algorithm on the discrete CartPole-v0 environment, using the following commands:


python cs285/scripts/run_hw2.py --env_name CartPole-v0 -n 100 -b 1000 \ -dsa --exp_name q1_sb_no_rtg_dsa


python cs285/scripts/run_hw2.py --env_name CartPole-v0 -n 100 -b 1000 \ -rtg -dsa --exp_name q1_sb_rtg_dsa

python cs285/scripts/run_hw2.py --env_name CartPole-v0 -n 100 -b 1000 \ -rtg --exp_name q1_sb_rtg_na


python cs285/scripts/run_hw2.py --env_name CartPole-v0 -n 100 -b 5000 \ -dsa --exp_name q1_lb_no_rtg_dsa

python cs285/scripts/run_hw2.py --env_name CartPole-v0 -n 100 -b 5000 \ -rtg -dsa --exp_name q1_lb_rtg_dsa


python cs285/scripts/run_hw2.py --env_name CartPole-v0 -n 100 -b 5000 \ -rtg --exp_name q1_lb_rtg_na

What’s happening here:

    • -n : Number of iterations.

    • -b : Batch size (number of state-action pairs sampled while acting according to the current policy at each iteration).

    • -dsa : Flag: if present, sets standardize_advantages to False. Otherwise, by default, standardizes advantages to have a mean of zero and standard deviation of one.

    • -rtg : Flag: if present, sets reward_to_go=True. Otherwise, reward_to_go=False by default.

    • --exp_name : Name for experiment, which goes into the name for the data logging directory.

Various other command line arguments will allow you to set batch size, learning rate, network architecture, and more. You can change these as well, but keep them xed between the 6 experiments mentioned above.

Deliverables for report:

    • Create two graphs:

{ In the rst graph, compare the learning curves (average return at each iteration) for the experiments pre xed with q1_sb_. (The small batch experiments.)

{ In the second graph, compare the learning curves for the experiments pre xed with q1_lb_. (The large batch experiments.)

    • Answer the following questions brie y:

{ Which value estimator has better performance without advantage-standardization: the trajectory-centric one, or the one using reward-to-go?

{ Did advantage standardization help? { Did the batch size make an impact?
Berkeley CS 285    Deep Reinforcement Learning, Decision Making, and Control    Fall 2021



    • Provide the exact command line con gurations (or #@params settings in Colab) you used to run your experiments, including any parameters changed from their defaults.

What to Expect:

    • The best con guration of CartPole in both the large and small batch cases should converge to a maximum score of 200.

Experiment 2 (InvertedPendulum). Run experiments on the InvertedPendulum-v2 continuous control environment as follows:


python cs285/scripts/run_hw2.py --env_name InvertedPendulum-v2 \

--ep_len 1000 --discount 0.9 -n 100 -l 2 -s 64 -b <b*> -lr <r*> -rtg \

--exp_name q2_b<b*>_r<r*>

where your task is to nd the smallest batch size b* and largest learning rate r* that gets to optimum (maximum score of 1000) in less than 100 iterations. The policy performance may uctuate around 1000; this is ne. The precision of b* and r* need only be one signi cant digit.

Deliverables:

    • Given the b* and r* you found, provide a learning curve where the policy gets to optimum (maximum score of 1000) in less than 100 iterations. (This may be for a single random seed, or averaged over multiple.)

    • Provide the exact command line con gurations you used to run your experiments.

    • Implementing Neural Network Baselines

You will now implement a value function as a state-dependent neural network baseline. This will require lling in some TODO sections skipped in Section 4. In particular:

    • This neural network will be trained in the update method of MLPPolicyPG along with the policy gradient update.

• In pg

agent.py:estimate

advantage, the predictions of this network
will

be subtracted
from the
reward-to-go to yield an estimate of the advantage. This implements
P
T
1
t
0
tr(sit0
; ait0)

(sit).


t0=t




V

    • More Complex Experiments

Note: The following tasks take quite a bit of time to train. Please start early! For all remaining experiments, use the reward-to-go estimator.


Experiment 3 (LunarLander). You will now use your policy gradient implementation to learn a controller for LunarLanderContinuous-v2. The purpose of this problem is to test and help you debug your baseline implementation from Section 6.

Run the following command:


python cs285/scripts/run_hw2.py \

--env_name LunarLanderContinuous-v2 --ep_len 1000

--discount 0.99 -n 100 -l 2 -s 64 -b 40000 -lr 0.005 \ --reward_to_go --nn_baseline --exp_name q3_b40000_r0.005

Deliverables:

    • Plot a learning curve for the above command. You should expect to achieve an average return of around 180 by the end of training.
Berkeley CS 285    Deep Reinforcement Learning, Decision Making, and Control    Fall 2021



Experiment 4 (HalfCheetah). You will be using your policy gradient implementation to learn a controller for the HalfCheetah-v2 benchmark environment with an episode length of 150. This is shorter than the default episode length (1000), which speeds up training signi cantly. Search over batch sizes b 2 [10000; 30000; 50000] and learning rates r 2 [0:005; 0:01; 0:02] to replace <b> and <r> below.

python cs285/scripts/run_hw2.py --env_name HalfCheetah-v2 --ep_len 150 \ --discount 0.95 -n 100 -l 2 -s 32 -b <b> -lr <r> -rtg --nn_baseline \ --exp_name q4_search_b<b>_lr<r>_rtg_nnbaseline


Deliverables:

    • Provide a single plot with the learning curves for the HalfCheetah experiments that you tried. Describe in words how the batch size and learning rate a ected task performance.

Once you’ve found optimal values b* and r*, use them to run the following commands (replace the terms in angle brackets):


python cs285/scripts/run_hw2.py --env_name HalfCheetah-v2 --ep_len 150 \ --discount 0.95 -n 100 -l 2 -s 32 -b <b*> -lr <r*> \

--exp_name q4_b<b*>_r<r*>

python cs285/scripts/run_hw2.py --env_name HalfCheetah-v2 --ep_len 150 \ --discount 0.95 -n 100 -l 2 -s 32 -b <b*> -lr <r*> -rtg \ --exp_name q4_b<b*>_r<r*>_rtg


python cs285/scripts/run_hw2.py --env_name HalfCheetah-v2 --ep_len 150 \ --discount 0.95 -n 100 -l 2 -s 32 -b <b*> -lr <r*> --nn_baseline \ --exp_name q4_b<b*>_r<r*>_nnbaseline


python cs285/scripts/run_hw2.py --env_name HalfCheetah-v2 --ep_len 150 \ --discount 0.95 -n 100 -l 2 -s 32 -b <b*> -lr <r*> -rtg --nn_baseline \ --exp_name q4_b<b*>_r<r*>_rtg_nnbaseline

Deliverables: Provide a single plot with the learning curves for these four runs. The run with both reward-to-go and the baseline should achieve an average score close to 200.

    • Implementing Generalized Advantage Estimation

You will now use the value function you previously implemented to implement a simpli ed version of GAE- .

This will require    lling in the remaining TODO section in pg agent.py:estimate advantage.



Experiment 5 (HopperV2). You will now use your implementation of policy gradient with generalized advantage estimation to learn a controller for a version of Hopper-v2 with noisy actions. Search over 2 [0; 0:95; 0:99; 1] to replace < > below. Note that with a correct implementation, = 1 is equivalent to the vanilla neural network baseline estimator. Do not change any of the other hyperparameters (e.g. batch size, learning rate).


python cs285/scripts/run_hw2.py \

--env_name Hopper-v2 --ep_len 1000

--discount 0.99 -n 300 -l 2 -s 32 -b 2000 -lr 0.001 \

--reward_to_go --nn_baseline --action_noise_std 0.5 --gae_lambda < > \ --exp_name q5_b2000_r0.001_lambda< >

Deliverables:
Berkeley CS 285    Deep Reinforcement Learning, Decision Making, and Control    Fall 2021



    • Provide a single plot with the learning curves for the Hopper-v2 experiments that you tried. Describe in words how a ected task performance. The run with the best performance should achieve an average score close to 400.

    • Bonus!

Choose any (or all) of the following:

    • A serious bottleneck in the learning, for more complex environments, is the sample collection time. In infrastructure/rl trainer.py, we only collect trajectories in a single thread, but this process can be fully parallelized across threads to get a useful speedup. Implement the parallelization and report on the di erence in training time.

    • In PG, we collect a batch of data, estimate a single gradient, and then discard the data and move on. Can we potentially accelerate PG by taking multiple gradient descent steps with the same batch of data? Explore this option and report on your results. Set up a fair comparison between single-step PG and multi-step PG on at least one MuJoCo gym environment.


10    Submission

10.1    Submitting the PDF

Your report should be a document containing

    (a) All graphs and answers to short explanation questions requested for Experiments 1-4.

    (b) All command-line expressions you used to run your experiments.

    (c) (Optionally) Your bonus results (command-line expressions, graphs, and a few sentences that comment on your ndings).

10.2    Submitting the code and experiment runs

In order to turn in your code and experiment logs, create a folder that contains the following:

    • A folder named run logs with ll the experiment runs from this assignment. These folders can be copied directly from the cs285/d ta folder. Do not change the names originally assigned to the folders, as speci ed by expnamein the instructions. Video logging is disabled by default in the code, but if you turned it on for debugging, you need to run those again with --video log freq -1, or else the le size will be too large for submission.

    • The cs285 folder with all the .py les, with the same names and directory structure as the original homework repository (excluding the cs285/data folder). Also include any special instructions we need to run in order to produce each of your gures or tables in the form of a README le.


As an example, the unzipped version of your submission should result in the following le structure. Make sure that the submit.zip le is below 15MB.
Berkeley CS 285    Deep Reinforcement Learning, Decision Making, and Control    Fall 2021



submit.zip


 run logs


 q1 lb rtg na CartPole-v0 12-09-2019 17-53-4


 events.out.tfevents.1567529456.e3a096ac8ff4

 q3 b40000 r0.005 LunarLanderContinuous-v2 12-09-2019 00-17-58


 events.out.tfevents.1567529456.e3a096ac8ff4  ...

 cs285


 agents


 bc agent.py


 ...

 policies

 ...

 ...

 README.md

 ...

10.3    Turning it in

Turn in your assignment by the deadline on Gradescope. Uploade the zip le with your code to HW2 Code, and upload the PDF of your report to HW2.

More products