Q-learning is an RL algorithm to find the optimal q-value function. It is a fundamental algorithm in RL, that lies behind the impressive achievements in this field in the last 10 years. Welcome to my reinforcement learning course ❤️ This is part 2 of my hands-on course on reinforcement learning, which takes you from zero to HERO 🦸♂️. If you missed , please read it to get the reinforcement learning jargon and basics in place. part 1 Today we will learn about Q-learning, a classic RL algorithm born in the 90s. And we will train an agent to drive a taxi 🚕🚕🚕! Well, a simplified version of a taxi environment, but a taxi at the end of the day. All the code for this lesson is in Git clone it to follow along with today’s problem. . this Github repo Part 2 Contents The taxi driving problem 🚕 Environment, actions, states, rewards Random agent baseline 🤖🍷 Q-learning agent 🤖🧠 Hyper-parameter tuning 🎛️ Recap ✨ Homework 📚 What’s next? ❤️ 1. The taxi driving problem 🚕 We will teach an agent to drive a taxi using Reinforcement Learning. Driving a taxi in the real world is a very complex task to start with. Because of this, we will work in a simplified environment that captures the 3 essential things a good taxi driver does, which are: pick up passengers and drop them at their desired destination. drive safely, meaning no crashes. drive them in the shortest time possible. We will use an environment from OpenAI Gym, called the environment. Taxi-v3 There are four designated locations in the grid world indicated by R(ed), G(reen), Y(ellow), and B(lue). When the episode starts, the taxi starts off at a random square and the passenger is at a random location (R, G, Y or B). The taxi drives to the passenger’s location, picks up the passenger, drives to the passenger’s destination (another one of the four specified locations), and then drops off the passenger. While doing so, our taxi driver needs to drive carefully to avoid hitting any wall, marked as . Once the passenger is dropped off, the episode ends. | Before we get there, let’s understand well what are the actions, states, and rewards for this environment. 2. Environment, actions, states, rewards 👉🏽 notebooks/00_environment.ipynb Let’s first load the environment: import gym env = gym.make("Taxi-v3").env What are the actions the agent can choose from at each step? 0 drive down 1 drive up 2 drive right 3 drive left 4 pick up a passenger 5 drop off a passenger print("Action Space {}".format(env.action_space)) And the ? states 25 possible taxi positions, because the world is a 5x5 grid. 5 possible locations of the passenger, which are R, G, Y, B, plus the case when the passenger is in the taxi. 4 destination locations Which gives us 25 x 5 x 4 = 500 states print("State Space {}".format(env.observation_space)) What about ? rewards -1 default per-step reward. Because we want to encourage the agent to spend the shortest time, by penalizing each extra step. This is what you expect from a taxi driver, don’t you? Why -1, and not simply 0? +20 reward for delivering the passenger to the correct destination. -10 reward for executing a pickup or dropoff at the wrong location. You can read the rewards and the environment transitions (state, action ) → next_state from . env.P # env.P is double dictionary. # - The 1st key represents the state, from 0 to 499 # - The 2nd key represens the action taken by the agent, # from 0 to 5 # example state = 123 action = 0 # move south # env.P[state][action][0] is a list with 4 elements # (probability, next_state, reward, done) # # - probability # It is always 1 in this environment, which means # there are no external/random factors that determine the # next_state # apart from the agent's action a. # # - next_state: 223 in this case # # - reward: -1 in this case # # - done: boolean (True/False) indicates wheter the # episode has ended (i.e. the driver has dropped the # passenger at the correct destination) print('env.P[state][action][0]: ', env.P[state][action][0]) By the way, you can render the environment under each state to double-check this vectors make sense: env.P From state=123 # Need to call reset() at least once before render() will work env.reset() env.s = 123 env.render(mode='human') the agent moves south to get to action=0 state=223 env.s = 223 env.render(mode='human') And the reward is -1, as neither the episode ended, nor the driver incorrectly picked or dropped. 3. Random agent baseline 🤖🍷 👉🏽 notebooks/01_random_agent_baseline.ipynb Before you start implementing any complex algorithm, you should always build a baseline model. This advice applies not only to Reinforcement Learning problems but Machine Learning problems in general. It is very tempting to jump straight into the complex/fancy algorithms, but unless you are really experienced, you will fail terribly. Let’s use a random agent 🤖🍷 as a baseline model. class RandomAgent: """ This taxi driver selects actions randomly. You better not get into this taxi! """ def __init__(self, env): self.env = env def get_action(self, state) -> int: """ We have `state` as an input to keep a consistent API for all our agents, but it is not used. i.e. The agent does not consider the state of the environment when deciding what to do next. This is why we call it "random". """ return self.env.action_space.sample() agent = RandomAgent(env) We can see how this agent performs for a given initial state=123 # set initial state of the environment env.reset() state = 123 env.s = state epochs = 0 penalties = 0 # wrong pick up or dropp off reward = 0 # store frames to latter plot them frames = [] done = False while not done: action = agent.get_action(state) state, reward, done, info = env.step(action) if reward == -10: penalties += 1 frames.append({ 'frame': env.render(mode='ansi'), 'state': state, 'action': action, 'reward': reward } ) epochs += 1 print("Timesteps taken: {}".format(epochs)) print("Penalties incurred: {}".format(penalties)) 1,420 steps is a lot! 😵 You will get different numbers when you run this code on your laptop, because of the randomness in this agent. But still, the results will be consistently bad. To get a more representative measure of performance, we can repeat the same evaluation loop times starting each time at a random state. n=100 from tqdm import tqdm n_episodes = 100 # For plotting metrics timesteps_per_episode = [] penalties_per_episode = [] for i in tqdm(range(0, n_episodes)): # reset environment to a random state state = env.reset() epochs, penalties, reward, = 0, 0, 0 done = False while not done: action = agent.get_action(state) next_state, reward, done, info = env.step(action) if reward == -10: penalties += 1 state = next_state epochs += 1 timesteps_per_episode.append(epochs) penalties_per_episode.append(penalties) If you plot and you can observe that none of them decreases as the agent completes more episodes. In other words, the agent is NOT LEARNING anything. timesteps_per_episode penalties_per_episode If you want summary statistics of performance you can take averages: print(f'Avg steps to complete ride: {np.array(timesteps_per_episode).mean()}') print(f'Avg penalties to complete ride: {np.array(penalties_per_episode).mean()}') Implementing agents that learn is the goal of Reinforcement Learning, and of this course too. Let’s implement our first “intelligent” agent using Q-learning, one of the earliest and most used RL algorithms that exist. 4. Q-learning agent 🤖🧠 👉🏽 notebooks/02_q_agent.ipynb (by 🧠 and 🧠) is an algorithm to find the optimal q-value function. Q-learning Chris Walkins Peter Dayan As we said in , the q-value function associated with a policy **π **is the total reward the agent expects to get when at state the agent takes action and follows policy thereafter. part 1 Q(s, a) s a π The optimal q-value function **Q*(s, a) **is the q-value function associated with the optimal policy π*. If you know you can infer π*: i.e. you pick as the next action the one that maximizes Q*(s, a) for the current state s. Q*(s, a) Q-learning is an iterative algorithm to compute better and better approximations to the optimal q-value function , starting from an arbitrary initial guess Q*(s, a) Q⁰(s, a) In a tabular environment like with a finite number of states and actions, a q-function is essentially a matrix. It has as many rows as states and columns as actions, i.e. 500 x 6. Taxi-v3 Ok, but how exactly do you compute the next approximation Q¹(s, a) from Q⁰(s, a)? This is the key formula in Q-learning: As our q-agent navigates the environment and observes the next state and reward , you update your q-value matrix with this formula. s’ r What is the learning rate 𝛼 in this formula? The (as usual in machine learning) is a small number that controls how large are the updates to the q-function. You need to tune it, as too large of a value will cause unstable training, and too small might not be enough to escape local minima. learning rate And this discount factor ? 𝛾 The is a (hyper) parameter between 0 and 1 that determines how much our agent cares about rewards in the distant future relative to those in the immediate future. discount factor When 𝛾=0, the agent only cares about maximizing immediate reward. As it happens in life, maximizing immediate reward is not the best recipe for optimal long-term outcomes. This happens in RL agents too. When 𝛾=1, the agent evaluates each of its actions based on the sum total of all of its future rewards. In this case the agent weights equally immediate rewards and future rewards. The discount factor is typically an intermediate value, e.g. 0.6. To sum up… if you train long enough with a decent learning rate and discount factor and the agent explores enough the state space and you update the q-value matrix with the Q-learning formula your initial approximation will eventually converge to optimal q-matrix. Voila! Let’s implement a Python class for a Q-agent then. import numpy as np class QAgent: def __init__(self, env, alpha, gamma): self.env = env # table with q-values: n_states * n_actions self.q_table = np.zeros([env.observation_space.n, env.action_space.n]) # hyper-parameters self.alpha = alpha # learning rate self.gamma = gamma # discount factor def get_action(self, state): """""" return np.argmax(self.q_table[state]) def update_parameters(self, state, action, reward, next_state): """""" # Q-learning formula old_value = self.q_table[state, action] next_max = np.max(self.q_table[next_state]) new_value = \ old_value + \ self.alpha * (reward + self.gamma * next_max - old_value) # update the q_table self.q_table[state, action] = new_value Its API is the same as for the above, but with an extra method ). This method takes the transition vector and updates the q-value matrix approximation using the Q-learning formula from above. RandomAgent update_parameters( (state, action, reward, next_state) self.q_table Now, we need to plug this agent into a training loop and call its method every time the agent collects a new experience. update_parameters() Also, remember we need to guarantee the agent explores enough the state space. Remember the exploration-exploitation parameter we talked about in ? This is when the parameter enters into the game. part 1 epsilon Let’s train the agent for and use n_episodes = 10,000 epsilon = 10% import random from tqdm import tqdm # exploration vs exploitation prob epsilon = 0.1 n_episodes = 10000 # For plotting metrics timesteps_per_episode = [] penalties_per_episode = [] for i in tqdm(range(0, n_episodes)): state = env.reset() epochs, penalties, reward, = 0, 0, 0 done = False while not done: if random.uniform(0, 1) < epsilon: # Explore action space action = env.action_space.sample() else: # Exploit learned values action = agent.get_action(state) next_state, reward, done, info = env.step(action) agent.update_parameters(state, action, reward, next_state) if reward == -10: penalties += 1 state = next_state epochs += 1 timesteps_per_episode.append(epochs) penalties_per_episode.append(penalties) And plot and timesteps_per_episode penalties_per_episode import pandas as pd import matplotlib.pyplot as plt fig, ax = plt.subplots(figsize = (12, 4)) ax.set_title("Timesteps to complete ride") pd.Series(timesteps_per_episode).plot(kind='line') plt.show() fig, ax = plt.subplots(figsize = (12, 4)) ax.set_title("Penalties per ride") pd.Series(penalties_per_episode).plot(kind='line') plt.show() Nice! These graphs look much much better than for the . Both metrics decrease with training, which means our agent is learning 🎉🎉🎉. RandomAgent We can actually see how the agent drives starting from the same as we used for the . state = 123 RandomAgent # set initial state of the environment state = 123 env.s = state epochs = 0 penalties = 0 reward = 0 # store frames to latter plot them frames = [] done = False while not done: action = agent.get_action(state) next_state, reward, done, info = env.step(action) agent.update_parameters(state, action, reward, next_state) if reward == -10: penalties += 1 frames.append({ 'frame': env.render(mode='ansi'), 'state': state, 'action': action, 'reward': reward } ) state = next_state epochs += 1 print("Timesteps taken: {}".format(epochs)) print("Penalties incurred: {}".format(penalties)) If you want to compare hard numbers you can evaluate the performance of the q-agent on, let’s say, 100 random episodes and compute the average number of timestamps and penalties incurred. A little bit about epsilon-greedy policies When you evaluate the agent, it is still good practice to use a positive value, and not epsilon epsilon = 0. W hy so? Isn’t our agent fully trained? Why do we need to keep this source of randomness when we choose the next action? The reason is to prevent overfitting. Even for such a small state, action space in (i.e. 500 x 6) it is likely that during training our agent has not visited enough certain states. Taxi-v3 Hence, its performance in these states might not be 100% optimal, causing the agent to get “caught” in an almost infinite loop of suboptimal actions. If epsilon is a small positive number (e.g. 5%) we can help the agent escape these infinite loops of suboptimal actions. By using a small epsilon at evaluation we are adopting a so-called . epsilon-greedy strategy Let’s evaluate our trained agent on using Observe how the loop looks almost exactly as the train loop above, but without the call to n_episodes = 100 epsilon = 0.05. update_parameters() import random from tqdm import tqdm # exploration vs exploitation prob epsilon = 0.05 n_episodes = 100 # For plotting metrics timesteps_per_episode = [] penalties_per_episode = [] for i in tqdm(range(0, n_episodes)): state = env.reset() epochs, penalties, reward, = 0, 0, 0 done = False while not done: if random.uniform(0, 1) < epsilon: # Explore action space action = env.action_space.sample() else: # Exploit learned values action = agent.get_action(state) next_state, reward, done, info = env.step(action) agent.update_parameters(state, action, reward, next_state) if reward == -10: penalties += 1 state = next_state epochs += 1 timesteps_per_episode.append(epochs) penalties_per_episode.append(penalties) print(f'Avg steps to complete ride: {np.array(timesteps_per_episode).mean()}') print(f'Avg penalties to complete ride: {np.array(penalties_per_episode).mean()}') These numbers look much much better than for the . RandomAgent We can say our agent has learned to drive the taxi! Q-learning gives us a method to compute optimal q-values. But, , and ? what about the hyper-parameters alpha gamma epsilon I chose them for you, rather arbitrarily. But in practice, you will need to tune them for your RL problems. Let’s explore their impact on learning to get a better intuition of what is going on. 5. Hyper-parameter tuning 🎛️ 👉🏽 notebooks/03_q_agent_hyperparameters_analysis.ipynb Let’s train our q-agent using different values for (learning rate) and (discount factor). As for we keep it at 10%. alpha gamma epsilon In order to keep the code clean, I encapsulated the q-agent definition inside and the training loop inside the function in src/q_agent.py train() src/loops.py # No need to copy paste the same QAgent # definition in every notebook, don't you think? from src.q_agent import QAgent # hyper-parameters # RL problems are full of these hyper-parameters. # For the moment, trust me when I set these values. # We will later play with these and see how they impact learning. alphas = [0.01, 0.1, 1] gammas = [0.1, 0.6, 0.9] import pandas as pd from src.loops import train # exploration vs exploitation prob # let's start with a constant probability of 10%. epsilon = 0.1 n_episodes = 1000 results = pd.DataFrame() for alpha in alphas: for gamma in gammas: print(f'alpha: {alpha}, gamma: {gamma}') agent = QAgent(env, alpha, gamma) _, timesteps, penalties = train(agent, env, n_episodes, epsilon) # collect timesteps and penalties for this pair # of hyper-parameters (alpha, gamma) results_ = pd.DataFrame() results_['timesteps'] = timesteps results_['penalties'] = penalties results_['alpha'] = alpha results_['gamma'] = gamma results = pd.concat([results, results_]) # index -> episode results = results.reset_index().rename( columns={'index': 'episode'}) # add column with the 2 hyper-parameters results['hyperparameters'] = [ f'alpha={a}, gamma={g}' for (a, g) in zip(results['alpha'], results['gamma']) ] Let us plot the per episode for each combination of hyper-parameters. timesteps import seaborn as sns import matplotlib.pyplot as plt fig = plt.gcf() fig.set_size_inches(12, 8) sns.lineplot('episode', 'timesteps', hue='hyperparameters', data=results) The graph looks artsy-fartsy, but a bit too noisy 😵. Something you can observe though is that when the learning is slower. (learning rate) controls how much we update the q-values in each iteration. Too small of a value implies slower learning. alpha = 0.01 alpha Let’s discard and do 10 runs of training for each combination of hyper-parameters. We average the for each episode number, from 1 to 1000, using these 10 runs. alpha = 0.01 timesteps I created the function in to keep the notebook code cleaner: train_many_runs() src/loops.py from src.loops import train_many_runs alphas = [0.1, 1] gammas = [0.1, 0.6, 0.9] epsilon = 0.1 n_episodes = 1000 n_runs = 10 results = pd.DataFrame() for alpha in alphas: for gamma in gammas: print(f'alpha: {alpha}, gamma: {gamma}') agent = QAgent(env, alpha, gamma) timesteps, penalties = train_many_runs(agent, env, n_episodes, epsilon, n_runs) # collect timesteps and penalties for this pair of # hyper-parameters (alpha, gamma) results_ = pd.DataFrame() results_['timesteps'] = timesteps results_['penalties'] = penalties results_['alpha'] = alpha results_['gamma'] = gamma results = pd.concat([results, results_]) # index -> episode results = results.reset_index().rename( columns={'index': 'episode'}) results['hyperparameters'] = [ f'alpha={a}, gamma={g}' for (a, g) in zip(results['alpha'], results['gamma'])] It looks like is the value that works best, while seems to have less of an impact. alpha = 1.0 gamma Congratulations! You have tuned your first learning rate in this course 🥳 Tunning hyper-parameters can be time-consuming and tedious. There are excellent libraries to automate the manual process we just followed, like , but this is something we will play with later in the course. For the time being, enjoy the speed-up in training we have just found. Optuna Wait, what happens with this that I told you to trust me on? epsilon = 10% Is the current 10% value the best? Let’s check it ourselves. We take the best and we found, i.e. alpha gamma alpha = 1.0 (we could have taken or too) gamma = 0.9 0.1 0.6 And train with different epsilons = [0.01, 0.1, 0.9] # best hyper-parameters so far alpha = 1.0 gamma = 0.9 epsilons = [0.01, 0.10, 0.9] n_runs = 10 n_episodes = 200 results = pd.DataFrame() for epsilon in epsilons: print(f'epsilon: {epsilon}') agent = QAgent(env, alpha, gamma) timesteps, penalties = train_many_runs(agent, env, n_episodes, epsilon, n_runs) # collect timesteps and penalties for this pair of # hyper-parameters (alpha, gamma) results_ = pd.DataFrame() results_['timesteps'] = timesteps results_['penalties'] = penalties results_['epsilon'] = epsilon results = pd.concat([results, results_]) # index -> episode results = results.reset_index().rename(columns={'index': 'episode'}) And plot the resulting and curves: timesteps penalties fig = plt.gcf() fig.set_size_inches(12, 8) sns.lineplot('episode', 'timesteps', hue='epsilon', data=results) plt.show() fig = plt.gcf() fig.set_size_inches(12, 8) sns.lineplot('episode', 'penalties', hue='epsilon', data=results) As you can see, both and seem to work equally well, as they strike the right balance between exploration and exploitation. epsilon = 0.01 epsilon = 0.1 On the other side, is too large of a value, causing “too much” randomness during training, and preventing our q-matrix to converge to the optimal one. Observe how the performance plateaus at around per episode. epsilon = 0.9 250 timesteps In general, the best strategy to choose the hyper-parameter is . That is, at the beginning of training, when the agent is very uncertain about its q-value estimation, it is best to visit as many states as possible, and for that, a large is great (e.g. 50%) epsilon progressive epsilon-decay epsilon As training progresses, and the agent refines its q-value estimation, it is no longer optimal to explore that much. Instead, by decreasing the agent can learn to perfect and fine-tune the q-values, to make them converge faster to the optimal ones. Too large of an can cause convergence issues as we see for . epsilon epsilon epsilon = 0.9 We will be tunning epsilons along the course, so I will not insist too much for the moment. Again, enjoy what we have done today. It is pretty remarkable. 6. Recap ✨ Congratulations on (probably) solving your first Reinforcement Learning problem. These are the key learnings I want you to sleep on: The difficulty of a Reinforcement Learning problem is directly related to the number of possible actions and states. is a tabular environment (i.e. finite number of states and actions), so it is an easy one. Taxi-v3 Q-learning is a learning algorithm that works excellent for tabular environments. No matter what RL algorithm you use, there are hyper-parameters you need to tune to make sure your agent learns the optimal strategy. Tunning hyper-parameters is a time-consuming process but necessary to ensure our agents learn. We will get better at this as the course progresses. 7. Homework 📚 👉🏽 notebooks/04_homework.ipynb This is what I want you to do: the repo to your local machine. Git clone the environment for this lesson Setup 01_taxi. Open and try completing the 2 challenges. 01_taxi/otebooks/04_homework.ipynb I call them challenges (not exercises) because they are not easy. I want you to try them, get your hands dirty, and (maybe) succeed. In the first challenge, I dare you to update the function to accept an episode-dependent epsilon. train() src/loops.py In the second challenge, I want you to upgrade your Python skills and implement paralleling processing to speed up hyper-parameter experimentation. As usual, if you get stuck and you need feedback drop me a line at plabartabajo@gmail.com. I will be more than happy to help you. 8. What’s next? ❤️ In the next part, we are going to solve a new RL problem. A harder one. Using a new RL algorithm. With lots of Python. And there will be new challenges. And fun! See you soon! Do you want to become a Machine Learning PRO, and access top courses on Machine Learning and Data Science? 👉🏽 Please ⭐ the course GitHub repo Have a great day 🧡❤️💙 Pau