================================================================= TITLE: Deep learning week 1 DATE: 2024-12-20 (December 20, 2024 at 12:11 PM) DURATION: 01:15:19 (4519 seconds) WORDS: 10,991 words SPEECH ID: 26XPVAWXMUEDJIRM ================================================================= --- AI SUMMARY --- Deep learning, neural networks, backpropagation, mini-batch gradient descent, ReLU activations, word embeddings, unsupervised learning, attention mechanisms, reinforcement learning, optimization algorithms, exponentially weighted averages, bias correction, gradient descent, machine learning, artificial intelligence. --- FULL TRANSCRIPT --- [02:24:00] 0: Hi, Masha, I'm really glad you could join us here today. You know, today, you're not just a researcher or engineer deep learning. You become one of the institutions, one of the icons of deep learning, but I really like to hear the story of how it started. So how did you end up, you know, getting into deep learning and then pursuing this journey, [104:37:20] 1: right? Well, actually, it started when I was a kid, adolescent, reading a lot of science fiction, like I guess many of us. And when I started my graduate studies in 1985 I started reading your night papers, and that's where I got all excited. And it became really a passion. And [202:02:40] 0: actually, what was that like in what mid 80s, right? 1985 papers? Yeah, it was, well, [265:30:39] 1: you know, coming from the courses I had taken in classical AI with expert systems, and suddenly discovering that there was all this world of thinking about how humans might be learning and human intelligence, and how we might draw connections between that and artificial intelligence and computers, that was really exciting for me when I discovered this literature, and I started reading the connectionists, of course, so the papers from Jeff Hinton, remote heart and so on. And I worked on the current nets. I worked on speech recognition. I worked on Hmm, so graphical models. And then quickly I moved to at&t, Bell Labs and MIT, where I did postdocs, and that's where I discovered some of the issues with the long term dependencies with training neural nets. And then shortly after, I got recruited UDM back in Montreal, where I had spent most of my childhood, adolescence, years, [552:00:00] 0: um, you know. And so as someone who's been there for the last several decades and seen, you know, seen it all, certainly seen a lot of it, um, tell me a bit about how you're thinking about deep learning, about neural networks, has evolved over this, over this time. We start [623:28:00] 1: with experiments, with intuitions and theory sort of comes later. We now understand a lot better, for example, why backdrop is working so well, why depth is so important. And you know, these kinds of notions we didn't have any solid justification for in those days when, when we started working on deep nets in the early 2000s we had the intuition that it made a lot of sense that a deeper network should be more powerful, and we didn't know how to take that and prove it. And of course, our experiments initially didn't work, [785:46:40] 0: and actually were there were the most important things that you think turned out to be right, and what were the biggest surprises of what turns out to be wrong compared to what we knew 30 years ago? [833:04:00] 1: Oh sure, so one of the biggest mistake I made was to think that, like everyone else in the 90s, that you needed smooth nonlinearities in order to for backdrop to work, because I thought that, you know, if, if we had something like rectifying nonlinearities, where you have a flat part, that would be really hard to train, because the derivative would be zero in so many places. And when we started experimenting with experimenting with ReLU with deep nets in 2010 you know, I was obsessed with the idea that, Oh, we should be careful about whether neurons won't saturate too much on the zero part. But in the end, it turned out that actually, the ReLU was working a lot better than the sigmoids and attach. And that was a big surprise. We did this, exploring this because of the biological connection, actually not because we thought that it would be easier to optimize, but it turned out work better, whereas I thought it would be harder to try. [1128:00:00] 0: So let me ask you, what is the relationship between deep learning and the brain? That's the obvious answer, but I'm curious, what's your answer to that? [1169:57:20] 1: Well, the initial insight that really got me excited with neonats was this idea from the connectionists that information is distributed across the activation of many neurons, rather than being represented by sort of the grandmother cell, as we're calling it, a symbolic representation. That was the traditional view in classical AI. And I still believe this is a really important thing. And I see people we just discovering the importance of that even even recently. So, so that's that was really a foundation. The death thing is something that came later in the early 2000s but it wasn't something I was thinking about in the 90s version, for example, [1402:56:00] 0: I remember you build a lot of relatively shallow but very disputed representations, or the word embeddings like very early [1440:21:19] 1: on, right? That's right, yeah, that's that's one of the things that I got really excited about in the late 90s with, actually, my brother Sandy and I worked on the idea that we could use neural nets to tackle the curse of dimensionality, which was believed to be one of the central issues with statistical learning, and the fact that we could have these representations could be used to represent joint distributions over many random variables in a very efficient way. And it turned out to work quite well. And then I extended this to joint distributions over sequences of words, and this is how the word embeddings were born. Because I thought, oh, you know, this will allow generalization across words that have similar semantic meaning and so on. [1660:16:00] 0: So over the last couple of decades, your research group has invented more ideas than anyone can summarize in a few moments. So curious, what are the inventions or ideas young most proud of from your group? [1721:09:19] 2: Right? So I think I mentioned long term dependencies, the study of that. I think people still don't understand it well enough. Then there's the story I mentioned about curse of dimensionality, joint distributions with neural nets, which became, more recently, the need that Hugo and Rochelle did. And then, as I said, that gave rise to also the work on learning word embeddings for joint distributions for words. Then came, I think, probably the best known events of the work we did with deep learning and stacks of auto encoders and stacks of RBMs. [1898:07:59] 1: One thing then it was the work on understanding better the difficulties of training deep nets with exactly Laurel, with the initialization ideas and also the vanishing gradient in deep nets. And that work actually was the one which gave rise to the experiments showing the importance of piecewise linear activation functions. [2013:57:19] 2: Then I would say some of the most important work regards the work we did with unsupervised learning, the denoising encoders, the GaNS, which are for these days with [2086:50:40] 2: translation attention, which turned out to be really important for making attention, making translation work, and It's currently used in industrial systems like Google Translate. But this attention thing actually really changed my views on neural nets. Neural nets, we used to think it's machines that can map a vector to a vector, but but really, with attention mechanisms, you can now handle any kind of data structure. And this is, this is really opening up a lot of interesting avenues, direction of actually connecting to biology. One thing that I've been working in the last couple of years is, how could we come up with something like backdrop that brains could implement? And we have a few papers in that direction that seems to be interesting for the neuroscience people. And then we're continuing on that direction. [2334:50:40] 0: One of the topics that I don't even think a lot about is the relationship between the brain. Can you tell us a bit more about that? [2374:34:40] 2: The biological thing is something I've been thinking about for a while, actually, and in having a lot of would say daydreaming, because it's, it's like, I think of it like a puzzle. So we have these pieces of evidence from brain and from learning in the brain, like spike time, independent plasticity and and on the other hand, we have all these concepts, from machine learning, the idea of globally training the whole system with respect to an objective function, the idea of backdrop. And what does backdrop mean? Like, what does credit assignment really mean? When I started thinking about how brains could do something like bankrupt, it prompted me to think about, well, maybe there's like some more general concepts behind bankrupt, which make it so efficient, which allows to be efficient with bankrupt. Maybe there's a larger family of ways to do credit assignment, and that connects to questions that people in reinforcement learning have been asking. So it's interesting how sometimes asking a simple question leads you to thinking about so many different things and forces you to think about so many elements that you'd like to bring together like a big puzzle. So this has gone for a number of years. I mean to say that this whole endeavor, like many of the ones that I've covered, has been highly inspired by Jeff Hinton's thoughts. So in particular, he gave this talk in 2007 I think the first deep learning workshop on what he thought was the way that the brain is working, how temporal, kind of temporal code used. [2912:42:40] 3: Then she doing some [2922:18:40] 2: of the job that I brought and that led to a lot of the ideas that I've explored recently. Yeah, so it's kind of an interesting story that has [2983:28:00] 2: been running for a decade now. One of the topics I [2998:08:00] 0: would just speak about multiple times as well is also, can you [3043:01:20] 2: so unsupervised learning is really important right now, our industrial systems are based on supervised learning, which essentially requires humans to define what the important concepts are for the problem, and We label those concepts in the data, and we build all these amazing toys and services and systems using this, but cues are able to do much more. We are able to explore and discover new concepts on observation and interaction with the book, a two year old is able to understand intuitive physics. In other words, she understands gravity, she understands pressure, she understands inertia, she understands liquid solids, and of course, her parents never told her about any of these stuff, right? So how did you figure that? So that's the kind of question that unsupervised learning is trying to answer. It's not just about we have labels or we don't have labels. It's about actually building a construction, a mental construction, that explains how the world works by observation, and more recently, I think, combining the ideas and improvised learning with the ideas and reinforcement learning, because I believe that there's a very strong indication about the important concepts that we're trying to disentangle and trying to separate from each other that a human or machine can get by interacting with The book, by by exploring the world and trying things and trying to control things. So these are, I think, tightly coupled to the original ideas of supervised learning. So my take on high supervised learning 15 years ago, when, when we started doing the order for some vrbms and so on, was very focused on the idea of running good representations, and I still think this is a central question. But the thing we don't know is, how what is a good representation? How do we figure out an objective function, for example? So we've tried many things over the years, and that's actually one of the cool things about unprovided learning research that there are so many different ideas, so different ways that this problem can be attacked, and that's just maybe there's another one we'll discover, you know, next year, that's completely different and and maybe the brain is using something else, completely different. So it's, it's not incremental research, it's something that, in itself, is very exploratory. You don't have, like, a good definition of what's the right objective function to even measure that a system is doing a good job of enterprise learning. So of course, it's challenging, but at the same time is, you know, it leaves open a wide field for the possibilities, which is what researchers really love, is something that occurs to me. [3850:13:20] 0: So today, there's so much going on, I think, with past the point where it's possible for everyone to read every single so what did you dreading today excites you to know. [3919:49:20] 2: So I'm I'm very ambitious, and I feel like the current state of the science learning is far from where I get to see it. And I have the impression that our systems right now make mistakes that suggest the kind of mistakes that suggest that they have a very superficial understanding of the world. So what excites me the most now is sort of direction of research where we're not trying to build systems and we do something useful. We're just going back to principles about how can a computer observe the world, interact with the world and discover how that world works, even if that world is simple, something that we can program as a kind of video game, we don't know how to do that, and that's cool, because I don't have to compete with Google and Facebook yet, Baidu and so on, right? Because this is a kind of basic research that can be done by anyone in their garage, and could change the world. So there are many, of course, many directions to practice, but I see a lot of the fruitful interactions between ideas and deep learning and enforcement learning being really important there. And, you know, I'm really excited that the progress this direction could have a huge impact on practical applications, actually, because if you look at some of the big challenges that we have in applications, like how we deal with new domains or categories, and we can have two three examples, and in cases where humans are very good at solving those problems, so these transfer learning and generalization issues, they would become much easier to tackle if we have systems that had a better understanding of how they would work, a deeper understanding, right, what is actually going on, what are the causes of what I'm seeing, and how could I, you Know, influence what I'm seeing by my actions. So these, these are the kinds of questions I'm really excited about these days. I tend to connect also. So the deep learning research that has evolved for the last couple of decades with even older questions in AI, because a lot of success in deep learning has been with perception. So what's left, right? What's left is sort of high level conditions, which is about understanding at an abstract level how things work. So our program of understanding high level abstractions, I think, has, you know, not reached those high level of abstractions. And so we have to get there. We have to think about reasoning, about sequential processing of information. We have to think of how causality works and how machines can discover all these things by themselves, potentially guided by humans, but as much as possible, in an autonomous way. [4698:13:20] 0: And it sounds like, from part of what you said, that your family research approaches where you experiment on toy problem, not into the strategy, right, but on the small problem, and you're optimistic that that transfers to bigger problems later. [4765:14:40] 2: Yes, yes, yes. I mean, it transfers in a meta way, right? Of course, we're going to have to do some work to escape and address those problems, but my main motivation for going to those toy problems is that we can understand better our failures, and we can reduce the problem to something We can intuitively, sort of manipulate and understand more easily. So sort of a classical divide and conquer, you know, science approach. And also, I think something people can think about enough is the cycle, the research cycle can be much faster, right? So, if I can do an experiment in a few hours, I can, I can progress much faster if I have to find a huge model that tries to capture the whole common sense, and you know, everything in the general knowledge, which will eventually will do is just take each experiment. Just takes too much time with time hardware. So while our hardware friends are holding machines that can be 1000 or a million times faster I'm doing those toy experiments. [5101:52:00] 0: Also just speak about the size of deep learning, not just as an engineering security, but doing more work to understand what's really going on. [5147:44:00] 2: Share your thoughts on that. Yeah, absolutely. I fear that a lot of work that we're doing is sort of like blind people trying to find their way. And you can get a lot of luck and find interesting things that way, but, but really, if we sort of stop a little bit and try to understand what we're doing in a way that's transferable because, because we go down to principles, to theory. But when I say necessarily math, but I don't, I'm not like, of course, I like math and so on, but, but I don't think that we need that everything we formalize mathematically but be formalized logically. In other sense that I can convince somebody that you know this should work with that this makes sense. This is the most important aspect, and then math allows us to make that stronger and tighter. But really it's more about understanding and it's about also doing our research, not to be the next baseline or a benchmark, or beat the other guys, the other lab or the other company. It's more about, you know, what kind of question Should we ask that will allow us to understand better the phenomena of interest, like, you know? What makes, for example, training in deeper networks harder or a new current nets harder. We have some ideas, but a lot of things we don't understand yet. So we can maybe design experiments whose goal is not to have, like a better algorithm, but just to understand better the algorithms we currently have, or why, what circumstances make particular algorithm work better, and why? I mean, it's the why, that's what science is about this why today [5624:48:00] 0: there are a lot of people that want to answer the view, and I sure you've answered this a lot in one on one second with all the people watching this on video. What advice would you have? [5685:04:00] 2: So first of all, there are different motivations, different things you could do. What you need to become a deep learning researcher may not do the same as if you want to be an engineer who's going to use deep learning to build products. There's a different level of understanding that's needed in both cases, but in any case, in both cases, [5787:17:20] 2: practice, practice. So [5804:26:39] 2: to really master a subject like deep learning, of course, you have to read a lot. [5829:30:40] 2: You have to practice programming, the things yourself. [5848:42:39] 2: Very often, I introduce students who have used software, and these days, there's so much so good software around that you can just plug and play and understand nothing of what you're doing, or at such a superficial level that then it becomes hard to figure out when it doesn't work and what's going on. So actually trying to implement things yourself, even if it's inefficient, but just to make sure you really understand what is going on is really useful. And don't [5976:21:19] 0: just use one of the COVID frameworks. You can see everything. You don't really know what just [6005:41:20] 2: happened exactly. And I would say even more than that, try to derive the thing yourself from, you know, first principles that you can so that really helps. But yeah, I mean the usual things you have to do, like reading, looking at other people's code, writing your own purpose, doing a lot of experiment, making sure you understand everything you do. So I mean, especially for the science part of it, trying to ask, Why am I doing this? Why are people doing this? Maybe the answer is, is somewhere in the book, and you have to read more. But it's even better if you can actually figure it out by yourself. [6196:32:00] 0: And in fact, things to read you. And even the fellow Aaron COVID wrote a highly, very, highly regarded. [6234:45:20] 2: Thank you. Thank you. Yes, it's setting a lot. It's like it's crazy. I feel like there's more people reading this book than people who can read it right now, but, but yeah, also, Proceedings of the ICLR Ike or conference, is probably the best concentrated place of good papers. Of course, they're really good papers at nips and ICML and other conferences, but if you really want to go for a lot of good papers, just read the last few ICLR proceedings, and that will give you a really good view of the field. [6395:54:40] 0: Any other thoughts when people ask you for advice, you know, how does someone right become good at deep learning? Well, [6445:20:00] 2: it depends on where you come from. Don't be afraid by the math. Just develop the intuitions, and then the math become really easier to understand once, once you get the hang of what's going on at the input level. And one good news is that you don't need five years of things being to become proficient at anybody you can actually learn pretty quickly. If you have a good background in computer science and math, you can, you can learn enough to use it and build things and start research experiments in just a few months, you know, something like six months. For people with the right training, maybe they don't know anything about machine learning, but if they're good in math and computer science, it can be very fast and of course, so that means you need to have the right training and mapping through science. Sometimes what you what you learn in just computer science courses, is not enough. We need some, some, you know, continuous path especially so this is probability algebra and optimization, for example, and [6755:28:00] 2: calculus, yeah, [6760:00:00] 0: thanks a lot, Josh for sharing all these comments and insights and advice, even though I've learned, even though I've known you for a long time, there are many details of your history. [6811:49:20] 2: Well, thank you, Andrew for doing this, this special recording, and what you're doing and well, it's going to be used by a lot of people. Maybe they don't [6874:18:40] 4: want to shut down. I wouldn't go that far, but they want a people. They want change in Washington, DC. These [6943:22:40] 5: days, Washington, the government. We had to shut down twice in order to get one funds attention. Once we got a 900 zero and said, terrible. We were the first majority since 1928 and it happened after [7006:56:00] 5: the two shutdowns because American [7021:36:00] 5: people said, These guys, they actually mean what they say. So I would urge the public stand firm to very large scale change, sign it out, and wrestle with this company, that while the Washington news media and the Washington audience and the Washington are on their location, the rest of the country is going to look forward, finally, [7121:41:20] 5: headlines that finally, have a majority that actually intends to change. What would [7139:54:40] 4: you say to those people who are facing the holiday season looking at a check down because you've been through this before, and people were not out in the streets broken I mean, they were watching the situation just like we're watching it now. Saying this is changing, the [7210:18:40] 5: huge part of the part [7231:28:00] 5: of the market is going [7250:08:00] 5: to continue as usual. People are going to go to Christmas dinner. People are going to watch NFL, football, relax. This is, this is the constitutional system, the founding body fear dictatorship, the divided power out. We can barely get to work going forward. [7322:13:20] 5: That's exactly what the [7333:41:20] 5: counterpart has intended, and it takes very hard work. Exactly. This is the largest transition since Franklin Roosevelt in 1932 and it is going to be difficult, because this is riding a wild record. This is building. This is going to be a wild river flying to liberty, dinners to a dramatically better, more prosperous and safer future [7436:58:40] 4: Speaker, thank you very much [7450:02:40] 4: for your time. Merry Christmas to you and listen very [7459:38:40] 3: Christmas. Thanks. [7475:54:40] 4: Well, we'll link to this, and we have [7487:12:00] 4: a doctor in [7497:36:00] 4: the house to get here. Those puberty blockers for minors are now banned in the United Kingdom, but here in the United States is fueling fresh debate on this Friday about transgender treatment for children. Opponents of the drugs now argue it's about putting science over politics immediately. It's an emergency. Plus Robert Kennedy Jr and his health agenda are getting new back from Republicans on Capitol Hill. [7616:05:20] 6: My grandma always said that, if you have your help, clean up everything, and right now, America does not have their help. So what's the winning the RFP to [7651:49:20] 7: his wings? Let's make America helping you. [7670:02:40] 4: So RFK Jr has been always buying in 25 meetings with senators and one week trying to shore up support for this confirmation. There are still some concerns, questions about the stance on the hot button topics. I'm [7730:18:40] 4: going to run them past Dr Mark Seba, can focus next. We're [7765:04:00] 3: going to knock out the middleman. We're going to get drug costs down at levels that nobody has ever seen before. [7805:52:00] 4: President Elect Donald Trump defending his choice of Robert F Kennedy Jr, to have the Health and Human Services Department. Kennedy was on half the film this week meeting with senators ahead of this confirmation hearing. RFK Jr is teaching them and the rest of the country about this Make America healthy again. It focuses on our health and food ethics, putting the spotlight on obesity and practices re examining vaccine approvals, banning toxic chemicals and cutting corporate influence. Republican Senator Marjorie Marshall, the medical doctor, watched a new make America healthy again caucus in support of our patients. Let's make [7992:10:40] 7: America healthy again. And I think that starts with food as medicine as well. With a healthy diet. With a healthy [8050:24:00] 7: diet diseases. [8086:56:00] 4: Absolutely committed to doing this, Junior. [8096:48:00] 0: Hello and welcome back in this week we learned about optimization algorithms that will enable you to train in your networks much faster. You've heard me say before that applied machine learning is a highly empirical process. It's highly iterative process in which you just have to train a lot of models to find one that works really well. So it really helps to do the train models quickly. One thing that makes it more difficult is that deep learning tends to work best in the regime of Big Data enable the training neural network on a huge data set, and training on the large data set is just slow. So what you find is that having fast optimization algorithms, having good optimization algorithms, can really speed up the efficiency of mu and dt. So let's get started by talking about green badge gradient descent. You've learned previously that vectorization allows you to efficiently compute on all n examples, that allows you to process your whole training set without an explicit volume. So that's why we will take my training examples and stack them into this huge matrix, capital, x3, and then eventually it goes up to x, m, m training samples. And similarly for y, this is y1, y2, [8444:53:20] 0: three, so on, up to y m. [8465:46:40] 0: So the dimension of X was n, x by m, and this was one by M. Factorization allows you to process all m examples quickly, relatively quickly. If m is very large, then it can still be slow. So for example, what if M was 5 million or 50 million or even bigger, with the implementation of gradient descent on your whole training set? What you have to do is you have to process your entire training set before you take one little step of gradient descent, and then you have to process your entire training sets of 5 million training samples again before you take another little step of breathing descent. So it turns out that you can get a faster algorithm if you let grading descent start to make some progress, even before you finish processing your entire your giant training sets of 5 million examples in particular. Here's what you can do. Let's say that you split up your training set into smaller your little baby training sets. And these baby training sets are called mini batches. And let's say each of your baby training sets have just 1000 examples in each so you take x1 through x1 1000, and you call that your first little baby training set, also called a mini batch. And then you take the next 1000 examples, x1 1001, through x2 1000, there's the next 1000 examples, and call the next one and so on. And I'm going to introduce a new notation. I'm going to call this X superscript with curly braces one, and I'm going to call this X superscript with curly braces two. Now if you have 5 million examples total, and each of these little mini branches has 1000 examples, that means you have 5000 of these, because, you know, 5000 times 1000 equals 5 million. So altogether, you would have 5000 of these new badges. So it ends with X superscript, COVID versus 5000 and then similarly, you do the same thing for y. You also split up your training data for y accordingly. So you call that y1 then this is y1, 1001, through y2, 1000. This becomes called y2, and so on until you have y5 1000. [9168:00:00] 0: So now [9183:01:19] 0: mini batch number t is going to be comprised of X t and Y t, and that is 1000 training examples with the corresponding input output pairs before moving on. Just to make sure my notation is clear, we have previously used superscript round brackets, I to index in the training set. So xi is the i training sample. We use superscript square brackets, l to index into the different layers of the neural network. So ZL comes from the z values for the L here of the neural network. And here, we're introducing the curly brackets T to index into different mini batches so you have x t, y t, and to check your understanding of these, what's the dimension of X t and Y t. Well, X is n x by m. So if x1 is 1000 training examples, or the x values for 1000 examples, then this dimension should be n x by 1000 and x2 should also be n x by 1000 and so on. So all of these should have dimension, and that's the 1000 and these should have dimension one by 1000 [9572:10:40] 0: to explain the name of this algorithm. Batch gradient descent refers to the gradient descent algorithm we've been talking about previously, that you process your entire training set all at the same time. And the name comes from viewing that as processing your entire batch of training examples all at the same time. Not just a great name, but that's just what it's called, mini batch gradient descent. In contrast, refers to the algorithm which we'll talk about on the next slide, and we should process a single mini batch x, t, y, t, at the same time, rather than processing the entire training set, x, y at the same time. So let's see how mini batch gradient descent works. To run mini batch gradient descent on your training sets you would run for T equals one to 5000 because we had 5000 mini batches of size 1000 each. And what you're going to do inside the for loop is basically implement one step of gradient descent [9846:34:40] 0: using [9855:54:40] 0: x, t, comma, y, t, and is as if you had a training set of size 1000 examples. And it was as if you were to implement the algorithm you're already familiar with, but just on this little training set size of m equals 1000 rather than having an explicit for loop over all 1000 examples. You would use vectorization to process all 1000 examples sort of all at the same time. So let's write this out. First, you implement for a prop on the inputs, so just on x, t, and you do that by implementing, you know, z1, equals w1, now, previously, we just have x there, right? But now you are processing entire trading set. You just processing the first mini batch, so it becomes x, t, processing mini batch t, [10134:18:40] 0: and then you would have [10145:20:00] 0: a one equals g1, [10161:04:00] 0: of z1, this will get capital Z, since we're this is actually a vectorized implementation, [10193:04:00] 0: and so on, until you end up with [10209:36:00] 0: a L, whereas I guess GL of ZL, and then this is your prediction. And you notice that here you should use a vectorized implementation. It's just that this vectorized implementation processes 1000 examples at a time, rather than 5 million examples. Next you compute the cost function J, which I'm going to write as one over 1000 since here, 1000 is the size of your little training set something i equals one through L. So [10465:20:00] 0: really the you know, loss, y, hat, i, y, I, and this notation for clarity, refers to examples from the mini batch, x, t, y, t, and then, if you're using regularization, you can also have this regularization term. This means the two denominator times sum over l, for being a sum of the way, which is the square. So because this is really the cost on just one unit batch, and then that index is cost j with a superscript T in current braces. So you notice that everything we're doing is exactly the same as when we were previously implementing gradient descent, except that instead of doing it on X Y, you're now doing it on x, t, y, t. Next you implement [10726:40:00] 0: back prop to compute [10742:56:00] 0: gradients [10753:52:00] 0: with respect to really, with respect to this, J, T, so you're still using only x, t, y t, and then you update the weights, you know, W every W L gets updated as W L minus alpha d, W L, and similarly for B, [10893:14:40] 0: and so this is one pass through your training set using mini batch gradient descent. The code I've written down here is also called doing one epoch of training. An epoch is a word that just means a single pass through the training set. So whereas with Batch gradient descent, a single pass through the training set allows you to take only one gradient descent step with mini batch gradient descent a single pass through the training set. That is one epoch, allows you to take 5000 gradient descent steps. Now, of course, you want to take multiple pulses through the training set, which you usually want to you might want another for loop or another while loop out there, so you keep taking pauses through the training set until, hopefully you converge, or have approximately converged, when you have a large training set, mainly Batch gradient descent runs much faster than Batch gradient descent, and it's pretty much what everyone in deep learning will use when you're training on a large data set. In the next video, let's delve deeper into mini batch business end so you can get a better understanding of What it's doing and why it works so well. You [11346:13:20] 0: in the previous video, you saw how you can use mini batch gradient descent to start making progress, to start taking gradient descent steps, even when you're just part way through processing your trading set, even for the first time in this video, you learn more details of how to implement gradient descent and gain a better understanding of what it's doing and why it works with Batch gradient descent on every iteration you go through the entire trading set, and you expect The cost to go down on every single iteration. So you plot the cost function J as a function of different iterations, it should decrease on every single iteration, and if it ever goes up even on one iteration, then something's wrong. Maybe a learning rate's too big on mini batch. Gradient descent, though, if you plot progress in your cost function, then it may not decrease on every iteration. In particular, on every iteration you're processing some x, t, y, t, and so if you plot the cost function, J, t, [11639:01:20] 0: which is computed [11652:53:20] 0: using just x, t, y, t, then is as if on every iteration, you're training on a different training set, or really trading on a different mini batch. So you plot the cost function J, you're more likely to see something that looks like this. It should train downwards, but is also going to be a little bit noisier. I so if you plot j of t as you're trading mini batch driven descent and maybe over multiple epochs, you might expect to see a curve like this. So it's okay if it doesn't go down on every duration, but it should trend downwards. And the reason it'll be a little bit noisy is that maybe x1 y1, is just a relatively easy mini batch, so your cost might be a bit lower, but then maybe just by chance, x2 y2, is just a harder mini batch, maybe you need some less label examples in it, in which case the cost would be a bit higher and so on. So that's why you get these oscillations as you plot the cost when you're running mini batch gradient descent. Now, one of the parameters you need to choose is the size of your mini batch. So M was the training set size on one extreme, if the mini batch size is equal to m, then you just end up with Batch gradient descent. So in this extreme, you would just have one mini batch x1, y1, and this mini batch is equal to your entire training set. So setting the mini batch size M just gives you back gradient descent. The other extreme would be if your mini batch size [12128:05:20] 0: were equal to one. [12144:53:20] 0: This gives you an algorithm called stochastic gradient descent. And here every example is this old mini batch. [12227:22:40] 0: So what you do in this case is you look at the first mini batch, so x1, y1, but when your mini batch size is one. This just has your first training example, and you take a gradient descent step with your first training example, and then you next take a look at your second mini batch, which is just your second training example, and take a grand descent step with that. And then you deal with the third training example and so on, looking at just one single training example at a time. So let's look at what these two extremes will do on optimizing this cost function. If these are the contours of the cost function you're trying to minimize so that your minimum is there, then Batch gradient descent might start somewhere and be able to take relatively low noise, relatively large steps, and just keep marching to the minimum. In contrast with Stochastic gradient descent, if you start somewhere, let's pick a different starting point. Then on every iteration, you're taking gradient descent with just a single train example, so most of the time you hit to what the global minimum, but sometimes you hit in the wrong direction. That one example happens to point you in a bad direction. So Stochastic gradient descent can be extremely noisy, and on average, it'll take you in a good direction, but sometimes it'll head in the wrong direction, as well as stochastic gradient descent won't ever converge. You'll always just kind of oscillate and wander around the region of the minimum. But it won't ever just head to the minimum and stay there. In practice, the mini batch size you use will be somewhere in between, somewhere between in one and m and one and m are respectively too small and too large, and here's why, if you use batch grading descent. [12784:10:40] 0: So this is your mini batch size equals m. You it, then you're processing a huge training set on every iteration. So the main disadvantage of this is that it takes too much time, too long per iteration. Assuming you have a very large training set, if you have your small training set, then bad training is fine. If you go to the opposite, if you use stochastic gradient descent, then it's nice that you get to make progress after processing just one example, that's actually not a problem, and the noisiness can be ameliorated or can be reduced by just using the smaller learning rate. But the huge disadvantage of stochastic gradient descent is that you lose almost all your speed up from vectorization, because here you're processing a single training example at a time. The way you process each example is going to be very inefficient. So what works best in practice is something in between [13084:15:59] 0: where you have some, you know, [13108:48:00] 0: mini batch size that's not too big or too small. And this gives you, in practice, the fastest learning. And you notice that this has two good things going for it. One is that you do get a lot of vectorization. So in the example we used on the previous video, if your mini batch size was 1000 examples, then you know you might rather vectorize across 1000 examples, which is going to be much faster than processing the examples one at a time and second, you can also make progress [13311:38:40] 0: without needing To wait [13325:30:40] 0: till you process the entire training set. [13356:48:00] 0: So again, using the numbers we have from the previous video, each epoch, each positive training set allows you to see 5000 gradient descent steps. So in practice, there'll be some in between mini batch size that works best. And so with mini batch descent. If you start here, maybe one iteration does this, two iterations d4 and it's not guaranteed to always head toward the minimum, but it tends to head more consistently in the Bachelor and minimum than Stochastic gradient descent. And then it doesn't always exactly converge or oscillate in a very small region. If that's an issue, you can always reduce the learning rate slowly. We'll talk more about learning rate to K or how to reduce the learning rate in a later video. So if the mini batch size should not be m and should not be one, but it should be something in between, how do you go about choosing it? Well, here are some guidelines. First, if you have a small training set, [13620:00:00] 0: just use Batch gradient descent. [13641:20:00] 0: You know, if you have a small training set, then no point using meaning Batch gradient descent. You can process the whole training set quite fast, so you might as well use Batch gradient descent. What a small training set mean? I would say, you know, if it's less than maybe 2000 it'd be perfectly fine to just use bathroom descent. Otherwise, if you have a bigger training set, typical mini batch sizes would be anything from 64 up to maybe 512. Are quite typical. And because of the way computer memory is laid out in access, sometimes your code runs faster. If your mini batch size is a is a power of two, right? So 64 is two to the six is two to the seven, two to the eight, two to the nine. So often I'll implement my mini batch size to be a power of two. I note in a previous video, I used a mini batch size of 1000 if you really want to do that, I would recommend you just use 1024 which is to the power of 10. And you do see mini batch sizes of size 1024, it is a bit more rare. This range of mini batch size is a little bit more common. One last tip is to make sure that your mini batch, all of your x, t, comma, y t, that that fits in, you know, CPU, GPU, memory, and this really depends on your application and how large a single training example is. But if you ever process a mini batch that doesn't actually fit in CP, PP, memory, whatever, using the process the data, then you find that the performance suddenly falls off a clip and suddenly much worse. So I hope this gives you a sense of the typical range of mini batch sizes that people use in practice. Of course, the mini batch size is actually another hyper parameter that you might do a quick search over to try to figure out which one is most efficient of reducing your cost function J. So what I would do is just try several different values, try a few different powers of two and then see we can pick one that makes your gradient descent optimization algorithm as efficient as possible. But hopefully this gives you a set of guidelines for how to get started with that hyper parameter search. You now know how to implement Mimi bash gradient descent and make your algorithm run much faster, especially when you're trading on a large trading set. But it turns out there are even more efficient algorithms than gradient descent or mini batch gradient descent. Let's start talking about them in the next few videos. [14392:10:40] 0: I want to show you a few optimization algorithms that are faster than gradient descent. In order to understand those algorithms, you need to use something called exponentially weighted averages, also called exponentially weighted moving averages in statistics. Let's first talk about that, and it will use this to build up some more sophisticated optimization algorithms. So even though I now live in the United States, I was born in London. So for this example, I got the daily temperature from London from last year. So on January 1, temperature was 40 degrees Fahrenheit. And I know most of the world uses a Celsius system, but I guess I live in the United States, which uses Fahrenheit, so that's four degrees Celsius. And on January 2, it was nine degrees Celsius, and so on. And then about halfway through the year, a year has 365, days. So this would be sometime day number 118, would be sometime in late May, I guess, or 60 degrees Fahrenheit, which is 15 degrees Celsius. And so start to get, you know, warm up to what summer and was colder in January. So you plot the data, you end up with this, with day one being sometime in January, that being the beginning of summer, and that's the end of the year, late December. So this would be January. January 1 is the middle of the year, approaching summer, and this would be the data from the end of the year, so this data looks a little bit noisy. And if you want to compute the trends, kind of the local average, or moving average of the temperature, here's what you can do. Let's initialize B zero equals zero, and then on every day, we're going to average it with a weight of 0.9 times whatever was the previous value plus 0.1 times that day's temperature. So theta one here would be the temperature from this first day and then the second day. We're again going to take a weighted average so 0.9 times a b, previous value, plus open one times today's temperature, and so on. V2 plus open one times state of the meat and so on. And the more general formula is, V on the given day is 0.9 times B from the previous day, plus 0.1 times the temperature of that day. So if you compute this and plot it in red, this is what you get. You get a moving average of what's called an exponentially weighted average of the daily temperature. So let's look at the equation we had from the previous slide. It was VT equals previously we had 0.9 then I turned that into a parameter beta, beta times BT minus one plus increases 0.1 then turn that into one minus beta times theta t. So previously you had beta equals 0.9 it turns out that, for reasons we go into later, when you compute this, you can think of dt as approximately [15240:48:00] 0: averaging over [15258:40:00] 0: something like one over One minus beta days temperature. So for example, when beta goes 0.9 you could think of this as averaging over the last 10 days temperature, and that was the red line. Now let's try something else. Let's set beta to be very close to one. Let's say it's 0.98 then if you look at one over one minus 0.98 this is equal to 50. So this is, you know, think of this as averaging over about the last 50 base temperature. And if you plot that, you get this green line. So notice a couple things. With this very high value of beta, the plot you get is much smoother because you're now averaging over more days of temperature. So the curve is just less wicked. Is now smoother. But on the flip side, the curve is now shifted further to the right, because you're not averaging over a much larger window of temperatures. And by averaging over a larger window, this formula, this exponentially weighted average formula, it adapts more slowly when the temperature changes, so there's just a bit more latency. And the reason for that is when beta is zero, 0.98, then it's giving a lot of weight to the previous value, and much smaller weight just point 02, to whatever you're seeing right now. So when the temperature changes, when temperature goes up or down, this exponentially weighted average just adapts more slowly when beta is still large. Now let's try another value that you set beta to another extreme. Let's say it is 0.5 then this, by the formula, will happen, right? This is something like averaging over just two days temperature, and you plot that, you get this yellow line. And by averaging only over two days temperature, you have a much is as if you're averaging of a much shorter window. So you're much more noisy, much more susceptible to how long it is, but this adapts much more quickly to if the temperature changes. So this formula is how you implement an exponentially weighted average. Again, it's called an exponentially weighted moving average into statistics literature, you're going to call it exponentially weighted average for short, and by varying this parameter, or later, we'll see it's actually a hyper parameter of your learning algorithm. You can get slightly different effects and will usually be some value in between that works best. That gives you the red curve, which, though maybe, looks at a better average of the temperature. You now know the basics of how to compute expansion through weighted averages. In the next video, let's gain a bit more intuition about what it's doing you [16016:48:00] 0: in the last video, we talked about exponentially weighted averages. This will turn out to be a key component of several optimization algorithms that you use to train your neural networks. So in this video, I want to delve a little bit deeper into intuitions for what this algorithm is really doing. Recall that this is a key equation for implementing exponentially weighted averages. And so if beta equals 0.9 you got the red line. If it was much closer to one, it was 0.98 you get the green line. And it was much smaller, maybe 0.5 you get the yellow line. Let's look a bit more than that to understand how this is computing averages of their daily temperature. So here's that equation again, and let's set beta equals 0.9 and write out a few equations that this corresponds to. So whereas when implementing it, you have t going from zero to one to two to three increasing values of T. To analyze it, I've written it with decreasing values of t, right? And this goes on. So let's take this first equation here and understand what v1 100 really is. So v1 100 is going to be, let me reverse these two terms. It's going to be 0.1 times theta 100 plus 0.9 times whatever the value was on the previous day. Now, but what is V 99 well, we just plug it in from this equation. So this is just going to be 0.1 times theta 99 and again, I've reversed these two terms, right? Plus 0.9 times V 98 so then what is V 98 well, you can just get that from here. So you can just plug in here, 0.1 times theta. 98 plus 0.9 times V 97 and so on. And if you multiply all of these terms out, you can show that v1 100 is 0.1 times theta 100 plus. Now let's look at coefficient on theta 99 it's going to be 0.1 times 0.9 times theta 99 now let's look at the coefficient on theta 98 there's a 0.1 here, times 0.9 times 0.9 so we expand out the algebra. This becomes 0.1 times 0.9 squared times theta 98 and if you keep expanding this out, you find that this becomes 0.1 times 0.9 cubed theta 97 plus 0.1 times 0.1 to the fourth times. Theta, 96 plus.dot.so this is really a weighted sum, and thus a weighted average of theta 100 which is the current day's temperature. We're looking for a perspective of v1 100, which you calculate on the 100th day of the year. But so is a sum of theta 100 theta, 99 theta 98 theta 97 theta 96 and so on. So one way to draw this in pictures would be if, let's say we have some number of days of temperature. So this is theta and this is t right. So theta 100 would be some value. Then theta 99 will be some value. Theta 98 so these are, so this is T equals 199 98 and so on, right? So you have some number of days of temperature, and what we have is then an exponentially decaying function. So starting from 0.1 [16973:09:20] 0: to 0.9, times 0.1 [16990:18:40] 0: to 0.9 squared times 0.12 [17011:38:40] 0: and so on. So you have this exponentially decaying function. And the way you compute v1 100 is you take the element wise product between these two functions and sum it up. So you take this value, theta, 100 times 0.1 times this value, theta, 99 times 0.1 times 0.9 that's the second term and so on. So it's really taking the daily temperature, multiplying with this exponentially decaying function, and then summing it up. And this becomes your v1 100. It turns out that up to details I'll talk about later, that all of these coefficients add up to one, or add up to very close to one, up to a detail called bias correction, which talk about in the next video. But because of that, this really is an exponentially weighted average. And finally, you might wonder, you know, how many days temperature is this averaging over? Well, it turns out that 0.9 right to the power of 10, is about 0.35 and this turns out to be about one over e1, over the base of natural logarithms, and more generally, if you have one minus epsilon. So in this example, epsilon would be 0.1 so this was 0.9 then one minus epsilon to the one over epsilon. This is about one over e this is about 0.3 4.35, and so in other words, it takes about 10 days for the height of this to decay to, you know, around 1/3 already one over e of the peak. So it's because of this that when beta equals 0.9 we say that it takes this is as if you're computing and especially weighted average that focuses on just the last 10 days temperature, because it's after 10 days that the weight decays to, you know, less than about a third of the weight of the current day. Whereas, in contrast, if beta was equal to 0.98 then, well, what do you mean 0.98 to the power of in order for this to be really small, it turns out that 0.98 to the power of 50 will be approximately equals one over e. So the weight will be pretty big. It'll be bigger than one over E for the first 50 days, and then it'll decay quite rapidly over that so intuitively, this is a hard and fast thing. You can think of this as averaging over about 50 days temperature, because in this example, to use, the notation here on the left is as if epsilon is equal to 0.02 so one over epsilon is 50. And this, by the way, is how we got the formula that we're averaging over one over one minus beta, or so days. Right here epsilon, we place a row of one minus beta. It tells you up to some constant roughly how many days temperature. You should think of this as averaging over but this is just a rule of thumb for how to think about it, and is in a formal mathematical statement. Finally, let's talk about how you actually implement this. Recall that we started with v zero, in which size is zero. They compute v1 on the first day, v2 and so on. Now, to explain the algorithm, it was useful to write down v zero, v1 v2 and so on as distinct variables. But if you're implementing this in practice, is what you do. You initialize V to be equal to zero, and then on day one you would set V equals beta times v plus one minus beta times theta one. And then on the next day you would update V to be equal to beta v plus one minus beta theta two, and so on. And I'm sometimes we use this notation, v subscript theta, to denote that V is computing this exponentially weighted average of the parameter theta. So just to say this again, but in a for loop format, you set v theta equals zero, and then repeatedly, kind of on each day, you would get mixed theta t and then sets B, theta gets updated as beta times the old value of b, theta plus one minus beta times the current value of v theta. So one of the advantages of this exponentially weighted average formula is that it takes a little memory. You just need to keep, you know, just one row number in computing memory, and you keep on overwriting it with with this formula based on the latest values that you got. And it's really this reason, the efficiency it just takes, you know, one line of code, basically, and just storage and memory for a single row number to compute this exponentially weighted average is really not the best way, and not the most accurate way, to compute an average. If you were to compute a moving window where you explicitly sum over the last 10 days, the last 50 days temperature and just divide by 10 or divide by 50, that usually gives you a better estimate. But the disadvantage of that of explicitly keeping all the temperatures around in some of the last 10 days is requires more memory, and it's just more complicated to implement and it's computationally more expensive. So for things, we'll see some examples next few videos where you need to compute averages of a lot of variables. This is a very efficient way to do so, both from a computation and memory efficiency point of view, which is why it's used a lot to machine learning, not to mention that it's just one line of code, which is maybe another advantage. So now you know how to implement exponentially weighted averages. There's one more technical detail. There's what you're learning about, called bias correction. Let's see that in the next video, and then after that, you'll be able to use this to build a better optimization algorithm than the straightforward gradient descent. [18634:34:40] 0: Learn how to implement exponentially weighted averages. There's one technical detail called bias correction with the nuclear computation of these averages more accurate. Let's see how that works. In the previous video, you saw this figure for beta equals 0.9 this figure for beta equals 0.98 but it turns out that if you implement the formula as written here, you won't actually get the green curve. When say beta equals 0.98 you actually get the purple curve here, and you notice that the purple curve starts off really low. So let's see how to fix that. When you're implementing a moving average, you initialize it with v zero equals zero, and then v1 is equal to 0.9 8v. Zero plus 0.02 theta, one. But v zero is equal to zero, so that term just goes away. So v1 is just 0.02 times theta one. So that's why, if the first day's temperature is, say 40 degrees Fahrenheit, then v1 will be 0.02 times 40, which is 0.8 so you get a much lower value down here. So it's not a very good estimate of the first day's temperature. V2 will be 0.98 times v1 plus 0.02 times theta two. And if you plug in v1 which is this down here, and multiply it out, then you find that b2 is actually equal to 0.98 times 0.02 times theta one plus 0.02 times theta two, and that 0.0196 [19082:45:20] 0: theta one plus 0.02 [19098:13:19] 0: theta two. So again, you know assuming theta one and theta two are positive numbers. When you compute this, v2 will be much less than theta one or theta two. So v2 isn't a very good estimate of the first two days temperature of the year. So it turns out that there's a way to modify this estimate that makes it much better, that makes it more accurate, especially during this initial phase of your estimates, which is that instead of taking VT, take VT divided by one minus beta to the power of t, where t is the current day that you're on. So let's take a concrete example. When t is equal to two one minus beta, the power of t is one minus 0.98 squared. And it turns out that this is 0.0396 and so your estimate of the temperature on day two becomes v2 divided by 0.0396 [19373:41:20] 0: and this is going to be 0.0196 [19388:10:39] 0: times. Theta one plus 0.02 theta two. You notice that these two things adds up to the normal is 0.0396 and so this becomes a weighted average of theta one and theta two, and this removes this bias. So you notice that as t becomes large, beta to the T will become, will approach zero, which is why, when t is large enough, the bias correction makes almost no difference. This is why, when t is large, the purple line and the green line, you know, pretty much overlap. But during this initial phase of learning, when you're still warming up, your estimates when bias correction can help you to obtain a better estimate of the temperature, and there's this bias correction that helps you go from the purple line to the green line. So in machine learning, for most implementations of the exponentially weighted average, people don't often bother to implement bias corrections, because most people would rather just wait that initial period and a slightly more bias estimate and go from there. But if you are concerned about the bias during this initial phase, while your exponentially weighted moving average is still warming up, then bias correction to help you get a better estimate early on, so that you now know how to implement exponentially weighted moving averages, let's go on and use this to build some better optimization algorithms. [19780:48:00] 0: There's another there's an algorithm called Momentum, or gradient descent, with momentum that almost always works faster than the standard gradient descent algorithm in one sentence. The basic idea is to compute an exponentially weighted average of your gradients, and then we use that gradient to update your weights instead in this video, let's unpack that one sentence description and see how you can actually implement this as the most of an example, let's say that you're trying to optimize a cost function which has contours like this. So the red dot denotes the position of the minimum. Maybe you start gradient descent here. And if you take one iteration of gradient descent, either batch or mini batch in descent, maybe you end up heading there, but now you're on the other side of this ellipse. You kind of you take another step of gradient descent where you end up doing that [20041:33:20] 0: sort of take a lot of steps, right, just slowly oscillate.