1. Home
  2. Explore
  3. Computer Science
  4. Neural Networks Overview

Study methods

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

Neural Networks Overview

Transcript

Okay it's that time, so let's go ahead and  get started on what we will be calling lecture   eight. So you'll have noticed that there was  no lecture seven this year, we had the holiday,   so our last lecture was lecture six but the  naming convention that we were using is by week   rather than by how many lectures we've had before,  so this is lecture eight. So we hope as usual   that you will participate in things like the  Discourse and so just look for that “Lecture 8” category for this one. Okay, so last time that  we were together for a lecture, we talked about   neural networks: we defined neural networks, we  talked about all kinds of different things like   how to represent them both from a mathematical  standpoint like notation but also in this function   graph that we had talked about. And in particular,  we really focused on two layer neural networks. So

in particular, one layer was creating a new set  of features and that was a new idea for us last   lecture, this idea of learning a set of features  as part of the learning that we were doing rather   than just having them be something that we specify  in advance, that we come up with in advance.   So that was a new idea and when we were doing  that, we were having our layer that was learning   those features be fully connected, so every output  was connected to every input by a set of weights and then finally we had a fully connected layer  that did the classification or did the regression.   In some sense, that wasn't new, that layer, like  you could think of the neural networks that we   talked about last time with two layers. If you  got rid of that hidden layer, you're back to   logistic regression or linear regression or one  of the things we've seen before. So in some sense,   at this point, we've talked about one layer  neural networks, we've talked about two-layer

neural networks, and today we're going  to talk about deep neural networks which   basically means more than two layers and,  in particular, more than one hidden layer.   So last time, we had this output layer that  did the classification, put the features we've   learned together into either a classification or  a regression, and then we had the hidden layer   that was constructing the features and so that's  what we're going to be growing: we're going to be growing the number of hidden layers. So  we'll talk about that today. In particular,   there are lots of ways that you could do that, but  we're going to talk about one particular way and   hopefully in a moment you'll see why. It's been  a very popular way to have a deep neural network.   This is convolutional neural networks, it  might be called CNN, might be called ConvNets,   there are lots of different words but we're  going to talk about that as another hypothesis

class for things like classification, things like  regression. Along the way, we're going to describe   the layers that really define convolutional  networks, so things like filters and max pooling   which we'll have seen from the reading and time  permitting we'll talk about how that learning is   a little bit different than what you had  seen in fully connected neural networks,   although we'll still be doing these ideas of  gradient descent, stochastic gradient descent and then the way that you specifically apply  those in neural networks like back propagation.   So it's not so different, it's just that the  back propagation looks a little bit different.   Okay, so let's go ahead and start talking about  this, but let's also start talking about why   are we talking about convolutional neural networks  as a particular example of deep learning and the   reality is that there's been this big change,  this big excitement, around machine learning,

in general, and deep neural nets, in particular,  for the past few years and a lot of that, in some   ways, really kicked off with convolutional neural  nets, although I'll add a little nuance to that   statement in a moment. Okay, so there was this  ImageNet large scale visual recognition challenge.   Basically the idea is that there were tons of  images that have been labeled with things like   Amazon Mechanical Turk, they're in this particular  challenge. They restricted to about a thousand categories, these are like the classes we've been  talking about in classification. The exact setup   is a little bit more complex than what I've been  describing. If you're really interested in this,   I strongly recommend you check out the Wikipedia  article is pretty cool. Also, there's this great

retrospective by Olga Russakovsky and a number  of other people with Fei-Fei Li and I've cited   that at the bottom of this slide as well, so those  are pretty cool to check out, but anyway there's   this challenge with a thousand categories—so  that's a lot more classes than we've typically   been dealing with—and they're all these different  image categories, like “is there a strawberry,”   “is there a cow,” “what's going on in this image?”  And the idea was to label the images correctly. That was the challenge given to the various  teams and so if you look at how, in this case,   four teams performed in 2011 (so 2010 was the  first year of the challenge), you can see they're   all getting over 20% of the images wrong.  So that's the way you should be reading this   vertical axis: it goes from 0 to 1, and 1 would  mean you're basically getting everything wrong.

And so in 2010 and 2011, the winners of this  challenge were using things that we haven't really   talked about so much in this class but were really  big at the time called support vector machines and then something really different happened in  2012 where this one particular submission was,   in fact, a deep convolutional neural network and  it was called AlexNet. This was a convolutional   neural network around eight layers. We'll see  what that means shortly, it's also, I have to say,   about eight layers because the way people count  layers can be different from one thing to another,   but you get a sense of how many there were:  there more than two but not yet a hundred.

And it just really outperformed everything  else, so much so that by the next year,   basically everybody, certainly most of the  entries, were convolutional neural networks   and, in fact, in 2015, the ImageNet winner was a  much deeper convolutional neural network having   over 100 layers by Microsoft, and so this  seems like it was really changing things.   I think this paper has gotten like 60,000  citations or something, it's probably even more now. I feel like these types of papers you  blink and it's like a thousand more citations.

But it's also worth noting that, like many things  in science, it really wasn't as much of a phase   change as it looks like from the outside. A lot  of these tools had been building up for years. So while there's this very recent AI boom, which  was very much set off by AlexNet and related work, in the 60s and 80s and today, neural networks  were around and they were very popular.   And so the idea of a neural network didn't start  existing and, in fact, the idea of a convolutional   neural network didn't start existing. In  1980, there was something like a ConvNet,   this neocognitron. In 1989, Yann LeCun et al  were looking at ConvNets for classifying images,   they were recognizing digits for post  office use, they were using back propagation

and so you can ask yourself: what changed from the  80s and 90s? What really set this off? And I think   there are quite a few different things. So one  is there's just better computation, in general,   and that's been the story of a lot of the past few  decades. GPUs really made a huge difference, they   were perfectly aligned with the computations that  people were doing in convolutional neural nets, they really massively enhanced parallelization.  And then also, there was just a lot more labeled   data, so there's things like Amazon Mechanical  Turk, there's lots of data on the Internet,   for better or for worse. So this is an area of  controversy sometimes that there's so many images   that people share and they aren't necessarily  realizing that they're sharing it publicly and   those are getting used and swept up or maybe even  if they're not sharing them publicly but there's certainly a lot more labeled images now and that  can really lend itself to this kind of thing.

Okay so this is just a little teaser for “why  convolutional neural nets?” They're really a   big part of this AI boom that we've been seeing  in the past few years and a really key component   of that story. But of course, we also care about  them because they can do useful things. So there   are a lot of great potential uses of image  classification: the ability to detect tumors   from medical scans, certainly that's something we  would love to do, we'd love to detect different types of tumors there. I mean, in general,  there are so many things that we'd love to do   with medical imaging and this seems to have a  lot of possibilities here. Also, of course, image   searches online, we really engage with images. And  autonomous driving: we want to be able to detect   things like stop signs and do some pretty complex  things with images and so for all of these reasons   we'd really like the ability to work with these  kinds of data structures and certainly we haven't

looked at anything that's a very structured data  set yet in this class, we've looked at individual   data points. Okay so we're gonna be talking a  lot about images today and so let's just recall   that images are made of pixels. So one of the  big questions we'll be starting off with is:   how do I encode this image in a way that now I  can do learning on it in the way that we're used   to in this class? We have to have a data point  that's like an x_1, x_2, x_3, x_4 and we have to put it in and put it into our classifier and so  this is starting to get us to the point where   we see “oh, where are those x's going to be coming  from?” They're going to be coming from the pixels.

Okay, so in particular today, we're going to  focus on particularly simple form of images:   images that are gray scale images. And in  fact, we're going to go even simpler than that:   we're going to look at black and white  images just for today. You can totally use   other types of images, other types of data  with convolutional neural nets. This will   basically be purely for illustration. So  for instance, if I had this image “hi”, it might be composed of a bunch of pixels. Each  pixel, in general, when we're looking at grayscale   images is going to take a value between 0 and P.  Again, in today's particularly simple examples,   we're going to have just 0 being black  and 1 being white and so P is very simple:   it's just 1. Even for grayscale images, you don't  have to do that in general. So in particular,   in your lab you're going to see P being a  more typical grayscale image value of 255.

Okay but here, we're gonna be focusing  on just these these 0-1 images again,   just for ease of illustration. And so if  I had this image that was the word “hi,”   I would encode it in zeros and ones in the  following way. Now again, we still want to ask:   okay, well how are we going to actually use  this image as an input for a neural network?   Because again, we think about what have we been  doing to our data to turn it into an input to some kind of machine learning model? Well again,  we've had something like x_1, x_2, x_3, x_4   and so what we can do is we can take this  image and essentially flatten it and that   can turn it into the x_1, x_2, x_3, x_4 and so  what I mean by that is exactly the following:

I just find some ordering of the pixels and I call  the first one x_1, and I call the second one x_2,   and I call the third one x_3. In some senses,  just in terms of encoding this, the ordering   isn't so important. In other senses, it will  be important and we'll get to that shortly. Okay so at this point, I have a vector of values:  I have my x_1, I have my x_2, I have my x_3,   up to my x_n, or... I always, always get this  wrong: they should be superscripts. Oh no, no,   no, they shouldn't be, they should be subscripts,  so these are in fact the dimensions of one data   point. Great okay, so these are all my features  essentially for this data point, for this image   and so now I want to feed it into a  neural net and so let's first ask: what would that look like with the neural  nets that we were looking at before?

Okay so previously, we saw these  two layer neural nets in this class and so this is the example we saw from last  week. So one thing that we've done here   is we put these parentheses around the  superscripts just to super emphasize that   they were superscripts. You'll have noticed in  the reading and the labs and everything that   eventually that just gets to be too much to  write and so we just write the superscripts   and so that's always a little tricky making sure  when it's a superscript and when it's a power but here, we're just emphasizing that. We have  these two layers and so now we want to say,   “okay, how would we feed an image into this?”  Well we totally can. I mean we have our x_1   through our number of features that we feed in  the beginning and so we already have the ability   to put this into a two-layer neural network, to  put this image in and do something with it. The   only issue is that we could do better essentially,  that we could do things to have better performance

and that's essentially what we're going to be  focusing on. So something I want to highlight here   is, remember: we had these two layers and the  first layer was constructing a set of features and then the second layer, this output layer,  was taking those features and then making some   kind of classification. And so what we're going to  be focusing on today is we're not going to change   anything about the output layer. We're still going  to take features, do some kind of classification   with it or regression, and that will be  unchanged, that will look like something like   maybe logistic regression or linear regression or  what have you. What we're going to be focusing on

is that hidden input layer or the, in general,  the build up of the feature. Okay so something to   notice about all the layers that we talked about  last week is that they're all fully connected.   So every input is connected to every output  by weight. Again, we're not going to change   anything about that in the output layer: we're  still going to have our usual logistic regression   or linear regression, but we are going  to change that in the hidden layers. So instead of having this fully connected  situation where every input is connected   to every output, we're going to think about,  essentially, is there anything more that we know   about this problem if we're looking at something  like images? And in fact there is. And in general,   a rule of thumb in machine learning is at least  to get started if you know something about the   problem, you don't want to force your method  to try to relearn it, you want to encode that, you want to add that into the method so it can  focus on learning the things that are important.

And so what do we know about an image  problem that we didn't know maybe perhaps   about a general problem? Well there's a lot  of things that are very special about images.   So one is spatial locality. Like let's say that  I'm looking at some kind of self-driving car   application and I want to detect stop signs, I  want to know is there a stop sign in this picture.   Well, in general, I expect the stop sign is  going to be in a bunch of pixels that are near each other, I don't expect that the stop sign is  going to be like up in the upper right hand corner   and also in the lower left hand corner and like  dispersed throughout the image. I think that the   way that stop signs work is they tend to be in the  same place, all their parts are in the same place.   So that's something that we have about images  that we would like to encode. Another thing we

have about images or that we know about images is  translation and variants. So I could have a stop   sign over on this part of the image, but I could  also have a stop sign in the middle of the image,   I could also have a stop sign in another part  of the image and those are all stop signs   and I would not want to have a situation  where I need to have my neural network   learn a totally new representation of a  stop sign no matter where it is in the image. I would like to say I'm going to detect  this stop sign no matter where it's located.

And so we'd like to have a layer or a set  of features that encodes these principles   and right now our fully connected layer totally  doesn't. It says that pixel 20 is fundamentally   very different from pixel 1 and the pixels in one  part of the image are totally different from the   pixels in another part of the image and learns  weights for those different parts of the images   and so we need to come up with some new  method that's going to encode these principles because we don't have them right now and that's  essentially going to be the idea of these filters   and max pooling and things like that  that go into convolutional neural nets.

Okay so let's start looking  at a concrete example of this. In particular, we're going to look at a 1d image.  So we're just looking at these two dimensional   images. I think we tend to think of our images  as being two-dimensional, but here we're going to   look at a 1d image. Now first, I want to point out  that a one-dimensional signal is not a toy signal.   There are a lot of real-life one-dimensional  signals. So an example of this is if you look at   medical time series, like measuring your heart  rate over time, even measuring something like

the number of COVID cases over time, these  are one-dimensional signals. And in fact,   people have applied very recently. Here's just one  example paper among, I'm sure, many convolutional   neural nets to one-dimensional signals. They  might not be interpreting them as images so much,   but it's still a one-dimensional signal and it's  going to look like this, it's just maybe not going   to be zeros and ones, it might be something more  general. Okay we see this in a lot of other areas too. So you can get time series, for instance,  in a lot of spatiotemporal applications. So maybe   I'm looking at something like monthly temperature  at a location. You can get it in genetics,   so the genome can be thought of as basically  one-dimensional, so you can get data like that.   Another way that I might get a signal like this,  maybe even a zero-one signal, is maybe I'm doing   some astronomy application: I point my telescope  at a particular part of the sky and I might be

interested in understanding is there a light  in that part of the sky, maybe it's a star,   maybe it's a planet, maybe I'm seeing a satellite  passing through and so I could be just basically   saying, for every maybe 10 seconds, am I seeing  light in that part of the sky and then maybe I   want to detect the satellites because they're  going to be passing so quickly and so they'll be   ephemeral lights, they'll be just going for just  a moment as opposed to a longer time. So this is just a maybe a motivating example to have in  your head for what this 1d image might represent.

Okay so now what we're going to do  is we're going to apply a filter   to this image. So what I mean by a filter,  here's an example: it's just three numbers   and we're going to take a dot product between  these three numbers and basically every set of   three numbers in the image and get an output,  and we're going to call that a convolution. Okay so for instance, here are three  numbers in my image, here's my filter.   So what I'm going to do to get my output is I'm  going to multiply the first two: 0 times -1.

I'm going to multiply the second two numbers: 0  times 1 and add that in. I'm going to multiply the   last two numbers: 1 times -1 and I'm going to add  that in. So this is just a dot product between the   vectors [0, 0, 1] and [-1, 1, -1], the filter.  Okay I can compute this dot product, the whole   thing is going to be -1, and then I put it in. Now  if you have ever heard the words convolution or   correlation in the past, you might be thinking  “hey, this really sounds like a correlation, it doesn't sound like maybe the thing that I've  heard of as a convolution in the past” and a   genuinely challenging fact of life is that there  are just overloading of terms in so many different   areas and so you should think of this as being  a convolution for the purposes of convolutional   neural networks. This is how the term is used  here, that doesn't mean that it's always going   to be used the same elsewhere. If anything, this  is one of those things that makes you really

realize you have to be careful when talking to  somebody else to see does that word mean the same   thing that you think it means. I think a great  example of this is the word normal: it means like   a million different things in a million different  areas, across physics, across statistics, across   machine learning, everything, and so convolution  is just one of those words. But here's what we're going to be meaning by convolution: essentially  taking this dot product and moving it along. Okay   so we're going to do the same thing for the next  three set of contiguous pixels here. So we're   again going to take this dot product between these  two values and we're going to put that in for the   output and just to check, could everybody respond  to me or anybody who is up for it, respond to me in the chat and just say what is the dot product  going to be for this particular set of pixels?

Awesome lots of totally perfect responses,  everybody's getting that this is going to be zero. Okay and then we just keep doing this: so  we we go to the next three set of pixels,   we do the next three set of pixels,  go to the next three set of pixels   and so on and so on until we've gone through every  set of three contiguous pixels, every unique set   of three contiguous pixels. Okay so now we've  performed convolution and a typical thing that   we'll do at this point is then, you can think  of these as being the pre-activations just like we had in our two layer neural network: we had  something where we did some kind of combination,   some linear combination of weights and our data  and we got pre-activations and then we put them   through one of these activation functions. So  let's say that in this case we'll use a ReLU.

Okay so if we do that, then we recall that  basically that's going to put an output of 0 when   our input is 0 or below and it's gonna just put  out the output again, put in the same value again,   if the input's above 0. So here, we're gonna get a  zero, here we're gonna get a zero, we're gonna do   zero here, we'll finally get a one and then  we'll get a bunch of zeros. Okay and so I think   the question that we wanna ask now and this is  a real question that again I'm asking to you and you should feel very free to respond in the chat  is: what does this filter do? We've applied this   filter but why like does it actually do anything  for us? What does it tell us about this image?

Great. I like the description of it as finding a lonely  1. It's finding an isolated 1. It's finding a 1   that is flanked by two 0s. In this particular  case, we're only looking at zero-one data and   so basically what's happening is we're doing this  convolution and we're gonna get a negative number   or a zero if we don't have a one flanked by two  zeros and we're going to get something above zero   if we do have a one flanked by two zeros. Again,  if you think of this in terms of the astronomy example, if we get a lot of ones that could  be because we're seeing a star and it's just   moving slowly because of the rotation of the earth  across our telescope, but if we get a single one,   maybe that's a satellite zooming across the  sky and we want to detect those satellites   and so we would be interested in  finding where are these isolated ones.

Okay great, now something that you'll notice  here is that if our satellite zoomed by in   the first pixel or the last pixel, we wouldn't  pick it out right now because what we've done,   is we've had to take every three pixels that  come together and so we just won't get things   at the very edge of this image, at the very ends.  Let's start in the beginning and so if we wanted   to pick that out, something that we might do is  we might add padding to both sides of the image. And so a really typical thing to do with padding  is to make it zeros. I can't emphasize enough   how much everything that we're talking about  is a choice and so what you should really do,   just like everything we've talked about  so far in this class, is you should ask   yourself what are you trying to accomplish with  any particular application and then ask yourself   what type of padding or what choices in general  are appropriate for that application. But here,

if we wanted to detect these satellites, say,  then we might say “hey, zero padding is reasonable   because we want to be able to detect something  that could have been at the edge like a one   at the edge.” Okay so in this case, what we'll  do is, once we've added our padding, we've added   our zeros to the edge, now we have the ability  to apply the filter just a little bit further.   So now, we can apply the filter here  to the three pixels at the beginning, including the padded pixel and it's just the  same as before: we just do this dot product,   now with the padded pixel included and  in this case we'll get out of zero.

Okay and then same thing at the end: we'll just do  the dot product now with the padded pixel included   because that lets us go a little farther and we  get out of zero. And something you'll notice here,   this is a very typical thing to try to accomplish  with padding, is that usually we're trying to   get the image after convolution to be the  same size as the image before convolution.   So you can say, in some sense, the filter is  telling you what's happening in each pixel and so now you'll have an answer  for each pixel essentially. And so once we've done that, we're then going  to put these final pixels again through a ReLU, and in this case we'll just  get some zeros out at the end.

Okay. Now so far with this filter, we've just  applied it as is, but we can apply it with a bias   and so what we mean by that, is we take the dot  product that we were taking before and we just add   the bias term at the end. This is essentially  what we've been doing with biases all along:   we've been having, for instance in logistic  regression, you had essentially a weight vector   which was theta and then you added this theta  naught at the end. This is exactly like that: you have this weight vector which is described by the  filter and then we add this bias at the end and so   we're doing the same thing here. The only thing  that's different is that we're moving this along   within our data instead of just applying it to the  whole data which is what we would have done with   weight vectors before in the class. So here, for  instance, we would do this dot product. So the dot

product hopefully is pretty clearly zero because  it's just zero times everything and then we add   the bias at the end, that's going to be one, and  so we would get one out from this convolution.   Okay and then we would do the same thing  going forward and we do our dot product,   in this case, we get out negative one, we  add our bias as one and we get out zero. And then we just keep doing this: that fills in  all of our numbers and finally we apply the ReLU as before. In this case, you see that we get  quite a lot more non-zero numbers once we have   this bias, so this bias really does change the  kind of output that we're getting from our filter.

Okay. Now in general, we don't have  to have a filter that's just made up   of integers, we could have absolutely any real  weights here. So in general, if we have this   filter that has size three, so its length is  three, then we would have these three weights.   Let's call them w_1, w_2, and w_3,  again they can totally be real values, the [-1, 1, -1] is nice for illustration, but we  could have 0.9, we could have -1.2, you have all   these different values for the w's and the same  things with the bias b: the b will, in general,   be will be something that is real valued as well  and we just do the same thing, we do this dot   product. Once we have our w_1, w_2, w_3 and our b,  we do this dot product with each set of 3 pixels:   we add the bias b and we get our convolution and  then we can put this through that ReLU layer.

And it does not have to be a filter of  size 3: it could be a filter of size two,   it could be a filter of size four, it  could be a totally different size filter,   it's really a choice and that is part  of the architecture, the building   of these filters and, in general,  of the convolutional neural network. Okay. So in this particular case, we  did an example of size three and we   saw what it did and, in general, it's worth asking   what are these filters doing? What are  we accomplishing with them and so on?

Now just to think for a moment, we  want to think: what happened here? We could have taken this one dimensional image  and we could have applied a fully connected   neural network layer to create our features, we  could have done everything we did last time with   the activation, with the activation function and  all of that, and so let's ask ourselves: what's   fundamentally changed here? In this particular  case, supposing we did have a filter of size 3   with a bias b, this is another real question for  the chat: how many weights will I have, including the bias term? So essentially I'm asking how  many parameters do I have when I use this filter?

Great. So in particular, the number of parameters I'm  gonna have here, as many of you are pointing out,   is four because I have the three weights  w_1, w_2, w_3, and I have the bias b. So even though I move those around in the  image like I'm going to move them to the   next pixel and so on, I'm going to use the same  weights every time I do that and so essentially   at the end of the day. we're going to build up  this model that depends on these weights but we're   going to try to then learn our parameters. So  this is what we've done a million times now: we've   built up a model that depends on some parameters  and then we try to learn those parameters and here if we had this filter, the number of  parameters we'd have to learn would be four,   because it would be the three weights that  we just keep moving along and this bias b.

Okay, now let's ask ourselves: what if  instead we had used a fully connected layer? So what would a fully connected layer  look like here? First of all, let's notice   that I have 10 inputs, those are my pixels, so  a data point consists of 10 pixels, in this case   x_1, x_2, x_3, up to x_10. In this case, this  particular data point is [0, 0, 1, 1, 1, 0, 1,   0, 0, 0], but I would have other data points and  things to do like that but the input size is 10.   And then my 10 outputs are what I get after this  convolution and ReLU and so I could have instead had a fully connected layer with 10 inputs  and 10 outputs and so now my question   for you, which I see some of you are already  answering which is awesome, is: how many weights,   including biases, do I have for a fully  connected layer with 10 inputs and 10 outputs?

Okay lots of awesome answers here: let's  walk through this for anybody who isn’t   immediately thinking of this, or even  if you are thinking of it just to check   your mental map. So if we had this fully  connected layer, we would have 10 inputs, we'd have 10 outputs and so for every output,  we would have to have a weight for every one   of the inputs and for our bias. And so that  means, for every one of our 10 outputs, we have

10 weights plus a bias, so 11 total weights.  Now we just multiply those together and we get   that we'd have 110 weights. And so even for this  super simple case, because your images generally   aren't going to be 10 pixels long, and this is  just going to be way more different if you get   to higher levels of pixels, but even for this  very simple case, we see that there's a huge   difference between the number of weights in the  convolutional layer and a fully connected layer and the fully connected layer, because everything  has to connect to everything else, you get this   huge number of weights and biases whereas with  this filter we just have a very small number   of weights and biases because we're reusing  them essentially again and again and again

and so if this is capable of encoding the things  that we want, this could potentially be a really   big savings. We don't have to learn 110  parameters. Now we just have to learn   four parameters. Another way to think about  this, again, is like in terms of that stop sign:   if I had to learn this fully connected layer  and I wanted to learn the kind of information   that was in this filter, I'd have to relearn  this filter for every single part of the image, I'd have to relearn that there could be a stop  sign in every part of the image and so here,   instead we're saying “hey I just  want to learn this one filter   over the whole image” and so that's where the  savings is coming from. Now of course, that   depends on the thing that we want to learn and  encapsulating what we're actually interested in,   but if that's true, then  this is a really big savings.

Okay so that's a 1d example, let's go on to  something that's a little bit more representative   of the types of images that we would see  in real life and that's two dimensions, at least in terms of images. Again I can't  emphasize enough: 1d signals absolutely   occur in so many parts of real life, but for  images I think we're used to two dimensions. Okay so let's look at a 2d image: this is actually  our familiar 2d image from before that says “hi” and it's just composed of a five by five set of  pixels here. And so with our five by five set of   pixels, we're now going to apply a filter and  now, because we're dealing with 2d images, our   filter will also be two dimensional. So in this  case, it happens to be a three by three filter,

and the way that we're gonna apply the filter  is very analogous to what we did before in terms   of convolution: we're gonna line it up with  the part of the image that has the same size,   a subset of that image, and then we're  going to multiply together the values   in each pixel that aligns. So in particular,  we're going to start by taking this upper corner:   we have a 1 and a -1. When we  multiply those together, we get a -1.

Now, we're going to take the next two sets  of pixels that align between the filter and   the subset of the image: we have a 0 and a -1.  We'll multiply those together and we'll get a 0. Now, we'll take the next two sets of pixels  that align: it's a 1 and a -1. We'll multiply   those together and we'll get a -1. Now we'll  go to the next two sets of pixels that align:   we have a 1 and a -1. We multiply  those together and we get a -1. Okay we get a 0 and a 1. We  multiply those together, we get a 0.

1 and -1 is a -1. We keep doing the same thing  and so for every one of these nine elements,   we're multiplying it together. Now this is  really the same operation as a dot product. One   way that you could think about this, as doing  this dot product, is that you're flattening this subset of the image and you're also  flattening the filter in the same way to   make a vector and then you're doing a  dot product between those two vectors.   But it's also perfectly legit to think of it  as just being you're multiplying the pixels   that align in the two things together, you're  adding them all up and you're getting a value,   in this case, it happens to be negative seven,  and so we'll put that down here. And eventually, we're going to fill in something that looks like  an image by doing these convolution operations.

Okay and so the next thing we would do is  we'd find another unique subset of the image, say this one, to perform this convolution. Now  what we could do is we could step through again   and say “okay, I'm multiplying a 0 and a -1  and I get a 0” and so forth, but something we   might notice about this filter is that it's -1s  all around the outside and a 1 in the middle,   and so one way you could quickly calculate  this is to say “oh, I'm going to take   negative times the number of ones in my image  that aren't in the middle, of my image subset, and then I'm going to add one if there's a one  in the middle.” So in this particular case, I'll   notice there are three ones in my image subset,  that thing that's in the blue square, that aren't   in the middle and there's one 1 in the middle,  so I can take -3 + 1 and my output will be -2.

Okay so just to check on that, let's say I  go to the next step. Can you tell me in the   chat what is going to be the convolution of  this filter with this subset of the image? Awesome looking good. Great, so I think many  of you used the shorthand and said “hey,   there's four ones that are on the outside,  not in the center of this subset of the image,   and there's no 1 in the middle  and so I'm going to get out a -4.”

Okay. And then what we would do is  we would just keep applying this: so we would say “okay, I've made this convolution  here, I can go to the next subset of the image   that looks like my filter, this next 3 by 3 subset  of the image.” I apply this filter convolution,   I get out, in this case, -5, -2, -5.  I just keep doing these calculations. Now something you'll notice here

is that, at this point, I've applied this filter  to my image and now it's much more striking than   in the 1d case that I've dramatically reduced the  size of my image by going through this convolution   step: I went from having a 5x5 image to having  a 3x3 image and, again, there are a few things   I might be interested in here. One, I might be  interested in having my output be the same as   my image. In part, though, because I want to say  something about the pixels around the outside. If I want to be able to talk about these  edge pixels, if I want to be able to ask   if my filter applies to them, I'm going to  need to do something more because right now   I can't center my filter at those edge pixels.  And so this is what padding will accomplish.   If I apply padding all around the outside, then  I can center my filter at those edge pixels,   and I can pick up something about the edge pixels.  So again, it depends what you want to do but this

would be a very typical thing to do here to be  able to say something about those edge pixels   and to be able to apply our filter there  we would want to apply this padding. Okay so once we've applied the padding, you can  go in now and there are new sets of these 3 by   3 image segments that I can apply my filter  to. So in particular, this is one of them and it's just the same operation  as before: we say how many ones are   around the outside of this little image  segment, this is subset of the image:   there's one, there's one inside, and so we're  going to get zero by adding those together.

Now we can go to the next step and we  can keep doing this and we can fill in,   now, an outer layer around what we had before. So we just step through, we do the same operations  as before and we just have a lot more that   we can do because of this padding. And you'll  notice, now, once we've applied this padding, by design, we now get back something that has  the same size as our original image. So our   original image was 5 by 5. Now that we can apply  this filter with this particular set of padding,   we can get this nice 5 by 5 image out.  Now it won't always be the case that the   right amount of padding is just a padding of  size one. That happens to be because of this   particular size of filter but it is a very  typical thing to use an amount of padding that will let you get back something that  looks like the size of the original image.

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent