Deep Reinforcement Learning: An Overview!
By Akhil Devarashetti
Who am I?
Computer Vision Engineer at BioTrillion
Masters degree in Artificial Intelligence�from University of Cincinnati in 2020
Websites: akhil.ai, ml.gallery
What I’ll cover today
RL Concepts:
- Environments
- Policy Gradients
- Q Learning
- Curiosity
- Model-Based
For each RL concept:
- Some theory
- Code
- Demo
What is Reinforcement Learning?
Any algorithm that learns by trial and error
Random Moves
Learn
Good Moves
Win/Lose
Why Care?
Any business value? Practicality?
Sources:
https://deepmind.com/blog/article/deepmind-ai-reduces-google-data-centre-cooling-bill-40
https://venturebeat.com/2021/02/23/how-reinforcement-learning-chooses-the-ads-you-see/
https://www.analyticsvidhya.com/blog/2020/10/reinforcement-learning-stock-price-prediction/
https://openai.com/projects/five/
But why I love RL is because …
It’s fun!
Closest thing to Artificial General Intelligence
But why I love RL is because … It is challenging!
Let’s get the basics out of the way
Env (state)
Agent
Actions: up, down, left, right
Reward:
+10 if green box is reached
-10 if red box is reached
Next state
Objective: Maximize rewards
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
State Transitions
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
LEFT
DOWN
0
Reward
0
Reward
(s1, a1, r1)
(s2, a2, r2)
(s3, a3, r3)
… (st,at,rt)
Episode
Working with environments - gym.Env class
Takes an action
Returns next_state and reward
Working with environments - Demo run
Is this agent intelligent?
What does an agent do?
Agent chooses an action given a state
action = agent(state)
Can we replace this agent with a neural network?
Neural network as agent
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
Neural Network
Up
Down
Left
Right
| | | | |
| | | | |
| | | | |
| | | | |
Softmax
(sampled, not argmax)
(4, 4, 4)
(objects, height, width)
Neural Net Agent
Neural Network in PyTorch
Replaced random agent with neural net agent
Is this agent intelligent?
How do we TRAIN this network?
The objective is to maximize rewards. So that is what we’ll do.
Object/Loss function = Maximize rewards!��maximize(r1 + r2 + r3 + r4 + … + rt)
But neural networks like to MINIMIZE the objective/loss
So let’s minimize(-1 * (r1 + r2 + r3 + r4 + … + rt))
There’s no gradient attached!
Probabilities: p1 p2 p3 … pt
minimize(-1 * (r1*p1 + r2*p2 + r3*p3 + … + rt*pt))
Reward weighting with probabilities
Policy Gradients!
Code Sample
Sampling actions
Loss function
minimize(-1 * (r1*p1 + r2*p2 + r3*p3 + … + rt*pt))
Oh, and there’s credit assignment
Some games win in the last step
Ex: gridworld?
Steps leading up to the end are most significant.
Weights: … , 0.77, 0.81, 0.86, 0.9, 0.95, 1
λ = 0.95
Weights = reversed(λ, λ2, λ3, λ4, …)
Some games lose in the last step
Ex: balancing a pole
Steps leading up to the end are least significant.
Weights: 1, 0.95, 0.9, 0.86, 0.81, 0.77, …
Objective function: minimize(-1 * (r1*p1*λ + r2*p2*λ2 + r3*p3*λ3 + … + rt*pt*λt))
Problem with PG?
I can learn only after playing the whole episode :(
Let’s predict reward
For every action, there is an equal and op… reward
Ex: Stock investing.
Actions: [invest , don’t invest ]
Reward: [stock up , stock down ]
Let’s predict tomorrow’s reward i.e. reward for current action.
Reward Predictor
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
Neural Network
Up
Down
Right
Left
-1 | | | | |
-1 | | | | |
| | 10 | | |
-1 | | | | |
Predicted rewards
But there’s no immediate reward :(
We get a reward only after navigating and reaching the end position.
-1 reward for all actions.
So which action is the best?
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
Psst! taking left has a higher long-term reward.
Immediate and Long Term Rewards
return(statet) = Rt = rt + (𝞬 . rt+1) + (𝞬2 . rt+2) + …
Prediction
Rt = rt + 𝞬 . Rt+1
Return = reward … plus a tiny bit of future rewards
𝞬 = small number b/w 0 to 1
Predict
Label
Return Predictor
Q Learning!
Qt
st
env.step
rt
st+1
Qt+1
Rt ← rt + 𝞬 . Rt+1
Let’s look at some code
Random action 10% of the time
Next state’s Q value
rt + 𝞬Rt+1
MSE loss
Ways to improve
Q Learning:
Policy Gradients:
Combining PG and DQN: Advantage Actor Critic
Q Learning Revolution
The famous Atari paper by DeepMind
0%!!??
Sparse Reward Problem
Curiosity!
Our agent is too reliant on external rewards.
No motivation to explore.
How is a human curious?
Curiosity!
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
LEFT
Intrinsic Curiosity Module
MSE( , )
Qt
st
env.step
rt
st+1
Qt+1
Qt
st
step
rt
st+1
Qt+1
Extrinsic reward
st
~st+1
at
~st+1
st+1
Intrinsic reward
DQN with intrinsic reward
Rt ← (rt ex + rt in ) + 𝞬 . Rt+1
Noisy TV Problem
Random events in the environment.
Examples:
How is this a problem?
How to solve noisy TV?
st
~st+1
at
~at
st
st+1
st
st+1
st
st+1
Other techniques
Model Based
Can you ask a neural network that plays chess - What’s your plan?
Neural Networks lack planning.
Game Tree
Demo
Curse of Dimensionality
Narrow down the search space with probability distribution over valid actions.
2. Too Deep
After a certain depth is reached,
predict the value of that branch with value network.
What if we don’t have an emulator for planning?
Well, we can learn the dynamics - learned model
MuZero
h(s) → ~s
f(~s) → p, v
g(~st, a) → ~st+1, r
p = probability distribution over actions�v = value of the state
Other techniques
Other topics:
The End
Resources: