10.9 State-action value function | Random (stochastic) environment (Optional) -[ML | Andrew Ng]
 In some applications, when you take an action, the outcome is not always completely reliable. For example, if you command your Mars rover to go left, maybe there's a little bit of a rock slide or maybe the floor is really slippery and so it slips and goes in the wrong direction. In practice, many robots don't always manage to do exactly what you tell them because of wind blowing and off course or the wheel slipping or something else. So there's a generalization of the reinforcement learning framework we've talked about so far, which models random or stochastic environments. In this optional video, we'll talk about how these reinforcement learning problems work. Continuing with our simplified Mars rover example, let's say you take the action and command it to go left. Most of the time it will succeed, but what if 10% of the time or 0.1% of the time, it actually ends up accidentally slipping and going in the opposite direction. So if you command it to go left, it has a 90% chance or 0.9 chance of correctly going the left direction, but an 0.1 chance of actually heading to the right. So that it has a 9% chance of ending up in state 3 in this example and a 10% chance of ending up in state 5. Conversely, if you were to command it to go right and take the action right, it has a 0.9 chance of ending up in state 5 and a 0.1 chance of ending up in state 3. This would be an example of a stochastic environment. Let's see what happens in this reinforcement learning problem. Let's say you use this policy shown here, where you go left in states 2, 3, and 4 and go right or try to go right in state 5. If you were to start in state 4 and you were to follow this policy, then the actual sequence of states you visit may be random. For example, in state 4, you will go left and maybe you're a little bit lucky and it actually gets to state 3. And then you try to go left again and maybe it actually gets there, you try to go left again and it gets to that state. If this is what happens, you end up with the sequence of rewards 0, 0, 0, 100. But if you were to try this exact same policy a second time, maybe you're a little less lucky. The second time you start here, try to go left and say it succeeds. So 0 from state 4, 0 from state 3. Here you try to go left, but you got unlucky this time and the robot slips and ends up heading back to state 4 instead. And then you try to go left, then left, then left and eventually it gets to that reward of 100. In that case, this will be the sequence of rewards you observe. This went from 4 to 3, back to 4, 3, 2, then 1. Or it's even possible if you tell it from state 4 to go left following the policy, you may get unlucky even on the first step and you end up going to state 5 because it slipped. And then state 5, you command it to go right and it succeeds as you end up here. And in this case, the sequence of rewards you see will be 0, 0, 40 because it went from 4 to 5 and then state 6. We had previously written out the return as this sum of discounted rewards. But when the reinforcement learning problem is stochastic, there isn't one sequence of rewards that you see for sure. Instead, you see this sequence of different rewards. So in a stochastic reinforcement learning problem, what we're interested in is not maximizing the return because that's a random number. What we're interested in is maximizing the average value of the sum of discounted rewards. And by average value, I mean if you were to take your policy and try it out a thousand times or a hundred thousand times or a million times, you get lots of different reward sequences like that. And if you were to take the average over all of these different sequences of the sum of discounted rewards, then that's what we call the expected return. In statistics, the term expected is just another way of saying average. But what this means is we want to maximize what we expect to get on average in terms of the sum of discounted rewards. The mathematical notation for this is to write this as E. E stands for expected value of R1 plus gamma R2 plus and so on. So the job of reinforcement learning algorithm is to choose a policy pi to maximize the average or the expected sum of discounted rewards. So to summarize, when you have a stochastic reinforcement learning problem or a stochastic Markov decision process, the goal is to choose a policy that tells what action A to take in state S so as to maximize the expected return. The last way that this changes what we've talked about is it modifies Bellman equation a little bit. So here's Bellman equation exactly as we've written down. But the difference now is that when you take the action A in state S, the next state S prime you get to is random. When you're in state V and you try to go left, the next state S prime, it could be the state two, or it could be the state four. So S prime is now random, which is why we also put an average operator or an expected operator here. So we say that the total return from state S taking action A once and then behaving optimally is equal to the reward you get right away, also called the immediate reward, plus the discount factor gamma plus what you expect to get on average of the future returns. If you want to sharpen your intuition about what happens with these stochastic reinforcement learning problems, you go back to the optional lab that I had shown you just now, where this parameter misstep probability is the probability of your Mars rover going in the opposite direction than you had commanded it to. So if we set misstep prop to be 0.1 and we execute the notebook, and so these numbers up here are the optimal return if you were to take the best possible actions, take this optimal policy, but the robot were to step in the wrong direction 10% of the time. And these are the Q values for the stochastic MDP. Notice that these values are now a little bit lower because you can't control the robot as well as before. The Q values as well as the optimal returns have gone down a bit. And in fact, if you were to increase the misstep probability, say 40% of the time, the robot doesn't even go in the direction you had commanded it to, only 60% of the time it goes where you told it to, then these values end up even lower because your degree of control over the robot has decreased. So I encourage you to play with the optional lab and change the value of the misstep probability and see how that affects the optimal return or the optimal expected return as well as the Q values Q of SA. Now in everything we've done so far, we've been using this Markov decision process, this Mars rover with just six states. For many practical applications, the number of states will be much larger. In the next video, we'll take the reinforcement learning or Markov decision process framework we've talked about so far and generalize it to this much richer and maybe even more interesting set of problems with much larger and particular with continuous state spaces. Let's take a look at that in the next video.
[{"start": 0.0, "end": 9.040000000000001, "text": " In some applications, when you take an action, the outcome is not always completely reliable."}, {"start": 9.040000000000001, "end": 13.82, "text": " For example, if you command your Mars rover to go left, maybe there's a little bit of"}, {"start": 13.82, "end": 18.16, "text": " a rock slide or maybe the floor is really slippery and so it slips and goes in the wrong"}, {"start": 18.16, "end": 19.16, "text": " direction."}, {"start": 19.16, "end": 25.2, "text": " In practice, many robots don't always manage to do exactly what you tell them because of"}, {"start": 25.2, "end": 28.88, "text": " wind blowing and off course or the wheel slipping or something else."}, {"start": 28.88, "end": 34.12, "text": " So there's a generalization of the reinforcement learning framework we've talked about so far,"}, {"start": 34.12, "end": 38.12, "text": " which models random or stochastic environments."}, {"start": 38.12, "end": 43.08, "text": " In this optional video, we'll talk about how these reinforcement learning problems work."}, {"start": 43.08, "end": 48.58, "text": " Continuing with our simplified Mars rover example, let's say you take the action and"}, {"start": 48.58, "end": 51.239999999999995, "text": " command it to go left."}, {"start": 51.239999999999995, "end": 56.8, "text": " Most of the time it will succeed, but what if 10% of the time or 0.1% of the time, it"}, {"start": 56.8, "end": 63.12, "text": " actually ends up accidentally slipping and going in the opposite direction."}, {"start": 63.12, "end": 69.24, "text": " So if you command it to go left, it has a 90% chance or 0.9 chance of correctly going"}, {"start": 69.24, "end": 74.86, "text": " the left direction, but an 0.1 chance of actually heading to the right."}, {"start": 74.86, "end": 80.72, "text": " So that it has a 9% chance of ending up in state 3 in this example and a 10% chance of"}, {"start": 80.72, "end": 82.75999999999999, "text": " ending up in state 5."}, {"start": 82.76, "end": 88.48, "text": " Conversely, if you were to command it to go right and take the action right, it has a"}, {"start": 88.48, "end": 96.48, "text": " 0.9 chance of ending up in state 5 and a 0.1 chance of ending up in state 3."}, {"start": 96.48, "end": 101.08000000000001, "text": " This would be an example of a stochastic environment."}, {"start": 101.08000000000001, "end": 105.32000000000001, "text": " Let's see what happens in this reinforcement learning problem."}, {"start": 105.32, "end": 113.91999999999999, "text": " Let's say you use this policy shown here, where you go left in states 2, 3, and 4 and"}, {"start": 113.91999999999999, "end": 117.11999999999999, "text": " go right or try to go right in state 5."}, {"start": 117.11999999999999, "end": 124.11999999999999, "text": " If you were to start in state 4 and you were to follow this policy, then the actual sequence"}, {"start": 124.11999999999999, "end": 127.39999999999999, "text": " of states you visit may be random."}, {"start": 127.39999999999999, "end": 133.44, "text": " For example, in state 4, you will go left and maybe you're a little bit lucky and it"}, {"start": 133.44, "end": 136.04, "text": " actually gets to state 3."}, {"start": 136.04, "end": 140.68, "text": " And then you try to go left again and maybe it actually gets there, you try to go left"}, {"start": 140.68, "end": 143.76, "text": " again and it gets to that state."}, {"start": 143.76, "end": 151.12, "text": " If this is what happens, you end up with the sequence of rewards 0, 0, 0, 100."}, {"start": 151.12, "end": 156.48, "text": " But if you were to try this exact same policy a second time, maybe you're a little less"}, {"start": 156.48, "end": 157.48, "text": " lucky."}, {"start": 157.48, "end": 161.92, "text": " The second time you start here, try to go left and say it succeeds."}, {"start": 161.92, "end": 164.6, "text": " So 0 from state 4, 0 from state 3."}, {"start": 164.6, "end": 169.76, "text": " Here you try to go left, but you got unlucky this time and the robot slips and ends up"}, {"start": 169.76, "end": 172.32, "text": " heading back to state 4 instead."}, {"start": 172.32, "end": 177.07999999999998, "text": " And then you try to go left, then left, then left and eventually it gets to that reward"}, {"start": 177.07999999999998, "end": 178.07999999999998, "text": " of 100."}, {"start": 178.07999999999998, "end": 182.44, "text": " In that case, this will be the sequence of rewards you observe."}, {"start": 182.44, "end": 187.32, "text": " This went from 4 to 3, back to 4, 3, 2, then 1."}, {"start": 187.32, "end": 192.23999999999998, "text": " Or it's even possible if you tell it from state 4 to go left following the policy, you"}, {"start": 192.23999999999998, "end": 197.88, "text": " may get unlucky even on the first step and you end up going to state 5 because it slipped."}, {"start": 197.88, "end": 202.28, "text": " And then state 5, you command it to go right and it succeeds as you end up here."}, {"start": 202.28, "end": 207.51999999999998, "text": " And in this case, the sequence of rewards you see will be 0, 0, 40 because it went from"}, {"start": 207.51999999999998, "end": 210.79999999999998, "text": " 4 to 5 and then state 6."}, {"start": 210.8, "end": 218.56, "text": " We had previously written out the return as this sum of discounted rewards."}, {"start": 218.56, "end": 224.64000000000001, "text": " But when the reinforcement learning problem is stochastic, there isn't one sequence of"}, {"start": 224.64000000000001, "end": 226.24, "text": " rewards that you see for sure."}, {"start": 226.24, "end": 230.0, "text": " Instead, you see this sequence of different rewards."}, {"start": 230.0, "end": 237.32000000000002, "text": " So in a stochastic reinforcement learning problem, what we're interested in is not maximizing"}, {"start": 237.32000000000002, "end": 240.28, "text": " the return because that's a random number."}, {"start": 240.28, "end": 247.0, "text": " What we're interested in is maximizing the average value of the sum of discounted rewards."}, {"start": 247.0, "end": 252.56, "text": " And by average value, I mean if you were to take your policy and try it out a thousand"}, {"start": 252.56, "end": 257.36, "text": " times or a hundred thousand times or a million times, you get lots of different reward sequences"}, {"start": 257.36, "end": 258.36, "text": " like that."}, {"start": 258.36, "end": 263.4, "text": " And if you were to take the average over all of these different sequences of the sum of"}, {"start": 263.4, "end": 269.2, "text": " discounted rewards, then that's what we call the expected return."}, {"start": 269.2, "end": 275.08, "text": " In statistics, the term expected is just another way of saying average."}, {"start": 275.08, "end": 282.5, "text": " But what this means is we want to maximize what we expect to get on average in terms"}, {"start": 282.5, "end": 285.15999999999997, "text": " of the sum of discounted rewards."}, {"start": 285.15999999999997, "end": 293.36, "text": " The mathematical notation for this is to write this as E. E stands for expected value of"}, {"start": 293.36, "end": 297.48, "text": " R1 plus gamma R2 plus and so on."}, {"start": 297.48, "end": 304.6, "text": " So the job of reinforcement learning algorithm is to choose a policy pi to maximize the average"}, {"start": 304.6, "end": 307.92, "text": " or the expected sum of discounted rewards."}, {"start": 307.92, "end": 313.20000000000005, "text": " So to summarize, when you have a stochastic reinforcement learning problem or a stochastic"}, {"start": 313.20000000000005, "end": 318.52000000000004, "text": " Markov decision process, the goal is to choose a policy that tells what action A to take"}, {"start": 318.52000000000004, "end": 322.40000000000003, "text": " in state S so as to maximize the expected return."}, {"start": 322.4, "end": 328.35999999999996, "text": " The last way that this changes what we've talked about is it modifies Bellman equation"}, {"start": 328.35999999999996, "end": 329.78, "text": " a little bit."}, {"start": 329.78, "end": 333.15999999999997, "text": " So here's Bellman equation exactly as we've written down."}, {"start": 333.15999999999997, "end": 338.12, "text": " But the difference now is that when you take the action A in state S, the next state S"}, {"start": 338.12, "end": 340.67999999999995, "text": " prime you get to is random."}, {"start": 340.67999999999995, "end": 345.59999999999997, "text": " When you're in state V and you try to go left, the next state S prime, it could be the state"}, {"start": 345.59999999999997, "end": 349.2, "text": " two, or it could be the state four."}, {"start": 349.2, "end": 356.28, "text": " So S prime is now random, which is why we also put an average operator or an expected"}, {"start": 356.28, "end": 358.15999999999997, "text": " operator here."}, {"start": 358.15999999999997, "end": 365.2, "text": " So we say that the total return from state S taking action A once and then behaving optimally"}, {"start": 365.2, "end": 370.28, "text": " is equal to the reward you get right away, also called the immediate reward, plus the"}, {"start": 370.28, "end": 378.64, "text": " discount factor gamma plus what you expect to get on average of the future returns."}, {"start": 378.64, "end": 385.15999999999997, "text": " If you want to sharpen your intuition about what happens with these stochastic reinforcement"}, {"start": 385.15999999999997, "end": 391.52, "text": " learning problems, you go back to the optional lab that I had shown you just now, where this"}, {"start": 391.52, "end": 399.56, "text": " parameter misstep probability is the probability of your Mars rover going in the opposite direction"}, {"start": 399.56, "end": 401.4, "text": " than you had commanded it to."}, {"start": 401.4, "end": 408.96, "text": " So if we set misstep prop to be 0.1 and we execute the notebook, and so these numbers"}, {"start": 408.96, "end": 416.91999999999996, "text": " up here are the optimal return if you were to take the best possible actions, take this"}, {"start": 416.91999999999996, "end": 424.08, "text": " optimal policy, but the robot were to step in the wrong direction 10% of the time."}, {"start": 424.08, "end": 428.09999999999997, "text": " And these are the Q values for the stochastic MDP."}, {"start": 428.1, "end": 432.68, "text": " Notice that these values are now a little bit lower because you can't control the robot"}, {"start": 432.68, "end": 434.44, "text": " as well as before."}, {"start": 434.44, "end": 438.8, "text": " The Q values as well as the optimal returns have gone down a bit."}, {"start": 438.8, "end": 444.88, "text": " And in fact, if you were to increase the misstep probability, say 40% of the time, the robot"}, {"start": 444.88, "end": 449.84000000000003, "text": " doesn't even go in the direction you had commanded it to, only 60% of the time it goes where"}, {"start": 449.84000000000003, "end": 455.44, "text": " you told it to, then these values end up even lower because your degree of control over"}, {"start": 455.44, "end": 457.8, "text": " the robot has decreased."}, {"start": 457.8, "end": 463.92, "text": " So I encourage you to play with the optional lab and change the value of the misstep probability"}, {"start": 463.92, "end": 469.16, "text": " and see how that affects the optimal return or the optimal expected return as well as"}, {"start": 469.16, "end": 472.68, "text": " the Q values Q of SA."}, {"start": 472.68, "end": 478.72, "text": " Now in everything we've done so far, we've been using this Markov decision process, this"}, {"start": 478.72, "end": 481.8, "text": " Mars rover with just six states."}, {"start": 481.8, "end": 486.56, "text": " For many practical applications, the number of states will be much larger."}, {"start": 486.56, "end": 491.72, "text": " In the next video, we'll take the reinforcement learning or Markov decision process framework"}, {"start": 491.72, "end": 496.96, "text": " we've talked about so far and generalize it to this much richer and maybe even more interesting"}, {"start": 496.96, "end": 502.64, "text": " set of problems with much larger and particular with continuous state spaces."}, {"start": 502.64, "end": 517.3199999999999, "text": " Let's take a look at that in the next video."}]