# Network in Network (NiN)

**URL:** <https://d2l.discourse.group/t/network-in-network-nin/80>\
**Category:** pytorch\
**Created:** [May 29, 2020, 10:44pm UTC](https://d2l.discourse.group/t/network-in-network-nin/80 "2020-05-29T22:44:44Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![mli](https://yyz2.discourse-cdn.com/free1/user_avatar/d2l.discourse.group/mli/32/6_2.png) [@mli](https://d2l.discourse.group/u/mli)\
**Post date:** [May 29, 2020, 10:44pm UTC](https://d2l.discourse.group/t/network-in-network-nin/80/1 "2020-05-29T22:44:44Z")

</div>

[http://d2l.ai/chapter\_convolutional-modern/nin.html](http://d2l.ai/chapter_convolutional-modern/nin.html)

---

<div class="post-metadata">

**Author:** ![Nish](https://avatars.discourse-cdn.com/v4/letter/n/9dc877/32.png) [@Nish](https://d2l.discourse.group/u/Nish)\
**Post date:** [August 25, 2020, 5:59pm UTC](https://d2l.discourse.group/t/network-in-network-nin/80/2 "2020-08-25T17:59:12Z")

</div>

Am I correct in thinking that there is some implicit rounding down of dimensions going on when the integer division by the stride would leave a remainder?

The base formula for the width of the output representation of a convolution layer is given by `(width_of_input - filter_size + 2*padding) / stride + 1`

Taking the first NiN block as an example, the input is a 224x224 large image. This layer has a 11x11 kernel with stride of 4, and padding explicitly set to 0 (the 1x1 conv layers shouldn’t change the dimension so I ignore them here). The above formula gives `(224 - 11)/4 + 1` as the output dimension, which would give 54.25 - I see the output shape from the dimensions test in the textbook chapter is `54`.

The same thing happens for many other layers in the network - an imperfect division by a stride. When this happens, does PyTorch implicitly crop some columns from the left/right/top/bottom at random to make it round down?

Unrelatedly, here are my calculations for the resource usage of the NiN in the textbook chapter:

```auto
INPUT: 224 x 224 x 1	ACTIVS: 224 * 224 PARAMS: 0
CONV(1,96,11,4,0) ACTIVS: 96 * 54 * 54	PARAMS: (11*11*1)*96
CONV(96,96,1,1,0) ACTIVS: 96 * 54 * 54	PARAMS: (1*1*96)*96
CONV(96,96,1,1,0) ACTIVS: 96 * 54 * 54	PARAMS: (1*1*96)*96
MaxPool(3,2) ACTIVS: 96 * 26 * 26 PARAMS: 0
NiNBlock(96,256,5,1,2)	ACTIVS: 3*(256 * 26 * 26) PARAMS: 256 * (256+256+(5*5*96)) 																					
MaxPool(3,2) ACTIVS: 256 * 12 * 12	PARAMS: 0
NiNBlock(256,384,3,1,1)	ACTIVS: 3*(384 * 12 * 12)	PARAMS: 384 * (384+384+(3*3*256))																														
MaxPool(3,2) ACTIVS:	384 * 5 * 5 PARAMS: 0
Dropout ACTIVS: 384 * 5 * 5 PARAMS: 0
NiNBlock(384,10,3,1,1)	ACTIVS: 3*(10 * 5 * 5) PARAMS: 10 * (10+10+(3*3*384))																														
AdaptiveMaxPool ACTIVS: 10 PARAMS: 0
Flatten ACTIVS: 10 PARAMS: 0

```

When training: we need 2 \* the ACTIVS sum (values + gradients), and 3 \* the PARAMS sum (values, gradients, and a cache for momentum/Adam)

When testing: we just need the sum of activs + params. If we’re being clever we can erase previous ACTIVS as we go, so we only need the sum of the largest two consecutive ACTIVS. We also don’t need the Dropout ACTIVS.

---

<div class="post-metadata">

**Author:** ![goldpiggy](https://yyz2.discourse-cdn.com/free1/user_avatar/d2l.discourse.group/goldpiggy/32/10_2.png) [@goldpiggy](https://d2l.discourse.group/u/goldpiggy)\
**Post date:** [August 27, 2020, 5:41pm UTC](https://d2l.discourse.group/t/network-in-network-nin/80/3 "2020-08-27T17:41:03Z")

</div>

> [@Nish](#):
>
> When this happens, does PyTorch implicitly crop some columns from the left/right/top/bottom at random to make it round down?

Hey @Nish, great question. I am not exactly sure which edge does PyTorch choose to round down. However, if you use some level of padding, it won’t lose any information since you just remove that edge’s padding away.

---

<div class="post-metadata">

**Author:** ![sushmit86](https://yyz2.discourse-cdn.com/free1/user_avatar/d2l.discourse.group/sushmit86/32/804_2.png) [@sushmit86](https://d2l.discourse.group/u/sushmit86)\
**Post date:** [May 28, 2021, 7:06pm UTC](https://d2l.discourse.group/t/network-in-network-nin/80/4 "2021-05-28T19:06:45Z")

</div>

Any idea on q2 why are there two 1x1 layers ?

---

<div class="post-metadata">

**Author:** ![sushmit86](https://yyz2.discourse-cdn.com/free1/user_avatar/d2l.discourse.group/sushmit86/32/804_2.png) [@sushmit86](https://d2l.discourse.group/u/sushmit86)\
**Post date:** [June 11, 2021, 11:42pm UTC](https://d2l.discourse.group/t/network-in-network-nin/80/5 "2021-06-11T23:42:01Z")

</div>

Has anybody used pytorch\_lightning ?

---

<div class="post-metadata">

**Author:** ![DXD](https://yyz2.discourse-cdn.com/free1/user_avatar/d2l.discourse.group/dxd/32/2339_2.png) [@DXD](https://d2l.discourse.group/u/DXD)\
**Post date:** [August 24, 2021, 4:33pm UTC](https://d2l.discourse.group/t/network-in-network-nin/80/6 "2021-08-24T16:33:04Z")

</div>

For exercise 3 part 1, considering only a single call of `nin_block`, would it be correct to say that for `k=kernel_size, c=out_channels`, that there are `c(2+k**2)` floating point values, being the parameters of the single block? This comes from there being `k**2` weights for each kernel with `c` kernels in a filter resulting in `ck**2`, and from there being two fully connected layers, each with `c` singular kernels.

Also, what does “amount of computation” mean?

---

<div class="post-metadata">

**Author:** ![fanbyprinciple](https://yyz2.discourse-cdn.com/free1/user_avatar/d2l.discourse.group/fanbyprinciple/32/2137_2.png) [@fanbyprinciple](https://d2l.discourse.group/u/fanbyprinciple)\
**Post date:** [September 4, 2021, 6:43am UTC](https://d2l.discourse.group/t/network-in-network-nin/80/7 "2021-09-04T06:43:52Z")

</div>

### Exercises

1. Tune the hyperparameters to improve the classification accuracy.

sample\_net = nn.Sequential(nn.Conv2d(1, 32, kernel\_size=3,padding=1),  
nn.ReLU(),  
nn.Conv2d(32, 64, kernel\_size=3, padding=1),  
nn.ReLU(),  
nn.MaxPool2d(kernel\_size=2),  
nn.Dropout(p=0.25),  
nn.Flatten(),  
nn.Linear(64 \* 14 \* 14, 128),nn.ReLU(), nn.Dropout(p=0.5),  
nn.Linear(128, 10)  
)  
```

gives me an accuracy of 83.

```
So I am thinking its a learning rate issue. But how do I find an optimium lr.

```

1. Why are there two 1 × 1 convolutional layers in the NiN block? Remove one of them, and then observe and analyze the experimental phenomena.

2. Calculate the resource usage for NiN.

3. What are possible problems with reducing the 384 × 5 × 5 representation to a 10 × 5 × 5 representation in one step.

---

<div class="post-metadata">

**Author:** ![Joey](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@Joey](https://d2l.discourse.group/u/Joey)\
**Post date:** [December 4, 2021, 12:22pm UTC](https://d2l.discourse.group/t/network-in-network-nin/80/8 "2021-12-04T12:22:21Z")

</div>

![image](https://global.discourse-cdn.com/free1/uploads/d2l/original/2X/7/72f7ffb2cb91f36ad34dc5018f707816fc6bb458.png)  
 ![image](https://global.discourse-cdn.com/free1/uploads/d2l/original/2X/2/277934c2736d79853ce80a45ae137f24922fdc3e.png)  
I used 1 1x1 conv layer and the test acc is improved. I don’t known why.

---

<div class="post-metadata">

**Author:** ![guangye](https://avatars.discourse-cdn.com/v4/letter/g/f05b48/32.png) [@guangye](https://d2l.discourse.group/u/guangye)\
**Post date:** [January 30, 2022, 7:59am UTC](https://d2l.discourse.group/t/network-in-network-nin/80/9 "2022-01-30T07:59:42Z")

</div>

As for Exe-2, intuitively with only one 1_1 conv layer, we get reduced representative capability, such as c = a^b. However I experimented with only one conv1_1 layer in each NiN block.

However I got even better result: loss 0.364, train acc 0.868, test acc 0.858. Is it as expected?

---

<div class="post-metadata">

**Author:** ![thainq](https://yyz2.discourse-cdn.com/free1/user_avatar/d2l.discourse.group/thainq/32/3536_2.png) [@thainq](https://d2l.discourse.group/u/thainq)\
**Post date:** [February 12, 2022, 1:05pm UTC](https://d2l.discourse.group/t/network-in-network-nin/80/10 "2022-02-12T13:05:12Z")

</div>

Anyone know why does the author use 1x1 conv layer instead of any difference conv layer ? what the actually purpose of it?

---

<div class="post-metadata">

**Author:** ![sanjaradylov](https://yyz2.discourse-cdn.com/free1/user_avatar/d2l.discourse.group/sanjaradylov/32/1419_2.png) [@sanjaradylov](https://d2l.discourse.group/u/sanjaradylov)\
**Post date:** [February 12, 2022, 4:05pm UTC](https://d2l.discourse.group/t/network-in-network-nin/80/11 "2022-02-12T16:05:44Z")

</div>

@thainq NiN replaces fully-connected output layers with 1x1 convolutions to reduce the number of parameters and hence prevent overfitting. The inspiration was probably the same as when fully-connected image-representation layers were first replaced by convolutions in models like LeNet (or Neocognitron 🤔?). As a result, we have an equivalent or even better model with fewer parameters.

---

<div class="post-metadata">

**Author:** ![pi-tau](https://yyz2.discourse-cdn.com/free1/user_avatar/d2l.discourse.group/pi-tau/32/8663_2.png) [@pi-tau](https://d2l.discourse.group/u/pi-tau)\
**Post date:** [June 14, 2023, 8:58am UTC](https://d2l.discourse.group/t/network-in-network-nin/80/12 "2023-06-14T08:58:26Z")

</div>

Q: Why are there two `1x1` convolutional layers per NiN block? Increase their number to three. Reduce their number to one. What changes?

Here is my take on this question:  
The `3x3` convolution acts as the first layer of the “micro” network, so with two `1x1` convolutions we get a three layer fully-connected micro network. As far as I remember reading somewhere, fc nets with more than three hidden layers get really hard to train and do not provide much improvement, hence the need for other architectures. So probably using two `1x1` convolutions is somewhat the optimal limit.  
Reducing to number of `1x1` convolutions to one should in theory reduce the expressiveness of the micro network, but we still have a two layer net which might be quite enough.

I think that using the pattern `1x1` + `3x3` + `1x1` for a “bottleneck residual block” (see Fig.5 from the ResNet paper) is quite common these days. The first `1x1` convolution reduces the number of channels for the expensive `3x3` and the second `1x1` has a dual purpose:

1. it up-scales the channels
2. we get a micro two layer fc net

For example see the PixelRNN paper ([https://arxiv.org/pdf/1601.06759.pdf](https://arxiv.org/pdf/1601.06759.pdf)), Figure 5.

Would love to hear other opinions!

---

<div class="post-metadata">

**Author:** ![pandalabme](https://yyz2.discourse-cdn.com/free1/user_avatar/d2l.discourse.group/pandalabme/32/9473_2.png) [@pandalabme](https://d2l.discourse.group/u/pandalabme)\
**Post date:** [August 27, 2023, 12:29pm UTC](https://d2l.discourse.group/t/network-in-network-nin/80/13 "2023-08-27T12:29:27Z")

</div>

My solutions to the exs: [8.3](https://pandalab.me/archives/nin)

---

<div class="post-metadata">

**Author:** ![Mohamed\_Ahmed\_Naji](https://yyz2.discourse-cdn.com/free1/user_avatar/d2l.discourse.group/mohamed_ahmed_naji/32/12690_2.png) [@Mohamed\_Ahmed\_Naji](https://d2l.discourse.group/u/Mohamed_Ahmed_Naji)\
**Post date:** [June 8, 2024, 5:44am UTC](https://d2l.discourse.group/t/network-in-network-nin/80/14 "2024-06-08T05:44:27Z")

</div>

1. Why are there two 1×1 convolutional layers per NiN block? Increase their number to three. Reduce their number to one. What changes?

- if it 1, reduces learning
- if it 3, makes learning unstable  
 ![image](https://global.discourse-cdn.com/free1/uploads/d2l/original/2X/e/e8a081d5c47342943840e26513e39f3c252b6d14.png)

---

<div class="post-metadata">

**Author:** ![AayushSameerShah](https://yyz2.discourse-cdn.com/free1/user_avatar/d2l.discourse.group/aayushsameershah/32/14503_2.png) [@AayushSameerShah](https://d2l.discourse.group/u/AayushSameerShah)\
**Post date:** [December 25, 2024, 9:42am UTC](https://d2l.discourse.group/t/network-in-network-nin/80/15 "2024-12-25T09:42:15Z")

</div>

LoL I have run the same code block… top to bottom… the accuracy turns out to be 41%.  
The learning rate could be the culprit here… it is relatively smaller than the other versions.

 ![image](https://global.discourse-cdn.com/free1/uploads/d2l/original/2X/0/0558f227bd8cb64745a9b6b35540b602fc77b28b.png)
