InstanceNorm

class paddle.fluid.dygraph.InstanceNorm(num_channels, epsilon=1e-05, param_attr=None, bias_attr=None, dtype='float32')[source]

This interface is used to construct a callable object of the InstanceNorm class. For more details, refer to code examples.

Can be used as a normalizer function for convolution or fully_connected operations. The required data format for this layer is one of the following:

DataLayout: NCHW [batch, in_channels, in_height, in_width]

Refer to Instance Normalization: The Missing Ingredient for Fast Stylization for more details.

\(input\) is the input features over a mini-batch.

\[\begin{split}\mu_{\beta} &\gets \frac{1}{HW} \sum_{i=1}^{HW} x_i \qquad &//\ \ mean\ of\ one\ feature\ map\ in\ mini-batch \\ \sigma_{\beta}^{2} &\gets \frac{1}{HW} \sum_{i=1}^{HW}(x_i - \ \mu_{\beta})^2 \qquad &//\ variance\ of\ one\ feature\ map\ in\ mini-batch \\ \hat{x_i} &\gets \frac{x_i - \mu_\beta} {\sqrt{\ \sigma_{\beta}^{2} + \epsilon}} \qquad &//\ normalize \\ y_i &\gets \gamma \hat{x_i} + \beta \qquad &//\ scale\ and\ shift\end{split}\]

Note

H means height of feature map, W means width of feature map.

Parameters
  • num_channels (int) – Indicate the number of channels of the input Tensor.

  • epsilon (float, optional) – A value added to the denominator for numerical stability. Default is 1e-5.

  • param_attr (ParamAttr, optional) – The parameter attribute for Parameter scale of instance_norm. If it is set to None or one attribute of ParamAttr, instance_norm will create ParamAttr as param_attr, the name of scale can be set in ParamAttr. If the Initializer of the param_attr is not set, the parameter is initialized one. Default: None.

  • bias_attr (ParamAttr, optional) – The parameter attribute for the bias of instance_norm. If it is set to None or one attribute of ParamAttr, instance_norm will create ParamAttr as bias_attr, the name of bias can be set in ParamAttr. If the Initializer of the bias_attr is not set, the bias is initialized zero. Default: None.

  • dtype (str, optional) – Indicate the data type of the input Tensor, which can be float32 or float64. Default: float32.

Returns

None.

Examples

import paddle.fluid as fluid
from paddle.fluid.dygraph.base import to_variable
import numpy as np
import paddle

# x's shape is [1, 3, 1, 2]
x = np.array([[[[1.0, 8.0]], [[10.0, 5.0]], [[4.0, 6.0]]]]).astype('float32')
with fluid.dygraph.guard():
    x = to_variable(x)
    instanceNorm = paddle.nn.InstanceNorm(3)
    ret = instanceNorm(x)
    # ret's shape is [1, 3, 1, 2]; value is [-1 1 0.999999 -0.999999 -0.999995 0.999995]
    print(ret)
forward(input)

Defines the computation performed at every call. Should be overridden by all subclasses.

Parameters
  • *inputs (tuple) – unpacked tuple arguments

  • **kwargs (dict) – unpacked dict arguments