10.12 Continuous State Spaces | Learning the state-value function -[Machine Learning | Andrew Ng]
 Let's see how we can use reinforcement learning to control the lunar lander or for other reinforcement learning problems. The key idea is that we're going to train a neural network to compute or to approximate the state action value function Q of s, a, and that in turn will let us pick good actions. Let's see how this works. The heart of the learning algorithm is we're going to train a neural network that inputs the current state and the current action and computes or approximates Q of s, a. In particular, for the lunar lander, we're going to take the state s and any action a and put them together. Concretely, the state was that list of eight numbers that we saw previously. So you have x, y, x dot, y dot, theta, theta dot, and then LR for whether the lakes are grounded. So that's a list of eight numbers to describe the state. Then finally, we have four possible actions, nothing left, main or main engine and right. And we can encode any of those four actions using a one heart feature vector. So if action were the first action, we may encode it using 1, 0, 0, 0. Or if it was the second action to find the left thruster, we may encode it as 0, 1, 0, 0. So this list of 12 numbers, eight numbers for the state and then four numbers, a one heart encoding of the action is the inputs we'll have to the neural network. And I'm going to call this x. We'll then take these 12 numbers and feed them to a neural network with say 64 units in the first hidden layer, 64 units in the second hidden layer, and then a single output in the output layer. And the job of the neural network is an output Q of SA, the state action value function for the lunar lander given the input S and A. And because we'll be using neural network training algorithms in a little bit, I'm also going to refer to this value Q of SA as the target value y that we'll train the neural network to approximate. Notice that I did say reinforcement learning is different from supervised learning. But what we're going to do is not input a state and have it output an action. What we're going to do is input a state action pair and have it try to output Q of SA and using a neural network inside your reinforcement learning algorithm this way will turn out to work pretty well. We'll see the details in a little bit. So don't worry about it if it doesn't make sense yet. But if you can train a neural network with appropriate choices of parameters in hidden layers and in the output layer to give you a good estimates of Q of SA, then whenever your lunar lander is in some state S, you can then use the neural network to compute Q of SA for all four actions. You can compute Q of S nothing, Q of S left, Q of S main, Q of S right. And then finally, whichever of these has the highest value, you pick the corresponding action A. So for example, if out of these four values, Q of S main is largest, then you would decide to go and fire the main engine of the lunar lander. So the question becomes, how do you train a neural network to output Q of SA? It turns out the approach will be to use Bellman's equations to create a training set with lots of examples X and Y, and then we'll use supervised learning exactly as you learned in the second course when we talked about neural networks to learn using supervised learning, a mapping from X to Y. That is a mapping from the state action pair to this target value Q of SA. But how do you get a training set with values for X and Y that you can then train a neural network on? Let's take a look. So here's the Bellman equation. Q of SA equals R of S plus gamma max of A prime Q of S prime A prime. So the right hand side is what you want Q of SA to be equal to. So I'm going to call this value on the right hand side Y and the input to the neural network is a state and an action. So I'm going to call that X. And the job of a neural network is to input X, that is input the state action pair and try to accurately predict what will be the value on the right. So in supervised learning, we were training a neural network to learn a function F, which depends on a bunch of parameters W and B, the parameters of the various layers of the neural network. And it was a job of the neural network to input X and hopefully output something close to the target value Y. So the question is, how can we come up with a training set with values X and Y for a neural network to learn from? Here's what we're going to do. We're going to use the lunar lander and just try taking different actions in it. If we don't have a good policy yet, we'll take actions randomly. By the left fester, by the right fester, by the main engine, do nothing. And by just trying out different things in the lunar lander simulator, we'll observe a lot of examples of when we're in some state and we took some action, maybe a good action, maybe a terrible action, either way. And then we got some rewards R of S for being in that state. And as a result of our action, we got to some new state S prime. As you take different actions in the lunar lander, you see these S, A, R of S, S prime, and we call them tuples in Python codes many times. For example, maybe one time you're in some state S and just to give this an index, I'm going to call this S1. And you happen to take some action A1, this could be nothing left main fester or right. As a result of which you got some reward, and you wound up at some state S prime one. And maybe a different time you're in some other state S2, you took some other action, could be a good action, could be a bad action, could be any of the four actions, and you got the reward and then you wound up with S prime two, and so on, multiple times. And maybe you've done this 10,000 times or even more than 10,000 times. So you would have to save the way with not just S1, A1, and so on, but up to S10,000, A10,000. It turns out that each of these lists of four elements, each of these tuples will be enough to create a single training example, X1, Y1. In particular, here's how you do it. There are four elements in this first tuple. The first two will be used to compute X1, and the second two will be used to compute Y1. In particular, X1 is just going to be S1, A1 put together. S1 would be eight numbers, the state of the lunar lander, A1 would be four numbers, the one-hot encoding of whatever action this was, and Y1 would be computed using the right hand side of the Bellman equation. In particular, the Bellman equation says when you input S1, A1, you want Q of S1, A1 to be this right hand side, to be equal to R of S1 plus gamma max over A prime of Q of S1 prime A prime. And notice that these two elements of the tuple on the right give you enough information to compute this. You know what is R of S1, that's the reward you've saved away here, plus the discount factor gamma times max over all actions A prime of Q of S prime 1, that's the state you got to in this example, and then take the max over all possible actions A prime. And so I'm going to call this Y1, and when you compute this, this will be some number like 12.5 or 17 or 0.5 or some other number, and we'll save that number here as Y1 so that this pair, X1, Y1, becomes the first trading example in this little data set we're computing. Now you may be wondering, wait, where does Q of S prime A prime or Q of S prime 1 A prime come from? Well, initially, we don't know what is the Q function, but it turns out that when you don't know what is the Q function, you can start off with taking a totally random guess for what is the Q function. And we'll see on the next slide that the algorithm will work nonetheless. And every step, Q here is just going to be some guess that will get better over time, it turns out, of what is the actual Q function. Let's look at the second example. If you had a second experience where you state S2, took action A2, got that reward, and then got to that state, then we would create a second training example in this data set, X2, where the input is now S2, A2, so the first two elements go to computing the input X, and then Y2 will be equal to R of S2 plus gamma max over A prime Q of S prime 2 A prime. And whatever this number is, Y2, we put this over here in our small but growing training set. And so on and so forth until maybe you end up with 10,000 training examples with these X, Y pairs. And what we'll see later is that we'll actually take this training set where the Xs are inputs with 12 features and the Ys are just numbers, and we train a neural network with, say, the mean squared error loss to try to predict Y as a function of the input X. So what I describe here is just one piece of the learning algorithm we'll use. Let's put it all together on the next slide and see how it all comes together into a single algorithm. So let's take a look at what the full algorithm for learning the Q function is like. First, we're going to take our neural network and initialize all the parameters of the neural network randomly. Initially, we have no idea what is the Q function. So let's just pick totally random values of the weights and we'll pretend that this neural network is our initial random guess for the Q function. This is a little bit like when you are training linear regression and you initialize all the parameters randomly and then use gradient descent to improve the parameters. Initializing randomly for now is fine. What's important is whether the algorithm can slowly improve the parameters to get to a better estimate. Next, we will repeatedly do the following. We will take actions in the lunar land. So fly around randomly, take some good actions, take some bad actions. It's okay either way. But you get lots of these tuples of when it was in some state, you took some action A, got a reward R of S, and you got to some state S prime. And what we will do is store the 10,000 most recent examples of these tuples. As you run this algorithm, you will see many, many steps in the lunar lander, maybe hundreds of thousands of steps. But to make sure we don't end up using excessive computer memory, common practice is to just remember the 10,000 most recent such tuples that we saw taking actions in the MDP. This technique of storing the most recent examples only is sometimes called the replay buffer in a reinforcement learning algorithm. So for now, we just find the lunar lander randomly, sometimes crashing, sometimes not, and getting these tuples as experience for our learning algorithm. Occasionally, then, we will train the neural network. In order to train the neural network, here's what we'll do, we'll look at these 10,000 most recent tuples we had saved and create a training set of 10,000 examples. So training set needs lots of pairs of X and Y. And for our training examples, X will be the SA from this part of the tuple. So it'll be a list of 12 numbers, the eight numbers for the state and the four numbers for the one-hot encoding of the action. And the target value that we want a neural network to try to predict will be Y equals R of S plus gamma max of A prime Q of S prime A prime. How do we get this value of Q? Well, initially, is this neural network that we had randomly initialized. So it may not be a very good guess, but it's a guess. Simply creating these 10,000 training examples will have training examples X1, Y1 through X 10,000, Y 10,000. And so we'll train a neural network. And I'm going to call the new neural network Q new, such that Q new of SA learns to approximate Y. So this is exactly training that neural network to output F with parameters W and B to input X to try to approximate the target value Y. Now, this neural network should be a slightly better estimate of what the Q function or the state action value function should be. And so what we'll do is we're going to take Q and set it to this new neural network that we had just learned. Many of the ideas in this algorithm are due to min at all. And it turns out that if you run this algorithm where you start with a really random guess of the Q function, but use Bellman's equations to repeatedly try to improve the estimates of the Q function, then by doing this over and over, taking lots of actions, training a model that will improve your guess for the Q function. And so for the next model you train, you now have a slightly better estimate of what is the Q function. And then the next model you train will be even better. And when you update Q equals Q new, then for the next time you train a model, Q of S prime A prime will be an even better estimate. And so as you run this algorithm on every iteration, Q of S prime A prime hopefully becomes an even better estimate of the Q function. So that when you run the algorithm long enough, this will actually become a pretty good estimate of the true value of Q of S A so that you can then use this to pick hopefully good actions for the MDP. The algorithm you just saw is sometimes called the DQN algorithm, which stands for deep Q network, because you're using deep learning and neural network to train a model to learn the Q function. So hence DQN or deep Q network, DQ using a neural network. And if you use the algorithm as I described it, it will kind of work okay on the lunar lander. Maybe it'll take a long time to converge. Maybe it won't land perfectly, but it'll sort of work. But it turns out that with a couple refinements to the algorithm, it can work much better. So in the next few videos, let's take a look at some refinements to the algorithm that you just saw.
[{"start": 0.0, "end": 8.64, "text": " Let's see how we can use reinforcement learning to control the lunar lander or for other reinforcement"}, {"start": 8.64, "end": 10.200000000000001, "text": " learning problems."}, {"start": 10.200000000000001, "end": 16.86, "text": " The key idea is that we're going to train a neural network to compute or to approximate"}, {"start": 16.86, "end": 23.64, "text": " the state action value function Q of s, a, and that in turn will let us pick good actions."}, {"start": 23.64, "end": 25.2, "text": " Let's see how this works."}, {"start": 25.2, "end": 30.48, "text": " The heart of the learning algorithm is we're going to train a neural network that inputs"}, {"start": 30.48, "end": 38.879999999999995, "text": " the current state and the current action and computes or approximates Q of s, a."}, {"start": 38.879999999999995, "end": 45.8, "text": " In particular, for the lunar lander, we're going to take the state s and any action a"}, {"start": 45.8, "end": 47.519999999999996, "text": " and put them together."}, {"start": 47.519999999999996, "end": 53.519999999999996, "text": " Concretely, the state was that list of eight numbers that we saw previously."}, {"start": 53.52, "end": 61.52, "text": " So you have x, y, x dot, y dot, theta, theta dot, and then LR for whether the lakes are"}, {"start": 61.52, "end": 62.84, "text": " grounded."}, {"start": 62.84, "end": 66.2, "text": " So that's a list of eight numbers to describe the state."}, {"start": 66.2, "end": 73.0, "text": " Then finally, we have four possible actions, nothing left, main or main engine and right."}, {"start": 73.0, "end": 78.66, "text": " And we can encode any of those four actions using a one heart feature vector."}, {"start": 78.66, "end": 86.24, "text": " So if action were the first action, we may encode it using 1, 0, 0, 0."}, {"start": 86.24, "end": 92.84, "text": " Or if it was the second action to find the left thruster, we may encode it as 0, 1, 0,"}, {"start": 92.84, "end": 93.84, "text": " 0."}, {"start": 93.84, "end": 99.96, "text": " So this list of 12 numbers, eight numbers for the state and then four numbers, a one"}, {"start": 99.96, "end": 105.24, "text": " heart encoding of the action is the inputs we'll have to the neural network."}, {"start": 105.24, "end": 107.52, "text": " And I'm going to call this x."}, {"start": 107.52, "end": 113.84, "text": " We'll then take these 12 numbers and feed them to a neural network with say 64 units"}, {"start": 113.84, "end": 119.16, "text": " in the first hidden layer, 64 units in the second hidden layer, and then a single output"}, {"start": 119.16, "end": 121.32, "text": " in the output layer."}, {"start": 121.32, "end": 128.07999999999998, "text": " And the job of the neural network is an output Q of SA, the state action value function for"}, {"start": 128.07999999999998, "end": 134.6, "text": " the lunar lander given the input S and A. And because we'll be using neural network"}, {"start": 134.6, "end": 140.84, "text": " training algorithms in a little bit, I'm also going to refer to this value Q of SA as the"}, {"start": 140.84, "end": 146.16, "text": " target value y that we'll train the neural network to approximate."}, {"start": 146.16, "end": 150.95999999999998, "text": " Notice that I did say reinforcement learning is different from supervised learning."}, {"start": 150.95999999999998, "end": 155.88, "text": " But what we're going to do is not input a state and have it output an action."}, {"start": 155.88, "end": 162.4, "text": " What we're going to do is input a state action pair and have it try to output Q of SA and"}, {"start": 162.4, "end": 167.36, "text": " using a neural network inside your reinforcement learning algorithm this way will turn out"}, {"start": 167.36, "end": 169.0, "text": " to work pretty well."}, {"start": 169.0, "end": 171.56, "text": " We'll see the details in a little bit."}, {"start": 171.56, "end": 174.8, "text": " So don't worry about it if it doesn't make sense yet."}, {"start": 174.8, "end": 180.0, "text": " But if you can train a neural network with appropriate choices of parameters in hidden"}, {"start": 180.0, "end": 187.36, "text": " layers and in the output layer to give you a good estimates of Q of SA, then whenever"}, {"start": 187.36, "end": 193.96, "text": " your lunar lander is in some state S, you can then use the neural network to compute"}, {"start": 193.96, "end": 197.08, "text": " Q of SA for all four actions."}, {"start": 197.08, "end": 202.84, "text": " You can compute Q of S nothing, Q of S left, Q of S main, Q of S right."}, {"start": 202.84, "end": 208.72000000000003, "text": " And then finally, whichever of these has the highest value, you pick the corresponding"}, {"start": 208.72000000000003, "end": 216.68, "text": " action A. So for example, if out of these four values, Q of S main is largest, then"}, {"start": 216.68, "end": 221.64000000000001, "text": " you would decide to go and fire the main engine of the lunar lander."}, {"start": 221.64000000000001, "end": 228.44, "text": " So the question becomes, how do you train a neural network to output Q of SA?"}, {"start": 228.44, "end": 234.32, "text": " It turns out the approach will be to use Bellman's equations to create a training set with lots"}, {"start": 234.32, "end": 241.64000000000001, "text": " of examples X and Y, and then we'll use supervised learning exactly as you learned in the second"}, {"start": 241.64, "end": 247.67999999999998, "text": " course when we talked about neural networks to learn using supervised learning, a mapping"}, {"start": 247.67999999999998, "end": 248.92, "text": " from X to Y."}, {"start": 248.92, "end": 255.79999999999998, "text": " That is a mapping from the state action pair to this target value Q of SA."}, {"start": 255.79999999999998, "end": 262.47999999999996, "text": " But how do you get a training set with values for X and Y that you can then train a neural"}, {"start": 262.47999999999996, "end": 263.47999999999996, "text": " network on?"}, {"start": 263.47999999999996, "end": 265.2, "text": " Let's take a look."}, {"start": 265.2, "end": 267.36, "text": " So here's the Bellman equation."}, {"start": 267.36, "end": 273.28000000000003, "text": " Q of SA equals R of S plus gamma max of A prime Q of S prime A prime."}, {"start": 273.28000000000003, "end": 278.90000000000003, "text": " So the right hand side is what you want Q of SA to be equal to."}, {"start": 278.90000000000003, "end": 285.32, "text": " So I'm going to call this value on the right hand side Y and the input to the neural network"}, {"start": 285.32, "end": 287.86, "text": " is a state and an action."}, {"start": 287.86, "end": 294.56, "text": " So I'm going to call that X. And the job of a neural network is to input X, that is input"}, {"start": 294.56, "end": 301.4, "text": " the state action pair and try to accurately predict what will be the value on the right."}, {"start": 301.4, "end": 307.38, "text": " So in supervised learning, we were training a neural network to learn a function F, which"}, {"start": 307.38, "end": 312.36, "text": " depends on a bunch of parameters W and B, the parameters of the various layers of the"}, {"start": 312.36, "end": 313.92, "text": " neural network."}, {"start": 313.92, "end": 323.04, "text": " And it was a job of the neural network to input X and hopefully output something close"}, {"start": 323.04, "end": 325.36, "text": " to the target value Y."}, {"start": 325.36, "end": 334.48, "text": " So the question is, how can we come up with a training set with values X and Y for a neural"}, {"start": 334.48, "end": 336.40000000000003, "text": " network to learn from?"}, {"start": 336.40000000000003, "end": 337.68, "text": " Here's what we're going to do."}, {"start": 337.68, "end": 343.26, "text": " We're going to use the lunar lander and just try taking different actions in it."}, {"start": 343.26, "end": 347.20000000000005, "text": " If we don't have a good policy yet, we'll take actions randomly."}, {"start": 347.2, "end": 353.08, "text": " By the left fester, by the right fester, by the main engine, do nothing."}, {"start": 353.08, "end": 360.15999999999997, "text": " And by just trying out different things in the lunar lander simulator, we'll observe"}, {"start": 360.15999999999997, "end": 365.64, "text": " a lot of examples of when we're in some state and we took some action, maybe a good action,"}, {"start": 365.64, "end": 368.32, "text": " maybe a terrible action, either way."}, {"start": 368.32, "end": 373.32, "text": " And then we got some rewards R of S for being in that state."}, {"start": 373.32, "end": 378.58, "text": " And as a result of our action, we got to some new state S prime."}, {"start": 378.58, "end": 384.84, "text": " As you take different actions in the lunar lander, you see these S, A, R of S, S prime,"}, {"start": 384.84, "end": 388.15999999999997, "text": " and we call them tuples in Python codes many times."}, {"start": 388.15999999999997, "end": 393.32, "text": " For example, maybe one time you're in some state S and just to give this an index, I'm"}, {"start": 393.32, "end": 395.53999999999996, "text": " going to call this S1."}, {"start": 395.53999999999996, "end": 401.48, "text": " And you happen to take some action A1, this could be nothing left main fester or right."}, {"start": 401.48, "end": 408.8, "text": " As a result of which you got some reward, and you wound up at some state S prime one."}, {"start": 408.8, "end": 413.40000000000003, "text": " And maybe a different time you're in some other state S2, you took some other action,"}, {"start": 413.40000000000003, "end": 418.08000000000004, "text": " could be a good action, could be a bad action, could be any of the four actions, and you"}, {"start": 418.08000000000004, "end": 424.40000000000003, "text": " got the reward and then you wound up with S prime two, and so on, multiple times."}, {"start": 424.40000000000003, "end": 428.8, "text": " And maybe you've done this 10,000 times or even more than 10,000 times."}, {"start": 428.8, "end": 435.48, "text": " So you would have to save the way with not just S1, A1, and so on, but up to S10,000,"}, {"start": 435.48, "end": 436.88, "text": " A10,000."}, {"start": 436.88, "end": 443.6, "text": " It turns out that each of these lists of four elements, each of these tuples will be enough"}, {"start": 443.6, "end": 449.32, "text": " to create a single training example, X1, Y1."}, {"start": 449.32, "end": 451.56, "text": " In particular, here's how you do it."}, {"start": 451.56, "end": 454.96000000000004, "text": " There are four elements in this first tuple."}, {"start": 454.96, "end": 461.32, "text": " The first two will be used to compute X1, and the second two will be used to compute"}, {"start": 461.32, "end": 463.08, "text": " Y1."}, {"start": 463.08, "end": 471.32, "text": " In particular, X1 is just going to be S1, A1 put together."}, {"start": 471.32, "end": 477.88, "text": " S1 would be eight numbers, the state of the lunar lander, A1 would be four numbers, the"}, {"start": 477.88, "end": 483.4, "text": " one-hot encoding of whatever action this was, and Y1 would be computed using the right hand"}, {"start": 483.4, "end": 486.23999999999995, "text": " side of the Bellman equation."}, {"start": 486.23999999999995, "end": 494.64, "text": " In particular, the Bellman equation says when you input S1, A1, you want Q of S1, A1 to"}, {"start": 494.64, "end": 504.88, "text": " be this right hand side, to be equal to R of S1 plus gamma max over A prime of Q of"}, {"start": 504.88, "end": 508.32, "text": " S1 prime A prime."}, {"start": 508.32, "end": 513.72, "text": " And notice that these two elements of the tuple on the right give you enough information"}, {"start": 513.72, "end": 515.12, "text": " to compute this."}, {"start": 515.12, "end": 520.98, "text": " You know what is R of S1, that's the reward you've saved away here, plus the discount"}, {"start": 520.98, "end": 527.96, "text": " factor gamma times max over all actions A prime of Q of S prime 1, that's the state"}, {"start": 527.96, "end": 533.42, "text": " you got to in this example, and then take the max over all possible actions A prime."}, {"start": 533.42, "end": 540.86, "text": " And so I'm going to call this Y1, and when you compute this, this will be some number"}, {"start": 540.86, "end": 550.9599999999999, "text": " like 12.5 or 17 or 0.5 or some other number, and we'll save that number here as Y1 so that"}, {"start": 550.9599999999999, "end": 558.38, "text": " this pair, X1, Y1, becomes the first trading example in this little data set we're computing."}, {"start": 558.38, "end": 566.32, "text": " Now you may be wondering, wait, where does Q of S prime A prime or Q of S prime 1 A prime"}, {"start": 566.32, "end": 567.32, "text": " come from?"}, {"start": 567.32, "end": 572.84, "text": " Well, initially, we don't know what is the Q function, but it turns out that when you"}, {"start": 572.84, "end": 577.04, "text": " don't know what is the Q function, you can start off with taking a totally random guess"}, {"start": 577.04, "end": 579.16, "text": " for what is the Q function."}, {"start": 579.16, "end": 583.48, "text": " And we'll see on the next slide that the algorithm will work nonetheless."}, {"start": 583.48, "end": 589.16, "text": " And every step, Q here is just going to be some guess that will get better over time,"}, {"start": 589.16, "end": 592.72, "text": " it turns out, of what is the actual Q function."}, {"start": 592.72, "end": 594.24, "text": " Let's look at the second example."}, {"start": 594.24, "end": 600.04, "text": " If you had a second experience where you state S2, took action A2, got that reward, and then"}, {"start": 600.04, "end": 605.48, "text": " got to that state, then we would create a second training example in this data set,"}, {"start": 605.48, "end": 613.6800000000001, "text": " X2, where the input is now S2, A2, so the first two elements go to computing the input"}, {"start": 613.6800000000001, "end": 628.24, "text": " X, and then Y2 will be equal to R of S2 plus gamma max over A prime Q of S prime 2 A prime."}, {"start": 628.24, "end": 634.48, "text": " And whatever this number is, Y2, we put this over here in our small but growing training"}, {"start": 634.48, "end": 635.64, "text": " set."}, {"start": 635.64, "end": 643.48, "text": " And so on and so forth until maybe you end up with 10,000 training examples with these"}, {"start": 643.48, "end": 646.72, "text": " X, Y pairs."}, {"start": 646.72, "end": 653.32, "text": " And what we'll see later is that we'll actually take this training set where the Xs are inputs"}, {"start": 653.32, "end": 661.5600000000001, "text": " with 12 features and the Ys are just numbers, and we train a neural network with, say, the"}, {"start": 661.56, "end": 668.8399999999999, "text": " mean squared error loss to try to predict Y as a function of the input X."}, {"start": 668.8399999999999, "end": 674.3199999999999, "text": " So what I describe here is just one piece of the learning algorithm we'll use."}, {"start": 674.3199999999999, "end": 678.1199999999999, "text": " Let's put it all together on the next slide and see how it all comes together into a single"}, {"start": 678.1199999999999, "end": 679.76, "text": " algorithm."}, {"start": 679.76, "end": 685.4799999999999, "text": " So let's take a look at what the full algorithm for learning the Q function is like."}, {"start": 685.4799999999999, "end": 690.92, "text": " First, we're going to take our neural network and initialize all the parameters of the neural"}, {"start": 690.92, "end": 692.36, "text": " network randomly."}, {"start": 692.36, "end": 696.28, "text": " Initially, we have no idea what is the Q function."}, {"start": 696.28, "end": 700.92, "text": " So let's just pick totally random values of the weights and we'll pretend that this neural"}, {"start": 700.92, "end": 705.24, "text": " network is our initial random guess for the Q function."}, {"start": 705.24, "end": 710.24, "text": " This is a little bit like when you are training linear regression and you initialize all the"}, {"start": 710.24, "end": 715.5999999999999, "text": " parameters randomly and then use gradient descent to improve the parameters."}, {"start": 715.5999999999999, "end": 717.56, "text": " Initializing randomly for now is fine."}, {"start": 717.56, "end": 722.3599999999999, "text": " What's important is whether the algorithm can slowly improve the parameters to get to"}, {"start": 722.3599999999999, "end": 723.64, "text": " a better estimate."}, {"start": 723.64, "end": 726.88, "text": " Next, we will repeatedly do the following."}, {"start": 726.88, "end": 729.7199999999999, "text": " We will take actions in the lunar land."}, {"start": 729.7199999999999, "end": 733.1999999999999, "text": " So fly around randomly, take some good actions, take some bad actions."}, {"start": 733.1999999999999, "end": 734.8599999999999, "text": " It's okay either way."}, {"start": 734.8599999999999, "end": 740.16, "text": " But you get lots of these tuples of when it was in some state, you took some action A,"}, {"start": 740.16, "end": 743.7199999999999, "text": " got a reward R of S, and you got to some state S prime."}, {"start": 743.72, "end": 751.2, "text": " And what we will do is store the 10,000 most recent examples of these tuples."}, {"start": 751.2, "end": 757.76, "text": " As you run this algorithm, you will see many, many steps in the lunar lander, maybe hundreds"}, {"start": 757.76, "end": 759.36, "text": " of thousands of steps."}, {"start": 759.36, "end": 765.0400000000001, "text": " But to make sure we don't end up using excessive computer memory, common practice is to just"}, {"start": 765.0400000000001, "end": 772.5600000000001, "text": " remember the 10,000 most recent such tuples that we saw taking actions in the MDP."}, {"start": 772.56, "end": 779.56, "text": " This technique of storing the most recent examples only is sometimes called the replay"}, {"start": 779.56, "end": 782.88, "text": " buffer in a reinforcement learning algorithm."}, {"start": 782.88, "end": 789.1999999999999, "text": " So for now, we just find the lunar lander randomly, sometimes crashing, sometimes not,"}, {"start": 789.1999999999999, "end": 793.52, "text": " and getting these tuples as experience for our learning algorithm."}, {"start": 793.52, "end": 797.5999999999999, "text": " Occasionally, then, we will train the neural network."}, {"start": 797.6, "end": 803.08, "text": " In order to train the neural network, here's what we'll do, we'll look at these 10,000"}, {"start": 803.08, "end": 810.9200000000001, "text": " most recent tuples we had saved and create a training set of 10,000 examples."}, {"start": 810.9200000000001, "end": 814.6800000000001, "text": " So training set needs lots of pairs of X and Y."}, {"start": 814.6800000000001, "end": 822.1600000000001, "text": " And for our training examples, X will be the SA from this part of the tuple."}, {"start": 822.1600000000001, "end": 826.1600000000001, "text": " So it'll be a list of 12 numbers, the eight numbers for the state and the four numbers"}, {"start": 826.16, "end": 828.7199999999999, "text": " for the one-hot encoding of the action."}, {"start": 828.7199999999999, "end": 835.12, "text": " And the target value that we want a neural network to try to predict will be Y equals"}, {"start": 835.12, "end": 840.6, "text": " R of S plus gamma max of A prime Q of S prime A prime."}, {"start": 840.6, "end": 842.16, "text": " How do we get this value of Q?"}, {"start": 842.16, "end": 846.4, "text": " Well, initially, is this neural network that we had randomly initialized."}, {"start": 846.4, "end": 849.64, "text": " So it may not be a very good guess, but it's a guess."}, {"start": 849.64, "end": 856.72, "text": " Simply creating these 10,000 training examples will have training examples X1, Y1 through"}, {"start": 856.72, "end": 861.92, "text": " X 10,000, Y 10,000."}, {"start": 861.92, "end": 864.8, "text": " And so we'll train a neural network."}, {"start": 864.8, "end": 872.4, "text": " And I'm going to call the new neural network Q new, such that Q new of SA learns to approximate"}, {"start": 872.4, "end": 873.4, "text": " Y."}, {"start": 873.4, "end": 880.88, "text": " So this is exactly training that neural network to output F with parameters W and B to input"}, {"start": 880.88, "end": 885.68, "text": " X to try to approximate the target value Y."}, {"start": 885.68, "end": 892.0, "text": " Now, this neural network should be a slightly better estimate of what the Q function or"}, {"start": 892.0, "end": 894.52, "text": " the state action value function should be."}, {"start": 894.52, "end": 900.56, "text": " And so what we'll do is we're going to take Q and set it to this new neural network that"}, {"start": 900.56, "end": 902.52, "text": " we had just learned."}, {"start": 902.52, "end": 906.96, "text": " Many of the ideas in this algorithm are due to min at all."}, {"start": 906.96, "end": 913.1999999999999, "text": " And it turns out that if you run this algorithm where you start with a really random guess"}, {"start": 913.1999999999999, "end": 918.96, "text": " of the Q function, but use Bellman's equations to repeatedly try to improve the estimates"}, {"start": 918.96, "end": 923.96, "text": " of the Q function, then by doing this over and over, taking lots of actions, training"}, {"start": 923.96, "end": 928.3199999999999, "text": " a model that will improve your guess for the Q function."}, {"start": 928.32, "end": 933.1600000000001, "text": " And so for the next model you train, you now have a slightly better estimate of what is"}, {"start": 933.1600000000001, "end": 934.6400000000001, "text": " the Q function."}, {"start": 934.6400000000001, "end": 937.7600000000001, "text": " And then the next model you train will be even better."}, {"start": 937.7600000000001, "end": 943.1600000000001, "text": " And when you update Q equals Q new, then for the next time you train a model, Q of S prime"}, {"start": 943.1600000000001, "end": 945.84, "text": " A prime will be an even better estimate."}, {"start": 945.84, "end": 951.0, "text": " And so as you run this algorithm on every iteration, Q of S prime A prime hopefully"}, {"start": 951.0, "end": 955.5400000000001, "text": " becomes an even better estimate of the Q function."}, {"start": 955.54, "end": 960.4399999999999, "text": " So that when you run the algorithm long enough, this will actually become a pretty good estimate"}, {"start": 960.4399999999999, "end": 968.68, "text": " of the true value of Q of S A so that you can then use this to pick hopefully good actions"}, {"start": 968.68, "end": 970.36, "text": " for the MDP."}, {"start": 970.36, "end": 975.64, "text": " The algorithm you just saw is sometimes called the DQN algorithm, which stands for deep Q"}, {"start": 975.64, "end": 982.4, "text": " network, because you're using deep learning and neural network to train a model to learn"}, {"start": 982.4, "end": 983.68, "text": " the Q function."}, {"start": 983.68, "end": 989.0799999999999, "text": " So hence DQN or deep Q network, DQ using a neural network."}, {"start": 989.0799999999999, "end": 994.8, "text": " And if you use the algorithm as I described it, it will kind of work okay on the lunar"}, {"start": 994.8, "end": 995.8, "text": " lander."}, {"start": 995.8, "end": 997.64, "text": " Maybe it'll take a long time to converge."}, {"start": 997.64, "end": 1001.0799999999999, "text": " Maybe it won't land perfectly, but it'll sort of work."}, {"start": 1001.0799999999999, "end": 1005.92, "text": " But it turns out that with a couple refinements to the algorithm, it can work much better."}, {"start": 1005.92, "end": 1009.88, "text": " So in the next few videos, let's take a look at some refinements to the algorithm that"}, {"start": 1009.88, "end": 1016.88, "text": " you just saw."}]