0% found this document useful (0 votes)
2 views

lecture-06

The document provides an overview of supervised, unsupervised, and reinforcement learning, focusing on reinforcement learning's goal of maximizing rewards through agent-environment interactions. It details the Markov Decision Process, value functions, Q-learning, and the architecture of Q-networks used in deep Q-learning. Additionally, it discusses the importance of experience replay for efficient learning in training Q-networks.

Uploaded by

dsmlab986
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

lecture-06

The document provides an overview of supervised, unsupervised, and reinforcement learning, focusing on reinforcement learning's goal of maximizing rewards through agent-environment interactions. It details the Markov Decision Process, value functions, Q-learning, and the architecture of Q-networks used in deep Q-learning. Additionally, it discusses the importance of experience replay for efficient learning in training Q-networks.

Uploaded by

dsmlab986
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 98

So far… Supervised Learning

Data: (x, y)
x is data, y is label

Goal: Learn a function to map x -> y Cat

Examples: Classification,
regression, object detection,
semantic segmentation, image Classification
captioning, etc.

This image is CC0 public domain


So far… Unsupervised Learning

Data: x
Just data, no labels!
1-d density estimation
Goal: Learn some underlying
hidden structure of the data

Examples: Clustering,
dimensionality reduction, feature
learning, density estimation, etc. 2-d density estimation
2-d density images left and right
are CC0 public domain
Today: Reinforcement Learning

Problems involving an agent


interacting with an environment,
which provides numeric reward
signals

Goal: Learn how to take actions


in order to maximize reward
Overview

- What is Reinforcement Learning?


- Markov Decision Processes
- Q-Learning
- Policy Gradients
Reinforcement Learning

Agent

Environment
Reinforcement Learning

Agent

State st

Environment
Reinforcement Learning

Agent

State st
Action at

Environment
Reinforcement Learning

Agent

State st Reward rt Action at

Environment
Reinforcement Learning

Agent

State st Reward rt Action at


Next state st+1

Environment
Cart-Pole Problem

Objective: Balance a pole on top of a movable cart

State: angle, angular speed, position, horizontal velocity


Action: horizontal force applied on the cart
Reward: 1 at each time step if the pole is upright

This image is CC0 public domain


Robot Locomotion

Objective: Make the robot move forward

State: Angle and position of the joints


Action: Torques applied on joints
Reward: 1 at each time step upright +
forward movement
Atari Games

Objective: Complete the game with the highest score

State: Raw pixel inputs of the game state


Action: Game controls e.g. Left, Right, Up, Down
Reward: Score increase/decrease at each time step
Go

Objective: Win the game!

State: Position of all pieces


Action: Where to put the next piece down
Reward: 1 if win at the end of the game, 0 otherwise

This image is CC0 public domain


How can we mathematically formalize the RL problem?

Agent

State st Reward rt Action at


Next state st+1

Environment
Markov Decision Process
- Mathematical formulation of the RL problem
- Markov property: Current state completely characterises the state of the
world

Defined by:

: set of possible states


: set of possible actions
: distribution of reward given (state, action) pair
: transition probability i.e. distribution over next state given (state, action) pair
: discount factor
Markov Decision Process
- At time step t=0, environment samples initial state s0 ~ p(s0)
- Then, for t=0 until done:
- Agent selects action at
- Environment samples reward rt ~ R( . | st, at)
- Environment samples next state st+1 ~ P( . | st, at)
- Agent receives reward rt and next state st+1

- A policy is a function from S to A that specifies what action to take in


each state
- Objective: find policy * that maximizes cumulative discounted reward:
A simple MDP: Grid World
states
actions = {
1. right ★
2. left Set a negative “reward”
★ for each transition
3. up
(e.g. r = -1)
4. down
}

Objective: reach one of terminal states (greyed out) in


least number of actions
A simple MDP: Grid World

★ ★

★ ★

Random Policy Optimal Policy


The optimal policy *
We want to find optimal policy * that maximizes the sum of rewards.

How do we handle the randomness (initial state, transition probability…)?


The optimal policy *
We want to find optimal policy * that maximizes the sum of rewards.

How do we handle the randomness (initial state, transition probability…)?


Maximize the expected sum of rewards!

Formally: with
Definitions: Value function and Q-value function
Following a policy produces sample trajectories (or paths) s0, a0, r0, s1, a1, r1, …
Definitions: Value function and Q-value function
Following a policy produces sample trajectories (or paths) s0, a0, r0, s1, a1, r1, …

How good is a state?


The value function at state s, is the expected cumulative reward from following the policy
from state s:
Definitions: Value function and Q-value function
Following a policy produces sample trajectories (or paths) s0, a0, r0, s1, a1, r1, …

How good is a state?


The value function at state s, is the expected cumulative reward from following the policy
from state s:

How good is a state-action pair?


The Q-value function at state s and action a, is the expected cumulative reward from
taking action a in state s and then following the policy:
Bellman equation
The optimal Q-value function Q* is the maximum expected cumulative reward achievable
from a given (state, action) pair:
Bellman equation
The optimal Q-value function Q* is the maximum expected cumulative reward achievable
from a given (state, action) pair:

Q* satisfies the following Bellman equation:

Intuition: if the optimal state-action values for the next time-step Q*(s’,a’) are known,
then the optimal strategy is to take the action that maximizes the expected value of
Bellman equation
The optimal Q-value function Q* is the maximum expected cumulative reward achievable
from a given (state, action) pair:

Q* satisfies the following Bellman equation:

Intuition: if the optimal state-action values for the next time-step Q*(s’,a’) are known,
then the optimal strategy is to take the action that maximizes the expected value of

The optimal policy * corresponds to taking the best action in any state as specified by Q*
Solving for the optimal policy
Value iteration algorithm: Use Bellman equation as an iterative update

Qi will converge to Q* as i -> infinity


Solving for the optimal policy
Value iteration algorithm: Use Bellman equation as an iterative update

Qi will converge to Q* as i -> infinity

What’s the problem with this?


Solving for the optimal policy
Value iteration algorithm: Use Bellman equation as an iterative update

Qi will converge to Q* as i -> infinity

What’s the problem with this?


Not scalable. Must compute Q(s,a) for every state-action pair. If state is e.g. current game state
pixels, computationally infeasible to compute for entire state space!
Solving for the optimal policy
Value iteration algorithm: Use Bellman equation as an iterative update

Qi will converge to Q* as i -> infinity

What’s the problem with this?


Not scalable. Must compute Q(s,a) for every state-action pair. If state is e.g. current game state
pixels, computationally infeasible to compute for entire state space!

Solution: use a function approximator to estimate Q(s,a). E.g. a neural network!


Solving for the optimal policy: Q-learning
Q-learning: Use a function approximator to estimate the action-value function
Solving for the optimal policy: Q-learning
Q-learning: Use a function approximator to estimate the action-value function

If the function approximator is a deep neural network => deep q-learning!


Solving for the optimal policy: Q-learning
Q-learning: Use a function approximator to estimate the action-value function

function parameters (weights)


If the function approximator is a deep neural network => deep q-learning!
Solving for the optimal policy: Q-learning
Remember: want to find a Q-function that satisfies the Bellman Equation:
Solving for the optimal policy: Q-learning
Remember: want to find a Q-function that satisfies the Bellman Equation:

Forward Pass
Loss function:

where
Solving for the optimal policy: Q-learning
Remember: want to find a Q-function that satisfies the Bellman Equation:

Forward Pass
Loss function:

where

Backward Pass
Gradient update (with respect to Q-function parameters θ):
Solving for the optimal policy: Q-learning
Remember: want to find a Q-function that satisfies the Bellman Equation:

Forward Pass
Loss function:
Iteratively try to make the Q-value
where close to the target value (yi) it
should have, if Q-function
corresponds to optimal Q* (and
Backward Pass optimal policy *)
Gradient update (with respect to Q-function parameters θ):
[Mnih et al. NIPS Workshop 2013; Nature 2015]

Case Study: Playing Atari Games

Objective: Complete the game with the highest score

State: Raw pixel inputs of the game state


Action: Game controls e.g. Left, Right, Up, Down
Reward: Score increase/decrease at each time step
[Mnih et al. NIPS Workshop 2013; Nature 2015]

Q-network Architecture
:
FC-4 (Q-values)
neural network
with weights FC-256

32 4x4 conv, stride 2

16 8x8 conv, stride 4

Current state st: 84x84x4 stack of last 4 frames


(after RGB->grayscale conversion, downsampling, and cropping)
[Mnih et al. NIPS Workshop 2013; Nature 2015]

Q-network Architecture
:
FC-4 (Q-values)
neural network
with weights FC-256

32 4x4 conv, stride 2

16 8x8 conv, stride 4

Input: state st

Current state st: 84x84x4 stack of last 4 frames


(after RGB->grayscale conversion, downsampling, and cropping)
[Mnih et al. NIPS Workshop 2013; Nature 2015]

Q-network Architecture
:
FC-4 (Q-values)
neural network
with weights FC-256

32 4x4 conv, stride 2


Familiar conv layers,
FC layer
16 8x8 conv, stride 4

Current state st: 84x84x4 stack of last 4 frames


(after RGB->grayscale conversion, downsampling, and cropping)
[Mnih et al. NIPS Workshop 2013; Nature 2015]

Q-network Architecture
: Last FC layer has 4-d
FC-4 (Q-values)
neural network output (if 4 actions),
with weights FC-256 corresponding to Q(st,
a1), Q(st, a2), Q(st, a3),
32 4x4 conv, stride 2 Q(st,a4)
16 8x8 conv, stride 4

Current state st: 84x84x4 stack of last 4 frames


(after RGB->grayscale conversion, downsampling, and cropping)
[Mnih et al. NIPS Workshop 2013; Nature 2015]

Q-network Architecture
: Last FC layer has 4-d
FC-4 (Q-values)
neural network output (if 4 actions),
with weights FC-256 corresponding to Q(st,
a1), Q(st, a2), Q(st, a3),
32 4x4 conv, stride 2 Q(st,a4)
16 8x8 conv, stride 4
Number of actions between 4-18
depending on Atari game

Current state st: 84x84x4 stack of last 4 frames


(after RGB->grayscale conversion, downsampling, and cropping)
[Mnih et al. NIPS Workshop 2013; Nature 2015]

Q-network Architecture
: Last FC layer has 4-d
FC-4 (Q-values)
neural network output (if 4 actions),
with weights FC-256 corresponding to Q(st,
a1), Q(st, a2), Q(st, a3),
32 4x4 conv, stride 2 Q(st,a4)
A single feedforward pass
to compute Q-values for all 16 8x8 conv, stride 4
actions from the current Number of actions between 4-18
state => efficient! depending on Atari game

Current state st: 84x84x4 stack of last 4 frames


(after RGB->grayscale conversion, downsampling, and cropping)
[Mnih et al. NIPS Workshop 2013; Nature 2015]

Training the Q-network: Loss function (from before)


Remember: want to find a Q-function that satisfies the Bellman Equation:

Forward Pass
Loss function:
Iteratively try to make the Q-value
where close to the target value (yi) it
should have, if Q-function
corresponds to optimal Q* (and
Backward Pass optimal policy *)
Gradient update (with respect to Q-function parameters θ):
[Mnih et al. NIPS Workshop 2013; Nature 2015]

Training the Q-network: Experience Replay


Learning from batches of consecutive samples is problematic:
- Samples are correlated => inefficient learning
- Current Q-network parameters determines next training samples (e.g. if maximizing
action is to move left, training samples will be dominated by samples from left-hand
size) => can lead to bad feedback loops
[Mnih et al. NIPS Workshop 2013; Nature 2015]

Training the Q-network: Experience Replay


Learning from batches of consecutive samples is problematic:
- Samples are correlated => inefficient learning
- Current Q-network parameters determines next training samples (e.g. if maximizing
action is to move left, training samples will be dominated by samples from left-hand
size) => can lead to bad feedback loops

Address these problems using experience replay


- Continually update a replay memory table of transitions (st, at, rt, st+1) as game
(experience) episodes are played
- Train Q-network on random minibatches of transitions from the replay memory,
instead of consecutive samples
[Mnih et al. NIPS Workshop 2013; Nature 2015]

Training the Q-network: Experience Replay


Learning from batches of consecutive samples is problematic:
- Samples are correlated => inefficient learning
- Current Q-network parameters determines next training samples (e.g. if maximizing
action is to move left, training samples will be dominated by samples from left-hand
size) => can lead to bad feedback loops

Address these problems using experience replay


- Continually update a replay memory table of transitions (st, at, rt, st+1) as game
(experience) episodes are played
- Train Q-network on random minibatches of transitions from the replay memory,
instead of consecutive samples
Each transition can also contribute
to multiple weight updates
=> greater data efficiency
[Mnih et al. NIPS Workshop 2013; Nature 2015]

Putting it together: Deep Q-Learning with Experience Replay


[Mnih et al. NIPS Workshop 2013; Nature 2015]

Putting it together: Deep Q-Learning with Experience Replay

Initialize replay memory, Q-network


[Mnih et al. NIPS Workshop 2013; Nature 2015]

Putting it together: Deep Q-Learning with Experience Replay

Play M episodes (full games)


[Mnih et al. NIPS Workshop 2013; Nature 2015]

Putting it together: Deep Q-Learning with Experience Replay

Initialize state
(starting game
screen pixels) at the
beginning of each
episode
[Mnih et al. NIPS Workshop 2013; Nature 2015]

Putting it together: Deep Q-Learning with Experience Replay

For each timestep t


of the game
[Mnih et al. NIPS Workshop 2013; Nature 2015]

Putting it together: Deep Q-Learning with Experience Replay

With small probability,


select a random
action (explore),
otherwise select
greedy action from
current policy
[Mnih et al. NIPS Workshop 2013; Nature 2015]

Putting it together: Deep Q-Learning with Experience Replay

Take the action (at),


and observe the
reward rt and next
state st+1
[Mnih et al. NIPS Workshop 2013; Nature 2015]

Putting it together: Deep Q-Learning with Experience Replay

Store transition in
replay memory
[Mnih et al. NIPS Workshop 2013; Nature 2015]

Putting it together: Deep Q-Learning with Experience Replay

Experience Replay:
Sample a random
minibatch of transitions
from replay memory
and perform a gradient
descent step
https://www.youtube.com/watch?v=V1eYniJ0Rnk
Video by Károly Zsolnai-Fehér. Reproduced with permission.
Policy Gradients
What is a problem with Q-learning?
The Q-function can be very complicated!

Example: a robot grasping an object has a very high-dimensional state => hard
to learn exact value of every (state, action) pair
Policy Gradients
What is a problem with Q-learning?
The Q-function can be very complicated!

Example: a robot grasping an object has a very high-dimensional state => hard
to learn exact value of every (state, action) pair

But the policy can be much simpler: just close your hand
Can we learn a policy directly, e.g. finding the best policy from a collection of
policies?
Policy Gradients
Formally, let’s define a class of parametrized policies:

For each policy, define its value:


Policy Gradients
Formally, let’s define a class of parametrized policies:

For each policy, define its value:

We want to find the optimal policy

How can we do this?


Policy Gradients
Formally, let’s define a class of parametrized policies:

For each policy, define its value:

We want to find the optimal policy

How can we do this?


Gradient ascent on policy parameters!
REINFORCE algorithm
Mathematically, we can write:

Where r( ) is the reward of a trajectory


REINFORCE algorithm
Expected reward:
REINFORCE algorithm
Expected reward:

Now let’s differentiate this:


REINFORCE algorithm
Expected reward:

Now let’s differentiate this: Intractable! Gradient of an


expectation is problematic when p
depends on θ
REINFORCE algorithm
Expected reward:

Now let’s differentiate this: Intractable! Gradient of an


expectation is problematic when p
depends on θ

However, we can use a nice trick:


REINFORCE algorithm
Expected reward:

Now let’s differentiate this: Intractable! Gradient of an


expectation is problematic when p
depends on θ

However, we can use a nice trick:


If we inject this back:

Can estimate with


Monte Carlo sampling
REINFORCE algorithm
Can we compute those quantities without knowing the transition probabilities?

We have:
REINFORCE algorithm
Can we compute those quantities without knowing the transition probabilities?

We have:

Thus:
REINFORCE algorithm
Can we compute those quantities without knowing the transition probabilities?

We have:

Thus:
Doesn’t depend on
And when differentiating: transition probabilities!
REINFORCE algorithm
Can we compute those quantities without knowing the transition probabilities?

We have:

Thus:
Doesn’t depend on
And when differentiating: transition probabilities!

Therefore when sampling a trajectory , we can estimate J( ) with


Intuition
Gradient estimator:

Interpretation:
- If r( ) is high, push up the probabilities of the actions seen
- If r( ) is low, push down the probabilities of the actions seen
Intuition
Gradient estimator:

Interpretation:
- If r( ) is high, push up the probabilities of the actions seen
- If r( ) is low, push down the probabilities of the actions seen

Might seem simplistic to say that if a trajectory is good then all its actions were
good. But in expectation, it averages out!
Intuition
Gradient estimator:

Interpretation:
- If r( ) is high, push up the probabilities of the actions seen
- If r( ) is low, push down the probabilities of the actions seen

Might seem simplistic to say that if a trajectory is good then all its actions were
good. But in expectation, it averages out!

However, this also suffers from high variance because credit assignment is
really hard. Can we help the estimator?
Variance reduction
Gradient estimator:
Variance reduction
Gradient estimator:

First idea: Push up probabilities of an action seen, only by the cumulative


future reward from that state
Variance reduction
Gradient estimator:

First idea: Push up probabilities of an action seen, only by the cumulative


future reward from that state

Second idea: Use discount factor to ignore delayed effects


Variance reduction: Baseline
Problem: The raw value of a trajectory isn’t necessarily meaningful. For
example, if rewards are all positive, you keep pushing up probabilities of
actions.

What is important then? Whether a reward is better or worse than what you
expect to get

Idea: Introduce a baseline function dependent on the state.


Concretely, estimator is now:
How to choose the baseline?

A simple baseline: constant moving average of rewards experienced so far


from all trajectories
How to choose the baseline?

A simple baseline: constant moving average of rewards experienced so far


from all trajectories

Variance reduction techniques seen so far are typically used in “Vanilla


REINFORCE”
How to choose the baseline?
A better baseline: Want to push up the probability of an action from a state, if
this action was better than the expected value of what we should get from
that state.

Q: What does this remind you of?


How to choose the baseline?
A better baseline: Want to push up the probability of an action from a state, if
this action was better than the expected value of what we should get from
that state.

Q: What does this remind you of?

A: Q-function and value function!


How to choose the baseline?
A better baseline: Want to push up the probability of an action from a state, if
this action was better than the expected value of what we should get from
that state.

Q: What does this remind you of?

A: Q-function and value function!


Intuitively, we are happy with an action at in a state st if
is large. On the contrary, we are unhappy with an action if it’s small.
How to choose the baseline?
A better baseline: Want to push up the probability of an action from a state, if
this action was better than the expected value of what we should get from
that state.

Q: What does this remind you of?

A: Q-function and value function!


Intuitively, we are happy with an action at in a state st if
is large. On the contrary, we are unhappy with an action if it’s small.

Using this, we get the estimator:


Actor-Critic Algorithm
Problem: we don’t know Q and V. Can we learn them?

Yes, using Q-learning! We can combine Policy Gradients and Q-learning


by training both an actor (the policy) and a critic (the Q-function).

- The actor decides which action to take, and the critic tells the actor
how good its action was and how it should adjust
- Also alleviates the task of the critic as it only has to learn the values
of (state, action) pairs generated by the policy
- Can also incorporate Q-learning tricks e.g. experience replay
- Remark: we can define by the advantage function how much an
action was better than expected
Actor-Critic Algorithm
Initialize policy parameters , critic parameters
For iteration=1, 2 … do
Sample m trajectories under the current policy

For i=1, …, m do
For t=1, ... , T do

End for
REINFORCE in action: Recurrent Attention Model (RAM)
Objective: Image Classification

Take a sequence of “glimpses” selectively focusing on regions of the


image, to predict class
- Inspiration from human perception and eye movements
- Saves computational resources => scalability
- Able to ignore clutter / irrelevant parts of image

State: Glimpses seen so far


Action: (x,y) coordinates (center of glimpse) of where to look next in image
Reward: 1 at the final timestep if image correctly classified, 0 otherwise
glimpse

[Mnih et al. 2014]


REINFORCE in action: Recurrent Attention Model (RAM)
Objective: Image Classification

Take a sequence of “glimpses” selectively focusing on regions of the


image, to predict class
- Inspiration from human perception and eye movements
- Saves computational resources => scalability
- Able to ignore clutter / irrelevant parts of image

State: Glimpses seen so far


Action: (x,y) coordinates (center of glimpse) of where to look next in image
Reward: 1 at the final timestep if image correctly classified, 0 otherwise
glimpse

Glimpsing is a non-differentiable operation => learn policy for how to take glimpse actions using REINFORCE
Given state of glimpses seen so far, use RNN to model the state and output next action
[Mnih et al. 2014]
REINFORCE in action: Recurrent Attention Model (RAM)

(x1, y1)

Input NN
image

[Mnih et al. 2014]


REINFORCE in action: Recurrent Attention Model (RAM)

(x1, y1) (x2, y2)

Input NN NN
image

[Mnih et al. 2014]


REINFORCE in action: Recurrent Attention Model (RAM)

(x1, y1) (x2, y2) (x3, y3)

Input NN NN NN
image

[Mnih et al. 2014]


REINFORCE in action: Recurrent Attention Model (RAM)

(x1, y1) (x2, y2) (x3, y3) (x4, y4)

Input NN NN NN NN
image

[Mnih et al. 2014]


REINFORCE in action: Recurrent Attention Model (RAM)

(x1, y1) (x2, y2) (x3, y3) (x4, y4) (x5, y5)

Softmax

Input NN NN NN NN NN y=2
image

[Mnih et al. 2014]


REINFORCE in action: Recurrent Attention Model (RAM)

Has also been used in many other tasks including fine-grained image recognition,
image captioning, and visual question-answering!

[Mnih et al. 2014]


More policy gradients: AlphaGo
Overview:
- Mix of supervised learning and reinforcement learning
- Mix of old methods (Monte Carlo Tree Search) and
recent ones (deep RL)

How to beat the Go world champion:


- Featurize the board (stone color, move legality, bias, …)
- Initialize policy network with supervised training from professional go games,
then continue training using policy gradient (play against itself from random
previous iterations, +1 / -1 reward for winning / losing)
- Also learn value network (critic)
- Finally, combine combine policy and value networks in a Monte Carlo Tree [Silver et al.,
Search algorithm to select actions by lookahead search Nature 2016]
This image is CC0 public domain
Summary
- Policy gradients: very general but suffer from high variance so
requires a lot of samples. Challenge: sample-efficiency
- Q-learning: does not always work but when it works, usually more
sample-efficient. Challenge: exploration

- Guarantees:
- Policy Gradients: Converges to a local minima of J( ), often good
enough!
- Q-learning: Zero guarantees since you are approximating Bellman
equation with a complicated function approximator

You might also like