r"""Applies Batch Normalization over a 4d input that is seen as a mini-batch of 3d inputs .. math:: y = \frac{x - mean[x]}{ \sqrt{Var[x] + \epsilon}} * gamma + beta This module differs from the built-in PyTorch BatchNorm2d as the mean and standard-deviation are reduced acr
| 203 | |
| 204 | |
| 205 | class SynchronizedBatchNorm2d(_SynchronizedBatchNorm): |
| 206 | r"""Applies Batch Normalization over a 4d input that is seen as a mini-batch |
| 207 | of 3d inputs |
| 208 | |
| 209 | .. math:: |
| 210 | |
| 211 | y = \frac{x - mean[x]}{ \sqrt{Var[x] + \epsilon}} * gamma + beta |
| 212 | |
| 213 | This module differs from the built-in PyTorch BatchNorm2d as the mean and |
| 214 | standard-deviation are reduced across all devices during training. |
| 215 | |
| 216 | For example, when one uses `nn.DataParallel` to wrap the network during |
| 217 | training, PyTorch's implementation normalize the tensor on each device using |
| 218 | the statistics only on that device, which accelerated the computation and |
| 219 | is also easy to implement, but the statistics might be inaccurate. |
| 220 | Instead, in this synchronized version, the statistics will be computed |
| 221 | over all training samples distributed on multiple devices. |
| 222 | |
| 223 | Note that, for one-GPU or CPU-only case, this module behaves exactly same |
| 224 | as the built-in PyTorch implementation. |
| 225 | |
| 226 | The mean and standard-deviation are calculated per-dimension over |
| 227 | the mini-batches and gamma and beta are learnable parameter vectors |
| 228 | of size C (where C is the input size). |
| 229 | |
| 230 | During training, this layer keeps a running estimate of its computed mean |
| 231 | and variance. The running sum is kept with a default momentum of 0.1. |
| 232 | |
| 233 | During evaluation, this running mean/variance is used for normalization. |
| 234 | |
| 235 | Because the BatchNorm is done over the `C` dimension, computing statistics |
| 236 | on `(N, H, W)` slices, it's common terminology to call this Spatial BatchNorm |
| 237 | |
| 238 | Args: |
| 239 | num_features: num_features from an expected input of |
| 240 | size batch_size x num_features x height x width |
| 241 | eps: a value added to the denominator for numerical stability. |
| 242 | Default: 1e-5 |
| 243 | momentum: the value used for the running_mean and running_var |
| 244 | computation. Default: 0.1 |
| 245 | affine: a boolean value that when set to ``True``, gives the layer learnable |
| 246 | affine parameters. Default: ``True`` |
| 247 | |
| 248 | Shape: |
| 249 | - Input: :math:`(N, C, H, W)` |
| 250 | - Output: :math:`(N, C, H, W)` (same shape as input) |
| 251 | |
| 252 | Examples: |
| 253 | >>> # With Learnable Parameters |
| 254 | >>> m = SynchronizedBatchNorm2d(100) |
| 255 | >>> # Without Learnable Parameters |
| 256 | >>> m = SynchronizedBatchNorm2d(100, affine=False) |
| 257 | >>> input = torch.autograd.Variable(torch.randn(20, 100, 35, 45)) |
| 258 | >>> output = m(input) |
| 259 | """ |
| 260 | |
| 261 | def _check_input_dim(self, input): |
| 262 | if input.dim() != 4: |
no outgoing calls