# How to implement MLPconv of Network in Network?

**URL:** <https://discuss.pytorch.org/t/how-to-implement-mlpconv-of-network-in-network/78833>\
**Category:** vision\
**Created:** [April 28, 2020, 2:12pm UTC](https://discuss.pytorch.org/t/how-to-implement-mlpconv-of-network-in-network/78833 "2020-04-28T14:12:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![BarCodeReader](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/barcodereader/32/18717_2.png) [@BarCodeReader](https://discuss.pytorch.org/u/BarCodeReader)\
**Post date:** [April 28, 2020, 2:12pm UTC](https://discuss.pytorch.org/t/how-to-implement-mlpconv-of-network-in-network/78833/1 "2020-04-28T14:12:30Z")

</div>

I did not see any pytorch implementation about this, so I want to give a try.

from the paper, it is said that, they apply a tiny MLP layer(or few layers) after the conv kernel. So this MLP layer is acting locally instead of globally, I saw some code just using conv2d with kernel size 1 attached to conv2d with kernel size 3, which is different from the original paper.

Because the convolution in CNN, before going to ReLU, is just a elementwise-multiplication with a sum action, so the MLPconv should be something like below:

```auto
# suppose we have a conv kernel K, which is 3x3 in size, and
# it is right about to do the element-wise multiplication with the image patch P, 
# which is also 3x3 in size(think about a 3x3 area of an image with NxM size). 
# Here I use * represents element-wise multiplication.

mlp1 = nn.Linear(9,9)
mlp2 = nn.Linear(9,1)
relu = F.relu()

# mlpconv action then is equal to a convolution action with 
# each of the step doing below calculation:
relu( mlp2( relu( mlp1( (P*K).view(-1) )))) 

```

but how to do this in pytorch???

I need some ideas and your help.

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [April 29, 2020, 3:15am UTC](https://discuss.pytorch.org/t/how-to-implement-mlpconv-of-network-in-network/78833/2 "2020-04-29T03:15:59Z")

</div>

How would the input channels be treated?  
Would the linear layer perform an operation such as:

```python
# input patch
patch = torch.randn(in_channels, height, width).view(-1)
lin = nn.Linear(in_channels * height * width, 1)
out = lin(patch)

```

If so, then this would be equal to a conv layer without overlapping windows, no?

---

<div class="post-metadata">

**Author:** ![BarCodeReader](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/barcodereader/32/18717_2.png) [@BarCodeReader](https://discuss.pytorch.org/u/BarCodeReader)\
**Post date:** [April 29, 2020, 2:50pm UTC](https://discuss.pytorch.org/t/how-to-implement-mlpconv-of-network-in-network/78833/4 "2020-04-29T14:50:22Z")

</div>

ok i implemented, with a sliding window…now they are updating…both mlp and conv2d

but it is really ugly…I still look forward your opnions

---

<div class="post-metadata">

**Author:** ![SwayStar123](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/swaystar123/32/72961_2.png) [@SwayStar123](https://discuss.pytorch.org/u/SwayStar123)\
**Post date:** [October 11, 2024, 3:08pm UTC](https://discuss.pytorch.org/t/how-to-implement-mlpconv-of-network-in-network/78833/5 "2024-10-11T15:08:42Z")

</div>

Here is a simple implementation of it

> <https://github.com/SwayStar123/MLPConv/blob/main/mlpconv.py>

---

<div class="post-metadata">

**Author:** ![qq-me](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/qq-me/32/70055_2.png) [@qq-me](https://discuss.pytorch.org/u/qq-me)\
**Post date:** [October 13, 2024, 4:10pm UTC](https://discuss.pytorch.org/t/how-to-implement-mlpconv-of-network-in-network/78833/6 "2024-10-13T16:10:49Z")

</div>

MLPConv is the same as pointwise convolution, it is is equivalent to a convolution with 1x1 kernel
