mindspore/docs/api/api_python/nn/mindspore.nn.InstanceNorm2d...

51 lines
2.9 KiB
ReStructuredText
Raw Normal View History

mindspore.nn.InstanceNorm2d
============================
2022-02-28 14:19:59 +08:00
.. py:class:: mindspore.nn.InstanceNorm2d(num_features, eps=1e-5, momentum=0.1, affine=True, gamma_init='ones', beta_init='zeros')
对四维输入实现实例归一化Instance Normalization Layer
该层在四维输入带有额外通道维度的mini-batch二维输入上应用实例归一化详见论文 `Instance Normalization:
The Missing Ingredient for Fast Stylization <https://arxiv.org/abs/1607.08022>`_ 。
使用mini-batch数据和学习参数进行训练参数见如下公式。
.. math::
y = \frac{x - \mathrm{E}[x]}{\sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta
其中\gamma和\beta是可学习的参数向量如果 `affine` 为True则大小为 `num_features` 。通过偏置估计函数计算标准偏差。
此层使用从训练和验证模式的输入数据计算得到的实例数据。
InstanceNorm2d和BatchNorm2d非常相似但略有不同。InstanceNorm2d应用于RGB图像等通道数据的每个通道而BatchNorm2d通常应用于批处理。
.. note::
需要注意的是,更新滑动平均和滑动方差的公式为 :math:`\hat{x}_\text{new} = (1 - \text{momentum}) \times x_t + \text{momentum} \times \hat{x}` ,其中 :math:`\hat{x}` 是估计的统计量, :math:`x_t` 是新的观察值。
**参数:**
- **num_features** (int) - 输入Tensor的通道数量。
- **eps** (float) - 添加到分母中的值以确保数值稳定。默认值1e-5。
- **momentum** (float) - 动态均值和动态方差所使用的动量。默认值0.1。
- **affine** (bool) - bool类型。设置为True时可以学习gamma和beta参数。默认值True。
- **gamma_init** (Union[Tensor, str, Initializer, numbers.Number]) - gamma参数的初始化方法。str的值引用自函数 `initializer` ,包括'zeros'、'ones'等。默认值:'ones'。
- **beta_init** (Union[Tensor, str, Initializer, numbers.Number]) - beta参数的初始化方法。str的值引用自函数 `initializer` ,包括'zeros'、'ones'等。默认值:'zeros'。
**输入:**
- **x** (Tensor) - shape为 :math:`(N, C, H, W)` 的Tensor。数据类型为float16或float32。
**输出:**
Tensor归一化缩放偏移后的Tensor其shape为 :math:`(N, C, H, W)` 。类型和shape与 `x` 相同。
**异常:**
- **TypeError** - `num_features` 不是整数。
- **TypeError** - `eps` 不是float。
- **TypeError** - `momentum` 不是float。
- **TypeError** - `affine` 不是bool。
- **TypeError** - `gamma_init` / `beta_init` 的类型不相同或者初始化的元素类型不是float32。
- **ValueError** - `num_features` 小于1。
- **ValueError** - `momentum` 不在范围[0, 1]内。
- **KeyError** - `gamma_init` / `beta_init` 中的任何一个是str并且不存在继承自 `Initializer` 的同义类。