Reinforcement Learning Fundamentals


Reinforcement Learning Fundamentals

Simon
Welcome back. Today we're exploring reinforcement learning, a branch of machine learning concerned with learning control strategies for interacting with complex environments. A useful definition is that reinforcement learning is a framework for learning how to interact with an environment through experience.

Michael
And the inspiration is strongly biological. Animals learn through trial and error, using positive and negative rewards and feedback to discover how to behave. So reinforcement learning is not simply being told the correct answer every time; it is learning which actions lead to useful outcomes.

Simon
Exactly. Imagine training a dog to hold a treat on its nose until it hears a command, and then allowing it to eat the treat. The reward reinforces the desired behavior. In robotics, the same general idea can be used to learn difficult behaviors, such as making a simulated bipedal robot walk.

Michael
Walking is a particularly challenging example because it is a nonlinear control problem. The long-term goal is to transfer what is learned in simulation into the real world, so robots and other physical agents can interact with their surroundings more effectively.

Simon
There are two pieces to keep separate. First, there is the reinforcement learning framework: the way an agent interacts with an environment and learns from feedback. Second, there is the difficult optimization problem of finding the agent's actions, or policy, that produce good results.

Michael
Let's build the framework from the ground up. At its center are an agent and an environment. The agent has agency: it observes something about the environment, chooses an action, and then receives whatever feedback the environment provides.

Simon
Take a mouse in a maze. The mouse is the agent, and the maze is the environment. At a given moment, the mouse measures its current state, which we can call s, but it does not necessarily observe the entire maze from above. It knows where it is now, and possibly information about where it has been, then it chooses an action, such as turning left, turning right, or moving forward.

Michael
The reward may be very sparse. The mouse might receive nothing for most of the journey and only get cheese, or perhaps a fruit loop in a training experiment, when it reaches the end. That means the mouse has to work out which earlier decisions helped produce the eventual reward.

Simon
This is sometimes described as semi-supervised learning. If the mouse received a piece of cheese after every correct turn, those rewards would act like labels in ordinary supervised learning. But when the reward is delayed and not directly attached to each individual action, the agent receives much less supervisory information.

Michael
And that is one reason reinforcement learning is so difficult. The agent has to use much more trial and error and often much more data to determine which actions were useful. Games provide another clear setting: chess, checkers, and tic-tac-toe all have rules and a finite set of possible actions.

Simon
Chess adds another complication because the environment includes an active opponent. The agent is not just following fixed game rules; it is trying to defeat another player who is also making decisions. That makes chess a useful example for understanding the central reinforcement learning problem.

Michael
The central goal is to design a policy: a strategy for choosing actions given a state, in order to maximize the chance of receiving future rewards. We write the policy as pi, and pi tells us the probability of taking action a when the agent is in state s.

Simon
Why describe it as a policy rather than a conventional control law? Because the environment may be probabilistic, and the agent may deliberately use a probabilistic strategy. In chess, for example, a player might choose one move most of the time but sometimes try another move, either to explore or because the situation may change.

Michael
The interaction unfolds over time. At time one, the agent observes a state and takes an action; at time two, it observes another state and takes another action, continuing through the sequence. Rewards can occur along the way, but in many important problems most rewards are empty until the end.

Simon
That creates the credit assignment problem. Suppose you play an excellent game of chess but make one mistake near the end and lose. Should the system treat the entire sequence of moves as bad? It has to determine which actions contributed positively and which action, or actions, led to the loss.

Michael
To help with that, we consider the value of a state under a particular policy. The value of state s given policy pi is the expected amount of reward the agent will receive in the future if it starts in state s and then follows that policy.

Simon
Future rewards are usually discounted. The discount factor, gamma, lies between zero and one and determines how strongly the agent favors an immediate reward over a reward much later. This reflects the general idea that an immediate reward is often preferred to an otherwise similar delayed reward.

Michael
In chess, the state could be a particular board position. Since the number of possible positions is enormous, a human cannot memorize the value of every one. Instead, people develop rules of thumb: for example, taking the opponent's queen while keeping your own usually suggests a better chance of eventually winning.

Simon
Counting pieces or assigning points to them would be a very simple proxy for a state's value. As a player gains experience, that value function can become more refined, helping the player recognize strong positions and improve the policy that leads toward them.

Michael
The overall reinforcement learning problem is therefore an optimization problem: find the policy that maximizes future rewards. Because the environment is often stochastic rather than perfectly deterministic, it is commonly modeled as a Markov decision process, or MDP.

Simon
In an MDP, being in a current state s and taking action a does not necessarily determine one guaranteed next state. Instead, there is a probability of transitioning to each possible next state. Backgammon is a useful example: the rules are fixed, but dice rolls introduce randomness into the transition.

Michael
That stochastic environment is another reason policies can be probabilistic. The agent has to choose actions while accounting for uncertain outcomes, rather than assuming that the same action will always produce exactly the same next state.

Simon
Sparse rewards make the credit assignment problem even harder. Chess may provide a clear reward only when one player checkmates the other. If an expert explained the quality of moves along the way, those intermediate signals would be denser rewards and would make learning faster.

Michael
When rewards are sparse, the system is sample inefficient. It may need to play many, many games before it gathers enough examples to learn a good policy. One way to help is reward shaping, where a human or expert designs proxy rewards that provide more frequent intermediate feedback on the way to the final objective.

Simon
There are many ways to optimize a policy. Approaches include differential programming, Monte Carlo methods, and temporal-difference learning. Temporal-difference methods provide a balance between differential programming and Monte Carlo ideas, can be model-free, and are connected to Bellman's optimization work in optimal control.

Michael
The larger connection is important. Machine learning and control theory often formulate difficult nonlinear, non-convex optimization problems. Machine learning typically uses data to solve them, while control methods also account for system dynamics. Reinforcement learning sits at the intersection of these fields.

Simon
A major challenge is the exploration-exploitation trade-off. Exploitation means using the strategy currently believed to work best. Exploration means trying actions that might fail but could reveal an even better strategy.

Michael
A learning system has to balance those choices. If it only exploits, it may never discover a better policy; if it only explores, it may fail to use what it has already learned. Policy iteration captures the broader idea of repeatedly updating a policy using new information and rewards so that it improves over time.

Simon
The policy update itself can use many optimization tools, including simulated annealing, evolutionary optimization, gradient descent, stochastic gradient descent, and Adam optimization. Modern work also uses deep neural networks to optimize policies, which is the direction of deep reinforcement learning.

Michael
A simple physical example is a robot learning to catch a ball in a cup. A human can first demonstrate that the task is possible, giving the robot an example through imitation learning. Then the robot uses visual information from a camera and trial and error to improve, eventually learning the physics of the task after many attempts.

Simon
Another example is swinging up and stabilizing a pendulum on a cart. Without a model of the physics, learning a useful control signal can require thousands of trials. A model-based approach, such as the Pilco learner, uses knowledge of physics to learn much more efficiently, sometimes approaching the behavior after only a small number of trials.

Michael
So having a model can dramatically improve sample efficiency. The learner can combine trial and error with known dynamics rather than discovering everything from scratch. That is especially valuable when physical experiments are expensive or time-consuming.

Simon
Now let's turn to Q-learning. Instead of learning a value function that depends only on the state and a policy separately, Q-learning learns a quality function of both state and action. Q(s, a) represents how good it is to be in state s, take action a, and then behave optimally in the future.

Michael
The update uses the old quality estimate, the reward that was received, a learning rate alpha, and the discounted best predicted quality of the next state. The maximum over future actions expresses the assumption that the agent will choose the best possible action later.

Simon
Once the quality function is known, action selection is straightforward. When the agent reaches state s, it compares the Q-values of the available actions and chooses the action with the highest quality. In that way, Q-learning combines information about the value of states and actions into a function that directly supports a policy.

Michael
There is also hindsight replay, which helps with sparse rewards. Instead of discarding an episode because the agent failed to achieve its original goal, the system looks back and treats the outcome as if a different goal had been intended.

Simon
For example, if a robot trying to put a ball in a cup misses and sends the ball somewhere else, that failed attempt may still contain a useful action sequence for getting the ball to that other location. By remembering it as successful for an alternative goal, the system gets additional reinforcement and learns more about the system's dynamics.

Michael
That makes hindsight replay more sample efficient and more reward efficient. It resembles how a person might learn from an unintended result: a tennis shot may go in the wrong direction, but that same motion could be useful for a different shot in the future.

Simon
So the complete picture is this: an agent observes states, takes actions according to a policy, receives rewards that may be sparse and delayed, and uses those experiences to improve its policy. Value functions estimate the future reward of states, MDPs describe stochastic state transitions, and methods such as reward shaping, Q-learning, model-based learning, and hindsight replay help address the difficulty of learning from limited feedback.

Michael
And the main unresolved challenge running through the whole subject is credit assignment: figuring out which actions were responsible for later rewards. Reinforcement learning brings together machine learning, control, optimization, and ideas inspired by how animals learn, with the ultimate aim of producing agents that can interact intelligently with complex environments.

Ask anything
We've read this whole study set, so questions about it get the best answers.