Zero Labs
Industry News / AI

Reinforcement Learning from Scratch Part — 1: Introduction

Source · Towards AI By Mayurnayak

From the publisher’s feed, collected in the ZeroLabs newsroom.

This is the Part-1 of Series Reinforcement Learning from Scratch. In this series we plan to cover the core concepts of modern RL from Q-table, Deep-Q Learning to policy gradients and how RL is used in training LLMs.In this Part-1 we will cover the basic concepts of RL, what and why part and terminologies and approaches to solve RL problemsNotation table for this blogWhat is Reinforcement Learning and why do we need it?Reinforcement learning is a framework or a method to train AI or AGENT by allowing it to interact (by taking ACTION) in its surroundings or ENVIRONMENT. The Environment gives back feedback (or REWARD) which is a indication on how good or bad the action taken by the agent was.A simple analogy is how human work and learns through time. A child learns by interacting with its nearby surroundings and learns from it. If it touches some hot object, it feels pain which is a form of Reward which the environment gives to the child (Agent). The child learns it and tends not to do the same action in future.If you are familiar with Machine learning concepts like supervised learning and unsupervised learning you may have a question, we already have these learning methods what is RL bringing new here? Simple answer is in Supervised learning we have labeled data where the goal of model is to generalize on that labeled data. The model tends to behave like the data we train it on. But in the RL scenario we dont have such labeled data to train our model. The model must learn to interact with the environment and learn from its feedback. For example, if we want our model to play a game it will be very inefficient to capture all the possible states ,actions and reward and form a dataset and train a model, that why we use RL in such cases.Lets now discuss about how we train the agent in Reinforcement Learninghttps://huggingface.co/learn/deep-rl-course/unit1/rl-frameworkLets consider we are playing a game where agent needs to go to the goal in order to finish the state. The agent starts at some initial state s0. We then see all the possible actions the agent can take in that state. We will choose the action which will lead us to the goal with minimum effort ie with more reward. Once we choose the action we perform that action in environment and go to state s1. The environment gives us a reward for landing on that state (could be positive or negative). Our goal is to train an agent which will complete the game maximising the cumulative reward or return.In reinforcement learning, return (often denoted as G_t) is the total accumulated reward an agent expects to collect over time, starting from a given time step t through the end of an episode or task.The agent explores the environment in training process and tries different ways to reach the goal. It could reach a goal or it could get killed in game by landing into some step and the process again continues from the initial state. An episode in reinforcement learning is a single, complete cycle of interaction between an agent and its environment, starting from an initial state and ending at a terminal state.Exploration and Exploitation trade offWhen an agent starts to navigate in the environment, it can choose the action that has the highest reward at that state, take that action, and go to the next state (s+1). Alternatively, it can explore other actions and go to other states in order to gain more information about the environment, finding states that will lead to higher rewards. During the training step, it is important to explore all the other states that could lead to a higher reward than the current one. This is an important concept in reinforcement learning: the exploration and exploitation tradeoff.In the diagram above, we can see that the rat can move left or up and gain one block of cheese at a time. If we consider that it has an infinite amount of cheese available, it can take one step up to gain one cheese, and then take another step up or right to gain one block of cheese at a time. However, if it explores the environment further, it can reach the top-left corner and get three blocks of cheese — a much higher reward than getting one block of cheese at a time. This is basically the concept of exploration and exploitation.Approaches to solve RL problemThere are two main concepts, or two main approaches, to solve a reinforcement learning problem: the policy-based method and the value-based method.In the policy-based method, we directly train the agent. For example, if the agent is a neural network, we directly optimize its parameters to select the next action. The agent takes the state as input and outputs an action or a probability distribution over actions. We then sample an action from that probability distribution and execute it in the environment. The core idea of policy-based methods is that we are training the policy itself.We can denote the policy as pi . For instance, if four actions are available — right, left, up, and down — it outputs a probability distribution across all four actions at that given state. Ultimately, our objective is to learn a policy that increases the probability of action that maximize the overall reward, or total return, at the end of an episode.The next approach is the value-based method. In value-based methods, we do not train the policy directly; instead, we learn a value function.This value function takes a state as input and outputs a value for that state. The value of a state is simply the expected return of being in that state.For example, consider an agent at state s0 that has the choice to transition to three potential next states. Using the value function, it evaluates all three candidate states to see which one promises a higher return. It then selects and transitions to the state with the highest expected return. This is the basic idea behind value-based methods.As shown in the equation above, the value function v_pi(s) represents the expected discounted return if we start at state $s$ and continue following our policy pi.There is a parameter here called gamma, which represents the discount factor. What is the discounted return? The discount factor discounts future rewards as the agent takes further steps in the environment. This ensures mathematical convergence and encourages the agent to achieve goals efficiently.For example, at the first state, the weight of gamma is 1 . Then, from the second state onward, gamma is raised to higher powers (e.g., gamma¹, gamma², and so on). A typical value for gamma is 0.95. For the second reward, 95% of the reward value is added, but by the third step, the reward contribution is reduced further due to squaring (gamma² = 0.9025). In this way, we encourage the agent to reach the goal in as few steps as possible, which is why we add the parameter gamma.There are two types of value-based functions.The first is the state-value function.In a state-value function, we learn a function that takes in a state and outputs the value for that state. The equation for the state-value function gives the expected return if an agent starts at that state and follows the policy afterwards.In simple terms, it takes in a state and returns a value for it. For example, if the agent transitions to a specific state, the state-value function tells us what expected return the agent will receive starting from that state.The other approach is the action-value function.The action-value function takes in a state and an action, and it outputs the expected return if the agent at state S takes action A and follows the policy forever after. Here, the function takes two inputs — a state and an action — and then outputs a value for taking that specific action at that state. In short, it tells us what expected return we get by choosing a particular action in a given state. This is the action-value function.ConclusionSo, in this post, we covered the basics of reinforcement learning, why we need it, and the different core approaches to solving RL problems.In the next part, we will explore how to actually train these models. We will dive into Q-learning, break down the Bellman equation, and compare Monte Carlo methods with Temporal Difference (TD) learning.At its core, reinforcement learning mirrors natural biological learning — it reflects how humans and animals learn through trial and error in their environments. Theoretically, RL represents a critical step toward Artificial General Intelligence (AGI), where the ultimate challenge comes down to designing effective reward functions and learning optimal policies to maximize them.ReferencesHugging face RL CourseReinforcement Learning: An Introduction,Book by Andrew Barto and Richard S. SuttonReinforcement Learning from Scratch Part — 1: Introduction was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.