Deep Reinforcement Learning
Transcript
Welcome to our fifth lecture in person for Stanford deep learning CS230. Um today's lecture is going to be about deep reinforcement learning. I actually switched uh the original plan of talking about neural network interpretability and LLM visualization simply because you you haven't had the chance to study attention maps, you know, convolutional neural networks and so it would have been an overkill to do that week five. So we're going to talk about neural network interpretability
and visualization in a later lecture actually. Um but today our focus will be on deep reinforcement learning uh which is probably my favorite lecture of uh the class. I I feel like I say that every week but it's okay. I like it. Um the agenda is pretty packed. We're going to start with uh deep reinforcement learning which you can think of as the marriage between deep learning and reinforcement learning. Together the baby is called deep reinforcement learning. and we're going to see how
reinforcement learning works and how neural networks can play a part in building a reinforcement learning um agent. Um in the second half of the class we will focus on a very specific um you know concept called reinforcement learning from human feedback that you might have heard of. It's one of the core concept that really made the difference between what you might have remembered as GPT2 and chat GPT. You know, that's the leap. Uh that's really the the the technique
that had uh that has you know democratized um access to LLM because of the performance improvements and the alignment with humans. So, we're going to see what what is this concept of R LHF and how um does it work and why does it allow us to align a language model to human preferences. Ready to go? As always, let's try to make it interactive. So, the motivation behind deep reinforcement learning and as usual, you're going to have all the most important papers that are covered in the
class listed at the bottom of each slide. Um reinforcement learning has grown in popularity. Um one of uh uh the you know very popular papers called human level control through deep reinforcement learning is the work um from deep mind um has showed us that a single algorithm/training method can allow us to train AI that can play many many Atari games better than humans. single algorithm over 40 50 games where it exceeds human capability which is quite impressive when you
thought about the fact that you know machine learning used to be niche and you would have to train a really niche algorithm to perform different task here's an algorithm that can just learn sort of every Atari game a little later you might have heard of Alpho Alph Go is a is an algorithm that was developed to beat and exceed human performance performance in the game of Go. We'll talk about it a little more. The game of Go is a very complex game. Some would argue way more complex than chess from a decision-m standpoint and from the possibilities that can happen on the board. And so, um, it it actually got solved, um, in 2017 again by the Deep M Deep Mind team and and David Silver's lab um, later on. And again another great paper from deep mind had showed us that reinforcement learning can also be used for strategy game that might be a touch more complex than chess or go that might actually involve multiple players
playing with each other or against each other. Some of you might have played Starcraft for example. That's an example of a game where um it requires a lot of long long-term thinking, short-term thinking. Another one is, you know, Dota. Some of you might have played Dota or League of Legend where you have a team playing against another team. Those are examples of games that involve multiple agents playing collaboratively. And it's pretty hard to develop systems that can play with each other against multiple opponents. Um and finally most recently this is 2022 so alongside the release of Chad GPT um this paper that introduces the concept of reinforcement learning with human feedback applied to aligning language models with human preferences and we'll talk about that later. So all all this to say that reinforcement learning allowed um us to exceed human performance in a variety of tasks. The first one um I want us to think about is the the game of go. So
let's say that you were asked to solve the game of go with classic supervised learning. Okay, everything we've seen together so far labeled data. How would you solve the game of go with classic supervised learning? What data would you collect? What would be the label etc. Yes.
>> Okay, good point. You look at history of plenty of games, hopefully from good players if you want the the algorithm to work. Um, and you look at X as the input being the current state of the board and Y as the next state of the board. And this would tell you what move was selected and you learn the move essentially. And hopefully if you do that across many many games, you know, you might you might see the the agent become more attuned to the game and and and develop better strategies. So, you know, really hopefully it's a professional player. What are the disadvantages of that or the shortcomings that you can anticipate? >> Yes.
>> Yeah. Yeah. Great point. You might not see the entire space of possible states of the board, which is what you said. So you might miss out on a lot of different strategies. So the game of go is actually a game with two players. One player that uses the black stones and one player that uses the white stones. And iteratively they're going to place those stones on the grid, a 13 by13 grid that you can see on screen with the goal of surrounding their opponent. So you're
constantly trying to surround the stones of the opponent and the opponent is trying to surround your stones. And so you can imagine that for every intersection on the grid there is multiple possibilities. Either there's a black stone or a white stone or nothing. And on a 13 by13 grid you can imagine how many possibilities of a board state there are. It's um impossible to capture all of that with historical moves from professional players. It would just never cover that. The same thing could be said in chess as well. you you know that even the professional players can plan X number of steps in advance but nobody knows where the game takes you and in the late stages of the games or the end games players always find themselves playing a different game and that's part of the magic of being good at chess. Um so yeah that's the problem. What's another problem or shortcoming beyond the fact that we can't observe possibly all the states?
Yes. >> Correct. Correct. You, if I repeat what you said, well, first, you don't even know if this was a good move, you know? So, maybe it was not even a good move and you're learning something that was not a good move and you're labeling it as a good move. And second, um, you're actually only getting partial information, meaning you don't have the information of what's in the person's mind and what strategy they're trying to execute. So your store, you're sort of looking at a single example among a
long-term strategy and you can't expect the model to guess what's the long-term strategy because it was just trained on X and Y and matching the inputs to a possible output. So you you don't really have any concept of a strategy at that point. it looks one off at every decisions of the model. Okay, those are really good points. Um the other one is the ground truth might be illdefined. What I mean by that is um even the best humans in the world do not play their best game every day and even
their best game is not the ground truth. And that creates an issue because you're essentially training against a target that is off by a certain margin. You're never going to get better than the best human and the best human is not the best possible um existing the best possible strategy at every point. So you could argue what if we get a panel of experts that we're monitoring and those are the best players in the world. Even with a panel of expert that decides every move, you're still have an illdefined ground truth, you know. So that's a big issue. Too many states in the game you mentioned and we will likely not generalize which is what you said meaning we're looking at one-off situations. We're not looking at entire strategies and so when we face a board state that we've never seen before because the model was not trained on strategy it sort of we get stuck. Yeah. Okay. And this is an example of a perfect application for reinforcement
learning because reinforcement learning is all about delayed labels and making sequences of good decisions. So if you had to remember in one sentence what's RL RL is making good sequences of decisions, sequences of good decisions, sorry. and do that automatically. Another way to look at it is the difference between you know classic supervised learning and RL is in uh in classic supervised learning you teach by example. In reinforcement learning you teach by experience which is also a different concept. You're not just showing cats and non-cats to a model. You're actually letting the model experience an environment until it figures out what were the best decision it made and learns from them.
Some examples of reinforcement learning applications I'm going to mention them. We we have we have gaming of course that we already covered. What are other applications of AI where we need good sequences of decisions? Yes. autonomous driving. Yeah, correct. I mean, in driving, you could argue RL could work and there's some RL going on, but what you mean, I think, is you you have some sort of a dynamic planning algorithm that allows you to strategize. If you see a a red light ahead, you
might start slowing down over time, but maybe it will turn green, so you might not slow down completely. This is an example of a strategy that you need, of course. >> Yeah. robotic controlling. That's a great example also related to autonomous driving. But imagine you uh want to teach to a robot to move from point A to point B. The number of good decisions that the robot needs to make in terms of moving each of their joints is tremendous. Like it's actually super
unlikely that a robot would move from A to B if it's not trained to make good sequences of decisions. What else? Actually, the biggest one nobody mentioned yet. >> It's not a great application. I don't like it, but it happens to be the biggest one over enforcement learning. Yeah. >> Yeah. Yeah. Yeah. Advertisement. Yeah. Marketing. You're right. So, yeah, we talked about robotics. Advertisement is another example. Um, advertisement is a long game. Like companies are
showing you multiple ads before you buy. And in fact, the reason rein reinforcement learning is important is because, you know, they're planning a strategy that might lead a buyer to execute a purchase over time and it requires long-term thinking. So there's a lot of reinforcement learning applied to marketing, advertisement, real time bidding processes etc. Okay, clear on what RL is and how it differs from classic supervised learning? No. Okay. Um, so let's put some
vocabulary around that concept. In reinforcement learning, you have an agent and the agent interacts with an environment. As the agent interacts with the environment, the agent will perform certain actions that we will denote a t where t is a time step. And the environment will show you states that transition from time step t to time step t + one. So subject to an action at an environment may transition from a st to st plus one. You can think of the game of go. I take the action of putting my
black stone on a certain grid intersection and the environment has changed. It moved from the state has changed. It moved from state time step t to time step t + one where my stone is on the grid. After that um state update happens, there's two things that the agent observes. the the the agent observes an observation that we will note OT and a reward RT. Okay, so those are the vocabulary words and of course the goal of the agent will be to maximize the rewards.
One thing to know about the observation, we'll talk about it a little more. Um the observation sometimes is equal to the state. Can someone guess why we might need two concepts instead of a single concept? Why is it important to have a state and an observation? >> Yes. >> Yes. Correct. So in some cases the environment may not be fully um you know transparent to the user and so for example in chess or in go the observation is actually equal to the state you see everything on your board
all the information is available to you if you play League of Legends or Starcraft you know the concept of you know I think in English it's called like a cloud or a fog I think it's a fog you only see certain part of the map until you have explored everything or until your friends are sort of visiting the other parts of the map. And so the observation is actually less information than the states of the environment. Okay. And then the last piece of vocabulary is a transition. When I refer to a transition, I refer of the process of getting from state T plus one, which means we're in state T. The agent takes an action A. It observes OT and a reward RT and it transition to the next state ST plus one. Question.
Uh wait what do you mean? You mean is there are there examples of environment where the state is so large that the entire >> Yeah. Possibly for computational reasons. Yeah. Yeah. You might have games. I mean, look at open world games. Like truly, you could you could argue, I don't know, there are some games where you might press start and you see the entire environment, but who cares of what's happening 20,000 kilometers west of you if you're in a certain location. That might not influence your strategy. So you might actually put some sort of a you know trust circle or like some sort
of a circle in which you observe which you think has 99% of the information you need possibly for computational reasons. That's a good point. Okay, let's get to a practical example of a reinforcement learning algorithm and develop it together. This example is called recycling is good because recycling is good but also because it's a simple example illustrative of reinforcement learning. So let's say we have a a small environment with uh five states. There is a starting state marked in brown
which is state two. It's our it's our initial state. And then on the right side, sorry, on the left side, you have state one, which is a garbage. And it's great to get to the garbage because you're going to be able to recite to to put in the garbage the, you know, the stuff that you have in your hands. You know, you're trying to throw away some garbage and the garbage can happens to be there and so we would expect there to be a reward. On the other side, if you actually go to the right, you might pass by state three, which is empty. You might pass by stage four where there is a chocolate uh packaging that is left on the ground that you can pick up and it's good to pick it up. And then on stage five, state five, you have the recycle bean which is more valuable than the garbage can because you can recycle and you should get better rewards for that. So that's our game. In this game, we define a reward that is associated with
the type of behaviors that we want the agents to learn. Um, and the reward is as followed. That's just one example. Plus two for throwing your garbage in the normal can, plus one for picking up the chocolate packaging, and plus 10 if you manage to make it to the recycle bin. Is it clear? Now, the goal will be, and that's the case in reinforcement learning often time to maximize the return. We'll define formally the return but think about it as maximize the amount of
rewards that you get as you go through this journey and you make your decisions. In this specific game, we have five states and there's three types of state. In brown is the initial states. We have normal states and we have in blue terminal states. When you get to a terminal states in reinforcement learning, it will typically end the game. it will end one episode of the game. You move to another episode. You'll get back to the starting state or initial state and you'll redo another episode.
The possible actions for our agent here are going to be fairly simple. Left and right. And we're going to add an additional rule that is important, which is that the garbage collector comes in 3 minutes and it takes a minute to get from one state to the other. Why is that an important rule to add to the game? Can you guess? Yeah. >> Yeah. Otherwise, you just go back and forth between uh stage three and stage four. You just collect a bunch of chocolate packaging and you never make
it to the bin. And so, it's not what we want. Yeah. Okay. So, how do we define the long-term return? The long-term return is going to be defined as capital R, which is the sum of rewards with a discount. Discount is a very important concept in reinforcement learning. It's also a very natural concept to think about. Can you think of what what the discount would represent in for humans? Do you have an example of what it could be? Yeah. >> Huh? money. >> Yeah. The value of money and time.
Exactly. Or the energy that a robot might have, things like that. Yeah. You you would rather get, you know, a dollar now than a dollar in 10 years knowing that there's some inflation, for example. That's the example of a discount. In reinforcement learning is the same. You know, let's say you have a strategy that takes so much time. You need to discount it because your robot might lose energy as you're going through it. For example, these counts can vary, you know, but they stay between zero and one. Um, so what is the best strategy to follow if gamma the discount is equal to one meaning you know time doesn't matter here if it's longer or shorter just want to maximize the return. Best strategy to follow.
Let's give it a try. Someone who hasn't spoken yet. >> Yes. >> Bounce around. But remember the rule of uh 3 minutes. You can't bounce around because you you will not get to the terminal state before the time allotted is done. But that would be a good idea if this rule was not true. What else could you do?
The idea? It's an easy one. No, it's not too hard. best strategy for gamma equals 1 and give me also the maximum reward you would get. People are are sleepy today. Yeah. >> Recycle. >> Go to the recycle. So, right, right, right. >> Yeah, that's right. Thank you. Right, right, right. And then what's your Sorry. What's your um what's your total reward? Yeah, that's right. 11. So that's where we get terminal state and we grab our reward of 11. Very good. Now assuming 0.9 for gamma. We're going to complexify things a little bit. I'm going to walk you through a very simple algorithm that you know allows us to sort of determine
the best strategy and we will put our numbers in a matrix. So for instance um we'll define a Q table and Q stands you know is a it's a it's a value function um where the the the name Q-learning Q star you might have heard um all of these things come from Q-learning and so let's say we have a Q table which has uh the size of number of states times number of actions so five rows two columns in our Every entry of the Q table is essentially representing how good it is to take action A in state B.
Do you agree that if we had a table with these numbers essentially we solve the problem meaning at any point the agent can just look in the table I am in state three let's look at column one that would tell me the value of action one and let's do that column two it would tell me the value of action two so I have everything I need to make my decisions so that table is really the the thing you want to find in this exercise now the way we will find the table is sort
of using a backtracking algorithm where we might actually codify the environment as a tree and traverse the tree. So here's what it looks like. I start in S2 and I have two options ahead of me. I can go to the left where I will get a reward of two. It's an immediate reward. The immediate reward is not discounted. It's an immediate reward. Remember the the formula for R. The immediate reward R0 is not discounted. That would take me to S1. It's a terminal state, so there's nothing to do after.
Second option, I go to the right and I get a reward of zero. That's my immediate reward. And I end up in state three. State three is not a terminal state. So I can go and do the same exercise from state three. In state three, I have two options. I can go to the left where I would see a reward of zero and I will end up in S2 or I will go to the right and I will get an immediate reward of plus one. It's an immediate reward. We're not discounting it. I will end up in S4 and from S4
again I have two options. Back to the left to S3 with zero reward or to the right with the amazing reward of plus 10 and the terminal state of S5. So that's my map of immediate rewards. That's not my discounted return. So what we're going to do now is we're going to backtrack up the tree in order to compute the discounted returns. Actually, if I'm in S3 right here, I see that I can get an immediate reward in S4 of one. And I want to compute my maximum return that I can get from when
I'm in S3. My maximum return is that in S4 I could get a plus 10, right? But I need to discount that. My discount is 0.9. So I multiply 10 by 0.9. What it tells me is that from S4 I can expect nine plus one, which I get as an immediate reward from moving from S3 to S4, I can update this number to 10. Meaning from S3 the best you can hope for is a discounted return of 10 which is 1 plus 0.9* 10. Everyone follows. Now let's do the same exercise one step before in S2. uh you know um in S2 um I
have um an immediate reward of zero for going to S3 or an immediate reward of two for going to S1. Um S1 is not going to be worth it. We already know that because when I'm in S3 I can actually expect 10 which I have to discount. 0.9 * 10 gives me 9 plus 0 immediate reward from S2 to S3. That tells me that the discounted return from state two which is our initial state is nine. Good follow just a simple backtracking. Now I can copy back this. So S3, I know that when I'm in S3, um uh you know, I
can expect zero immediate reward to um uh to sorry, if I if I'm if I'm in S2, I can expect uh zero immediate reward plus a discount times the plus 9 that I could expect in S3. And so that gives me values that should cover everything that we have in this Q table. So I I do that backtracking. I copy paste all of that into my Q table all the way up here. And this is what I get. We essentially finish the game at this point. We um can look uh at a certain row. So let's say I'm in state number
three. I look on the third row of that Q table and I see that I have two options. If I go back to S2, ultimately my discounted return will be 8.1, right? If I actually go to S4 on the right, I will get 10 because I will get 1 + 0.9 * 10, which is 10. So this is a toy example, but it tells you that if you were able to backtrack through the entire environment, you will be able to build a massive Q table and you will be able to give it to your agent to make its decisions. >> Yeah.
>> Sorry, can you repeat? >> Yeah, here I'm simplifying. I'm not considering the time uh remaining. But in practice um you if if I remove the time component so I remove the fact that there's a three minute deadline before the garbage collector comes then uh this would uh be slightly more difficult because you would have to do a time series essentially of adding the discount times the reward that you collect. Yeah, but I'm simplifying here and that's why I use the three-minut rule. [Music]
Any question on the Q table? Super. Okay. So, uh this was the Q table and in fact we can put together our strategy for gamma equals 0.9. The best strategy is still the same. You go to the right and you can expect a return of 9. Now, one of the most important concepts in reinforcement learning is this equation on the board called the Bellman optimality equation. Often time you'll see it noted as Q star of state S and action A equals R + gamma time the max of that
same function applied to S prime A prime. Let me explain this equation for you because it's super important. This equation is called the optimality equation because your optimal Q table will follow this equation. If you have finished the game, this equation can be applied to any state action pair and it will still be true. The intuition behind why the Bellman equation is the optimality equation is that um if you're in a if you have the perfect Q function Q table um and you're
in a certain state and you perform a certain action A you will observe a reward and this reward will uh you know you you have taken an action so you would be in a new state and from that new state you can repeat what you just did right and because uh you've done the backtracking and stuff like that, you will uh get this equation to be true because it's the reward plus discount times the best next action that you could be taking.
Does that make sense? Any question on that? That's exactly the backtracking that we did by the way. immediate reward plus discount times the best possible action that you can take in the next state s prime. The last concept I cover in terms of vocabulary is the policy. The policy is the function that given your state is going to tell you what to do. And in Q-learning the way this policy is defined is argmax of Qstar um across the action. So essentially what it says is like look in the table and look at a certain state s you want the policy which is what you should do. It's the function that tells you our best strategy. You just look at the two possible actions which one has the highest Q value and select that action. That's it.
This is a very simple example but it it's the core of um Q-learning that you know later on you will use policies widely. There's a lot of reinforcement learning algorithms but this concept of understanding the policies the function telling us our best strategy in Q-learning it's the argmax of the best Q value in the given state. It tells you which action to take that's the core thing you need to understand. So remember this belman equation because we're going to reuse it in a bit. The main issue um with this approach um of a Q table is that state and action spaces can be super large and having a matrix that you discover through backtracking um and where every time you want to do an action you have to look up the given state the possible action it becomes impossible. Like imagine you using this algorithm for the game of go where there's so many states, there's so many possible actions, you can put your stone anywhere on the board. You can imagine
how big this matrix becomes and how impossible it is to use. So that's our problem and that's the moment where deep learning comes into play. So let's look at it. uh the the the oh actually before I go there I'm just going to cover some vocabulary. We said the environment, the agent, the state, the action, the reward, the total return and the discount factor. We learned all of that. We saw that the Q table is the matrix of entries representing how good is it to take action A in state S. And the policy is the function that tells us what's the best strategy to adopt. And the bellman equation is satisfied by the optimal Q table.
So let's get to deep Q-learning which is what I was about to say is we are going to frame the problem slightly differently. So instead of using a Q table, we're going to use the fact that neural networks are universal function approximators and we're going to define a Q function that's essentially a neural network. So that the function can take a state S and an action A and tell you how good that action is in state S. So instead of a lookup in a matrix, you just run a forward pass in a neural
network and it gives you the answer. That feels like a better solution for games where there's a lot of states and a lot of actions. So here is a same problem statement. In the past we looked for a Q table and this time we will look for a neural network. One of the things we're going to do is to define the output layer to have two outputs. So given a certain state as input think about it as a one hot vector encoding the state. So this one is the example of state two 0 1 0 0
0. If you pass state two in this Q function with multiple layers, it will give you two outputs. One output that corresponds to Q of S action right and the other one Q of S action left because it's the two actions. If we had more actions to take, we would just increase the output layer and we might have many more neurons in the output layer.
So the big question is how the hell are we going to train that network? Because we're not in classic supervised learning. We don't have labels. So this one is a hard question but u what do you what would you do given we we don't have traditional x and y pairs how are you going to train this neural network because remember at the beginning this neural network will give you garbage it will take a state s and it might tell you go to the left or to the right but it's completely random so how are you going to tune it to the level where it makes really Good decisions.
Yes. >> Assume based on some prior knowledge. Tell me more. What? >> So what are the things we know about this problem right now? What are the the the rules of the game that we could use in order to I'm I'm seeing what you say. You say we could estimate what good looks like but based on what?
>> Okay. So reward structure you're saying that's one thing we have in every game. We have a reward structure for every state that definitely should be used in order to estimate the good what a good decision looks like. Yeah. The problem is not in every state you will see a reward. And if you look at many games of like go you might not see a reward until 50 moves. >> So what do you do in this case? >> Yes. Can we run through a bunch of actions and space and see what the
output is and get more data? >> Yeah. So you could you're actually um bringing up a sort of a tree search, right? You go down the tree, you do every possible action and then you backtrack. >> Not every possible action. >> So which actions? >> Trying to spread it out of the >> Okay, that's that's we're getting there. So first possibility is we just go down the tree in the game of go. You could put your stone everywhere. So the tree already start by a 13 by3 options and
then it's exponentially grows impossible. It's intractable. But what you said is what if there are certain actions that are more likely than others? Do we need actually to explore the entire tree? What's this like? What are you using when you're saying that? How do you determine what action might be better than another one? >> Expected return. And we're getting close. Yeah. But you know, how do you know the expected return without going through the tree once? At least >> you can estimate both. >> Okay. You can estimate it using what?
>> Yeah, maybe. Yeah. What? What? So that's exactly what we're going to do actually. But we're going to use the the Bellman equation because there are two things we know about this problem. We know the reward structure which you brought up and we also know that the perfect Q function will follow the Bellman equation that we know as well. At the end the Bellman equation should be respected meaning for every state if you want to know the Q value of that state given an action. The way you will get
that is you will look at the immediate reward plus the discount times the best Q value from the next state across all actions. that equation will be respected. So those are the only information we have and we're going to use them drastically to define our labels and sort of mimic a classic supervised learning approach. So here's what we have. We have our neural network. We have Q S to the left and QS to the right that represent how good it is to go to the left in that state versus the right. And then I've pasted the Bman equation on top right of the screen. We're going to define a loss function. So let's say for the sake of simplicity because those are scalar values that will use you know L2 loss quadratic loss that compares a certain label Y to um a certain Q value of a state and a certain action. So what we would like is to minimize this loss function meaning Y and the Q value for a given action in a given state is as close as possible to each other. and
we're going to leverage the reward and the Bman equation. So let's do um two things. Right now we don't have a Y. So in supervised learning you will have a picture of a cat. There's a cat. The Y is one or zero. Here we don't have a Y. So we have to come up with an estimate of a good Y at least better than random. So let's say at this point in time when I send a state S in the network, it turns out that Q of going to the left is higher than Q of going to the right.
Which means that today at that moment the Q function tells me it's better to go to the left than to go to the right. That is random at the beginning. It's completely random. Right? So what I'm going to do is I'm going to use as my target value Y the immediate reward that I observe on the left plus gamma times the best Q value that I can get. So the best action that I could take in the next step based on my current Q value.
That's very important. So remember this target is off. It's not a perfect target, but it's better than nothing. Meaning, not only it tells us, hey, there is a good reward to the left. We should consider that in saying that that might be a good move because we're seeing an immediate reward. But on top of that, we also know that at the end of training, the Q value should follow the Bellman equation. So why don't we set the target as the Bellman equation? So we add the discounted maximum future
reward when you are in the next state. So you were in state S. You go to the left now you're in state S next left and you look again at your Q values and you select the best one. Then you add that number here. So there is actually two forward path in that process. Right? There's one forward path where you send the state S in Q and you look at the two options left or right and you're like okay I'm going to the left and then you're like I'm going to compare that value to a target Y but to get that
target Y I need to do another forward path. So I take my action left I perform it I get an S prime state s next and I send that S next into the Q network. I look at the two options I have. I pick the best one and I add it here with a discount. So fundamentally what's happening is is the following is we have a Q network that's random at the beginning. It has never observed the rewards. We just know that at some point it will get to the Q um it will get to a perfect you know
policy. It will get to a perfect Q function. But the best we can do right now is to say as a guide to for our agent, we will look at the immediate reward and we will look at the Bellman equation which should tell us a better estimate than where we are right now and we will try to catch up to that estimate and then we do that again and again. So remember every time your Q gets better it gets better for the next state as well. So you know the Bellman equation tells you estimate it with the second forward path and you just keep getting better and better as you're observing more rewards.
left and >> uh how would it so describe the loop you >> imag >> for going to a right you again need the target >> yeah you would stop at that point so what you yeah this is a good question I I'll show you how we fix certain things but you do only one step meaning you have your Q value at this point and it tells you go to the left and you just want to target Y. So what you do is you put left and you look at your next state. You forward propagate your next state. You look at the two options. You pick the best. You don't go further. You
just use that one step. You look one step ahead essentially. You don't look multiple steps ahead. You could, but it would be more computationally heavy to do one more step again and so on. So yeah. Yeah. >> Yeah. It seems like you're learning the function locally like most people know like how fast that
>> it will typically be a function of the environment the state space um how long it will take to converge but you're perfectly right that um as the Q function gets better, the estimate Y also gets better. So the two things get better together, right? Because the Y is based on the Q function. And if the state space is massive, you might have a very difficult time training this model. There's better approaches that we'll see later. Yeah, correct. There was a question there.
>> Yeah. So here I'm just saying let's say when you send state S in Q the left happens to be higher than right. But the same happens on the other side. Let's say let's say the left is worse than right. Then what you will do is you will define your target Y as the reward that you observe on the right plus from the next state of having gone to the right what's the best action and what's the Q value for that pair and then it will give you the target for that scenario.
So one complication with this training is that when you want to differentiate L. So you want to perform a back propagation. You want to take the derivative of L with respect of the parameters of the network. You want Y to be a fixed thing, right? Because in supervised learning Y is not differentiable. It's just a fixed number zero or one or a certain number. So here we're going to simplify and we're going to say this term that is dependent on the Q network. So technically this term has parameters. So if you actually differentiate it, it will give you a value. We'll just hold it um fixed. So we say we do use our Q network to perform an estimate of our Y but we will not differentiate it. We will say it's not it's fixed. Yeah. >> Can you explain why we discount this? >> Yeah. >> Because you know going back to the reason we discount is like the value of time. It's like you probably want to say if you can win the game in 10 moves, win it in 10 moves rather than 100 moves. Um
or if you can get $1 today, get $1 today rather than in 10 years. All of that is why we have a discount here. And the discount is a is a hyperparameter that you would define as well. That would influence the strategy of your agent. >> Once more pass. >> We're going to see it after. Actually, I'm going to do a concrete example because it's a little complicated. Yeah. Yeah. >> Q. >> No, no, that that was it's a good point. It's not that. It's just Q of it's it's a 2 by two. It's a one by two. So you have left and right. I was just going down the first case. So I put the state left. Yeah. Yep.
>> What if the rewards are not fixed? I mean, in most games we're going to see right now, the rewards are going to be fixed by the designer of the game, the human that's designing the game. Um, in practice, you could have a separate function that um actually comes up with the reward. We're going to see an example later in the lecture where the reward might be different in different scenarios and there's a function or sometimes called a critic that determines what's the reward in a certain state. Okay, this is yeah one last question and then we move because we we're going to see a concrete example is going to be clear. >> Um so when we like hold it fix for laptop is that what differentiates this from iterating through all of the like possible >> Yeah. Yeah. So instead of doing the backtracking down the tree and going over everything, we're saying we're going to limit oursel to just picking the best action based on our current understanding of the network.
You see, like my network is kind of intelligent, not great. We're in the middle of training. It says that I should go to the left and then if I look at the next state when I'm in the left, it says I should go to the right. I will trust it because it's the best I have, best estimate I have, but I will discount that. And then if you keep repeating that, it turns out that not only your estimate gets better, but your model gets trained and then ultimately both together get to an optimality equation. So it's a it's a funky concept, right? But you get it. We're going to see examples. Um, okay. So uh then once you have been able to use the Belman equation to estimate your targets, you perform classic back propagation and you update the parameters of the network and you repeat that process. Okay. Uh here is concretely if you were to code it in pseudo code, here is what it would look like to train a NRL agent using Q-learning. We start by initializing our Q network parameters.

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