# Neural Nets: CNN confirming layer/filter arithmetic [duplicate]

I was hoping someone could just confirm some intuition about how convolutions work in convolutional neural networks. I have seen all of the tutorials on applying convolutional filters on an image, but most of those tutorials focus on one channel images, like a 128 x 128 x 1 image. I wanted to clarify what happens when we apply a convolutional filter to RGB 3 channel images.

Now this is not a unique question, I think a lot of people ask this question as well. It is just that there seem to be so many answers out there, each with their own variations, that it is hard to find a consistent answer. I included a post below that seems to comport with what my own intuition, but I was hoping one of the experts on SE could help validate the layer arithmetic, to make sure my intuition was not off.

How is the depth of the input related to the depth of the output of a convolutional layer?

Consider an Alexnet network with 5 convolutional layers and 3 fully connected layers. I borrowed the network from this post. Now, say the input is 227 x 227, and the filter is specified as 11 x 11 x 96 with stride 4. That means there are 96 filters each with dimensions 11x11x3, right? So there are a total of 363 parameters per filter--excluding the bias term-- and there are 96 of these filters to learn. So the 363*96 = 34848 filter values are learned just like the weights in the fully connected layers right?

My second question deals with the next convolutional network layer. In the next layer I will have an image that is 55 x 55 x 96 image. In this case, would the filter be 5x5x96--since there are now 96 feature maps on the image? So that means that each individual filter would need to learn 5x5x96 = 2400 filter values (weights), and that across all 256 filters this would mean 614,400 filter values?

I just wanted to make sure that I was understanding exactly what is being learned at each level.

Your first point is correct. The filters are stored in 4d arrays, with dimensions of (height, width, input channels, filter number) . The order may differ. Your second point is correct too. The filtered result get stacked together so the output dimensions are (Height,width,filter numbers) the next layer's filters are of size( filter width, filter width, last layer's filter number). Your understanding of CNN is correct. If you want additional resources on CNN, you can try Andrew Ng's class on CNN in Coursera. Hope you can learn more about CNN.

• Oh thanks so much. Yeah I just wanted to make sure I was understanding the structure correctly. The equations are good, but they hide the implementation details. And the deep learning frameworks like keras will wrap the operation in these high level functions, so you can't see what is going on underneath. Don't get me wrong, I love keras. I just did not want to treat the networks like "black boxes" where I just plug stuff in and get a result without understanding how to get the answer. I took Andrew Ng's course on Coursera and it was really nice. Commented Jun 3, 2019 at 8:58
• I wish Andrew Ng's original class had more tensorflow code to see how the details were implemented. I think he has some new courses out, but have not seen those. Commented Jun 3, 2019 at 8:59
• Andrew Ng's course has an assignment that needs you to implement the forward pass of a CNN layer from scratch in numpy. Btw, it is the Convolutional Neural Network course in the Deep Learning Specialization. Commented Jun 3, 2019 at 10:44
• Clement - thanks for the info, yeah I took the course a while ago so don't remember the details of the assignment. If I remember correctly, didn't he do the assignment in Numpy for the forward pass? I think it was a really good course. I wish that the Deep Learning specialization had had a deeper set of "lab" courses in tensorflow. I believe Coursera has a new set of deep learning classes where they are trying to provide more tf practice. Commented Jun 3, 2019 at 17:21