================================================================= TITLE: Deep learning week 3 DATE: 2024-12-30 (December 30, 2024 at 01:16 PM) DURATION: 33:35 (2015 seconds) WORDS: 3,933 words SPEECH ID: 26XPVAWXTIAWPDUB ================================================================= --- AI SUMMARY --- Deep learning, matrix multiplication, neural networks, deep learning frameworks, open source, programming criteria, running speeds, TensorFlow, gradient tape, cost function, hyperparameters, random sampling, log scale, exponentially weighted averages, course-defined search. --- FULL TRANSCRIPT --- [00:00] 0: To implement these models, to make an analogy, I think that hopefully you understand how to do a matrix multiplication, and you should be able to implement, you know how to code to multiply two matrices yourself, but as you build very large applications, you probably not want to implement your own matrix multiplication function. But instead you want to call a numerical linear algebra live and you take the do it more efficiently for you, but it still helps that you understand how multiplying two matrices work. So I think deep learning has now matured to that point where it's actually more practical, even more efficient, doing some things with some of the deep learning frameworks. So let's take a look at the frameworks out there today, there are many deep learning frameworks that makes it easy for you to implement your networks, and here are some of the leading ones. Each of these frameworks has a dedicated user and developer community, and I think each of these frameworks is a credible choice for some subsets of applications. There are a lot of people writing articles comparing these deep learning frameworks and how well these deep learning frameworks changes, and because these frameworks are often evolving and getting better month to month, I'll leave you to do a few internet searches yourself. And you want to see there are arguments on the pros and cons of some of these frameworks, but I think many of these frameworks are evolving and getting better very rapidly. So rather than too strongly endorsing any of these frameworks, I want to share you the criteria I would recommend you use to choose frameworks. One important criteria is the ease of programming, and that means both developing the neural network and iterating on it, as well as deploying for production, for actual use by values or millions, or maybe hundreds of millions of users, depending on what you're trying to do, a second closing criteria is running speeds, especially training on large data sets. Some frameworks will let you run and train your network more efficiently than others. And then one criteria that people don't often talk about, what I think is important is whether or not the framework is truly open. And for a framework to be truly open, it needs not only to be open source, but I think it needs good governance as well. Unfortunately, in the software industry, some companies have a history of open sourcing software, but maintaining single Corporation control of the software, and then over some number of years, as people start to use their software, some companies have a history of gradually closing off what was open source, or perhaps moving functionality into their own proprietary file services. So one thing I pay a bit of attention to is how much you trust that a framework will remain open source, you know, for a long time, rather than just being under the control of a single company, which, for whatever reason they choose to close it off in the future, even if the software is currently released under open source, but at least in the short term, depending on your preferences of language, whether you prefer Python or Java or C or something else, and depending on what application you're working on, whether It's computer vision or natural language processing or online advertising or something else. I think multiple of these frameworks could be a good choice. So that's it. On programming frameworks, by providing a higher level of abstraction than just a numerical linear algebra library, any of these programming frameworks can make you more efficient as you develop machine learning applications. [1015:54:40] 0: Here, just to print the initial value of w, we've not actually run train step yet, so All right, hopefully I have no syntax errors. So w is initially the value of zero, which is what we have initialized it to. And now let's run one step of our global learning algorithm and print the new value of w. And now is increased a little bit from zero to about 0.1 now let's run 1000 iterations of our train step, so for i in range 1000 [1178:29:20] 0: training step, [1188:53:20] 0: print W, and let's see what happens. Run pretty quickly. Aha. And now W is nearly five, which we knew was the minimum of this cost function. So isn't that cool? We just specified the cost function. Didn't have to take derivatives, and TensorFlow figured out how to minimize this for us. I hope this gives you a sense of the broad structure of a TensorFlow program, and as you do this week's programming exercise and play more with TensorFlow code yourself, some of these functions that I just used here will become more familiar. Just a couple things to notice. W is the parameter you want to optimize, so that's why we declared w as a variable, and all we had to do was use a gradient tape to record the order of the sequence of operations needed to compute the cost function, and that was forward prop and TensorFlow could figure out automatically how to take derivatives with respect to the cost function. And that's why in TensorFlow, you basically have to only implement the forward prop step, and it will figure out how to do the grading computation. Now, there's one more feature of TensorFlow that I want to show you. In the example we went through so far. The cost function is a fixed function of the parameter or the variable w, but whether the function you want to minimize is a function of not just the parameters of not just W, but also a function of your training set. So let's have some training data, x and x or x or y, and you train your neural network where the cost function depends on your data x or x and y, as well as the parameters W. How do you get that training data into a TensorFlow program? Let's go through another version of how to implement all this. [1740:05:20] 0: I'm still going to define w [1757:57:20] 0: as the variable. Also going to use the atom optimizer, but now I'm going to define x as a list of numbers as a an array, and I'm going to plug in one negative 10 [1838:18:40] 0: and 25 [1850:34:40] 0: and This will be [1863:22:40] 0: another so 32 [1873:14:40] 0: so these three numbers, one negative 10 and 25 will play the role of the coefficients of the cost function. So you can think of x as being that controls the coefficients of this quadratic cost function. [1945:20:00] 1: So I should get the hotel for the night, then go to that. [2009:25:20] 0: Plus so this is [2054:08:00] 0: the same [2099:22:40] 1: Yeah, because Jimmy, I guess Jimmy, wants to have a conversation with everybody, having the talk whatever situation that happened when he was there, happened in front of your kids and your parents house? He wants to have a conversation with us. He said that. He said that Lula hasn't even talked to him about it yet. She didn't say nothing about it during time. What happened? Yeah, he said he knew what happened. He said, like, you know, like [2255:17:20] 1: he didn't like the fact that he's like, Mama Papa's [2307:01:20] 2: not listening how we gonna keep the kids over there? Like, probably not gonna be bringing the kids over there, if you're gonna be like, undoing what we're trying to, you know, [2374:02:40] 2: get where you're coming from, [2392:10:40] 3: we're in control of the kids, right? [2412:26:40] 4: Right? You cut your finger? How you cut your finger? Yeah, [2488:53:20] 5: team found a conversation. [2533:57:20] 5: The door, putting this on. [2589:57:20] 5: Since mom should probably be in that conversation [2670:34:40] 5: so she can be like a witness in [2703:28:00] 4: like a witness, [2750:40:00] 2: to bring it in a different direction, like she conversation. Makes me like, I don't want to be this guy. I don't want to have to be thinking about that crap. [2844:26:40] 2: It's interesting that your sister hasn't Got it hasn't said anything. Oh. [3048:21:20] 1: All you can do is pray, right? [3063:17:20] 2: Maybe she didn't know how to respond. She just was, like, shocked. I [3151:54:40] 6: when he said that, he was like, both the girls were asking him not to do something, and he was doing his own thing. [3179:54:39] 2: And it was like, he says, I can chalk that up to sometimes that's just him being him. [3223:12:00] 1: When you said, stop eating cookies, they did. He did get a cookie. It was like, he was like, it wasn't like, he didn't get to eat no cookies at all. [3267:44:00] 2: So he just felt like, [3282:08:00] 6: and then I was like, I can't believe he even did that while Jimmy was there. [3329:09:20] 4: Like to do that in front of the husband like that. But it wasn't, it wasn't his kids [3390:13:20] 2: have probably was a mass respondent in a positive manner, if he was, he did that right in front of me. You. [3597:09:20] 4: Conversation with your [3610:29:20] 6: dad, kind of like framework ground rules. [3644:26:40] 4: I also don't want to make it seem like, oh. We want to feel like, Oh, we're better than we know how to raise kids. Otherwise that would be just like, [3686:50:40] 6: hey, if we're telling our kids [3704:26:40] 3: how to do something really fun. [3744:58:40] 5: Topics, and that's what I told and I'm like, Mom, Dad, [3795:28:00] 4: I'm The mom, I'm the authority. [3834:24:00] 5: Yeah, oh, figure out [3923:33:20] 2: a figure out a hotel that we can kind of [3959:54:40] 4: like I have a hotel up there, [3998:02:40] 6: but think about it. I had, I got to move to I'm not thinking about this. So now I'm gonna have to figure out a way how, like, remember, you're gonna have to be working. What remember I can, I can be driving. Remember I have, I have Wi Fi in the car. You Yeah, man, I'm not gonna be getting any call tomorrow. Even if I get a call, I can meet everybody out you. [4247:54:40] 5: Not that I Have my back Now I [4601:52:00] 2: Yeah, from there they're paying for it. Figure out [4634:40:00] 5: what figure out [4641:09:20] 2: what you tell me everyone that you need to take the [4691:01:20] 6: AI conventionally, but you didn't have to take it anymore, because I got everything already got, like on the otter all you didn't know you have it on [4740:26:40] 4: there. Every question you can take any course, [4774:18:40] 3: they both came downstairs. Yeah. On. [4846:56:00] 6: You get one time, one time you press it, and then you go, Oh, the one [4901:20:00] 4: last. It. I'll let you come back down here in a little bit, because we're gonna be going to go. We're gonna be going, [5063:54:40] 3: Yeah, the one I got has a pool. [5096:16:00] 1: Tomorrow is tomorrow is the 31st tonight is the 30th, 30th. That's what I'm saying. Like, [5136:48:00] 5: please, may I see the pool? [5165:20:00] 1: That's the pool. It's the indoor pool. [5184:37:20] 1: Yeah, look how big it is. [5209:09:20] 3: You can go in the pool. Yes, [5270:50:40] 1: yeah, now see what they have [5309:30:40] 0: in a grid like so, and systematically explore these values here, I'm placing down a five by five grid. In practice to be more or less than a five by grid. But you try out in this example, all 25 points, and then, you know, pick whichever hyper parameter works best. And this practice works okay when the number of hyperparameters is relatively small in deep learning, what we tend to do, and what I recommend you do instead, is choose the points at random. So go ahead and, you know, choose, maybe you have same number of points, right? 25 points, and then try out the hyper parameters on this randomly chosen set of points. And the reason you do that is that it's difficult to know in advance which hyperparams are going to be the most important for your problem. And as you saw in the previous slide, some hyperparams are actually much more important than others. So to take an example, let's say hyper parameter one turns out to be alpha, the learning rate. And to take an extreme example, let's say that hyper parameter two was that value epsilon that you have in the denominator of the atom alpha. So your choice of alpha matters a lot, and your choice of epsilon hardly matters. So if you sample on a grid, then you've really tried out five values of alpha, and you might find that all of the different values of epsilon give you essentially the same answer. So you've now trained 25 models and only gotten to try out five values for the learning rate alpha, which is, think that's really important. Whereas, in contrast, if you were to sample at random, then you know you will have tried out 25 distinct values of the learning rate alpha, and therefore you'd be more likely to find a value that works really well. I've explained this example using just two hyperparams. In practice, you might be searching over many more hyperparams in this so if you have, say, three hyperparams, I guess instead of searching over a square, you're searching over a cube where this third dimension is hyper parameter three, and then by sampling within this three dimensional cube, you get to try out a lot more values of each of your three hyper parameters. And in practice, you might be searching over even more hyper parameters than three, and sometimes it's just hard to know in advance which ones turn out to be the really important hyperparamsis for your application, and sampling at random, rather than in a grid, ensures that you're more richly exploring the set of possible values for the most important hyperparameters, whatever they turn out to be when you sample hyperparameters, another common practice is to use a course to find sampling scheme. So let's say, in this two dimensional example, that you sample these points, and maybe you found that this point worked the best, and maybe a few other points around it tended to work really well. Then in the course defined scheme, what you might do is zoom in to a smaller region of the hyperparamsis, and then sample more densely within this space, maybe again at random, but to then focus more resources on searching within this blue square if you're suspecting that the best setting the hyper parameters may be in this region. So after doing a core sample of this entire square, that tells you to then focus on on a smaller square, you can then start put more densely in this smaller square. So this type of a course defined search is also frequently used, and by trying out these different values of the hyper parameters, you can then pick whatever value allows you to do best on your training set, objective or does best on your development set, or whatever you're trying to optimize in your hyperparameter search process. So I hope this gives you a way to more systematically organize your hyperparameter search process. The two key takeaways are, use random sampling knowledge grid search and consider optionally, but consider implementing a course defined search process. But there's even more to hyper parameter search than this. Let's talk more in the next video about how to choose the right scale on which to sample your hyper parameters. [6485:20:00] 0: In the last video, you saw how sampling at random over the range of hyperparamsis can allow you to search over the space of hyperparamsis more efficiently. But it turns out that sampling at random doesn't mean sampling uniformly at random over the range of valid values. Instead, it's important to pick the appropriate scale on which to explore the hyperparamsis. In this video, I want to show you how to do that. Let's say that you're trying to choose the number of hidden units, NL, for a given layer l. And let's say that you think a good range of values is somewhere from 50 to 100 in that case, if you look at the number line, from 50 to 100 maybe picking some number of values at random within this number line is a pretty reasonable way to search for this particular hyperparameter. Or if you're trying to decide on the number of layers in your network, we're calling that capital L, maybe you think the total number of layers should be somewhere between two to four, then sampling uniformly at random among two, three and four might be reasonable, or even using a grid search where you explicitly evaluate the values two, three and four might be reasonable. So these were couple examples where sampling at uniformly at random over the range of contemplating might be a reasonable thing to do, but this is not true for all hyper parameters. Let's look at another example. Say you're searching for the hyper parameter alpha, the learning rate, and let's say that you suspect 0.0001 might be on the low end, or maybe it could be as high as one. Now if you draw the number line from 0.0001 to one, and sample values at random, uniformly at random, over this number line, or you know, about 90% of the values you sample will be between 0.1 and one. So you're using 90% of the resources to search between 0.1 and one, and only 10% of the resources to search between 0.01 and 0.1 so that doesn't seem right. Instead, it seems more reasonable to search for hyper parameters on the log scale, where, instead of using linear scale, you'd have 0.0001 [7122:18:40] 0: here and then 0.001 [7137:14:40] 0: 0.01 0.1 and then one, and you instead sample uniformly at random on this type of logarithmic scale. Now you have more resources dedicated to searching between point o1 and point o1 and between point o1 and point o1 and so on. So in Python, the way you implement this is that R equals negative four times NP, dot random, dot Rand, and then a randomly chosen value of alpha would be alpha equals 10 to the power of r. So after this first line, R will be a random number between minus four and zero. And so alpha here will be between 10 to the minus four and 10 to the zero. So 10 to the minus four is this left thing this 10 to the minus four, and one is 10 to the zero. In a more general case, if you're trying to sample between 10 to the A to 10 to the B on the log scale. And in this example, this is 10 to the A. And you can figure out what a is by taking the log base 10 of 0.01 which is going to tell you a is negative four, and this value on the right, this is 10 to the B. And you can figure out what B is by taking log base 10 of one which tells you b is equal to zero. So what you do is then sample R uniformly at random between A and B. So in this case, r would be minus four and zero, and you can set alpha or your randomly sample hyper parameter value as 10 to the R. Okay. So just to recap, to sample the log scale, you take the low value, take logs to figure out what is A. Take the high value, take a log to figure out what is b. So you're now trying to sample from 10 to the A to 10 to the B on a log scale. So you set R uniformly at random between A and B, and then you set the hyper parameter to be 10 to the R. So that's how you implement sampling on this log rhythmic scale. Finally, one other tricky case is sampling the hyper parameter beta used for computing exponentially weighted averages. So let's say you suspect that beta should be somewhere between 0.9 to 0.999 you know, maybe this is the range of values you want to search over. So remember that when computing exponentially weighted averages. Using 0.9 is like averaging over loss the last 10 values. Kind of like taking the average of 10 days temperature, whereas using 0.999 is like averaging over the last 1000 values. So similar to what we saw on the last slide, if you want to search point nine and point 999, it doesn't make sense to sample on the linear scale, right, uniformly at random between point nine and point 999, so a better way to think about this is that we want to explore the range of values of one minus beta, which is going to now range from 0.1 to 0.001 [8013:04:00] 0: so we'll sample [8024:32:00] 0: between beta take on values from 0.1 to maybe 0.1 to 0.001 so using the method we have think about on the previous slide, this is 10 to the minus one. This is 10 to the minus three. Notice on the previous slide we had the small value on the left and the large value on the right, but here we have a reverse with the large value on the left and the small value on the right. So what you do is you sample R only a random from negative three to negative one, and you set one minus beta equals 10 to the r, theta equals one minus 10 to the r, and this becomes a randomly sampled value of your hyper parameter chosen on the appropriate scale. And hopefully this makes sense in that this way you spend as much resources exploring the range 0.9 to 0.99 as you would exploring 0.99 to 0.999 so if you want a slightly more formal mathematical justification for why we're doing this, right? Why? Why is it such a bad idea as a sample on the linear scale, it is that when beta is close to one, the sensitivity of the results, you get changes, even with very small changes to beta. So beta goes from 0.9 to 0.9005 that was no big deal. This is hardly any change in your results. But if beta goes from 0.999 to 0.9995 this will have a huge impact on exactly what your algorithm is doing. Right in both of these cases, it's averaging over roughly 10 values. But here it's gone from an exponentially weighted average over about 1000 the last 1000 examples, to now the last 2000 examples. And it's because that formula you have one over one minus beta, this is very sensitive to small changes in beta, when beta is close to one. So what this whole sampling process does is it causes you to sample more densely in a regime of when beta is close to one, or alternatively, when one minus beta is close to zero, so that you can be more efficient in terms of how you distribute the samples to explore the space of possible outcomes more efficiently. So I hope this helps you select the right scale on which the sample hyperparams, in case you don't end up making the right scaling decision on some hyper parameter choice, don't worry too much about it. Even your sample on a uniform scale, where some other scale would have been superior, you might still get okay results, especially if you use a course defined search, so that in later iterations, you focus in more on the most useful range of hyperparamsis values to sample. Hope this helps you with your hyperparamsis in the next video I was also want [8807:44:00] Speaker None: to