1. Home
  2. Explore
  3. Computer Science
  4. Machine Learning Overview

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Machine Learning Overview

Transcript

All right. Let's begin. So last week, we talked about machine learning. And just recall, the learning algorithm is something that takes training data, which is input/output pairs and returns a predictor. And the predictor is the thing you go out into the real world, which takes inputs' and outputs' outputs. And for regression, that output is a number. For classification, that output is one of k options or a class, or a label. So that's what we did in machine learning.

Now, if you go back to the first lecture, recall the ingredients of intelligence. We have perception, reasoning, action, and learning. So a predictor really covers this latter bottom part. So it's basically reflexively mapping percepts to actions. And we're learning this mapping directly. So if you think about the neural nets that we were learning, even if it's deep, it's probably-- it's feedforward and it's fast. Now many problems in the real world

do require reasoning thinking, problem solving, planning. These are all related concepts. If you want to solve a hard math problem or solve a big, hard programming challenge, these clearly are not reflexive operations you should look at it, and then you output the answer. So this week's lecture is going to be about search. Search is one form of this type of reasoning when the world is deterministic. So as a few concrete examples, finding a sequence of moves to solve a Rubik's cube.

I guess, if you're a Rubik's cube expert, maybe this is reflex for you, but for most people, this requires a fair amount of search and problem solving, or finding the shortest path from point A to point B. This is going to be something that you explore in your homework. This is something that requires planning. You have to figure out your route, and then you go down the route, rather than just looking at where you are and just making a spontaneous decision. So now you might wonder, OK, it's 2025, why are we talking about search? Because wasn't the point that symbolic AI actually started in 1950s and they were all about search, how you can solve checkers, general problem solving, and so on. And that didn't really pan out. So why are we bothering with this? Shouldn't we just go all in on deep learning? So is it relevant today? So I want to pull a quote from this famous essay by Rich Sutton, which is a Turing Award winner, developed

a lot of the foundations of reinforcement learning called the Bitter lesson, which I think has become a little bit of a folklore. It's a short essay. You guys should go read it. It has some, I think, useful per perspectives. And in the essay, basically it says that "general methods that leverage computation are ultimately the most effective, and by a large margin." So a really rich is pushing back against doing a lot of handcrafting priors, because once you

have general methods and you have compute, then it sort of washes away any sort of prior knowledge. Now, I mean, that's true in some limit. There's obviously a time and place for prior knowledge as well. But putting that aside, he also says that the two methods that seem to scale arbitrarily in this way are search and learning. OK. So he calls out search and learning are really these pillars that if you believe in the bitter lesson, are the things that you should pay attention to.

And that's why we're talking about search. So search is increasingly important. Think about test time compute and language models, which we'll touch on in a bit later. These are cases where you actually just need to do a bunch of compute at test time. And you need to organize that compute somehow. The only thing is that search alone, the point from the '50s, that search alone cannot do this. You also need to learn learning. But now, the last decade has told us how to do learning and now we have really powerful models. And if you can couple them with search, which people are starting to do more, then you have a really powerful combination. And you can tackle some of the most challenging problems. OK. So that's the motivation. Feel free to interrupt or ask questions.

Yeah. What is search? Great question. I'm going to define it more narrowly. The rest of this lecture is going to be about more of the nuts and bolts and how you do search. And hopefully, it'll become clear. So let's formalize search using this abstraction called a search problem. So we'll do this through example. So these are going to be fairly toy simple examples. And so the first example is this. So imagine, you have a street with blocks numbered 1 through n. So n is 6 here.

So each of these is a grid lines as a block. And walking from block i to block i plus 1 takes one minute. Or you have this magic tram that you can take from-- that goes from i to 2 times i, and it takes two minutes. So the question is, how do you travel from 1 to n, which is 6 in the least amount of time? Now some of you are probably already solving this and thinking about strategies. I want you to actually stop doing that. Because that's not the point of a search problem.

The point of a search problem is to formalize, convert this natural language description into code so that you can have a general method that can solve these problems. Again, remember, the Bitter lesson. We want general techniques that can solve any search problem that you kind of have it. OK. I mean, if you solve it too, that's great for you. But we're going to have algorithms that solve this. And we'll see if you're right. OK. So we're going to define a search problem.

This is going to be parameterized by the number of locations, n. And the problem is going to have a start state. So the start state is this member of the search problem that tells you what state you start at. So the state that you start out is 1. And the problem also has a function called successors, which you pass in any given state. And it tells you where you can go, essentially. And so let's look at this. OK. So one thing you can do is you can walk. OK. So you're at a particular state, 1.

So you can take the action which is walk. It cost 1. So one minute, I'm translating cost to be time here. And then where do you end up, you get dumped into state plus 1. So you go from i to i plus 1. And this check is just to make sure you don't walk off the face of the Earth. It's you. The world stops at some locations. OK. So you can also take a tram. So here, the action is tram. The cost is 2. It takes two minutes to take this tram. And you end up in state 2 times state. OK.

So successors is the list of possible steps. So a step consists-- each step consists of the action, which is just a label, the cost of that action, and where do you get dumped into, the next state. So in this case, both walking and tram takes you to stay 2. But in general, they obviously will do different things. So we have start state. We have successors. And the final function that is part of the problem formulation is the function is end. So the is end takes arbitrary state and returns whether you've reached the destination and the search is done. So here, we're just checking, did you get to the number of locations or not? And in this case, the answer is obviously false because we just started. OK. So in general search problem has the following components. You have a start state, that returns

initial state, successors, that specifies what you can do, which includes the actions, their costs, the resulting states that you end up in. And finally, we have is end, which takes a state and returns whether state is an end state. Any questions about what a search problem is? OK. So now, given a search problem, what are we trying to do? The objective is to find a solution, which is a sequence of actions that minimizes the total cost. So here's a possible solution for n equals 10. So you walk to state 2, you take the transit state 4. You walk to state 5, you take the tram to state 10. OK. And this solution has cost 6 because that's 1 plus 2 plus 1 plus 2 is 6. OK. So this is one possible solution. There are many other solutions. You can walk all the way to 10.

That would be a cost of 10. And is this the best solution? How many of you think this is the best solution? How many of you don't think this is not the best solution? OK. Well, evenly split. So we will see. OK. So remember, it doesn't matter. We're going to be lazy and have the algorithm solve it for us. So you don't have to work that hard. The thing you have to get is make sure that your search problem is defined properly. And then you're good to go. OK.

So let's do a slightly more complicated example to just show you what states are really about. So suppose the magic tram requires tickets. And we only have a fixed number of tickets. And of course, this tram is probably in high demand. So we're not going to let anyone just get on the tram. So how do we change the search problem to incorporate this constraint? So let's see, what can we do here? So the state so far just captures where we are, the current location. OK.

But we also need to keep track of the number of tickets we have. So what we're going to do is we're going to define another search problem, limited travel search problem, which is going to be parameterized by the number of locations and the number of tickets you start out with. And now the start state is this going to be the structure travel state that has a location with one. And tickets equals the number of tickets that you started with. OK. So states can be composite things.

They don't have to be integers. They can be strings. In general, in the implementation, states are going to be any sort of hashable object. So what are successors look like now? So now, the successors takes the travel state and has to return a list of things to do. So there's only two things you can do. You can walk or you can take the tram. You can always walk because you don't need tickets to walk. And this is walk, has cost 1. And the new state is this travel state where the location has bumped up by one. And still have the same number of tickets because walking doesn't take tickets.

And now the second part of this is you can take the tram, and the tram cost 2, you end up in the location 2 times the location. And then oops, this wrapped. OK. Hopefully, is that-- OK. It looks like it's not going to unwrap. OK. There we go. But that's probably too small. Is that too small? OK. Let me make it bigger. So anyway, this is tickets minus 1. So we expanded one ticket to take the tram. But as a result, we're in location 2 times location. Now, there's one thing that we need to do here. And what is that?

So there is a check here that we need to make sure that we actually have tickets. You can only add the successor if you have non-zero number of tickets. Otherwise, it's the same problem. You just end up decrementing this counter and you can have minus 3 tickets and you go on. So this is what I mean by get your search problem definition correct. Because if you don't, then the algorithm is going to find a way to cheat sort of game your system. You can call this reward hacking if you want. OK.

So successors are walk to location 2. And you still have one ticket, or take the tram to location 2 and you have zero tickets. OK. And is end is just, have you reach the destination? And notice that it doesn't look at the number of tickets, which means that there are possibly more than one end state. Because you can have zero tickets left, location equals to unlock with zero tickets, one ticket, two tickets, and so on.

OK. So in general, the state is a really important notion when we talk about search problems and also MDPs and then games. The state contains any information that's needed to evaluate future actions, costs, and successors. OK. So as another example, suppose we put another constraint. There are a lot of rules in this city here. If you can't take the tram twice in a row. If you want to enforce this constraint, the state needs to contain enough information so you can even properly assess it.

The current state that we don't-- only know our current location and the number of tickets we have. And that's not enough. What we have to do is we have to add to the state, which is another field in the state structure, whether the last action was taking the tram. Because if we know that, then if we last took the tram, we can't take the tram. And if we don't, we didn't take the tram, then we can take the tram. OK. So there's just going to be this game, you have to play,

where you take essentially like word problems, and you have to map them onto this type of structure. What is the state? What are the successors cost, and actions? So now you can ask, well, why do I have to think about it? I thought the whole idea is I don't have to think. Why don't we just include everything into the state, the entire history of where we've been? And surely, that's enough information to evaluate the validity of actions and states and successors. You could do that.

But as we'll see later, some algorithms such as dynamic programming, scaling the number of states. So if you have a million states or an exponential number of states, then your algorithm is going to be really, really slow. So in general, you want to keep the number of states small. OK. So far, we focus on modeling. So what modeling means here is representing the problem formally. So there's no doubt in our mind what a solution looks like. And that solution in code should refer to correspond to your intuitive idea of what a good solution is. Now, the thing is that as you build up these search problems with more and more constraints, maybe you were clever and solve the first transportation problem. But now if I tell you, well, how do I go to 1 to 100,

and you can't use-- take the tram twice, and you also have three tickets. I would be very shocked if someone immediately came up with the answer in their head. But the point is, who cares? We have written it down formally, and now we're just going to pass it off to a learning-- not a learning, an algorithm. OK. Any questions about modeling, search problems, trains, magic trams?

Yeah. Do you use some protocols for [INAUDIBLE] the problems for [INAUDIBLE] problems. For example, if you use generally the easiest one, we have the problem just to reach with walking or train. And we add something. How we understand this can be useful for [INAUDIBLE] So I think the question is if you look at the easiest problem and you add constraints, how do you know this is useful? I mean, generally, when you're trying to solve a problem, whether it be a machine learning or search,

either you or someone comes to you with a description of a problem. And you have to translate it into something that's more formal. That process of modeling is often lossy. Sometimes you don't get all the constraints right. And of course, this is a sort of idealized setting. But you have to basically make some sort of lossy modeling assumptions to get it into a tractable form there that the algorithms can operate on. You can't really have your search algorithms operate on, just like a kind of vague description.

OK. So we said, w don't care. But OK, now we have to care about it because we actually do want to solve these search problems. And who's going to do it, except for us? So let's start by solving this problem exactly. OK. We really want the minimum cost solution. Later, I'll show you how you can cut corners if you're short on time. So remember the goal, given a search problem, which defines states, actions, costs, and is end, I want to find a sequence of actions that minimizes the total cost. OK.

So exhaustive search just says, simply try all the possibilities, all possible sequence of actions, and take the one that's best. OK. This should work. Now there are many ways to enumerate all solutions. We're going to choose one particular formulation that will allow us to effortlessly generalize to dynamic programming. And this will be also the formulation that generalizes to Markov decision processes, reinforcement learning games, and so on. So bear with me while I introduce what might seem a little bit more machinery than we need. It's because we're trying to think a little bit farther ahead.

So the key definition here is future cost. So a future cost is a function that takes a state and is the cost of the minimum cost solution from that state to the end state. OK. So here's a picture. Let me actually draw this on the board. So in general, you have a start state. And just for now, I'll assume that there's one end state. There could be multiple end states. And in general, from the starting state, there might be multiple actions. The best way to I think, understand

this is in terms of a graph, where these actions could lead to other states and so on and so forth. And here's a random state here. So let's call it S. And the future cost is-- there could be millions of states here. The future cost is if I started here, what is the total cost, the best total cost of getting to the end? OK. And future cost is something that you can apply to any given function. You can apply it to the start. If you apply it to the start, that gives you the solution to the problem. OK.

So how do you compute this future cost? Well, let's take it step by step. So in life, if you have to solve a really hard problem, what do you do? You have to take a first step. No matter what solution, there has to be a first step. OK. So let's start here. Well, there's actually-- OK. Many potential first steps here that you could take. Let me just do this. So I'm going to use S prime to denote the successors.

And then what I'm going to do is I'm going to say this is someone else's problem. Like if I've computed the future cost from S prime to end, then I can just piggyback off of that and say, well, I could have gone here with some cost. And then over here, I'm going to put maybe some squiggly thing. And this is the future cost of S prime. OK. So one particular route is take this action, end up here, and then do the optimal thing to the end. Or I can take this action, do the optimal thing to the end, or I can take this action and do the optimal thing to the end.

Which one is the optimal one? Well, it's whatever one minimizes the cost over all possible future successors. So the cost of this is cost plus future cost. The cost of this cost plus this. The cost of this action is this one plus that. And what we're going to do is we're simply going to take the minimum over that. So just writing out the pseudocode for this recurrence. The future cost of a state is the minimum over all possible successors. And for each successor I look at, it's the immediate cost of that, taking that action, plus the future cost from wherever I ended up to an end state. OK. Let me pause there to address any questions.

OK. How many of you are good with this? OK good. So let's go through an example here. We're going to look at our familiar travel search problem. And we're going to implement this exhaustive search function here. I'm going to implement four algorithms today. And for each one, I'm going to keep track of number explored. So this is the number of states that we've looked at. And that'll give us idea of time complexity. So we're going to define this helper function called future solution.

It's essentially the future cost function. But instead of just returning a number, it returns a solution which remember also contains the cost. And that's because we don't just care about solving the problem and returning a number, oh, 42 is a cost. Well, what do I do? What actions do I take? You also need the sequence of actions and that's encompassed in the solution. OK. So I'm going to start at the start state. And I'm going to call this future solution function.

And what I'm going to do, I've explored a state. I'm going to check. Did I get to the end. So I haven't gotten to the end yet. So then, I'm going to look at the possible successors, walk or tram. And then I'm going to consider, for each of the successor, there's a-- which is taking the first step, which is walk. And then I'm going to recurse, if you call future solution on first step.state, which is 2. So I'm going to recurse. That's going to take me to state 2 in this recurrence.

And then I'm going to look at the successors. I can walk a tram. And then are you going to consider walk? And I'm going to recurse. And then I'm going to do this again. And eventually, I reach-- arrive at an end state. And at the end state, the solution is-- what's the future cost of end? Zero. Because I'm at the end. I don't need to do anything. It doesn't cost me anything. And the steps is zero. I don't need to do anything. So that's the base case. Now I pop back up and I-- here, I'm just going to-- it's going to be hard to follow all the details, but I just want to give you the kind of rough shape of this.

Actually, let me just skip out of this because this is probably not-- you can follow this to trace through this when you study this afterwards. But hopefully the recurrence is clear. So let me just quickly go through this. At each state, if you're not at the end state, you look at all the possible successors. So these are these things coming out. And then for each successor, you recursively call future solution, which gives you these. And then what I'm doing is I'm going to just choose the best solution, which is the one that has the smallest cost at each state.

So in the end, I got walk, walk, walk, which has a cost of 3 as the best solution here. And I explored nine states as a result. OK. OK. These numbers are out of date. I had five states before, but now nonetheless, the number of states explored, nine, is larger than the total number of states, which is four. OK. Which means that we are exploring some states more than once. So we'll come back to this later. Any questions about exhaustive search? Yeah. I don't know how big the recursion is.

So that would be-- does that become an issue with Stack Overflow. Yeah. Yeah. So the question is, how long is the recursion going to be? So it is basically the length of your solution. And in the worst case, it's infinite. Because if you have cycles, then you can go around. I'll come back to that point a little bit later. But for now, let's just assume that's not that bad.

So let's try some larger problems here. So let's try 10 locations if I do exhaustive search. So remember, this is a question I asked like that is walk, tram, walk, tram the optimal solution? And it turns out to be yes. And that it got a cost of 6, which is optimal. It turns out to be multiple optimal solutions. You could also walk, walk, walk, walk, tram.

The minimum cost solution doesn't have to be unique. But the minimum cost, obviously, is unique. So here's another one, 17 locations now. Now you see the number explored states is growing quite fast right. And in fact, it is going to grow in general exponentially with the number of locations you have. Which means that the time complexity of doing exhaustive search is, worst case, exponential in the number of states. So this is a pretty bad news.

Because even if you have, let's just say, 100 states, 100 locations, it's going to take, I don't know, the exact number, but it's going to be it's probably not going to terminate anytime soon. OK. So that's kind of rough. What about the memory complexity? So the good news is that the memory is linear in the length of the solution. And it's basically what you have to keep track of is, this is done sort of implicitly through the stack in the recurrence is where basically the stack represents

the path that you took to a particular state. OK. So now, coming back to this earlier question. We are assuming that we can't have cycles, because what happens if you can go from state A to B to C and back to A is that you'll notice that this function, as I've written, won't actually terminate. Because eventually, when you go back to A and then you go to B, C, A. B, C, A, B, C, A, and it just never ends. Well, it ends when you run out of memory, but mathematically, it doesn't end.

And it's also not well defined. So next week, we'll see how value iteration for Markov decision processes actually gets around this and allows for cycles. Actually, I'll show you better. I'll show you Wednesday how you can get around this as well with uniform cost search. But in the meantime, what you can do is we can-- here's a trick. You can add the number of steps into the state. OK. So this eliminates all cycles. Because you're always moving forward in time.

So if you go A to B to C to A, now that A is going to be different because you've taken a number of different number of steps. So that's how one way you can break cycles. Now you have to-- still doesn't prevent the infinite looping. You have to separately define is n to be true when the number of steps exceeds some threshold. So now you're counting steps. You can say like after 100 steps, that's it. No more. And then you can define in let's say infinite cost

if you're exceeding the threshold just to say that those states are essentially no good. Yeah. The first solution that you're supposed to get to is also beyond this threshold. Is there a way to set that threshold so that we know that we might arrive at a better solution? Yeah. So the question is basically how do you set this threshold because it's possible that the optimal solution might be beyond this threshold. There's no general answer to this except for just look at the problem

and figure out what you need to do. Generally, when the costs are all positive, you can have-- essentially, if you look at the number of states, I think that should be a pretty good upper bound. I mean, it's probably can be very loose. Sometimes, if you have negative costs, the optimal answer is actually-- technically, if you find a negative cycle, you just keep on going around and your cost goes down and down and down. But that's degenerate.

If you detect that, then you just return minus infinity. Yeah. Is that function necessarily have to define success? So yeah. So the question is the n function have to also now determine whether it's successful or failure? Yes morally. The way I implemented this, again, is a little bit of a trick, where you say is n is just going to return Boolean. Is it true or false? And then I'm going to put it on the successors function to say like if I'm entering an n that state that is bad, meaning that I'm exceeding the threshold, then I'm going to give a cost of infinite there. Now when you're doing the min-- we're always trying to find-- remember, find the minimum cost solution. So infinity is not going to be the minimum cost solution if there's another solution.

So I'm sort of like hacking the framework to incorporate all these constraints. But this is part of the game here is that the states cost framework is fairly flexible. It's a kind of a game to figure out how to pack various constraints into this. OK. So this 237 is not great. And it's going to get worse. So how can we improve on the efficiency of exhaustive search? Any ideas? Yeah. Make comparisons for the best results. And [INAUDIBLE] now [INAUDIBLE] this could result in the best result. The suggestion is to compare the current result with the previous result. Well, I mean, the current approach-- let's see. I mean, do that comparison went through this minimum? So I am trying to find the cost.

So there's a solution that minimizes the cost. This min is doing a comparison. So any ideas? Any other ideas? Yeah. Like a lookup table. So before you compute the feature cost, you look at the table to see if it's computed before. And if it is, then-- Yeah. Yeah. So the suggestion here is you make a lookup table and you see if you've computed the future cost for a given state. And if you have, then don't do it again. Yeah. So that's exactly what we're going to do.

And the term for this is called dynamic programming. And the term, you might find it strange, but it comes from actually the '50s, Richard Bellman, who pioneered, defined Markov decision processes, which we'll look at next week, introduced this term. And the word dynamic means that it's sequential decision making, multiple actions over time. It's dynamic. There's a temporal aspect to it. And programming just means optimization. You're trying to find the minimum cost solution, for example. OK.

Programming doesn't mean you're Python. There's many ways to talk about dynamic programming the way I'm going to introduce it, which is to have the minimum change from what we've already done, is essentially I think about as just exhaustive search plus caching. And this is also known in some circles, it's called memoization. And recall that backtracking search or exhaustive search explores some states more than once. And dynamic programming says, well, if you already saw a state, don't do it again. OK.

So here's what we're going to do. It's going to be-- the dynamic program function is going to be identical to exhaustive search, except for I'm going to introduce three lines of code, I think. OK. They're going to be marked new. So the first thing is we're going to cache solutions for each state. So for each state, I'm going to cache the solution that we've computed so far. And then in this recurrence, the first thing I'm going to do is check if the state is already in the cache.

And if it's already in the cache, then I just return it. And the second thing or the, I guess, the third change is down here is after I computed the best solution, I'm just going to cache it. Everything else, all of this is identical. If you don't believe me, you can do a diff yourself. OK. So again, the three changes, introduce the cache, check if there's a cache.

If there's a state is on a cache, and then at the end, put it in the cache if it's not there. OK. Actually, this design pattern is so prevalent that you can even write Python decorator that says at memoize, and it will wrap your function and memoize it. But we didn't do that just to make it more transparent. OK. So I ran this. And at the end, you can see what the cache looks like. It has, for every state, the future action. So this is actually kind of nice because not only did you

solve the problem for the start state, but you can look at all the solutions, the future solutions from every state. So for one, remember the cost is six. From two, it's five. From three, you walk, walk, tram. From five, it's just taking the tram. Even from six, it's walk, walk, walk. And I guess, the rest of the way, it's walking because otherwise, you overshoot. OK.

Yeah. So it's best solution. Yes. The cache store is the best solution from each state. Where does it make that comparison? So the question is where did it make the comparison to know that it's the best solution? So if you remember, this is inherited from the exhaustive search. So here, I'm looking at all possible successors. And I'm accumulating all the solutions. And here, I do the minimum by key. So here, for every solution, I'm taking its cost and just taking the minimum.

So this is one way to write it, I think, which is a bit more functional. You could have also written it. You loop over, and you do the explicit like less-- if cost is less than the best so far, then update that, which is a bit more procedural way of doing this. This I find a little bit more elegant, but it's the same thing. OK. Are we good with dynamic programming? So let's try some larger problems now. So now, I have 17. Now the number explored states should be identical to the number of actual states here. Because I'm not exploring a state more than once, I can go up to 100 now and I can compute it for 100. So let's just look at the solution. So it's walk, walk, tram, tram, tram, walk, tram, tram. OK. So it kind of makes sense. One thing that's actually kind of fun about these search

problems is that you can define these problems. And then you look at the solution. And intuitively, you can see what you're supposed to do. Like you kind of want to get in position to take these trams so that you hop by powers of 2, but you don't want to overshoot. So then you have to line up exactly where it is. But in general, you can think about this as a fairly difficult reasoning problem, and you have to think through all the different possibilities.

So one of the things actually is nice is that you can just solve these problems and you look at the solution and you look at it, it's like, oh, that's kind of clever because it might not be as obvious as just like doing a bunch of walks and then taking the tram. OK. So let's discuss when would you want to use dynamic programming. Hopefully, I've solved the dynamic programming is like, oh, wow, exponential improvement in time complexity. That's great. So not quite so fast. There's some caveats here. One fact about life is that memory is more precious than time. Well, life meaning computer life, I guess. You can always run a program for longer, but memory doesn't grow. So if you want to run dynamic programming,

it's going to take memory a total number of states. And your computer is only has so much memory in it. And once you fill it up, then you just can't run dynamic programming at all. I mean, you can do things write to disk, but that gets really slow and you don't want to do that. OK. So that's when you can then use dynamic programming is when the number of states is actually fits. So if your number of states is like a trillion, probably it's not going to shouldn't do dynamic programming. OK.

Now the question is that you can do dynamic programming, when does it actually provide speedups over exhaustive search? And the intuition here is that it's useful when essentially there's a lot of ways to reach a given state. If every action takes you to a new state, then you might as well do exhaustive search, because essentially, the cache is always you're going to have a cache miss. So maybe I'll just draw two pictures here.

If your situation looks like that, where you have your start state, and then basically all your solutions look like this. They're all independent. Then should you do dynamic programming here? No. right? Whereas if you have your-- let's say you have your state and then--

so typically, you might see these kind of lattice patterns, where at each state, you can essentially go like this, then you should use dynamic programming here because the total number of states is essentially linear. But the number of paths through this is exponential. OK. So there are many ways to reach a state. That means dynamic programming is going to have leverage.

So unfortunately, a lot of problems actually do look like this. Later, we'll see that if you generate from a language model, essentially it kind of looks like that because every sequence is kind of unique. We'll come back to that later. OK. So dynamic programming is just exhaustive search plus caching. And remember, exhaustive search, the key idea is future cost, which defines its recurrence and allows you to break down a problem into subproblems. And then you should use dynamic programming when the number of states fits in memory. And there's a lot of ways to go between states.

Yeah. So [INAUDIBLE] the memory complexity like exhaustive search was linear to [INAUDIBLE] the solution. [INAUDIBLE] program is linear to the size of the state. Then, if those two are both linear, how much efficiency is there? So just to repeat. So the memory complexity of exhaustive search is essentially the length of the longest solution. And the memory complexity of dynamic programming is the number of states. And so the question is if both are linear, then what is the speed advantage?

If both are linear, you probably should-- which is the case I think in these cases, then you should use dynamic programming because linear in memory means that dynamic programming is essentially going to be linear or maybe quadratic time. Whereas exhaustive search can be exponential time still. So let's move on. So, so far, we've computed the exact minimum cost solution. And the time complexity is at least the number of states. If you're doing dynamic programming, it's the number of states. If you're doing exhaustive search, it could be exponential in that. So large number. So what if the state contains the set of locations? So remember, the state is the information you need to be able to assess the validity of the solution or even determine what the cost and actions are.

And sometimes you need to store the set of location. If you need to remember all the places you've been so far, so you don't repeat the location or something. There's also, what if this was a sequence of words generated so far? There's a lot of sequences of words. So you're not going to cache all of them. So in that case, exact search is going to be intractable. And so now, we have to move to approximate search. And the key idea is that we're just going to heuristically look

at a subset of the actions. And as a result, we might miss something. But oh, well, we can't do anything about it. There's no guarantees here. So this is now we're entering the realm of just heuristics. But often, these heuristics can work well in practice. Your mileage will depend on your exact problem setting. So I'm going to look at two algorithms, best of n and beam search. OK. So these are going to be fairly simple ideas here. So the simplest idea is to just choose random actions

until we reach the end state. Do this n times and take the best solution. That's best of n. You might have heard best of n somewhere and it's like, oh, what is that fancy algorithm. No, that's it. OK. So let's do this example here. Travel problem with 10 locations. I'm going to take this opportunity to define some forward looking terminology. So a policy is a function that maps a state to an action. It can be deterministic. It can be random. OK. We usually see policies in reinforcement learning.

But we can actually talk about policies in search as well. Because search is actually going to be a special case of Markov decision processes, which are the thing that you do in reinforcement learning. OK. So a policy map states to action. So let's start at say one. And let's define a uniform policy, which is one particular policy. What it does is given a state, it looks at the possible successors. And it just chooses one uniformly at random. OK. Pretty simple. And so every time you call it, you

might get a different answer. So sometimes you get walk, sometimes you get tram. Hopefully, you get roughly half and half. So that's a policy. Now we can apply a policy from the start state to an end state to get a solution. And that's what I'm going to call a rollout. Again, borrowing terminology from reinforcement learning. OK. So a rollout, you start at state while we're not at the end. And I also have this capped so that it just in case you infinite loop or something.

I'm going to take a step according to the policy. And I'm going to just keep track of the steps. And I'm going to advance the state. So I took this step. Now state is going to be 2. And so as I go through this, I'm going to take the tram, take the tram, take walk, walk. OK. So now I'm at the end. And now I have this solution which is that has sampled, which is tram, tram, tram, walk, walk, has cost of 8. OK. I'm going to roll it out again. Now, it has 6. OK.

I guess, I got lucky and it got the minimum cost solution. Roll out again. Less lucky, I get a 9. OK. So you get the idea. I just roll out a bunch of solutions randomly trying actions, and then I'm just going to take the best one. And this is captured in this best of n function, which essentially just roll, does a bunch of rollouts.

Just roll out, roll out, roll out, roll out, roll out. Let's jump here. And then I'm going to just choose the best solution, which has cost 6. OK. OK. So the guarantee here is that as n goes to infinity, so the number of rollouts you do, the solution will actually converge to the minimum cost solution, assuming your policy tries has positive probability on all the actions. So this is kind of nice because it says there is a way to get to the minimum cost solution. Now, the bad news is that this could take exponentially long. So n might have to be like centillion or something.

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent