# Adaptive Normalization

**URL:** <https://discuss.pytorch.org/t/adaptive-normalization/9157>\
**Category:** Uncategorized\
**Created:** [October 27, 2017, 5:27pm UTC](https://discuss.pytorch.org/t/adaptive-normalization/9157 "2017-10-27T17:27:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bala](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/bala/32/63561_2.png) [@bala](https://discuss.pytorch.org/u/bala)\
**Post date:** [October 27, 2017, 5:27pm UTC](https://discuss.pytorch.org/t/adaptive-normalization/9157/1 "2017-10-27T17:27:48Z")

</div>

I want to implement adaptive normalization as suggested in the paper Fast Image Processing with Fully- Convolutional networks. The normalization is defined as a_x + b_BN(x) where a and b are learnable scalar parameters and BN is the 2d batch normalization operator. This normalizer needs to be invoked during training after every leaky\_relu activated 2d convolution layer. How do I go about coding this normalizer?

---

<div class="post-metadata">

**Author:** ![SimonW](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/simonw/32/1702_2.png) [@SimonW](https://discuss.pytorch.org/u/SimonW)\
**Post date:** [October 27, 2017, 6:46pm UTC](https://discuss.pytorch.org/t/adaptive-normalization/9157/2 "2017-10-27T18:46:42Z")

</div>

Something like the following should work. There might be some errors as I didn’t look up the exact API, but those should be easy to fix.

```auto
class ABN2d(nn.Module):
  def __init__ (self):
    super(). __init__ ()
    self.bn = nn.BatchNorm2d(affine = False)
    self.register_parameter('a', Variable(torch.*Tensor(1, 1, 1, 1), requires_grad = True))
    self.register_parameter('b', Variable(torch.*Tensor(1, 1, 1, 1), requires_grad = True))

  def forward(self, x):
    return a * x + b * self.bn(x)

```

---

<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:** [October 27, 2017, 7:20pm UTC](https://discuss.pytorch.org/t/adaptive-normalization/9157/3 "2017-10-27T19:20:25Z")

</div>

I was implementing it myself and compared my approach to yours.

Here is my module:

```
class AdaptiveBatchNorm2d(nn.Module):
    def __init__ (self, num_features, eps=1e-5, momentum=0.1, affine=True):
        super(AdaptiveBatchNorm2d, self). __init__ ()
        self.bn = nn.BatchNorm2d(num_features, eps, momentum, affine)
        self.a = nn.Parameter(torch.FloatTensor(1, 1, 1, 1))
        self.b = nn.Parameter(torch.FloatTensor(1, 1, 1, 1))

    def forward(self, x):
        return self.a * x + self.b * self.bn(x)

```

Could you explain the difference between your approach registering the Variables as parameters and using `nn.Parameter`.  
I also don’t know, why you set `affine` to False.  
The original BN implementation introduced two parameters (gamma and beta)  
(Btw. is there any option to write greek letters? \beta seems not to work)

As I understand the new formulation would be:

```
a * x + b * gamma * x_norm + b * beta

```

Thus using `affine=False`, you would lose the `b * beta` term or am I missing something?

---

<div class="post-metadata">

**Author:** ![SimonW](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/simonw/32/1702_2.png) [@SimonW](https://discuss.pytorch.org/u/SimonW)\
**Post date:** [October 27, 2017, 7:34pm UTC](https://discuss.pytorch.org/t/adaptive-normalization/9157/4 "2017-10-27T19:34:42Z")

</div>

`nn.Parameter` and `register_parameter` should be equivalent. Turns out that I should use `Parameter` instead of `Variable` in above code.😛

I set affine to false because b, beta and gamma will be all trainable. So the formulation is equivalent but with less params except for gradient values.

---

<div class="post-metadata">

**Author:** ![bala](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/bala/32/63561_2.png) [@bala](https://discuss.pytorch.org/u/bala)\
**Post date:** [October 28, 2017, 5:51am UTC](https://discuss.pytorch.org/t/adaptive-normalization/9157/5 "2017-10-28T05:51:44Z")

</div>

But the pytorch doc says if affine = False, gamma and beta are not learnt during training?

---

<div class="post-metadata">

**Author:** ![SimonW](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/simonw/32/1702_2.png) [@SimonW](https://discuss.pytorch.org/u/SimonW)\
**Post date:** [October 28, 2017, 8:01pm UTC](https://discuss.pytorch.org/t/adaptive-normalization/9157/6 "2017-10-28T20:01:17Z")

</div>

If affine is false, there won’t be any gamma or beta.

I just realized that if affine = True, then it is possible to learn of an bias offset. If you also want that, you may either use affine = True at the cost of unnecessary parameter, or add another parameter. 🙂

---

<div class="post-metadata">

**Author:** ![Brando\_Miranda](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/brando_miranda/32/14355_2.png) [@Brando\_Miranda](https://discuss.pytorch.org/u/Brando_Miranda)\
**Post date:** [July 30, 2021, 7:16pm UTC](https://discuss.pytorch.org/t/adaptive-normalization/9157/7 "2021-07-30T19:16:29Z")

</div>

what exact paper are you referring to? is it this one [Adaptive Normalization: A novel data normalization approach for non-stationary time series | IEEE Conference Publication | IEEE Xplore](https://ieeexplore.ieee.org/document/5596746)

---

<div class="post-metadata">

**Author:** ![vinaykumar2491](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/vinaykumar2491/32/44652_2.png) [@vinaykumar2491](https://discuss.pytorch.org/u/vinaykumar2491)\
**Post date:** [November 26, 2021, 8:44pm UTC](https://discuss.pytorch.org/t/adaptive-normalization/9157/8 "2021-11-26T20:44:35Z")

</div>

> [@Brando\_Miranda](#):
>
> what exact paper are you referring to?

This is the paper title: “ **Fast Image Processing with Fully Convolution Networks** ” [https://arxiv.org/pdf/1709.00643.pdf](https://arxiv.org/pdf/1709.00643.pdf)
