# How many parameters in total we can learn in BatchNorm1d?

**URL:** <https://discuss.pytorch.org/t/how-many-parameters-in-total-we-can-learn-in-batchnorm1d/43360>\
**Category:** vision\
**Created:** [April 23, 2019, 6:35pm UTC](https://discuss.pytorch.org/t/how-many-parameters-in-total-we-can-learn-in-batchnorm1d/43360 "2019-04-23T18:35:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Intel\_Novel](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/intel_novel/32/9466_2.png) [@Intel\_Novel](https://discuss.pytorch.org/u/Intel_Novel)\
**Post date:** [April 23, 2019, 6:35pm UTC](https://discuss.pytorch.org/t/how-many-parameters-in-total-we-can-learn-in-batchnorm1d/43360/1 "2019-04-23T18:35:00Z")

</div>

[BatchNorm1d](https://pytorch.org/docs/master/_modules/torch/nn/modules/batchnorm.html#BatchNorm1d) is not that obvious when I checked the source code.

Let’s check this:

```auto
if self.affine:
            self.weight = Parameter(torch.Tensor(num_features))
            self.bias = Parameter(torch.Tensor(num_features))
else:
            self.register_parameter('weight', None)
            self.register_parameter('bias', None)

```

First `Parameter` is the instruction that `weight` will be the learnable parameter. This means we will learn the weight.

Then `register_parameter` adds a parameter to the module. But can you tell me what is the difference?

_I just assume this has to do something with making parameters that are shared for every mini batch and that are specific for every mini batch._

There is also similar [question](https://discuss.pytorch.org/t/nn-parameters-vs-nn-module-register-parameter/18641) without a clear answer.

The general question is: How many parameters in total we can learn in BatchNorm1d.  
I think [4](https://discuss.pytorch.org/t/what-is-standard-scale-of-batchnorm1d/43230/6), but I am not sure.

I know if we set `affine=True` we will learn `weight` and `bias` parameters.

Apart from these two, I somehow think we can also learn `mean` and `std`. However, I am not sure.

Maybe the current implementation of `_BatchNorm` (which is the base class for `BatchNorm1d`) is not learning mean and std(var).

Here is the last bit of code I will share with you:

```auto
self.register_buffer('running_mean', torch.zeros(num_features))
            self.register_buffer('running_var', torch.ones(num_features))
            self.register_buffer('num_batches_tracked', torch.tensor(0, dtype=torch.long))
else:
            self.register_parameter('running_mean', None)
            self.register_parameter('running_var', None)
            self.register_parameter('num_batches_tracked', None)

```

From this code I can understand that we can track `running_mean` and `running_var` (which should be variance I guess) and number of mini batches (`num_batches_tracked`) we processed so far. The first two are tensors, and the second is a number of mini batches tracked. So at least these two are not the constants. Right?

---

<div class="post-metadata">

**Author:** ![JuanFMontesinos](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/juanfmontesinos/32/76115_2.png) [@JuanFMontesinos](https://discuss.pytorch.org/u/JuanFMontesinos)\
**Post date:** [April 24, 2019, 7:30pm UTC](https://discuss.pytorch.org/t/how-many-parameters-in-total-we-can-learn-in-batchnorm1d/43360/2 "2019-04-24T19:30:44Z")

</div>

Hi,  
Check this post,

> [@\[SOLVED\] Register\_parameter vs register\_buffer vs nn.Parameter](https://discuss.pytorch.org/t/solved-register-parameter-vs-register-buffer-vs-nn-parameter/31953):
>
> I was looking at the code of batchnorm def \_\_init\_\_(self, num\_features, eps=1e-5, momentum=0.1, affine=True, track\_running\_stats=True): super(\_BatchNorm, self).\_\_init\_\_() self.num\_features = num\_features self.eps = eps self.momentum = momentum self.affine = affine self.track\_running\_stats = track\_running\_stats if self.affine: self.weight = Parameter(torch.Tensor(num\_features)) self.bias = Parame…

With respect to batch normalization the best you can do is to read the paper:

> **[Batch Normalization: Accelerating Deep Network Training by Reducing Internal...](https://arxiv.org/abs/1502.03167)**
>
> Training Deep Neural Networks is complicated by the fact that the distribution of each layer's inputs changes during training, as the parameters of the previous layers change. This slows down the training by requiring lower learning rates and careful...

---

<div class="post-metadata">

**Author:** ![Intel\_Novel](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/intel_novel/32/9466_2.png) [@Intel\_Novel](https://discuss.pytorch.org/u/Intel_Novel)\
**Post date:** [April 25, 2019, 11:38am UTC](https://discuss.pytorch.org/t/how-many-parameters-in-total-we-can-learn-in-batchnorm1d/43360/3 "2019-04-25T11:38:45Z")

</div>

I am providing this for my better understanding. So far I understood number of batches tracked will increase with every batch.

```auto
 if self.training and self.track_running_stats:
            # TODO: if statement only here to tell the jit to skip emitting this when it is None
            if self.num_batches_tracked is not None:
                self.num_batches_tracked += 1
                if self.momentum is None: # use cumulative moving average
                    exponential_average_factor = 1.0 / float(self.num_batches_tracked)
                else: # use exponential moving average
                    exponential_average_factor = self.momentum

```

`weight` and `bias` are learnable parameter of choice, and they can scale the output activation range effectively else it will be std (-1,1) as we saw with the mean of 0.

What are the running mean and running var?  
They are used for the [moving average calculation](https://en.wikipedia.org/wiki/Moving_average)  
Two types of moving averages we support as you can see:

- exponential moving average
- cumulative moving average

At the very end they will smoothen the curve which is what the batch norm do.  
 ![image](https://discuss.pytorch.org/uploads/default/original/2X/9/9c18d41c9d0946b5f3b2ebb8819862ad24e9c190.png)

So the running mean and running var are calculated as average, not learned like Parameter or some parameter in the nn.Linear module using the loss function.
