r"""Applies Synchronized Batch Normalization over a 2d or 3d input that is seen as a mini-batch. .. math:: y = \frac{x - mean[x]}{ \sqrt{Var[x] + \epsilon}} * gamma + beta This module differs from the built-in PyTorch BatchNorm1d as the mean and standard-deviation are redu
| 140 | |
| 141 | |
| 142 | class SynchronizedBatchNorm1d(_SynchronizedBatchNorm): |
| 143 | r"""Applies Synchronized Batch Normalization over a 2d or 3d input that is seen as a |
| 144 | mini-batch. |
| 145 | |
| 146 | .. math:: |
| 147 | |
| 148 | y = \frac{x - mean[x]}{ \sqrt{Var[x] + \epsilon}} * gamma + beta |
| 149 | |
| 150 | This module differs from the built-in PyTorch BatchNorm1d as the mean and |
| 151 | standard-deviation are reduced across all devices during training. |
| 152 | |
| 153 | For example, when one uses `nn.DataParallel` to wrap the network during |
| 154 | training, PyTorch's implementation normalize the tensor on each device using |
| 155 | the statistics only on that device, which accelerated the computation and |
| 156 | is also easy to implement, but the statistics might be inaccurate. |
| 157 | Instead, in this synchronized version, the statistics will be computed |
| 158 | over all training samples distributed on multiple devices. |
| 159 | |
| 160 | Note that, for one-GPU or CPU-only case, this module behaves exactly same |
| 161 | as the built-in PyTorch implementation. |
| 162 | |
| 163 | The mean and standard-deviation are calculated per-dimension over |
| 164 | the mini-batches and gamma and beta are learnable parameter vectors |
| 165 | of size C (where C is the input size). |
| 166 | |
| 167 | During training, this layer keeps a running estimate of its computed mean |
| 168 | and variance. The running sum is kept with a default momentum of 0.1. |
| 169 | |
| 170 | During evaluation, this running mean/variance is used for normalization. |
| 171 | |
| 172 | Because the BatchNorm is done over the `C` dimension, computing statistics |
| 173 | on `(N, L)` slices, it's common terminology to call this Temporal BatchNorm |
| 174 | |
| 175 | Args: |
| 176 | num_features: num_features from an expected input of size |
| 177 | `batch_size x num_features [x width]` |
| 178 | eps: a value added to the denominator for numerical stability. |
| 179 | Default: 1e-5 |
| 180 | momentum: the value used for the running_mean and running_var |
| 181 | computation. Default: 0.1 |
| 182 | affine: a boolean value that when set to ``True``, gives the layer learnable |
| 183 | affine parameters. Default: ``True`` |
| 184 | |
| 185 | Shape: |
| 186 | - Input: :math:`(N, C)` or :math:`(N, C, L)` |
| 187 | - Output: :math:`(N, C)` or :math:`(N, C, L)` (same shape as input) |
| 188 | |
| 189 | Examples: |
| 190 | >>> # With Learnable Parameters |
| 191 | >>> m = SynchronizedBatchNorm1d(100) |
| 192 | >>> # Without Learnable Parameters |
| 193 | >>> m = SynchronizedBatchNorm1d(100, affine=False) |
| 194 | >>> input = torch.autograd.Variable(torch.randn(20, 100)) |
| 195 | >>> output = m(input) |
| 196 | """ |
| 197 | |
| 198 | def _check_input_dim(self, input): |
| 199 | if input.dim() != 2 and input.dim() != 3: |
no outgoing calls