Reinforcement learning (RL) trains an agent to act in an environment from rewards alone; agents are model-free or model-based and learn by policy gradient or value-based methods. Also covers PPO and DQN, credit assignment, Gymnasium, current RL frameworks and where to learn RL.
Links
What is reinforcement learning?
Reinforcement learning (RL) is the third kind of machine learning, beside supervised and unsupervised. An agent acts in an environment and learns from rewards and penalties, not labeled examples. Uses include Go (AlphaGo beat Lee Sedol in 2016), Atari and Doom, self-driving cars, walking robots and trading.
DeepMind's Differentiable Neural Computer (2016), a neural network with learned external memory, targets the reasoning, knowledge representation and memory that AI still leaves open.
Supervised learning vs reinforcement learning for trading
Supervised learning predicts the next price from past prices, or learns buy, sell and hold from expert labels. An RL agent learns to trade by making and losing money, with portfolio value as the reward. MLG 026 is the Bitcoin trading project.
Credit assignment and the discount factor
Rewards arrive late: a purchase costs commission now, gains later. The credit assignment problem is finding which earlier action earned a later reward. The discount factor γ, between 0 and 1, weights future rewards against immediate ones; with value estimates, it addresses credit assignment.
return G = r1 + γ·r2 + γ²·r3 + … Model-free vs model-based reinforcement learning
"Model" means a model of the environment's transition dynamics: which state follows an action. A model-free agent reacts: its policy network maps observations to actions. A model-based agent is given or learns the dynamics and plans with them. AlphaGo and AlphaZero pair neural networks with Monte Carlo tree search over known rules; MuZero learns its model. Even model-free PPO learns to run in MuJoCo (Multi-Joint dynamics with Contact) simulations.
Policy gradient vs value-based methods
- Policy gradient: learn the policy directly. Handles continuous actions, such as how much to buy, with high variance. Proximal Policy Optimization (PPO) leads; Trust Region Policy Optimization (TRPO) preceded it.
- Value-based: learn each action's value from the Bellman equation and take the best. Usually discrete actions, lower variance. Deep Q-Network (DQN) leads.
- Actor-critic combines them: A2C/A3C, ACER, Deep Deterministic Policy Gradient (DDPG).
Neither wins outright; test both like hyperparameters (MLG 027).
Reinforcement learning frameworks
OpenAI Baselines, Tensorforce, Intel's Coach and rllab are unmaintained; rllab became garage. Most RL code is now PyTorch:
- Stable-Baselines3: PPO, A2C, DQN, DDPG, SAC and TD3, descended from a fork of OpenAI Baselines.
- RLlib, part of Ray, for scaled-out training.
- CleanRL: one file per algorithm.
- TorchRL: the PyTorch project's RL library.
Gymnasium environments and CartPole
OpenAI Gym continues as the Farama Foundation's Gymnasium, an API standard plus reference environments. A custom environment subclasses gymnasium.Env and implements reset and step. MuJoCo has been free since 2021 and open source since 2022; Gymnasium ships MuJoCo environments.
CartPole is the hello world: push a cart left or right to keep a pole upright, +1 per step it stays up.
Where to learn reinforcement learning
In order:
- Chapter 19 of Hands-On Machine Learning with Scikit-Learn and PyTorch: policy gradients, DQN, actor-critic and PPO on Gymnasium.
- Sutton and Barto's Reinforcement Learning: An Introduction (2nd edition, 2018), free from the authors.
- Russell and Norvig's Artificial Intelligence: A Modern Approach (4th edition, 2020), for search and planning.
- UC Berkeley CS285, Sergey Levine's deep RL course.
- David Silver's RL course (2015), which works as audio (convert video to mp3).
Next: MLG 032, on similarity and distance metrics.