r"""Applies Batch Normalization over a 4D tensor. .. math:: y = \frac{x - \mathrm{E}[x]}{ \sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta The mean and standard-deviation are calculated per-dimension over the mini-batches and :math:`\gamma` and :math:`\beta` are learnable
| 264 | |
| 265 | |
| 266 | class BatchNorm2d(_BatchNorm): |
| 267 | r"""Applies Batch Normalization over a 4D tensor. |
| 268 | |
| 269 | .. math:: |
| 270 | |
| 271 | y = \frac{x - \mathrm{E}[x]}{ \sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta |
| 272 | |
| 273 | The mean and standard-deviation are calculated per-dimension over |
| 274 | the mini-batches and :math:`\gamma` and :math:`\beta` are learnable |
| 275 | parameter vectors. |
| 276 | |
| 277 | By default, during training this layer keeps running estimates of its |
| 278 | computed mean and variance, which are then used for normalization during |
| 279 | evaluation. The running estimates are kept with a default :attr:`momentum` |
| 280 | of 0.9. |
| 281 | |
| 282 | If :attr:`track_running_stats` is set to ``False``, this layer will not |
| 283 | keep running estimates, batch statistics is used during |
| 284 | evaluation time instead. |
| 285 | |
| 286 | Because the Batch Normalization is done over the `C` dimension, computing |
| 287 | statistics on `(N, H, W)` slices, it's common terminology to call this |
| 288 | Spatial Batch Normalization. |
| 289 | |
| 290 | .. note:: |
| 291 | |
| 292 | The update formula for ``running_mean`` and ``running_var`` (taking ``running_mean`` as an example) is |
| 293 | |
| 294 | .. math:: |
| 295 | |
| 296 | \textrm{running_mean} = \textrm{momentum} \times \textrm{running_mean} + (1 - \textrm{momentum}) \times \textrm{batch_mean} |
| 297 | |
| 298 | which could be defined differently in other frameworks. Most notably, ``momentum`` of 0.1 in PyTorch |
| 299 | is equivalent to ``mementum`` of 0.9 here. |
| 300 | |
| 301 | Args: |
| 302 | num_features: usually :math:`C` from an input of shape |
| 303 | :math:`(N, C, H, W)` or the highest ranked dimension of an input |
| 304 | less than 4D. |
| 305 | eps: a value added to the denominator for numerical stability. |
| 306 | Default: 1e-5 |
| 307 | momentum: the value used for the ``running_mean`` and ``running_var`` computation. |
| 308 | Default: 0.9 |
| 309 | affine: a boolean value that when set to True, this module has |
| 310 | learnable affine parameters. Default: True |
| 311 | track_running_stats: when set to True, this module tracks the |
| 312 | running mean and variance. When set to False, this module does not |
| 313 | track such statistics and always uses batch statistics in both training |
| 314 | and eval modes. Default: True |
| 315 | freeze: when set to True, this module does not update the |
| 316 | running mean and variance, and uses the running mean and variance instead of |
| 317 | the batch mean and batch variance to normalize the input. The parameter takes effect |
| 318 | only when the module is initilized with track_running_stats as True. |
| 319 | Default: False |
| 320 | |
| 321 | Shape: |
| 322 | - Input: :math:`(N, C, H, W)` |
| 323 | - Output: :math:`(N, C, H, W)` (same shape as input) |
no outgoing calls