$29
Introduction
The goal of this assignment is to experiment with policy gradient and its variants, including variance reduction methods. Your goals will be to set up policy gradient for both contin-uous and discrete environments and experiment with variance reduction tricks, including implementing reward-to-go and neural network baselines.
Turn in your report and code for the full homework as described in Problem 2 by September 19, 2018.
2 Review
Recall that the reinforcement learning objective is to learn a that maximizes the objective function:
J( ) = E ( ) [r( )]
(1)
where
T
Yt
p(stjst 1; at 1) (atjst)
( ) = p(s1; a1; :::; sT ; aT ) = p(s1) (a1js1)
=2
and
T
Xt
r( ) = r(s1; a1; :::; sT ; aT ) =
r(st; at):
=1
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)
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)
(4)
N
=1
= N
t=1 r log (aitjsit)!
t=1 r(sit; ait)!
:
(5)
=1
1
N
T
T
Xi
X
X
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).
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, yielding following the modi ed objective, where the sum of rewards here is a sample estimate of the Q function, known as the \reward-to-go:"
r J( ) N
r log (aitjsit)
r(sit0
; ait0)!
:
(6)
1
N T
T
X X
Xt0
i=1 t=1
=t
Multiplying a discount factor to the rewards can be interpreted as encouraging the agent to focus on rewards closer in the future, which can also be thought of as a means for reducing variance (because there is more variance possible futures further into the future). We saw in lecture that the discount factor can be incorporated in two ways.
The rst way applies the discount on the rewards from full trajectory:
r J( ) N
=1
t=1 r log (aitjsit)!
t=1 t
1
N
T
T
Xi
X
X
and the second way applies the discount on the \reward-to-go:"
!
1r(sit; ait) (7)
r J( ) N
r log (aitjsit)
t0
tr(sit0; ait0)! :
(8)
1
N T
T
X X
Xt0
i=1 t=1
=t
.
2
We have seen in lecture that subtracting a baseline that is a constant with respect to from the sum of rewards
r J( ) = r E ( ) [r( ) b]
(9)
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. The value function is trained to approximate the sum of future rewards starting from a particular state:
T
V (st)
Xt0
[r(st0; at0) st] ;
(10)
E
j
=t
so the approximate policy gradient now looks like this:
r J( ) N
r log (aitjsit)
t0
tr(sit0; ait0)!
V (sit)! :(11)
1
N T
T
X X
Xt0
i=1 t=1
=t
Problem 1. State-dependent baseline: In lecture we saw that the policy gradient is unbiased if the baseline is a constant with respect to (Equation 9). The purpose of this problem is to help convince ourselves that subtracting a state-dependent baseline from the return keeps the policy gradient unbiased. For clarity we will use p ( ) instead of ( ), although they mean the same thing. Using the law of iterated expectations we will show that the policy gradient is still unbiased if the baseline b is function of a state at a particular timestep of (Equation 11). Recall from equation 3 that the policy gradient can be expressed as
E p ( ) [r log p ( )r( )] :
By breaking up p ( ) into dynamics and policy terms, we can discard the dynamics terms, which are not functions of :
T
T
!#
E p ( )
Xt
r log (atjst)
=1
X
r(st0; at0) :
t0=1
When we subtract a state dependent baseline b(st) (recall equation 11) we get
E p ( )
" T
r log (atjst)
T
r(st0; at0)!
b(st)!# :
X
Xt0
t=1
=1
3
Our goal for this problem is to show that
E p ( )
" =1 r log (atjst)b(st)#
= 0:
T
Xt
By linearity of expectation we can consider each term in this sum independently, so we can equivalently show that
T
X
E p ( ) [r log (atjst) (b(st))] = 0:
(12)
t=1
Using the chain rule, we can express p ( ) as a product of the state-action marginal (st; at) and the probability of the rest of the trajectory conditioned on (st; at) (which we denote as ( =st; atjst; at)):
p ( ) = p (st; at)p ( =st; atjst; at)
Please show equation 12 by using the law of iterated expectations, breaking E p ( ) by decoupling the state-action marginal from the rest of the trajectory.
Alternatively, we can consider the structure of the MDP and express p ( ) as a prod-uct of the trajectory distribution up to st (which we denote as (s1:t; a1:t 1)) and the trajectory distribution after st conditioned on the rst part (which we denote as
(st+1:T ; at:T js1:t; a1:t 1)):
p ( ) = p (s1:t; a1:t 1)p (st+1:T ; at:T js1:t; a1:t 1)
Explain why, for the inner expectation, conditioning on (s1; a1; :::; at 1; st ) is equivalent to conditioning only on st .
Please show equation 12 by using the law of iterated expectations, breaking E p ( ) by decoupling trajectory up to st from the trajectory after st.
Since the policy gradient with respect to can be decoupled as a summation of terms over timesteps t 2 [1; T ], because we have shown that the policy gradient is unbiased for each of these terms, the entire policy gradient is also unbiased with respect to a vector of state-dependent baselines over the timesteps: [b(s1); b(s2); :::b(sT )].
Code Setup
3.1 Files
The starter code is available here. The only le you need to modify in this homework is train_pg_f18.py. The les logz.py and plots.py are utility les; while you should
4
look at them to understand their functionality, you will not modify them. For the Lunar Lan-
der task, use the provided lunar_lander.py le instead of gym/envs/box2d/lunar_lander.py. After you ll in the appropriate methods, you should be able to just run python train_pg_f18.py with some command line options to perform the experiments. To visualize the results, you can run python plot.py path/to/logdir.
3.2 Overview
The function train_PG is used to perform the actual training for policy gradient. The pa-rameters passed into this function specify the algorithm’s hyperparameters and environment. The Agent class contains methods that de ne the computation graph, sample trajectories, estimate returns, and update the parameters of the policy.
At a high level, the data ow of the code is structured like this:
Build a static computation graph in Tensor ow.
Set up a Tensor ow session to initialize the parameters of the computation graph. This is the only time you need to set up the session.
Then we will repeat Steps 3 through 5 for N iterations:
Sample trajectories by executing the Tensor ow op that samples an action given an observation from the environment. Collect the states, actions, and rewards as numpy variables.
Estimate returns in numpy (estimated Q values, baseline predictions, advantages).
Update parameters by executing the Tensor ow op that updates the parameters given what you computed in Step 4.
Constructing the computation graph
Problem 2. Neural networks: We will now begin to implement a neural network that parametrizes .
Implement the utility function, build_mlp, which will build a feedforward neural network with fully connected units (Hint: use tf.layers.dense). Test it to make sure that it produces outputs of the expected size and shape. You do not need to include anything in your write-up about this, it will just make your life easier.
Next, implement the method Agent.build_computation_graph. At this point, you only need to implement the parts with the \Problem 2" header.
5
De ne the placeholder for the advantages in Agent.define_placeholders. We have already de ned placeholders for the observations and actions. The ad-vantages will correspond to r( ) in the policy gradient, which may or may not include a subtracted baseline value.
Create the symbolic operation Agent.policy forward pass: This outputs the parameters of a distribution (ajs). In this homework, when the distribu-tion is over discrete actions these parameters will be the logits of a categorical distribution, and when the distribution is over continuous actions these parame-ters will be the mean and the log standard deviation of a multivariate Gaussian distribution. This operation will be an input to Agent.sample action and Agent.get log prob.
Create the symbolic operation Agent.sample action: This produces a Ten-sor ow op, self.sy sampled ac that samples an action from (ajs). This operation will be called in Agent.sample trajectories.
Create the symbolic operation Agent.get log prob: Given an action that the agent took in the environment, this computes the log probability of that action under (ajs). This will be used in the loss function.
In Agent.build_computation_graph implement a loss function (which call use the result from Agent.get log prob) to whose gradient is
1
N
Xi
r J( )
N
r log ( i)r( i):
=1
Implement Policy Gradient
5.1 Implementing the policy gradient loop
Problem 3. Policy Gradient: Recall from lecture that an RL algorithm can viewed as consisting of three parts, which are re ected in the training loop of train_PG:
Agent.sample_trajectories: Generate samples (e.g. run the policy to collect trajectories consisting of state transitions (s; a; s0; r))
Agent.estimate_return: Estimate the return (e.g. sum together discounted re-wards from the trajectories, or learn a model that predicts expected total future dis-counted reward)
Agent.update_parameters: Improve the policy (e.g. update the parameters of the policy with policy gradient)
6
In our implementation, for clarity we will update the parameters of the value function baseline also in the third step (Agent.update_parameters), rather than in the second step (as was described in lecture). You only need to implement the parts with the \Problem 3" header.
Sample trajectories: In Agent.sample trajectories, use the Tensor ow ses-sion to call self.sy sampled ac to sample an action given an observation from the environment.
Estimate return: We will now implement r( ) from Equation 1. Please implement the method Agent.sum_of_rewards, which will return a sample estimate of the discounted return, for both the full-trajectory (Equation 7) case, where
T
r( i) = X t0 1r(sit; ait)
t=1
and for the \reward-to-go" case (Equation 8) where
T
r( i) = X t0 tr(sit0; ait0):
t0=t
In Agent.estimate_return, normalize the advantages to have a mean of zero and a standard deviation of one. This is a trick for reducing variance.
Update parameters: In Agent.update_parameters use the Tensor ow session to call the update operation self.update op to update the parameters of the policy. You will need to gure out the inputs to feed dict.
5.2 Experiments
After you have implemented the code, we will run experiments to get a feel for how di erent settings impact the performance of policy gradient methods.
Problem 4. CartPole: Run the PG algorithm in the discrete CartPole-v0 environment from the command line as follows:
python train_pg_f18.py CartPole-v0 -n 100 -b 1000 -e 3 -dna --exp_name sb_no_rtg_dna
python train_pg_f18.py CartPole-v0 -n 100 -b 1000 -e 3 -rtg -dna --exp_name sb_rtg_dna
python train_pg_f18.py CartPole-v0 -n 100 -b 1000 -e 3 -rtg --exp_name sb_rtg_na
python train_pg_f18.py CartPole-v0 -n 100 -b 5000 -e 3 -dna --exp_name lb_no_rtg_dna
python train_pg_f18.py CartPole-v0 -n 100 -b 5000 -e 3 -rtg -dna --exp_name lb_rtg_dna
python train_pg_f18.py CartPole-v0 -n 100 -b 5000 -e 3 -rtg --exp_name lb_rtg_na
7
What’s happening there:
-n : Number of iterations.
-b : Batch size (number of state-action pairs sampled while acting according to the current policy at each iteration).
-e : Number of experiments to run with the same con guration. Each experiment will start with a di erent randomly initialized policy, and have a di erent stream of random numbers.
-dna : Flag: if present, sets normalize_advantages to False. Otherwise, by default, normalize_advantages=True.
-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 directory.
Various other command line arguments will allow you to set batch size, learning rate, network architecture (number of hidden layers and the size of the hidden layers|for CartPole, you can use one hidden layer with 32 units), and more.
Deliverables for report:
Graph the results of your experiments using the plot.py le we provide. Create two graphs.
{ In the rst graph, compare the learning curves (average return at each iteration) for the experiments pre xed with sb_. (The small batch experiments.)
{ In the second graph, compare the learning curves for the experiments pre xed with lb_. (The large batch experiments.)
Answer the following questions brie y:
{ Which gradient estimator has better performance without advantage-centering| the trajectory-centric one, or the one using reward-to-go?
{ Did advantage centering help?
{ Did the batch size make an impact?
Provide the exact command line con gurations you used to run your experiments. (To verify batch size, learning rate, architecture, and so on.)
What to Expect:
The best con guration of CartPole in both the large and small batch cases converge to a maximum score of 200.
8
Problem 5. InvertedPendulum: Run experiments in InvertedPendulum-v2 contin-uous control environment as follows:
python train_pg_f18.py InvertedPendulum-v2 -ep 1000 --discount 0.9 -n 100 -e 3
-l 2 -s 64 -b <b* -lr <r* -rtg --exp_name hc_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.
Implement Neural Network Baselines
For the rest of the assignment we will use \reward-to-go."
Problem 6. Neural network baseline: We will now implement a value function as a state-dependent neural network baseline. The sections in the code are marked by \Problem 6."
In Agent.build_computation_graph implement V , a neural network that pre-dicts the expected return conditioned on a state. Also implement the loss function to train this network and its update operation self.baseline op.
In Agent.compute_advantage, use the neural network to predict the expected state-conditioned return (use the session to call self.baseline prediction), nor-malize it to match the statistics of the current batch of \reward-to-go", and subtract this value from the \reward-to-go" to yield an estimate of the advantage. This imple-
ments
Pt0
=t t0
tr(sit0; ait0)
V (sit).
T
In Agent.update_parameters, update the parameters of the the neural network baseline by using the Tensor ow session to call self.baseline op. \Rescale" the target values for the neural network baseline to have a mean of zero and a standard deviation of one.
9
More Complex Tasks
Note: The following tasks would take quite a bit of time to train. Please start early!
Problem 7: LunarLander For this problem, you will use your policy gradient implementa-tion to solve LunarLanderContinuous-v2. Use an episode length of 1000. The purpose of this problem is to help you debug your baseline implementation. Run the following com-mand:
python train_pg_f18.py LunarLanderContinuous-v2 -ep 1000 --discount 0.99 -n 100 -e 3 -l 2 -s 64 -b 40000 -lr 0.005 -rtg --nn_baseline --exp_name ll_b40000_r0.005
Deliverables:
Plot a learning curve for the above command. You should expect to achieve an average return of around 180.
Problem 8: HalfCheetah For this problem, you will use your policy gradient implemen-tation to solve HalfCheetah-v2. Use an episode length of 150, which is shorter than the default of 1000 for HalfCheetah (which would speed up your 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 train_pg_f18.py HalfCheetah-v2 -ep 150 --discount 0.9 -n 100 -e 3 -l 2 -s 32 -b <b -lr <r --exp_name hc_b<b_r<r
Deliverables:
How did the batch size and learning rate a ect the performance?
Once you’ve found suitable values of b and r among those choices (let’s call them b* and r*), use b* and r* and run the following commands (remember to replace the terms in the angle brackets):
python train_pg_f18.py HalfCheetah-v2 -ep 150 --discount 0.9 -n 100 -e 3 -l 2 -s 32 -b <b* -lr <r* --exp_name hc_b<b*_r<r*
python train_pg_f18.py HalfCheetah-v2 -ep 150 --discount 0.9 -n 100 -e 3 -l 2 -s 32 -b <b* -lr <r* -rtg --exp_name hc_b<b*_r<r*
python train_pg_f18.py HalfCheetah-v2 -ep 150 --discount 0.9 -n 100 -e 3 -l 2 -s 32 -b <b* -lr <r* --nn_baseline --exp_name hc_b<b*_r<r* python train_pg_f18.py HalfCheetah-v2 -ep 150 --discount 0.9 -n 100 -e 3
-l 2 -s 32 -b <b* -lr <r* -rtg --nn_baseline --exp_name hc_b<b*_r<r *
The run with reward-to-go and the baseline should achieve an average score close to 200. Provide a single plot plotting the learning curves for all four runs.
NOTE: In an earlier version of the homework (before 9/13/18) we had the discount as 0.9, in which case the run with reward-to-go and baseline should achieve an average
10
score close to 150, and there would not be much di erence between the runs with reward-to-go (with or without baseline). If you have already done this part with a discount of 0.9, you do not need to redo the problem, but you would then expect the best scoring run to be about 150 rather than 200.
Bonus!
Choose any (or all) of the following:
A serious bottleneck in the learning, for more complex environments, is the sample col-lection time. In train_pg_f18.py, we only collect trajectories in a single thread, but this process can be fully parallelized across threads to get a useful speedup. Im-plement the parallelization and report on the di erence in training time.
Implement GAE- for advantage estimation.1 Run experiments in a MuJoCo gym environment to explore whether this speeds up training. (Walker2d-v1 may be good for this.)
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.
Submission
Your report should be a document containing
Your mathematical response (written in LATEX) for Problem 1.
All graphs requested in Problems 4, 5, 7, and 8.
Answers to short explanation questions in section 5 and 7.
All command-line expressions you used to run your experiments.
(Optionally) Your bonus results (command-line expressions, graphs, and a few sen-tences that comment on your ndings).
Please also submit your modi ed train_pg_f18.py le. If your code includes additional les, provide a zip le including your train_pg_f18.py and all other les needed to run
1https://arxiv.org/abs/1506.02438
11
your code. Please include a README.md with instructions needed to exactly duplicate your results (including command-line expressions).
Turn in your assignment by September 19th 11:59pm on Gradescope. Uploade the zip le with your code to HW2 Code, and upload the PDF of your report to HW2.
12