oneflow.nn¶
Operators for neural networks¶
Copyright 2020 The OneFlow Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
-
class
oneflow.nn.
AdaptiveAvgPool1d
(output_size: Union[int, Tuple[int]])¶ Applies a 1D adaptive average pooling over an input signal composed of several input planes.
The output size is H, for any input size. The number of output features is equal to the number of input planes.
- Parameters
output_size – the target output size H
For example:
>>> import numpy as np >>> import oneflow as flow >>> import oneflow.nn as nn >>> m = nn.AdaptiveAvgPool1d(5) >>> input = flow.Tensor(np.random.randn(1, 64, 8)) >>> output = m(input) >>> output.size() oneflow.Size([1, 64, 5])
-
class
oneflow.nn.
AdaptiveAvgPool2d
(output_size)¶ Applies a 2D adaptive average pooling over an input signal composed of several input planes.
The output is of size H x W, for any input size. The number of output features is equal to the number of input planes.
- Parameters
output_size – the target output size of the image of the form H x W. Can be a tuple (H, W) or a single H for a square image H x H. H and W can be either a
int
, orNone
which means the size will be the same as that of the input.
For example:
>>> import numpy as np >>> import oneflow as flow >>> import oneflow.nn as nn >>> m = nn.AdaptiveAvgPool2d((5,7)) >>> input = flow.Tensor(np.random.randn(1, 64, 8, 9)) >>> output = m(input) >>> output.size() oneflow.Size([1, 64, 5, 7]) >>> m = nn.AdaptiveAvgPool2d(7) >>> input = flow.Tensor(np.random.randn(1, 64, 10, 9)) >>> output = m(input) >>> output.size() oneflow.Size([1, 64, 7, 7]) >>> m = nn.AdaptiveAvgPool2d((None, 7)) >>> input = flow.Tensor(np.random.randn(1, 64, 10, 9)) >>> output = m(input) >>> output.size() oneflow.Size([1, 64, 10, 7])
-
class
oneflow.nn.
AdaptiveAvgPool3d
(output_size)¶ Applies a 3D adaptive average pooling over an input signal composed of several input planes.
The output is of size D x H x W, for any input size. The number of output features is equal to the number of input planes.
- Parameters
output_size – the target output size of the form D x H x W. Can be a tuple (D, H, W) or a single number D for a cube D x D x D. D, H and W can be either a
int
, orNone
which means the size will be the same as that of the input.
For example:
>>> import numpy as np >>> import oneflow as flow >>> import oneflow.nn as nn >>> m = nn.AdaptiveAvgPool3d((5,7,9)) >>> input = flow.Tensor(np.random.randn(1, 64, 8, 9, 10)) >>> output = m(input) >>> output.size() oneflow.Size([1, 64, 5, 7, 9]) >>> m = nn.AdaptiveAvgPool3d(7) >>> input = flow.Tensor(np.random.randn(1, 64, 10, 9, 8)) >>> output = m(input) >>> output.size() oneflow.Size([1, 64, 7, 7, 7]) >>> m = nn.AdaptiveAvgPool3d((7, None, None)) >>> input = flow.Tensor(np.random.randn(1, 64, 10, 9, 8)) >>> output = m(input) >>> output.size() oneflow.Size([1, 64, 7, 9, 8])
-
class
oneflow.nn.
AvgPool1d
(kernel_size: Union[int, Tuple[int, int]], stride: Optional[Union[int, Tuple[int, int]]] = None, padding: Union[int, Tuple[int, int]] = 0, ceil_mode: bool = False, count_include_pad: bool = True)¶ Applies a 1D average pooling over an input signal composed of several input planes. In the simplest case, the output value of the layer with input size \((N, C, H, W)\), output \((N, C, H_{out}, W_{out})\) and kernel_size \(k\) can be precisely described as:
\[\begin{split}out(N_i, C_j, l) = \\frac{1}{k} \\sum_{m=0}^{k-1} input(N_i, C_j, stride[0] \\times h + m, stride*l + m)\end{split}\]If padding is non-zero, then the input is implicitly zero-padded on both sides for padding number of points. The parameters kernel_size, stride, padding can each be an int or a one-element tuple.
Note
When ceil_mode=True, sliding windows are allowed to go off-bounds if they start within the left padding or the input. Sliding windows that would start in the right padded region are ignored.
- Parameters
kernel_size – the size of the window.
strides – the stride of the window. Default value is kernel_size.
padding – implicit zero padding to be added on both sides.
ceil_mode – when True, will use ceil instead of floor to compute the output shape.
count_include_pad – when True, will include the zero-padding in the averaging calculation.
For example:
import oneflow as flow import numpy as np m = flow.nn.AvgPool1d(kernel_size=3, padding=1, stride=1) x = flow.tensor(np.random.randn(1, 4, 4)) y = m(x) y.shape oneflow.Size([1, 4, 4])
-
extra_repr
() → str¶ Set the extra representation of the module
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
-
class
oneflow.nn.
AvgPool2d
(kernel_size: Union[int, Tuple[int, int]], stride: Optional[Union[int, Tuple[int, int]]] = None, padding: Union[int, Tuple[int, int]] = 0, ceil_mode: bool = False, count_include_pad: bool = True, divisor_override: int = 0)¶ Performs the 2d-average pooling on the input.
In the simplest case, the output value of the layer with input size \((N, C, H, W)\), output \((N, C, H_{out}, W_{out})\) and kernel_size \((kH, kW)\) can be precisely described as:
\[out(N_i, C_j, h, w) = \frac{1}{kH * kW} \sum_{m=0}^{kH-1} \sum_{n=0}^{kW-1} input(N_i, C_j, stride[0] \times h + m, stride[1] \times w + n)\]- Parameters
kernel_size (Union[int, Tuple[int, int]]) – An int or list of ints that has length 1, 2. The size of the window for each dimension of the input Tensor.
strides (Union[int, Tuple[int, int]]) – An int or list of ints that has length 1, 2. The stride of the sliding window for each dimension of the input Tensor.
padding (Tuple[int, int]) – An int or list of ints that has length 1, 2. Implicit zero padding to be added on both sides.
ceil_mode (bool, default to False) – When True, will use ceil instead of floor to compute the output shape.
For example:
import oneflow as flow import numpy as np m = flow.nn.AvgPool2d(kernel_size=3, padding=1, stride=1) x = flow.tensor(np.random.randn(1, 4, 4, 4)) y = m(x) y.shape oneflow.Size([1, 4, 4, 4])
-
extra_repr
() → str¶ Set the extra representation of the module
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
-
class
oneflow.nn.
AvgPool3d
(kernel_size: Union[int, Tuple[int, int, int]], stride: Optional[Union[int, Tuple[int, int, int]]] = None, padding: Union[int, Tuple[int, int, int]] = 0, ceil_mode: bool = False, count_include_pad: bool = True, divisor_override: int = 0)¶ Applies a 3D average pooling over an input signal composed of several input planes. In the simplest case, the output value of the layer with input size \((N, C, D, H, W)\), output \((N, C, D_{out}, H_{out}, W_{out})\) and kernel_size \((kD, kH, kW)\) can be precisely described as:
\[\begin{split}out(N_i, C_j, d, h, w) = \\frac{1}{kD * kH * kW } \\sum_{k=0}^{kD-1} \\sum_{m=0}^{kH-1} \\sum_{n=0}^{kW-1} input(N_i, C_j, stride[0] \\times d + k, stride[1] \\times h + m, stride[2] \\times w + n)\end{split}\]If padding is non-zero, then the input is implicitly zero-padded on all three sides for padding number of points.
Note
When ceil_mode=True, sliding windows are allowed to go off-bounds if they start within the left padding or the input. Sliding windows that would start in the right padded region are ignored.
- Parameters
kernel_size – the size of the window.
strides – the stride of the window. Default value is kernel_size.
padding – implicit zero padding to be added on all three sides.
ceil_mode – when True, will use ceil instead of floor to compute the output shape.
count_include_pad – when True, will include the zero-padding in the averaging calculation.
divisor_override – if specified, it will be used as divisor, otherwise kernel_size will be used.
- Shape:
Input: \((N, C, D_{in}, H_{in}, W_{in})\)
Output: \((N, C, D_{out}, H_{out}, W_{out})\), where
\[\begin{split}D_{out} = \\left\\lfloor\\frac{D_{in} + 2 \\times \\text{padding}[0] - \\text{kernel_size}[0]}{\\text{stride}[0]} + 1\\right\\rfloor\end{split}\]\[\begin{split}H_{out} = \\left\\lfloor\\frac{H_{in} + 2 \\times \\text{padding}[1] - \\text{kernel_size}[1]}{\\text{stride}[1]} + 1\\right\\rfloor\end{split}\]\[\begin{split}W_{out} = \\left\\lfloor\\frac{W_{in} + 2 \\times \\text{padding}[2] - \\text{kernel_size}[2]}{\\text{stride}[2]} + 1\\right\\rfloor\end{split}\]
For example:
import oneflow as flow import numpy as np m = flow.nn.AvgPool3d(kernel_size=(2,2,2),padding=(0,0,0),stride=(1,1,1)) x = flow.tensor(np.random.randn(9, 7, 11, 32, 20)) y = m(x) y.shape oneflow.Size([9, 7, 10, 31, 19])
-
extra_repr
() → str¶ Set the extra representation of the module
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
-
class
oneflow.nn.
BCELoss
(weight: Optional[oneflow._oneflow_internal.Tensor] = None, reduction: str = 'mean')¶ This operator computes the binary cross entropy loss.
The equation is:
if reduction = “none”:
\[out = -(Target_i*log(Input_i) + (1-Target_i)*log(1-Input_i))\]if reduction = “mean”:
\[out = -\frac{1}{n}\sum_{i=1}^n(Target_i*log(Input_i) + (1-Target_i)*log(1-Input_i))\]if reduction = “sum”:
\[out = -\sum_{i=1}^n(Target_i*log(Input_i) + (1-Target_i)*log(1-Input_i))\]- Parameters
weight (oneflow.Tensor, optional) – The manual rescaling weight to the loss. Default to None, whose corresponding weight value is 1.
reduction (str, optional) – The reduce type, it can be one of “none”, “mean”, “sum”. Defaults to “mean”.
Attention
The input value must be in the range of (0, 1). Or the loss function may return nan value.
- Returns
The result Tensor.
- Return type
For example:
>>> import oneflow as flow >>> import numpy as np >>> input = flow.Tensor(np.array([[1.2, 0.2, -0.3], [0.7, 0.6, -2]]).astype(np.float32)) >>> target = flow.Tensor(np.array([[0, 1, 0], [1, 0, 1]]).astype(np.float32)) >>> weight = flow.Tensor(np.array([[2, 2, 2], [2, 2, 2]]).astype(np.float32)) >>> activation = flow.nn.Sigmoid() >>> sigmoid_input = activation(input) >>> m = flow.nn.BCELoss(weight, reduction="none") >>> out = m(sigmoid_input, target) >>> out tensor([[2.9266, 1.1963, 1.1087], [0.8064, 2.0750, 4.2539]], dtype=oneflow.float32) >>> m_sum = flow.nn.BCELoss(weight, reduction="sum") >>> out = m_sum(sigmoid_input, target) >>> out tensor(12.3668, dtype=oneflow.float32) >>> m_mean = flow.nn.BCELoss(weight, reduction="mean") >>> out = m_mean(sigmoid_input, target) >>> out tensor(2.0611, dtype=oneflow.float32) >>> m_none = flow.nn.BCELoss() >>> out = m_none(sigmoid_input, target) >>> out tensor(1.0306, dtype=oneflow.float32)
-
class
oneflow.nn.
BCEWithLogitsLoss
(weight: Optional[oneflow._oneflow_internal.Tensor] = None, reduction: str = 'mean', pos_weight: Optional[oneflow._oneflow_internal.Tensor] = None)¶ This operator combines the Sigmoid and BCELoss together. For numerical stability, we apply some math tricks instead of using Sigmoid layer with BCELoss.
The equation is:
if
reduction
="none"
:\[out = -weight*[Pos\_weight*y*log\sigma({x}) + (1-y)*log(1-\sigma(x))]\]if
reduction
="mean"
:\[out = -\frac{weight}{n}\sum_{i=1}^n[Pos\_weight*y*log\sigma({x}) + (1-y)*log(1-\sigma(x))]\]if
reduction
="sum"
:\[out = -weight*\sum_{i=1}^n[Pos\_weight*y*log\sigma({x}) + (1-y)*log(1-\sigma(x))]\]- Parameters
weight (Tensor, optional) – The manual rescaling weight to the loss. Default:
None
size_average (bool, optional) – Deprecated (see
reduction
). Default:True
reduce (bool, optional) – Deprecated (see
reduction
). Default:True
reduction (str, optional) – The reduce type, it can be one of
"none"
,"mean"
,"sum"
.'none'
: no reduction will be applied,'mean'
: the sum of the output will be divided by the number of elements in the output,'sum'
: the output will be summed. Default:"mean"
pos_weight (Tensor, optional) – The manual rescaling weight to the positive examples. Default:
None
- Shape:
Input: \((N,*)\) where * means, any number of additional dimensions
Target: \((N,*)\), same shape as the input
Output: scalar. If
reduction
is"none"
, then \((N,*)\), same shape as input.
For example:
>>> import oneflow as flow >>> input = flow.tensor([[1.2, 0.2, -0.3], [0.7, 0.6, -2], [0.7, 0.6, -2]], dtype=flow.float32) >>> target = flow.tensor([[0, 1, 0], [1, 0, 1], [1, 0, 1]], dtype=flow.float32) >>> weight = flow.tensor([[2, 2, 2], [2, 2, 2], [2, 2, 2]], dtype=flow.float32) >>> pos_weight = flow.tensor([1.2, 1.3, 1.4], dtype=flow.float32) >>> m = flow.nn.BCEWithLogitsLoss(weight=weight, pos_weight=pos_weight, reduction="none") >>> out = m(input, target) >>> out tensor([[2.9266, 1.5552, 1.1087], [0.9676, 2.0750, 5.9554], [0.9676, 2.0750, 5.9554]], dtype=oneflow.float32) >>> m = flow.nn.BCEWithLogitsLoss(weight=weight, pos_weight=pos_weight, reduction="mean") >>> out = m(input, target) >>> out tensor(2.6207, dtype=oneflow.float32) >>> m = flow.nn.BCEWithLogitsLoss(weight=weight, pos_weight=pos_weight, reduction="sum") >>> out = m(input, target) >>> out tensor(23.5865, dtype=oneflow.float32)
-
class
oneflow.nn.
BatchNorm1d
(num_features, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)¶ Applies Batch Normalization over a 2D or 3D input (a mini-batch of 1D inputs with optional additional channel dimension) as described in the paper Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift .
\[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 \(\gamma\) and \(\beta\) are learnable parameter vectors of size C (where C is the input size). By default, the elements of \(\gamma\) are set to 1 and the elements of \(\beta\) are set to 0. The standard-deviation is calculated via the biased estimator, equivalent to torch.var(input, unbiased=False).
Also by default, during training this layer keeps running estimates of its computed mean and variance, which are then used for normalization during evaluation. The running estimates are kept with a default
momentum
of 0.1.If
track_running_stats
is set toFalse
, this layer then does not keep running estimates, and batch statistics are instead used during evaluation time as well.Note
This
momentum
argument is different from one used in optimizer classes and the conventional notion of momentum. Mathematically, the update rule for running statistics here is \(\hat{x}_\text{new} = (1 - \text{momentum}) \times \hat{x} + \text{momentum} \times x_t\), where \(\hat{x}\) is the estimated statistic and \(x_t\) is the new observed value.Because the Batch Normalization is done over the C dimension, computing statistics on (N, L) slices, it’s common terminology to call this Temporal Batch Normalization.
- Parameters
num_features – \(C\) from an expected input of size \((N, C, L)\) or \(L\) from input of size \((N, L)\)
eps – a value added to the denominator for numerical stability. Default: 1e-5
momentum – the value used for the running_mean and running_var computation. Can be set to
None
for cumulative moving average (i.e. simple average). Default: 0.1affine – a boolean value that when set to
True
, this module has learnable affine parameters. Default:True
track_running_stats – a boolean value that when set to
True
, this module tracks the running mean and variance, and when set toFalse
, this module does not track such statistics, and initializes statistics buffersrunning_mean
andrunning_var
asNone
. When these buffers areNone
, this module always uses batch statistics. in both training and eval modes. Default:True
- Shape:
Input: \((N, C)\) or \((N, C, L)\)
Output: \((N, C)\) or \((N, C, L)\) (same shape as input)
For example:
>>> import oneflow as flow >>> import numpy as np >>> x = flow.Tensor(np.random.randn(20, 100)) >>> m = flow.nn.BatchNorm1d(100) >>> y = m(x)
-
class
oneflow.nn.
BatchNorm2d
(num_features, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)¶ Applies Batch Normalization over a 4D input (a mini-batch of 2D inputs with additional channel dimension) as described in the paper Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift .
\[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 \(\gamma\) and \(\beta\) are learnable parameter vectors of size C (where C is the input size). By default, the elements of \(\gamma\) are set to 1 and the elements of \(\beta\) are set to 0. The standard-deviation is calculated via the biased estimator, equivalent to torch.var(input, unbiased=False).
Also by default, during training this layer keeps running estimates of its computed mean and variance, which are then used for normalization during evaluation. The running estimates are kept with a default
momentum
of 0.1.If
track_running_stats
is set toFalse
, this layer then does not keep running estimates, and batch statistics are instead used during evaluation time as well.Note
This
momentum
argument is different from one used in optimizer classes and the conventional notion of momentum. Mathematically, the update rule for running statistics here is \(\hat{x}_\text{new} = (1 - \text{momentum}) \times \hat{x} + \text{momentum} \times x_t\), where \(\hat{x}\) is the estimated statistic and \(x_t\) is the new observed value.Because the Batch Normalization is done over the C dimension, computing statistics on (N, H, W) slices, it’s common terminology to call this Spatial Batch Normalization.
- Parameters
num_features – \(C\) from an expected input of size \((N, C, H, W)\)
eps – a value added to the denominator for numerical stability. Default: 1e-5
momentum – the value used for the running_mean and running_var computation. Can be set to
None
for cumulative moving average (i.e. simple average). Default: 0.1affine – a boolean value that when set to
True
, this module has learnable affine parameters. Default:True
track_running_stats – a boolean value that when set to
True
, this module tracks the running mean and variance, and when set toFalse
, this module does not track such statistics, and initializes statistics buffersrunning_mean
andrunning_var
asNone
. When these buffers areNone
, this module always uses batch statistics. in both training and eval modes. Default:True
- Shape:
Input: \((N, C, H, W)\)
Output: \((N, C, H, W)\) (same shape as input)
For example:
>>> import oneflow as flow >>> import numpy as np >>> x = flow.Tensor(np.random.randn(4, 2, 8, 3)) >>> m = flow.nn.BatchNorm2d(num_features=2, eps=1e-5, momentum=0.1) >>> y = m(x)
-
class
oneflow.nn.
BatchNorm3d
(num_features, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)¶ Applies Batch Normalization over a 5D input (a mini-batch of 3D inputs with additional channel dimension) as described in the paper Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift .
\[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 \(\gamma\) and \(\beta\) are learnable parameter vectors of size C (where C is the input size). By default, the elements of \(\gamma\) are set to 1 and the elements of \(\beta\) are set to 0. The standard-deviation is calculated via the biased estimator, equivalent to torch.var(input, unbiased=False).
Also by default, during training this layer keeps running estimates of its computed mean and variance, which are then used for normalization during evaluation. The running estimates are kept with a default
momentum
of 0.1.If
track_running_stats
is set toFalse
, this layer then does not keep running estimates, and batch statistics are instead used during evaluation time as well.Note
This
momentum
argument is different from one used in optimizer classes and the conventional notion of momentum. Mathematically, the update rule for running statistics here is \(\hat{x}_\text{new} = (1 - \text{momentum}) \times \hat{x} + \text{momentum} \times x_t\), where \(\hat{x}\) is the estimated statistic and \(x_t\) is the new observed value.Because the Batch Normalization is done over the C dimension, computing statistics on (N, D, H, W) slices, it’s common terminology to call this Spatial Batch Normalization.
- Parameters
num_features – \(C\) from an expected input of size \((N, C, D, H, W)\)
eps – a value added to the denominator for numerical stability. Default: 1e-5
momentum – the value used for the running_mean and running_var computation. Can be set to
None
for cumulative moving average (i.e. simple average). Default: 0.1affine – a boolean value that when set to
True
, this module has learnable affine parameters. Default:True
track_running_stats – a boolean value that when set to
True
, this module tracks the running mean and variance, and when set toFalse
, this module does not track such statistics, and initializes statistics buffersrunning_mean
andrunning_var
asNone
. When these buffers areNone
, this module always uses batch statistics. in both training and eval modes. Default:True
- Shape:
Input: \((N, C, D, H, W)\)
Output: \((N, C, D, H, W)\) (same shape as input)
For example:
>>> import oneflow as flow >>> import numpy as np >>> x = flow.Tensor(np.random.randn(3, 2, 5, 8, 4)) >>> m = flow.nn.BatchNorm3d(num_features=2, eps=1e-5, momentum=0.1) >>> y = m(x) >>> y.size() oneflow.Size([3, 2, 5, 8, 4])
-
class
oneflow.nn.
CELU
(alpha: float = 1.0, inplace: bool = False)¶ Applies the element-wise function:
\[\begin{split}\text{CELU}(x, \alpha) = \begin{cases} x & \text{ if } x \ge 0 \\ \alpha*(exp(\frac{x}{\alpha})-1) & \text{ otherwise } \\ \end{cases}\end{split}\]- Parameters
alpha – the \(\alpha\) value for the CELU formulation. Default: 1.0
inplace – can optionally do the operation in-place. Default:
False
- Shape:
Input: \((N, *)\) where * means, any number of additional dimensions
Output: \((N, *)\), same shape as the input
For example:
>>> import numpy as np >>> import oneflow as flow >>> x = np.array([-0.5, 0, 0.5]).astype(np.float32) >>> input = flow.Tensor(x) >>> celu = flow.nn.CELU(alpha=0.5) >>> out = celu(input) >>> out tensor([-0.3161, 0.0000, 0.5000], dtype=oneflow.float32)
-
extra_repr
()¶ Set the extra representation of the module
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
-
class
oneflow.nn.
COCOReader
(annotation_file: str, image_dir: str, batch_size: int, shuffle: bool = True, random_seed: Optional[int] = None, group_by_aspect_ratio: bool = True, remove_images_without_annotations: bool = True, stride_partition: bool = True, device: Optional[Union[oneflow._oneflow_internal.device, str]] = None, placement: Optional[oneflow._oneflow_internal.placement] = None, sbp: Optional[Union[oneflow._oneflow_internal.sbp.sbp, List[oneflow._oneflow_internal.sbp.sbp]]] = None)¶
-
class
oneflow.nn.
CTCLoss
(blank: int = 0, reduction: str = 'mean', zero_infinity: bool = False)¶ The Connectionist Temporal Classification loss. The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/stable/generated/torch.nn.CTCLoss.html#torch.nn.CTCLoss
Calculates loss between a continuous (unsegmented) time series and a target sequence. CTCLoss sums over the probability of possible alignments of input to target, producing a loss value which is differentiable with respect to each input node. The alignment of input to target is assumed to be “many-to-one”, which limits the length of the target sequence such that it must be \(\leq\) the input length.
- Parameters
blank (int, optional) – blank label. Default \(0\).
reduction (string, optional) – Specifies the reduction to apply to the output:
'none'
|'mean'
|'sum'
.'none'
: no reduction will be applied,'mean'
: the output losses will be divided by the target lengths and then the mean over the batch is taken. Default:'mean'
zero_infinity (bool, optional) – Whether to zero infinite losses and the associated gradients. Default:
False
Infinite losses mainly occur when the inputs are too short to be aligned to the targets.
- Shape:
Log_probs: Tensor of size \((T, N, C)\), where \(T = \text{input length}\), \(N = \text{batch size}\), and \(C = \text{number of classes (including blank)}\).
Targets: Tensor of size \((N, S)\) or \((\operatorname{sum}(\text{target_lengths}))\), where \(N = \text{batch size}\) and \(S = \text{max target length, if shape is } (N, S)\). It represent the target sequences. Each element in the target sequence is a class index. And the target index cannot be blank (default=0). In the \((N, S)\) form, targets are padded to the length of the longest sequence, and stacked. In the \((\operatorname{sum}(\text{target_lengths}))\) form, the targets are assumed to be un-padded and concatenated within 1 dimension.
Input_lengths: Tuple or tensor of size \((N)\), where \(N = \text{batch size}\). It represent the lengths of the inputs (must each be \(\leq T\)). And the lengths are specified for each sequence to achieve masking under the assumption that sequences are padded to equal lengths.
Target_lengths: Tuple or tensor of size \((N)\), where \(N = \text{batch size}\). It represent lengths of the targets. Lengths are specified for each sequence to achieve masking under the assumption that sequences are padded to equal lengths. If target shape is \((N,S)\), target_lengths are effectively the stop index \(s_n\) for each target sequence, such that
target_n = targets[n,0:s_n]
for each target in a batch. Lengths must each be \(\leq S\) If the targets are given as a 1d tensor that is the concatenation of individual targets, the target_lengths must add up to the total length of the tensor.
- Reference:
A. Graves et al.: Connectionist Temporal Classification: Labelling Unsegmented Sequence Data with Recurrent Neural Networks: https://www.cs.toronto.edu/~graves/icml_2006.pdf
For example:
>>> import oneflow as flow >>> log_probs = flow.tensor( ... [ ... [[-1.1031, -0.7998, -1.5200], [-0.9808, -1.1363, -1.1908]], ... [[-1.2258, -1.0665, -1.0153], [-1.1135, -1.2331, -0.9671]], ... [[-1.3348, -0.6611, -1.5118], [-0.9823, -1.2355, -1.0941]], ... [[-1.3850, -1.3273, -0.7247], [-0.8235, -1.4783, -1.0994]], ... [[-0.9049, -0.8867, -1.6962], [-1.4938, -1.3630, -0.6547]], ... ], dtype=flow.float32) >>> targets = flow.tensor([[1, 2, 2], [1, 2, 2]], dtype=flow.int32) >>> input_lengths = flow.tensor([5, 5], dtype=flow.int32) >>> target_lengths = flow.tensor([3, 3], dtype=flow.int32) >>> loss_mean = flow.nn.CTCLoss() >>> out = loss_mean(log_probs, targets, input_lengths, target_lengths) >>> out tensor(1.1376, dtype=oneflow.float32) >>> loss_sum = flow.nn.CTCLoss(blank=0, reduction="sum") >>> out = loss_sum(log_probs, targets, input_lengths, target_lengths) >>> out tensor(6.8257, dtype=oneflow.float32)
-
class
oneflow.nn.
CoinFlip
(batch_size: int = 1, random_seed: Optional[int] = None, probability: float = 0.5, device: Optional[Union[oneflow._oneflow_internal.device, str]] = None, placement: Optional[oneflow._oneflow_internal.placement] = None, sbp: Optional[Union[oneflow._oneflow_internal.sbp.sbp, List[oneflow._oneflow_internal.sbp.sbp]]] = None)¶
-
class
oneflow.nn.
CombinedMarginLoss
(m1: float = 1.0, m2: float = 0.0, m3: float = 0.0)¶ The operation implements “margin_softmax” in InsightFace: https://github.com/deepinsight/insightface/blob/master/recognition/arcface_mxnet/train.py The implementation of margin_softmax in InsightFace is composed of multiple operators. We fuse them for speed up.
- Parameters
x (oneflow.Tensor) – A Tensor
label (oneflow.Tensor) – label with integer data type
m1 (float) – loss m1 parameter
m2 (float) – loss m2 parameter
m3 (float) – loss m3 parameter
- Returns
A Tensor
- Return type
For example:
>>> import numpy as np >>> import oneflow as flow >>> np_x = np.array([[-0.7027179, 0.0230609], [-0.02721931, -0.16056311], [-0.4565852, -0.64471215]]) >>> np_label = np.array([0, 1, 1]) >>> x = flow.tensor(np_x, dtype=flow.float32) >>> label = flow.tensor(np_label, dtype=flow.int32) >>> loss_func = flow.nn.CombinedMarginLoss(0.3, 0.5, 0.4) >>> out = loss_func(x, label) >>> out tensor([[-0.0423, 0.0231], [-0.0272, 0.1237], [-0.4566, -0.0204]], dtype=oneflow.float32)
-
class
oneflow.nn.
ConstantPad1d
(padding: Union[int, tuple, list], value: Union[int, float] = 0)¶ Pads the input tensor boundaries with a constant value. The interface is consistent with PyTorch, and referenced from: https://pytorch.org/docs/stable/generated/torch.nn.ConstantPad1d.html?highlight=constantpad1d#torch.nn.ConstantPad1d
For N-dimensional padding, use
torch.nn.functional.pad()
.- Parameters
padding (int, list, tuple) – the size of the padding. If is int, uses the same padding in both boundaries. If a 2-tuple, uses (\(\text{padding_left}\), \(\text{padding_right}\))
value (int, float) – The constant value used for padding. Defaults to 0.
- Shape:
Input: \((N, C, W_{in})\)
Output: \((N, C, W_{out})\) where
\(W_{out} = W_{in} + \text{padding\_left} + \text{padding\_right}\)
For example:
>>> import oneflow as flow >>> import numpy as np >>> input = flow.tensor(np.arange(8).reshape(2,2,2).astype(np.float32)) >>> m = flow.nn.ConstantPad1d(padding=[1, 2], value=9.9999) >>> output = m(input) >>> output tensor([[[9.9999, 0.0000, 1.0000, 9.9999, 9.9999], [9.9999, 2.0000, 3.0000, 9.9999, 9.9999]], [[9.9999, 4.0000, 5.0000, 9.9999, 9.9999], [9.9999, 6.0000, 7.0000, 9.9999, 9.9999]]], dtype=oneflow.float32)
-
class
oneflow.nn.
ConstantPad2d
(padding: Union[int, tuple, list], value: Union[int, float] = 0)¶ The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/stable/generated/torch.nn.ConstantPad2d.html?highlight=constantpad2d#torch.nn.ConstantPad2d
This operator pads the input with constant value that user specifies. User can set the amount of padding by setting the parameter paddings.
- Parameters
padding (int, tuple, list) – the size of the padding. If is int, uses the same padding in all boundaries. If a 4-tuple, uses (\(\mathrm{padding_{left}}\), \(\mathrm{padding_{right}}\), \(\mathrm{padding_{top}}\), \(\mathrm{padding_{bottom}}\))
value (int, float) – The constant value used for padding. Defaults to 0.
- Shape:
Input: \((N, C, H_{in}, W_{in})\)
Output: \((N, C, H_{out}, W_{out})\) where
\(H_{out} = H_{in} + \mathrm{padding_{top}} + \mathrm{padding_{bottom}}\) \(W_{out} = W_{in} + \mathrm{padding_{left}} + \mathrm{padding_{right}}\)
For example:
>>> import oneflow as flow >>> import numpy as np >>> m = flow.nn.ConstantPad2d((2, 2, 1, 1), 1) >>> input = flow.tensor(np.arange(18).reshape((1, 2, 3, 3)).astype(np.float32)) >>> output = m(input) >>> output.shape oneflow.Size([1, 2, 5, 7]) >>> output tensor([[[[ 1., 1., 1., 1., 1., 1., 1.], [ 1., 1., 0., 1., 2., 1., 1.], [ 1., 1., 3., 4., 5., 1., 1.], [ 1., 1., 6., 7., 8., 1., 1.], [ 1., 1., 1., 1., 1., 1., 1.]], [[ 1., 1., 1., 1., 1., 1., 1.], [ 1., 1., 9., 10., 11., 1., 1.], [ 1., 1., 12., 13., 14., 1., 1.], [ 1., 1., 15., 16., 17., 1., 1.], [ 1., 1., 1., 1., 1., 1., 1.]]]], dtype=oneflow.float32)
-
class
oneflow.nn.
ConstantPad3d
(padding: Union[int, tuple, list], value: Union[int, float] = 0)¶ Pads the input tensor boundaries with a constant value. The interface is consistent with PyTorch, and referenced from: https://pytorch.org/docs/stable/generated/torch.nn.ConstantPad3d.html?highlight=constantpad3d#torch.nn.ConstantPad3d
For N-dimensional padding, use
flow.nn.functional.pad()
.- Parameters
padding (int, list, tuple) – the size of the padding. If is int, uses the same padding in all boundaries. If a 6-tuple, uses (\(\text{padding_left}\), \(\text{padding_right}\), \(\text{padding_top}\), \(\text{padding_bottom}\), \(\text{padding_front}\), \(\text{padding_back}\))
value (int, float) – The constant value used for padding. Defaults to 0.
- Shape:
Input: \((N, C, D_{in}, H_{in}, W_{in})\)
Output: \((N, C, D_{out}, H_{out}, W_{out})\) where
\(D_{out} = D_{in} + \text{padding_front} + \text{padding_back}\)
\(H_{out} = H_{in} + \text{padding_top} + \text{padding_bottom}\)
\(W_{out} = W_{in} + \text{padding_left} + \text{padding_right}\)
Examples:
>>> import oneflow as flow >>> import numpy as np >>> input = flow.tensor(np.arange(8).reshape(1,1,2,2,2).astype(np.int32)) >>> m = flow.nn.ConstantPad3d(padding=1, value=9) >>> output = m(input) >>> output tensor([[[[[9, 9, 9, 9], [9, 9, 9, 9], [9, 9, 9, 9], [9, 9, 9, 9]], [[9, 9, 9, 9], [9, 0, 1, 9], [9, 2, 3, 9], [9, 9, 9, 9]], [[9, 9, 9, 9], [9, 4, 5, 9], [9, 6, 7, 9], [9, 9, 9, 9]], [[9, 9, 9, 9], [9, 9, 9, 9], [9, 9, 9, 9], [9, 9, 9, 9]]]]], dtype=oneflow.int32)
-
class
oneflow.nn.
Conv1d
(in_channels: int, out_channels: int, kernel_size: Union[int, Tuple[int]], stride: Union[int, Tuple[int]] = 1, padding: Union[int, Tuple[int]] = 0, dilation: Union[int, Tuple[int]] = 1, groups: int = 1, bias: bool = True, padding_mode: str = 'zeros')¶ The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/master/generated/torch.nn.Conv1d.html#conv1d
Applies a 1D convolution over an input signal composed of several input planes.
In the simplest case, the output value of the layer with input size \((N, C_{\text{in}}, L)\) and output \((N, C_{\text{out}}, L_{\text{out}})\) can be precisely described as:
\[\text{out}(N_i, C_{\text{out}_j}) = \text{bias}(C_{\text{out}_j}) + \sum_{k = 0}^{C_{in} - 1} \text{weight}(C_{\text{out}_j}, k) \star \text{input}(N_i, k)\]where \(\star\) is the valid cross-correlation operator, \(N\) is a batch size, \(C\) denotes a number of channels, \(L\) is a length of signal sequence.
stride
controls the stride for the cross-correlation, a single number or a one-element tuple.padding
controls the amount of padding applied to the input. It can be either a string {{‘valid’, ‘same’}} or a tuple of ints giving the amount of implicit padding applied on both sides.dilation
controls the spacing between the kernel points; also known as the à trous algorithm. It is harder to describe, but this link has a nice visualization of whatdilation
does.
Note
padding='valid'
is the same as no padding.padding='same'
pads the input so the output has the shape as the input. However, this mode doesn’t support any stride values other than 1.- Parameters
in_channels (int) – Number of channels in the input image
out_channels (int) – Number of channels produced by the convolution
kernel_size (int or tuple) – Size of the convolving kernel
stride (int or tuple, optional) – Stride of the convolution. Default: 1
padding (int, tuple or str, optional) – Padding added to both sides of the input. Default: 0
padding_mode (string, optional) –
'zeros'
. Default:'zeros'
dilation (int or tuple, optional) – Spacing between kernel elements. Default: 1
groups (int, optional) – Number of blocked connections from input channels to output channels. Default: 1
bias (bool, optional) – If
True
, adds a learnable bias to the output. Default:True
- Shape:
Input: \((N, C_{in}, L_{in})\)
Output: \((N, C_{out}, L_{out})\) where
\[L_{out} = \left\lfloor\frac{L_{in} + 2 \times \text{padding} - \text{dilation} \times (\text{kernel\_size} - 1) - 1}{\text{stride}} + 1\right\rfloor\]
-
weight
¶ the learnable weights of the module of shape \((\text{out\_channels}, \frac{\text{in\_channels}}{\text{groups}}, \text{kernel\_size})\). The values of these weights are sampled from \(\mathcal{U}(-\sqrt{k}, \sqrt{k})\) where \(k = \frac{groups}{C_\text{in} * \text{kernel\_size}}\)
- Type
-
bias
¶ the learnable bias of the module of shape (out_channels). If
bias
isTrue
, then the values of these weights are sampled from \(\mathcal{U}(-\sqrt{k}, \sqrt{k})\) where \(k = \frac{groups}{C_\text{in} * \text{kernel\_size}}\)- Type
For example:
>>> import numpy as np >>> import oneflow as flow >>> import oneflow.nn as nn >>> arr = np.random.randn(20, 16, 50) >>> input = flow.Tensor(arr) >>> m = nn.Conv1d(16, 33, 3, stride=2) >>> output = m(input)
-
extra_repr
()¶ Set the extra representation of the module
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
-
reset_parameters
() → None¶
-
class
oneflow.nn.
Conv2d
(in_channels: int, out_channels: int, kernel_size: Union[int, Tuple[int, int]], stride: Union[int, Tuple[int, int]] = 1, padding: Union[int, Tuple[int, int]] = 0, dilation: Union[int, Tuple[int, int]] = 1, groups: int = 1, bias: bool = True, padding_mode: str = 'zeros')¶ The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/master/generated/torch.nn.Conv2d.html#conv2d
Applies a 2D convolution over an input signal composed of several input planes.
In the simplest case, the output value of the layer with input size \((N, C_{\text{in}}, H, W)\) and output \((N, C_{\text{out}}, H_{\text{out}}, W_{\text{out}})\) can be precisely described as:
\[\text{out}(N_i, C_{\text{out}_j}) = \text{bias}(C_{\text{out}_j}) + \sum_{k = 0}^{C_{\text{in}} - 1} \text{weight}(C_{\text{out}_j}, k) \star \text{input}(N_i, k)\]where \(\star\) is the valid 2D cross-correlation operator, \(N\) is a batch size, \(C\) denotes a number of channels, \(H\) is a height of input planes in pixels, and \(W\) is width in pixels.
stride
controls the stride for the cross-correlation, a single number or a tuple.padding
controls the amount of implicit padding on both sides forpadding
number of points for each dimension.dilation
controls the spacing between the kernel points; also known as the à trous algorithm. It is harder to describe, but this link has a nice visualization of whatdilation
does.groups
controls the connections between inputs and outputs.in_channels
andout_channels
must both be divisible bygroups
. For example,At groups=1, all inputs are convolved to all outputs.
At groups=2, the operation becomes equivalent to having two conv layers side by side, each seeing half the input channels and producing half the output channels, and both subsequently concatenated.
At groups=
in_channels
, each input channel is convolved with its own set of filters (of size \(\frac{\text{out_channels}}{\text{in_channels}}\)).,
The parameters
kernel_size
,stride
,padding
,dilation
can either be:a single
int
– in which case the same value is used for the height and width dimensiona
tuple
of two ints – in which case, the first int is used for the height dimension, and the second int for the width dimension
Note
When groups == in_channels and out_channels == K * in_channels, where K is a positive integer, this operation is also known as a “depthwise convolution”.
In other words, for an input of size \((N, C_{in}, L_{in})\), a depthwise convolution with a depthwise multiplier K can be performed with the arguments \((C_\text{in}=C_\text{in}, C_\text{out}=C_\text{in} \times \text{K}, ..., \text{groups}=C_\text{in})\).
- Parameters
in_channels (int) – Number of channels in the input image
out_channels (int) – Number of channels produced by the convolution
kernel_size (int or tuple) – Size of the convolving kernel
stride (int or tuple, optional) – Stride of the convolution. Default: 1
padding (int or tuple, optional) – Zero-padding added to both sides of the input. Default: 0
padding_mode (string, optional) –
'zeros'
. Default:'zeros'
dilation (int or tuple, optional) – Spacing between kernel elements. Default: 1
groups (int, optional) – Number of blocked connections from input channels to output channels. Default: 1
bias (bool, optional) – If
True
, adds a learnable bias to the output. Default:True
- Shape:
Input: \((N, C_{in}, H_{in}, W_{in})\)
Output: \((N, C_{out}, H_{out}, W_{out})\) where
\[H_{out} = \left\lfloor\frac{H_{in} + 2 \times \text{padding}[0] - \text{dilation}[0] \times (\text{kernel_size}[0] - 1) - 1}{\text{stride}[0]} + 1\right\rfloor\]\[W_{out} = \left\lfloor\frac{W_{in} + 2 \times \text{padding}[1] - \text{dilation}[1] \times (\text{kernel_size}[1] - 1) - 1}{\text{stride}[1]} + 1\right\rfloor\]
- Attr:
- weight (Tensor): the learnable weights of the module of shape
\((\text{out_channels}, \frac{\text{in_channels}}{\text{groups}},\) \(\text{kernel_size[0]}, \text{kernel_size[1]})\). The values of these weights are sampled from \(\mathcal{U}(-\sqrt{k}, \sqrt{k})\) where \(k = \frac{groups}{C_\text{in} * \prod_{i=0}^{1}\text{kernel_size}[i]}\)
- bias (Tensor): the learnable bias of the module of shape
(out_channels). If
bias
isTrue
, then the values of these weights are sampled from \(\mathcal{U}(-\sqrt{k}, \sqrt{k})\) where \(k = \frac{groups}{C_\text{in} * \prod_{i=0}^{1}\text{kernel_size}[i]}\)
For example:
>>> import numpy as np >>> import oneflow as flow >>> import oneflow.nn as nn >>> arr = np.random.randn(20, 16, 50, 100) >>> input = flow.Tensor(arr) >>> m = nn.Conv2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2), dilation=(3, 1)) >>> output = m(input)
-
extra_repr
()¶ Set the extra representation of the module
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
-
reset_parameters
() → None¶
-
class
oneflow.nn.
Conv3d
(in_channels: int, out_channels: int, kernel_size: Union[int, Tuple[int, int, int]], stride: Union[int, Tuple[int, int, int]] = 1, padding: Union[int, Tuple[int, int, int]] = 0, dilation: Union[int, Tuple[int, int, int]] = 1, groups: int = 1, bias: bool = True, padding_mode: str = 'zeros')¶ The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/master/generated/torch.nn.Conv3d.html#conv3d
Applies a 3D convolution over an input signal composed of several input planes.
In the simplest case, the output value of the layer with input size \((N, C_{in}, D, H, W)\) and output \((N, C_{out}, D_{out}, H_{out}, W_{out})\) can be precisely described as:
\[out(N_i, C_{out_j}) = bias(C_{out_j}) + \sum_{k = 0}^{C_{in} - 1} weight(C_{out_j}, k) \star input(N_i, k)\]where \(\star\) is the valid 3D cross-correlation operator
stride
controls the stride for the cross-correlation.padding
controls the amount of padding applied to the input. It can be either a string {{‘valid’, ‘same’}} or a tuple of ints giving the amount of implicit padding applied on both sides.dilation
controls the spacing between the kernel points; also known as the à trous algorithm. It is harder to describe, but this link has a nice visualization of whatdilation
does.
The parameters
kernel_size
,stride
,padding
,dilation
can either be:a single
int
– in which case the same value is used for the depth, height and width dimensiona
tuple
of three ints – in which case, the first int is used for the depth dimension, the second int for the height dimension and the third int for the width dimension
Note
padding='valid'
is the same as no padding.padding='same'
pads the input so the output has the shape as the input. However, this mode doesn’t support any stride values other than 1.- Parameters
in_channels (int) – Number of channels in the input image
out_channels (int) – Number of channels produced by the convolution
kernel_size (int or tuple) – Size of the convolving kernel
stride (int or tuple, optional) – Stride of the convolution. Default: 1
padding (int, tuple or str, optional) – Padding added to all six sides of the input. Default: 0
padding_mode (string, optional) –
'zeros'
. Default:'zeros'
dilation (int or tuple, optional) – Spacing between kernel elements. Default: 1
groups (int, optional) – Number of blocked connections from input channels to output channels. Default: 1
bias (bool, optional) – If
True
, adds a learnable bias to the output. Default:True
- Shape:
Input: \((N, C_{in}, D_{in}, H_{in}, W_{in})\)
Output: \((N, C_{out}, D_{out}, H_{out}, W_{out})\) where
\[D_{out} = \left\lfloor\frac{D_{in} + 2 \times \text{padding}[0] - \text{dilation}[0] \times (\text{kernel\_size}[0] - 1) - 1}{\text{stride}[0]} + 1\right\rfloor\]\[H_{out} = \left\lfloor\frac{H_{in} + 2 \times \text{padding}[1] - \text{dilation}[1] \times (\text{kernel\_size}[1] - 1) - 1}{\text{stride}[1]} + 1\right\rfloor\]\[W_{out} = \left\lfloor\frac{W_{in} + 2 \times \text{padding}[2] - \text{dilation}[2] \times (\text{kernel\_size}[2] - 1) - 1}{\text{stride}[2]} + 1\right\rfloor\]
-
weight
¶ the learnable weights of the module of shape \((\text{out\_channels}, \frac{\text{in\_channels}}{\text{groups}},\) \(\text{kernel\_size[0]}, \text{kernel\_size[1]}, \text{kernel\_size[2]})\). The values of these weights are sampled from \(\mathcal{U}(-\sqrt{k}, \sqrt{k})\) where \(k = \frac{groups}{C_\text{in} * \prod_{i=0}^{2}\text{kernel\_size}[i]}\)
- Type
-
bias
¶ the learnable bias of the module of shape (out_channels). If
bias
isTrue
, then the values of these weights are sampled from \(\mathcal{U}(-\sqrt{k}, \sqrt{k})\) where \(k = \frac{groups}{C_\text{in} * \prod_{i=0}^{2}\text{kernel\_size}[i]}\)- Type
For example:
>>> import numpy as np >>> import oneflow as flow >>> import oneflow.nn as nn >>> arr = np.random.randn(1, 2, 5, 5, 5) >>> input = flow.Tensor(arr) >>> m = nn.Conv3d(2, 4, kernel_size=3, stride=1) >>> output = m(input)
-
extra_repr
()¶ Set the extra representation of the module
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
-
reset_parameters
() → None¶
-
class
oneflow.nn.
ConvTranspose1d
(in_channels: int, out_channels: int, kernel_size: Union[int, Tuple[int]], stride: Union[int, Tuple[int]] = 1, padding: Union[int, Tuple[int]] = 0, output_padding: Union[int, Tuple[int]] = 0, groups: int = 1, bias: bool = True, dilation: Union[int, Tuple[int]] = 1, padding_mode: str = 'zeros')¶ Applies a 1D transposed convolution operator over an input image composed of several input planes.
This module can be seen as the gradient of Conv1d with respect to its input. It is also known as a fractionally-strided convolution or a deconvolution (although it is not an actual deconvolution operation).
This module supports TensorFloat32.
stride
controls the stride for the cross-correlation.padding
controls the amount of implicit zero padding on both sides fordilation * (kernel_size - 1) - padding
number of points. See note below for details.output_padding
controls the additional size added to one side of the output shape. See note below for details.dilation
controls the spacing between the kernel points; also known as the à trous algorithm. It is harder to describe, but this link has a nice visualization of whatdilation
does.
Note
The
padding
argument effectively addsdilation * (kernel_size - 1) - padding
amount of zero padding to both sizes of the input. This is set so that when aConv1d
and aConvTranspose1d
are initialized with same parameters, they are inverses of each other in regard to the input and output shapes. However, whenstride > 1
,Conv1d
maps multiple input shapes to the same output shape.output_padding
is provided to resolve this ambiguity by effectively increasing the calculated output shape on one side. Note thatoutput_padding
is only used to find output shape, but does not actually add zero-padding to output.Note
In some circumstances when using the CUDA backend with CuDNN, this operator may select a nondeterministic algorithm to increase performance. If this is undesirable, you can try to make the operation deterministic (potentially at a performance cost) by setting
torch.backends.cudnn.deterministic = True
. Please see the notes on randomness for background.- Parameters
in_channels (int) – Number of channels in the input image
out_channels (int) – Number of channels produced by the convolution
kernel_size (int or tuple) – Size of the convolving kernel
stride (int or tuple, optional) – Stride of the convolution. Default: 1
padding (int or tuple, optional) –
dilation * (kernel_size - 1) - padding
zero-padding will be added to both sides of the input. Default: 0output_padding (int or tuple, optional) – Additional size added to one side of the output shape. Default: 0
groups (int, optional) – Number of blocked connections from input channels to output channels. Default: 1
bias (bool, optional) – If
True
, adds a learnable bias to the output. Default:True
dilation (int or tuple, optional) – Spacing between kernel elements. Default: 1
- Shape:
Input: \((N, C_{in}, L_{in})\)
Output: \((N, C_{out}, L_{out})\) where
\[L_{out} = (L_{in} - 1) \times \text{stride} - 2 \times \text{padding} + \text{dilation} \times (\text{kernel_size} - 1) + \text{output_padding} + 1\]
-
weight
¶ the learnable weights of the module of shape \((\\text{in\_channels}, \frac{\\text{out\\_channels}}{\text{groups}},\) \(\\text{kernel\\_size})\). The values of these weights are sampled from \(\mathcal{U}(-\sqrt{k}, \sqrt{k})\) where \(k = \frac{groups}{C_\text{out} * \\text{kernel\\_size}}\)
- Type
-
bias
¶ the learnable bias of the module of shape (out_channels). If
bias
isTrue
, then the values of these weights are sampled from \(\mathcal{U}(-\sqrt{k}, \sqrt{k})\) where \(k = \frac{groups}{C_\text{out} * \\text{kernel\\_size}}\)- Type
-
reset_parameters
() → None¶
-
class
oneflow.nn.
ConvTranspose2d
(in_channels: int, out_channels: int, kernel_size: Union[int, Tuple[int, int]], stride: Union[int, Tuple[int, int]] = 1, padding: Union[int, Tuple[int, int]] = 0, output_padding: Union[int, Tuple[int, int]] = 0, groups: int = 1, bias: bool = True, dilation: int = 1, padding_mode: str = 'zeros')¶ Applies a 2D transposed convolution operator over an input image composed of several input planes.
This module can be seen as the gradient of Conv2d with respect to its input. It is also known as a fractionally-strided convolution or a deconvolution (although it is not an actual deconvolution operation).
- Parameters
in_channels (int) – Number of channels in the input image
out_channels (int) – Number of channels produced by the convolution
kernel_size (int or tuple) – Size of the convolving kernel
stride (int or tuple, optional) – Stride of the convolution. Default: 1
padding (int or tuple, optional) –
dilation * (kernel_size - 1) - padding
zero-padding will be added to both sides of each dimension in the input. Default: 0output_padding (int or tuple, optional) – Additional size added to one side of each dimension in the output shape. Default: 0
groups (int, optional) – Number of blocked connections from input channels to output channels. Default: 1
bias (bool, optional) – If
True
, adds a learnable bias to the output. Default:True
dilation (int or tuple, optional) – Spacing between kernel elements. Default: 1
- Shape:
Input: \((N, C_{in}, H_{in}, W_{in})\)
Output: \((N, C_{out}, H_{out}, W_{out})\) where
\[ \begin{align}\begin{aligned}H_{out} = (H_{in} - 1) \times \text{stride}[0] - 2 \times \text{padding}[0] + \text{dilation}[0]\\ \times (\text{kernel_size}[0] - 1) + \text{output_padding}[0] + 1\end{aligned}\end{align} \]\[ \begin{align}\begin{aligned}W_{out} = (W_{in} - 1) \times \text{stride}[1] - 2 \times \text{padding}[1] + \text{dilation}[1]\\ \times (\text{kernel_size}[1] - 1) + \text{output_padding}[1] + 1\end{aligned}\end{align} \]
-
weight
¶ the learnable weights of the module of shape \((\text{in_channels}, \frac{\text{out_channels}}{\text{groups}},\) \(\text{kernel_size[0]}, \text{kernel_size[1]})\). The values of these weights are sampled from \(\mathcal{U}(-\sqrt{k}, \sqrt{k})\) where \(k = \frac{groups}{C_\text{out} * \prod_{i=0}^{1}\text{kernel_size}[i]}\)
- Type
-
bias
¶ the learnable bias of the module of shape (out_channels) If
bias
isTrue
, then the values of these weights are sampled from \(\mathcal{U}(-\sqrt{k}, \sqrt{k})\) where \(k = \frac{groups}{C_\text{out} * \prod_{i=0}^{1}\text{kernel_size}[i]}\)- Type
Examples:
>>> import numpy as np >>> import oneflow as flow >>> import oneflow.nn as nn >>> m = nn.ConvTranspose2d(16, 33, 3, stride=2) >>> # non-square kernels and unequal stride and with padding >>> m = nn.ConvTranspose2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2)) >>> m = m.to("cuda") >>> input = flow.Tensor(np.random.randn(20, 16, 50, 100), device=flow.device("cuda")) >>> output = m(input) >>> output.size() oneflow.Size([20, 33, 93, 100])
-
reset_parameters
() → None¶
-
class
oneflow.nn.
ConvTranspose3d
(in_channels: int, out_channels: int, kernel_size: Union[int, Tuple[int, int, int]], stride: Union[int, Tuple[int, int, int]] = 1, padding: Union[int, Tuple[int, int, int]] = 0, output_padding: Union[int, Tuple[int, int, int]] = 0, groups: int = 1, bias: bool = True, dilation: Union[int, Tuple[int, int, int]] = 1, padding_mode: str = 'zeros')¶ Applies a 3D transposed convolution operator over an input image composed of several input planes. The transposed convolution operator multiplies each input value element-wise by a learnable kernel, and sums over the outputs from all input feature planes.
This module can be seen as the gradient of Conv3d with respect to its input. It is also known as a fractionally-strided convolution or a deconvolution (although it is not an actual deconvolution operation).
This module supports TensorFloat32.
stride
controls the stride for the cross-correlation.padding
controls the amount of implicit zero padding on both sides fordilation * (kernel_size - 1) - padding
number of points. See note below for details.output_padding
controls the additional size added to one side of the output shape. See note below for details.dilation
controls the spacing between the kernel points; also known as the à trous algorithm. It is harder to describe, but this link has a nice visualization of whatdilation
does.
The parameters
kernel_size
,stride
,padding
,output_padding
can either be:a single
int
– in which case the same value is used for the depth, height and width dimensionsa
tuple
of three ints – in which case, the first int is used for the depth dimension, the second int for the height dimension and the third int for the width dimension
Note
The
padding
argument effectively addsdilation * (kernel_size - 1) - padding
amount of zero padding to both sizes of the input. This is set so that when aConv3d
and aConvTranspose3d
are initialized with same parameters, they are inverses of each other in regard to the input and output shapes. However, whenstride > 1
,Conv3d
maps multiple input shapes to the same output shape.output_padding
is provided to resolve this ambiguity by effectively increasing the calculated output shape on one side. Note thatoutput_padding
is only used to find output shape, but does not actually add zero-padding to output.- Parameters
in_channels (int) – Number of channels in the input image
out_channels (int) – Number of channels produced by the convolution
kernel_size (int or tuple) – Size of the convolving kernel
stride (int or tuple, optional) – Stride of the convolution. Default: 1
padding (int or tuple, optional) –
dilation * (kernel_size - 1) - padding
zero-padding will be added to both sides of each dimension in the input. Default: 0output_padding (int or tuple, optional) – Additional size added to one side of each dimension in the output shape. Default: 0
groups (int, optional) – Number of blocked connections from input channels to output channels. Default: 1
bias (bool, optional) – If
True
, adds a learnable bias to the output. Default:True
dilation (int or tuple, optional) – Spacing between kernel elements. Default: 1
- Shape:
Input: \((N, C_{in}, D_{in}, H_{in}, W_{in})\)
Output: \((N, C_{out}, D_{out}, H_{out}, W_{out})\) where
\[D_{out} = (D_{in} - 1) \times \text{stride}[0] - 2 \times \text{padding}[0] + \text{dilation}[0] \times (\text{kernel_size}[0] - 1) + \text{output_padding}[0] + 1\]\[H_{out} = (H_{in} - 1) \times \text{stride}[1] - 2 \times \text{padding}[1] + \text{dilation}[1] \times (\text{kernel_size}[1] - 1) + \text{output_padding}[1] + 1\]\[W_{out} = (W_{in} - 1) \times \text{stride}[2] - 2 \times \text{padding}[2] + \text{dilation}[2] \times (\text{kernel_size}[2] - 1) + \text{output_padding}[2] + 1\]
-
weight
¶ the learnable weights of the module of shape \((\text{in_channels}, \frac{\text{out_channels}}{\text{groups}},\) \(\text{kernel_size[0]}, \text{kernel_size[1]}, \text{kernel_size[2]})\). The values of these weights are sampled from \(\mathcal{U}(-\sqrt{k}, \sqrt{k})\) where \(k = \frac{groups}{C_\text{out} * \prod_{i=0}^{2}\text{kernel_size}[i]}\)
- Type
-
bias
¶ the learnable bias of the module of shape (out_channels) If
bias
isTrue
, then the values of these weights are sampled from \(\mathcal{U}(-\sqrt{k}, \sqrt{k})\) where \(k = \frac{groups}{C_\text{out} * \prod_{i=0}^{2}\text{kernel_size}[i]}\)- Type
Examples:
>>> import oneflow as flow >>> import oneflow.nn as nn >>> # With square kernels and equal stride >>> m = nn.ConvTranspose3d(16, 33, 3, stride=2) >>> # non-square kernels and unequal stride and with padding >>> m = nn.ConvTranspose3d(16, 33, (3, 5, 2), stride=(2, 1, 1), padding=(0, 4, 2)) >>> input = flow.randn(20, 16, 10, 50, 100) >>> output = m(input)
-
reset_parameters
() → None¶
-
class
oneflow.nn.
CropMirrorNormalize
(color_space: str = 'BGR', output_layout: str = 'NCHW', crop_h: int = 0, crop_w: int = 0, crop_pos_y: float = 0.5, crop_pos_x: float = 0.5, mean: Sequence[float] = [0.0], std: Sequence[float] = [1.0], output_dtype: oneflow._oneflow_internal.dtype = oneflow.float32)¶
-
class
oneflow.nn.
CrossEntropyLoss
(weight: Optional[oneflow._oneflow_internal.Tensor] = None, ignore_index: int = - 100, reduction: str = 'mean')¶ This criterion combines
LogSoftmax
andNLLLoss
in one single class.It is useful when training a classification problem with C classes.
The input is expected to contain raw, unnormalized scores for each class.
input has to be a Tensor of size either \((minibatch, C)\) or \((minibatch, C, d_1, d_2, ..., d_K)\) with \(K \geq 1\) for the K-dimensional case (described later).
This criterion expects a class index in the range \([0, C-1]\) as the target for each value of a 1D tensor of size minibatch;
The loss can be described as:
\[\text{loss}(x, class) = -\log\left(\frac{\exp(x[class])}{\sum_j \exp(x[j])}\right) = -x[class] + \log\left(\sum_j \exp(x[j])\right)\]Can also be used for higher dimension inputs, such as 2D images, by providing an input of size \((minibatch, C, d_1, d_2, ..., d_K)\) with \(K \geq 1\), where \(K\) is the number of dimensions, and a target of appropriate shape (see below).
- Parameters
reduction (string, optional) – Specifies the reduction to apply to the output:
'none'
|'mean'
|'sum'
.'none'
: no reduction will be applied,'mean'
: the weighted mean of the output is taken,'sum'
: the output will be summed. Default:'mean'
For example:
>>> import oneflow as flow >>> import numpy as np >>> input = flow.tensor( ... [[-0.1664078, -1.7256707, -0.14690138], ... [-0.21474946, 0.53737473, 0.99684894], ... [-1.135804, -0.50371903, 0.7645404]], dtype=flow.float32) >>> target = flow.tensor(np.array([0, 1, 2]), dtype=flow.int32) >>> out = flow.nn.CrossEntropyLoss(reduction="none")(input, target) >>> out tensor([0.8020, 1.1167, 0.3583], dtype=oneflow.float32) >>> out_sum = flow.nn.CrossEntropyLoss(reduction="sum")(input, target) >>> out_sum tensor(2.2769, dtype=oneflow.float32) >>> out_mean = flow.nn.CrossEntropyLoss(reduction="mean")(input, target) >>> out_mean tensor(0.7590, dtype=oneflow.float32)
-
class
oneflow.nn.
Dropout
(p: float = 0.5, inplace: bool = False, generator=None)¶ During training, randomly zeroes some of the elements of the input tensor with probability
p
using samples from a Bernoulli distribution. Each channel will be zeroed out independently on every forward call.This has proven to be an effective technique for regularization and preventing the co-adaptation of neurons as described in the paper “Improving neural networks by preventing co-adaptation of feature detectors”.
Furthermore, the outputs are scaled by a factor of \(\frac{1}{1-p}\) during training. This means that during evaluation the module simply computes an identity function.
- Parameters
p – probability of an element to be zeroed. Default: 0.5
inplace – If set to
True
, will do this operation in-place. Default:False
- Shape:
Input: \((*)\). Input can be of any shape
Output: \((*)\). Output is of the same shape as input
For example:
>>> import numpy as np >>> import oneflow as flow >>> m = flow.nn.Dropout(p=0) >>> arr = np.array( ... [ ... [-0.7797, 0.2264, 0.2458, 0.4163], ... [0.4299, 0.3626, -0.4892, 0.4141], ... [-1.4115, 1.2183, -0.5503, 0.6520], ... ] ... ) >>> x = flow.Tensor(arr) >>> y = m(x) >>> y tensor([[-0.7797, 0.2264, 0.2458, 0.4163], [ 0.4299, 0.3626, -0.4892, 0.4141], [-1.4115, 1.2183, -0.5503, 0.6520]], dtype=oneflow.float32)
-
inplace
: bool¶
-
p
: float¶
-
class
oneflow.nn.
ELU
(alpha: float = 1.0, inplace: bool = False)¶ Applies the element-wise function:
\[\begin{split}\text{ELU}(x) = \begin{cases} x & \text{ if } x \gt 0 \\ \alpha*(exp(x)-1) & \text{ if } x \le 0 \\ \end{cases}\end{split}\]- Parameters
alpha – the \(\alpha\) value for the ELU formulation. Default: 1.0
inplace – can optionally do the operation in-place. Default:
False
- Shape:
Input: \((N, *)\) where * means, any number of additional dimensions
Output: \((N, *)\), same shape as the input
For example:
>>> import numpy as np >>> import oneflow as flow >>> x = np.array([-0.5, 0, 0.5]).astype(np.float32) >>> input = flow.Tensor(x) >>> elu = flow.nn.ELU() >>> out = elu(input) >>> out tensor([-0.3935, 0.0000, 0.5000], dtype=oneflow.float32)
-
extra_repr
()¶ Set the extra representation of the module
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
-
class
oneflow.nn.
Embedding
(num_embeddings: int, embedding_dim: int, padding_idx: Optional[int] = None, max_norm: Optional[float] = None, norm_type: Optional[float] = None, scale_grad_by_freq: bool = False, sparse: bool = False, _weight: Optional[oneflow._oneflow_internal.Tensor] = None)¶ A simple lookup table that stores embeddings of a fixed dictionary and size.
This module is often used to store word embeddings and retrieve them using indices. The input to the module is a list of indices, and the output is the corresponding word embeddings.
- Parameters
num_embeddings (int) – size of the dictionary of embeddings
embedding_dim (int) – the size of each embedding vector
padding_idx (int, optional) – If specified, the entries at
padding_idx
do not contribute to the gradient; therefore, the embedding vector atpadding_idx
is not updated during training, i.e. it remains as a fixed “pad”. For a newly constructed Embedding, the embedding vector atpadding_idx
will default to all zeros, but can be updated to another value to be used as the padding vector.
For example:
>>> import numpy as np >>> import oneflow as flow >>> indices = flow.tensor([[1, 2, 4, 5], [4, 3, 2, 9]], dtype=flow.int) >>> m = flow.nn.Embedding(10, 3) >>> y = m(indices)
-
reset_parameters
() → None¶
-
class
oneflow.nn.
FakeQuantization
(quantization_formula: str = 'google', quantization_bit: int = 8, quantization_scheme: str = 'symmetric')¶ Simulate the quantize and dequantize operations in training time.
The output will be computed as:
if quantization_scheme == “symmetric”:
\[ \begin{align}\begin{aligned}& quant\_max = 2^{quantization\_to\_bit - 1} - 1\\& quant\_min = -quant\_max\\& clamp(round(x / scale), quant\_min, quant\_max) * scale\end{aligned}\end{align} \]elif quantization_scheme == “affine”:
\[ \begin{align}\begin{aligned}& quant\_max = 2^{quantization\_to\_bit} - 1\\& quant\_min = 0\\& (clamp(round(x / scale + zero\_point), quant\_min, quant\_max) - zero\_point) * scale\end{aligned}\end{align} \]- Parameters
quantization_bit (int) – Quantize input to uintX / intX, X can be in range [2, 8]. Defaults to 8.
quantization_scheme (str) – “symmetric” or “affine”, quantize to signed / unsigned integer. Defaults to “symmetric”.
quantization_formula (str) – Support “google” or “cambricon”.
- Returns
Input tensor after quantize and dequantize operations.
- Return type
For example:
>>> import numpy as np >>> import oneflow as flow >>> weight = (np.random.random((2, 3, 4, 5)) - 0.5).astype(np.float32) >>> input_tensor = flow.tensor( ... weight, dtype=flow.float32 ... ) >>> quantization_bit = 8 >>> quantization_scheme = "symmetric" >>> quantization_formula = "google" >>> per_layer_quantization = True >>> min_max_observer = flow.nn.MinMaxObserver(quantization_formula=quantization_formula, quantization_bit=quantization_bit, ... quantization_scheme=quantization_scheme, per_layer_quantization=per_layer_quantization) >>> fake_quantization = flow.nn.FakeQuantization(quantization_formula=quantization_formula, quantization_bit=quantization_bit, ... quantization_scheme=quantization_scheme) >>> scale, zero_point = min_max_observer( ... input_tensor, ... ) >>> output_tensor = fake_quantization( ... input_tensor, ... scale, ... zero_point, ... )
-
class
oneflow.nn.
Flatten
(start_dim: int = 1, end_dim: int = - 1)¶ Flattens a contiguous range of dims into a tensor. For use with: nn.Sequential.
- Parameters
start_dim – first dim to flatten (default = 1).
end_dim – last dim to flatten (default = -1).
For example:
>>> import oneflow as flow >>> input = flow.Tensor(32, 1, 5, 5) >>> m = flow.nn.Flatten() >>> output = m(input) >>> output.shape oneflow.Size([32, 25])
-
extra_repr
() → str¶ Set the extra representation of the module
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
-
class
oneflow.nn.
FusedBatchNorm1d
(num_features, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)¶ Applies Fused Batch Normalization over a 2D or 3D input, the formula is:
\[out = ReLU(BatchNorm(input) + addend)\]The formula of Batch Normalization is:
\[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 \(\gamma\) and \(\beta\) are learnable parameter vectors of size C (where C is the input size). By default, the elements of \(\gamma\) are set to 1 and the elements of \(\beta\) are set to 0. The standard-deviation is calculated via the biased estimator, equivalent to torch.var(input, unbiased=False).
Also by default, during training this layer keeps running estimates of its computed mean and variance, which are then used for normalization during evaluation. The running estimates are kept with a default
momentum
of 0.1.If
track_running_stats
is set toFalse
, this layer then does not keep running estimates, and batch statistics are instead used during evaluation time as well.Note
This
momentum
argument is different from one used in optimizer classes and the conventional notion of momentum. Mathematically, the update rule for running statistics here is \(\hat{x}_\text{new} = (1 - \text{momentum}) \times \hat{x} + \text{momentum} \times x_t\), where \(\hat{x}\) is the estimated statistic and \(x_t\) is the new observed value.Because the Batch Normalization is done over the C dimension, computing statistics on (N, L) slices, it’s common terminology to call this Temporal Batch Normalization.
- Parameters
num_features – \(C\) from an expected input of size \((N, C, L)\) or \(L\) from input of size \((N, L)\)
eps – a value added to the denominator for numerical stability. Default: 1e-5
momentum – the value used for the running_mean and running_var computation. Can be set to
None
for cumulative moving average (i.e. simple average). Default: 0.1affine – a boolean value that when set to
True
, this module has learnable affine parameters. Default:True
track_running_stats – a boolean value that when set to
True
, this module tracks the running mean and variance, and when set toFalse
, this module does not track such statistics, and initializes statistics buffersrunning_mean
andrunning_var
asNone
. When these buffers areNone
, this module always uses batch statistics. in both training and eval modes. Default:True
- Shape:
Input: \((N, C)\) or \((N, C, L)\)
Output: \((N, C)\) or \((N, C, L)\) (same shape as input)
For example:
>>> import oneflow as flow >>> import numpy as np >>> x = flow.Tensor(np.random.randn(20, 100)).to("cuda") # FusedBatchNorm support in GPU currently. >>> m = flow.nn.FusedBatchNorm1d(num_features=100, eps=1e-5, momentum=0.1).to("cuda") >>> y = m(x, addend=None)
-
class
oneflow.nn.
FusedBatchNorm2d
(num_features, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)¶ Applies Fused Batch Normalization over a 4D input, the formula is:
\[out = ReLU(BatchNorm(input) + addend)\]The formula of Batch Normalization is:
\[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 \(\gamma\) and \(\beta\) are learnable parameter vectors of size C (where C is the input size). By default, the elements of \(\gamma\) are set to 1 and the elements of \(\beta\) are set to 0. The standard-deviation is calculated via the biased estimator, equivalent to torch.var(input, unbiased=False).
Also by default, during training this layer keeps running estimates of its computed mean and variance, which are then used for normalization during evaluation. The running estimates are kept with a default
momentum
of 0.1.If
track_running_stats
is set toFalse
, this layer then does not keep running estimates, and batch statistics are instead used during evaluation time as well.Note
This
momentum
argument is different from one used in optimizer classes and the conventional notion of momentum. Mathematically, the update rule for running statistics here is \(\hat{x}_\text{new} = (1 - \text{momentum}) \times \hat{x} + \text{momentum} \times x_t\), where \(\hat{x}\) is the estimated statistic and \(x_t\) is the new observed value.Because the Batch Normalization is done over the C dimension, computing statistics on (N, H, W) slices, it’s common terminology to call this Spatial Batch Normalization.
- Parameters
num_features – \(C\) from an expected input of size \((N, C, H, W)\)
eps – a value added to the denominator for numerical stability. Default: 1e-5
momentum – the value used for the running_mean and running_var computation. Can be set to
None
for cumulative moving average (i.e. simple average). Default: 0.1affine – a boolean value that when set to
True
, this module has learnable affine parameters. Default:True
track_running_stats – a boolean value that when set to
True
, this module tracks the running mean and variance, and when set toFalse
, this module does not track such statistics, and initializes statistics buffersrunning_mean
andrunning_var
asNone
. When these buffers areNone
, this module always uses batch statistics. in both training and eval modes. Default:True
- Shape:
Input: \((N, C, H, W)\)
Output: \((N, C, H, W)\) (same shape as input)
For example:
>>> import oneflow as flow >>> import numpy as np >>> x = flow.Tensor(np.random.randn(4, 2, 8, 3)).to("cuda") # FusedBatchNorm support in GPU currently. >>> m = flow.nn.FusedBatchNorm2d(num_features=2, eps=1e-5, momentum=0.1).to("cuda") >>> y = m(x, addend=None)
-
class
oneflow.nn.
FusedBatchNorm3d
(num_features, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)¶ Applies Fused Batch Normalization over a 5D input, the formula is:
\[out = ReLU(BatchNorm(input) + addend)\]The formula of Batch Normalization is:
\[\begin{split}y = \\frac{x - \\mathrm{E}[x]}{\\sqrt{\\mathrm{Var}[x] + \\epsilon}} * \\gamma + \\beta\end{split}\]The mean and standard-deviation are calculated per-dimension over the mini-batches and \(\gamma\) and \(\beta\) are learnable parameter vectors of size C (where C is the input size). By default, the elements of \(\gamma\) are set to 1 and the elements of \(\beta\) are set to 0. The standard-deviation is calculated via the biased estimator, equivalent to torch.var(input, unbiased=False).
Also by default, during training this layer keeps running estimates of its computed mean and variance, which are then used for normalization during evaluation. The running estimates are kept with a default
momentum
of 0.1.If
track_running_stats
is set toFalse
, this layer then does not keep running estimates, and batch statistics are instead used during evaluation time as well.Note
This
momentum
argument is different from one used in optimizer classes and the conventional notion of momentum. Mathematically, the update rule for running statistics here is \(\hat{x}_\text{new} = (1 - \text{momentum}) \times \hat{x} + \text{momentum} \times x_t\), where \(\hat{x}\) is the estimated statistic and \(x_t\) is the new observed value.Because the Batch Normalization is done over the C dimension, computing statistics on (N, D, H, W) slices, it’s common terminology to call this Spatial Batch Normalization.
- Parameters
num_features – \(C\) from an expected input of size \((N, C, D, H, W)\)
eps – a value added to the denominator for numerical stability. Default: 1e-5
momentum – the value used for the running_mean and running_var computation. Can be set to
None
for cumulative moving average (i.e. simple average). Default: 0.1affine – a boolean value that when set to
True
, this module has learnable affine parameters. Default:True
track_running_stats – a boolean value that when set to
True
, this module tracks the running mean and variance, and when set toFalse
, this module does not track such statistics, and initializes statistics buffersrunning_mean
andrunning_var
asNone
. When these buffers areNone
, this module always uses batch statistics. in both training and eval modes. Default:True
- Shape:
Input: \((N, C, D, H, W)\)
Output: \((N, C, D, H, W)\) (same shape as input)
For example:
>>> import oneflow as flow >>> import numpy as np >>> x = flow.Tensor(np.random.randn(3, 2, 5, 8, 4)).to("cuda") # FusedBatchNorm support in GPU currently. >>> m = flow.nn.FusedBatchNorm3d(num_features=2, eps=1e-5, momentum=0.1).to("cuda") >>> y = m(x, addend=None)
-
class
oneflow.nn.
GELU
¶ Gelu activation operator.
The equation is:
\[out = 0.5 * x * (1 + tanh(\sqrt{\frac{2}{\pi}} * (x + 0.044715x^{3})))\]- Parameters
x (oneflow.Tensor) – Input Tensor
- Returns
A Tensor.
- Return type
For example:
>>> import numpy as np >>> import oneflow as flow >>> x = np.array([-0.5, 0, 0.5]).astype(np.float32) >>> input = flow.Tensor(x) >>> gelu = flow.nn.GELU() >>> out = gelu(input) >>> out tensor([-0.1543, 0.0000, 0.3457], dtype=oneflow.float32)
-
class
oneflow.nn.
GLU
(dim: Optional[int] = - 1)¶ The GLU activation.
- Parameters
input (Tensor, float) – input tensor.
dim (int, optional) – dimension on which to split the input. Default: -1
- Shape:
Input: \((\ast_1, N, \ast_2)\) where * means, any number of additional dimensions
Output: \((\ast_1, M, \ast_2)\) where \(M=N/2\)
The formula is:
\[GLU(input) = GLU(a, b) = a \otimes sigmoid(b)\]Note
where input is split in half along dim to form a and b, ⊗ is the element-wise product between matrices.
For example:
>>> import oneflow as flow >>> import oneflow.nn as nn >>> m = nn.GLU() >>> x = flow.tensor([[1, 2, 3, 4], [5, 6, 7, 8]], dtype=flow.float32) >>> y = m(x) >>> y tensor([[0.9526, 1.9640], [4.9954, 5.9980]], dtype=oneflow.float32)
-
class
oneflow.nn.
GroupNorm
(num_groups: int, num_channels: int, eps: float = 1e-05, affine: bool = True)¶ The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/stable/generated/torch.nn.GroupNorm.html
Applies Group Normalization over a mini-batch of inputs as described in the paper Group Normalization
\[y = \frac{x - \mathrm{E}[x]}{ \sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta\]The input channels are separated into
num_groups
groups, each containingnum_channels / num_groups
channels. The mean and standard-deviation are calculated separately over the each group. \(\gamma\) and \(\beta\) are learnable per-channel affine transform parameter vectors of sizenum_channels
ifaffine
isTrue
. The standard-deviation is calculated via the biased estimator, equivalent to torch.var(input, unbiased=False).This layer uses statistics computed from input data in both training and evaluation modes.
- Parameters
num_groups (int) – number of groups to separate the channels into
num_channels (int) – number of channels expected in input
eps – a value added to the denominator for numerical stability. Default: 1e-5
affine – a boolean value that when set to
True
, this module has learnable per-channel affine parameters initialized to ones (for weights) and zeros (for biases). Default:True
.
- Shape:
Input: \((N, C, *)\) where \(C=\text{num_channels}\)
Output: \((N, C, *)\) (same shape as input)
For example:
>>> import oneflow as flow >>> import numpy as np >>> input = flow.Tensor(np.random.randn(20, 6, 10, 10)) >>> # Separate 6 channels into 3 groups >>> m = flow.nn.GroupNorm(3, 6) >>> # Separate 6 channels into 6 groups (equivalent with InstanceNorm) >>> m = flow.nn.GroupNorm(6, 6) >>> # Put all 6 channels into a single group (equivalent with LayerNorm) >>> m = flow.nn.GroupNorm(1, 6) >>> # Activating the module >>> output = m(input)
-
extra_repr
() → str¶ Set the extra representation of the module
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
-
reset_parameters
() → None¶
-
class
oneflow.nn.
Hardsigmoid
(inplace: bool = False)¶ Applies the element-wise function:
\[\begin{split}\text{Hardsigmoid}(x) = \begin{cases} 0 & \text{ if } x \le -3 \\ 1 & \text{ if } x \ge +3 \\ \frac{x}{6} + \frac{1}{2} & \text{ otherwise } \\ \end{cases}\end{split}\]- Parameters
inplace – can optionally do the operation in-place. Default:
False
- Shape:
Input: \((N, *)\) where * means, any number of additional dimensions
Output: \((N, *)\), same shape as the input
For example:
>>> import numpy as np >>> import oneflow as flow >>> x = np.array([-0.5, 0, 0.5]).astype(np.float32) >>> input = flow.Tensor(x) >>> hardsigmoid = flow.nn.Hardsigmoid() >>> out = hardsigmoid(input) >>> out tensor([0.4167, 0.5000, 0.5833], dtype=oneflow.float32)
-
extra_repr
()¶ Set the extra representation of the module
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
-
class
oneflow.nn.
Hardswish
(inplace: bool = False)¶ Applies the hardswish function, element-wise, as described in the paper: Searching for MobileNetV3.
\[\begin{split}\text{Hardswish}(x) = \begin{cases} 0 & \text{ if } x \le -3 \\ x & \text{ if } x \ge +3 \\ x*(x+3)/6 & \text{ otherwise } \\ \end{cases}\end{split}\]- Parameters
inplace – can optionally do the operation in-place. Default:
False
- Shape:
Input: \((N, *)\) where * means, any number of additional dimensions
Output: \((N, *)\), same shape as the input
>>> import numpy as np >>> import oneflow as flow >>> x = np.array([-0.5, 0, 0.5]).astype(np.float32) >>> input = flow.Tensor(x) >>> hardswish = flow.nn.Hardswish() >>> out = hardswish(input) >>> out tensor([-0.2083, 0.0000, 0.2917], dtype=oneflow.float32)
-
extra_repr
()¶ Set the extra representation of the module
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
-
class
oneflow.nn.
Hardtanh
(min_val: float = - 1, max_val: float = 1, inplace: bool = False, min_value: Optional[float] = None, max_value: Optional[float] = None)¶ Applies the HardTanh function element-wise
HardTanh is defined as:
\[\begin{split}\text{HardTanh}(x) = \begin{cases} 1 & \text{ if } x > 1 \\ -1 & \text{ if } x < -1 \\ x & \text{ otherwise } \\ \end{cases}\end{split}\]The range of the linear region \([-1, 1]\) can be adjusted using
min_val
andmax_val
.- Parameters
min_val – minimum value of the linear region range. Default: -1
max_val – maximum value of the linear region range. Default: 1
inplace – can optionally do the operation in-place. Default:
False
Keyword arguments
min_value
andmax_value
have been deprecated in favor ofmin_val
andmax_val
.- Shape:
Input: \((N, *)\) where * means, any number of additional dimensions
Output: \((N, *)\), same shape as the input
For example:
>>> import numpy as np >>> import oneflow as flow >>> m = flow.nn.Hardtanh() >>> arr = np.array([0.2, 0.3, 3.0, 4.0]) >>> x = flow.Tensor(arr) >>> out = m(x) >>> out tensor([0.2000, 0.3000, 1.0000, 1.0000], dtype=oneflow.float32)
-
extra_repr
()¶ Set the extra representation of the module
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
-
class
oneflow.nn.
Identity
(*args, **kwargs)¶ A placeholder identity operator that is argument-insensitive.
- Parameters
args – any argument (unused)
kwargs – any keyword argument (unused)
For example:
import numpy as np import oneflow as flow m = flow.nn.Identity() input = flow.Tensor(np.random.rand(2, 3, 4, 5)) output = m(input) # output = input
-
class
oneflow.nn.
InstanceNorm1d
(num_features: int, eps: float = 1e-05, momentum: float = 0.1, affine: bool = False, track_running_stats: bool = False)¶ The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/stable/generated/torch.nn.InstanceNorm1d.html
Applies Instance Normalization over a 3D input (a mini-batch of 1D inputs with optional additional channel dimension) as described in the paper Instance Normalization: The Missing Ingredient for Fast Stylization.
\[y = \frac{x - \mathrm{E}[x]}{ \sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta\]The mean and standard-deviation are calculated per-dimension separately for each object in a mini-batch. \(\gamma\) and \(\beta\) are learnable parameter vectors of size C (where C is the input size) if
affine
isTrue
. The standard-deviation is calculated via the biased estimator, equivalent to torch.var(input, unbiased=False).By default, this layer uses instance statistics computed from input data in both training and evaluation modes.
If
track_running_stats
is set toTrue
, during training this layer keeps running estimates of its computed mean and variance, which are then used for normalization during evaluation. The running estimates are kept with a defaultmomentum
of 0.1.Note
This
momentum
argument is different from one used in optimizer classes and the conventional notion of momentum. Mathematically, the update rule for running statistics here is \(\hat{x}_\text{new} = (1 - \text{momentum}) \times \hat{x} + \text{momentum} \times x_t\), where \(\hat{x}\) is the estimated statistic and \(x_t\) is the new observed value.Note
InstanceNorm1d
andLayerNorm
are very similar, but have some subtle differences.InstanceNorm1d
is applied on each channel of channeled data like multidimensional time series, butLayerNorm
is usually applied on entire sample and often in NLP tasks. Additionally,LayerNorm
applies elementwise affine transform, whileInstanceNorm1d
usually don’t apply affine transform.- Parameters
num_features – \(C\) from an expected input of size \((N, C, L)\) or \(L\) from input of size \((N, L)\)
eps – a value added to the denominator for numerical stability. Default: 1e-5
momentum – the value used for the running_mean and running_var computation. Default: 0.1
affine – a boolean value that when set to
True
, this module has learnable affine parameters, initialized the same way as done for batch normalization. Default:False
.track_running_stats – a boolean value that when set to
True
, this module tracks the running mean and variance, and when set toFalse
, this module does not track such statistics and always uses batch statistics in both training and eval modes. Default:False
- Shape:
Input: \((N, C, L)\)
Output: \((N, C, L)\) (same shape as input)
For example:
>>> import oneflow as flow >>> import numpy as np >>> # Without Learnable Parameters >>> m = flow.nn.InstanceNorm1d(100) >>> # With Learnable Parameters >>> m = flow.nn.InstanceNorm1d(100, affine=True) >>> x = flow.Tensor(np.random.randn(20, 100, 40)) >>> output = m(x)
-
class
oneflow.nn.
InstanceNorm2d
(num_features: int, eps: float = 1e-05, momentum: float = 0.1, affine: bool = False, track_running_stats: bool = False)¶ The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/stable/generated/torch.nn.InstanceNorm2d.html
Applies Instance Normalization over a 4D input (a mini-batch of 2D inputs with additional channel dimension) as described in the paper Instance Normalization: The Missing Ingredient for Fast Stylization.
\[y = \frac{x - \mathrm{E}[x]}{ \sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta\]The mean and standard-deviation are calculated per-dimension separately for each object in a mini-batch. \(\gamma\) and \(\beta\) are learnable parameter vectors of size C (where C is the input size) if
affine
isTrue
. The standard-deviation is calculated via the biased estimator, equivalent to torch.var(input, unbiased=False).By default, this layer uses instance statistics computed from input data in both training and evaluation modes.
If
track_running_stats
is set toTrue
, during training this layer keeps running estimates of its computed mean and variance, which are then used for normalization during evaluation. The running estimates are kept with a defaultmomentum
of 0.1.Note
This
momentum
argument is different from one used in optimizer classes and the conventional notion of momentum. Mathematically, the update rule for running statistics here is \(\hat{x}_\text{new} = (1 - \text{momentum}) \times \hat{x} + \text{momentum} \times x_t\), where \(\hat{x}\) is the estimated statistic and \(x_t\) is the new observed value.Note
InstanceNorm2d
andLayerNorm
are very similar, but have some subtle differences.InstanceNorm2d
is applied on each channel of channeled data like RGB images, butLayerNorm
is usually applied on entire sample and often in NLP tasks. Additionally,LayerNorm
applies elementwise affine transform, whileInstanceNorm2d
usually don’t apply affine transform.- Parameters
num_features – \(C\) from an expected input of size \((N, C, H, W)\)
eps – a value added to the denominator for numerical stability. Default: 1e-5
momentum – the value used for the running_mean and running_var computation. Default: 0.1
affine – a boolean value that when set to
True
, this module has learnable affine parameters, initialized the same way as done for batch normalization. Default:False
.track_running_stats – a boolean value that when set to
True
, this module tracks the running mean and variance, and when set toFalse
, this module does not track such statistics and always uses batch statistics in both training and eval modes. Default:False
- Shape:
Input: \((N, C, H, W)\)
Output: \((N, C, H, W)\) (same shape as input)
For example:
>>> import oneflow as flow >>> import numpy as np >>> # Without Learnable Parameters >>> m = flow.nn.InstanceNorm2d(100) >>> # With Learnable Parameters >>> m = flow.nn.InstanceNorm2d(100, affine=True) >>> x = flow.Tensor(np.random.randn(20, 100, 35, 45)) >>> output = m(x)
-
class
oneflow.nn.
InstanceNorm3d
(num_features: int, eps: float = 1e-05, momentum: float = 0.1, affine: bool = False, track_running_stats: bool = False)¶ The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/stable/generated/torch.nn.InstanceNorm3d.html
Applies Instance Normalization over a 5D input (a mini-batch of 3D inputs with additional channel dimension) as described in the paper Instance Normalization: The Missing Ingredient for Fast Stylization.
\[y = \frac{x - \mathrm{E}[x]}{ \sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta\]The mean and standard-deviation are calculated per-dimension separately for each object in a mini-batch. \(\gamma\) and \(\beta\) are learnable parameter vectors of size C (where C is the input size) if
affine
isTrue
. The standard-deviation is calculated via the biased estimator, equivalent to torch.var(input, unbiased=False).By default, this layer uses instance statistics computed from input data in both training and evaluation modes.
If
track_running_stats
is set toTrue
, during training this layer keeps running estimates of its computed mean and variance, which are then used for normalization during evaluation. The running estimates are kept with a defaultmomentum
of 0.1.Note
This
momentum
argument is different from one used in optimizer classes and the conventional notion of momentum. Mathematically, the update rule for running statistics here is \(\hat{x}_\text{new} = (1 - \text{momentum}) \times \hat{x} + \text{momentum} \times x_t\), where \(\hat{x}\) is the estimated statistic and \(x_t\) is the new observed value.Note
InstanceNorm3d
andLayerNorm
are very similar, but have some subtle differences.InstanceNorm3d
is applied on each channel of channeled data like 3D models with RGB color, butLayerNorm
is usually applied on entire sample and often in NLP tasks. Additionally,LayerNorm
applies elementwise affine transform, whileInstanceNorm3d
usually don’t apply affine transform.- Parameters
num_features – \(C\) from an expected input of size \((N, C, D, H, W)\)
eps – a value added to the denominator for numerical stability. Default: 1e-5
momentum – the value used for the running_mean and running_var computation. Default: 0.1
affine – a boolean value that when set to
True
, this module has learnable affine parameters, initialized the same way as done for batch normalization. Default:False
.track_running_stats – a boolean value that when set to
True
, this module tracks the running mean and variance, and when set toFalse
, this module does not track such statistics and always uses batch statistics in both training and eval modes. Default:False
- Shape:
Input: \((N, C, D, H, W)\)
Output: \((N, C, D, H, W)\) (same shape as input)
For example:
>>> import oneflow as flow >>> import numpy as np >>> # Without Learnable Parameters >>> m = flow.nn.InstanceNorm3d(100) >>> # With Learnable Parameters >>> m = flow.nn.InstanceNorm3d(100, affine=True) >>> x = flow.Tensor(np.random.randn(20, 100, 35, 45, 10)) >>> output = m(x)
-
class
oneflow.nn.
KLDivLoss
(reduction: str = 'mean', log_target: bool = False)¶ The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/stable/generated/torch.nn.KLDivLoss.html?highlight=kldivloss#torch.nn.KLDivLoss
The Kullback-Leibler divergence loss measure
Kullback-Leibler divergence is a useful distance measure for continuous distributions and is often useful when performing direct regression over the space of (discretely sampled) continuous output distributions.
As with
NLLLoss
, the input given is expected to contain log-probabilities and is not restricted to a 2D Tensor. The targets are interpreted as probabilities by default, but could be considered as log-probabilities withlog_target
set toTrue
.This criterion expects a target Tensor of the same size as the input Tensor.
The unreduced (i.e. with
reduction
set to'none'
) loss can be described as:\[l(x,y) = L = \{ l_1,\dots,l_N \}, \quad l_n = y_n \cdot \left( \log y_n - x_n \right)\]where the index \(N\) spans all dimensions of
input
and \(L\) has the same shape asinput
. Ifreduction
is not'none'
(default'mean'
), then:\[\begin{split}\ell(x, y) = \begin{cases} \operatorname{mean}(L), & \text{if reduction} = \text{`mean';} \\ \operatorname{sum}(L), & \text{if reduction} = \text{`sum'.} \end{cases}\end{split}\]In default
reduction
mode'mean'
, the losses are averaged for each minibatch over observations as well as over dimensions.'batchmean'
mode gives the correct KL divergence where losses are averaged over batch dimension only.'mean'
mode’s behavior will be changed to the same as'batchmean'
in the next major release.- Parameters
reduction (string, optional) – Specifies the reduction to apply to the output:
'none'
|'batchmean'
|'sum'
|'mean'
.'none'
: no reduction will be applied.'batchmean'
: the sum of the output will be divided by batchsize.'sum'
: the output will be summed.'mean'
: the output will be divided by the number of elements in the output. Default:'mean'
log_target (bool, optional) – Specifies whether target is passed in the log space. Default:
False
Note
reduction
='mean'
doesn’t return the true kl divergence value, please usereduction
='batchmean'
which aligns with KL math definition. In the next major release,'mean'
will be changed to be the same as'batchmean'
.- Shape:
Input: \((N, *)\) where \(*\) means, any number of additional dimensions
Target: \((N, *)\), same shape as the input
Output: scalar by default. If :attr:
reduction
is'none'
, then \((N, *)\), the same shape as the input
For example:
>>> import oneflow as flow >>> import numpy as np >>> input = flow.tensor([-0.9021705, 0.08798598, 1.04686249], dtype=flow.float32) >>> target = flow.tensor([1.22386942, -0.89729659, 0.01615712], dtype=flow.float32) >>> m = flow.nn.KLDivLoss(reduction="none", log_target=False) >>> out = m(input, target) >>> out tensor([ 1.3514, 0.0000, -0.0836], dtype=oneflow.float32) >>> m = flow.nn.KLDivLoss(reduction="mean", log_target=False) >>> out = m(input, target) >>> out tensor(0.4226, dtype=oneflow.float32) >>> m = flow.nn.KLDivLoss(reduction="sum", log_target=True) >>> out = m(input, target) >>> out tensor(5.7801, dtype=oneflow.float32)
-
class
oneflow.nn.
L1Loss
(reduction: str = 'mean')¶ This operator computes the L1 Loss between each element in input and target.
The equation is:
if reduction = “none”:
\[output = |Target - Input|\]if reduction = “mean”:
\[output = \frac{1}{n}\sum_{i=1}^n|Target_i - Input_i|\]if reduction = “sum”:
\[output = \sum_{i=1}^n|Target_i - Input_i|\]- Parameters
input (oneflow.Tensor) – The input Tensor.
target (oneflow.Tensor) – The target Tensor.
reduction (str) – The reduce type, it can be one of “none”, “mean”, “sum”. Defaults to “mean”.
- Returns
The result Tensor.
- Return type
For example:
>>> import oneflow as flow >>> import numpy as np >>> input = flow.tensor([[1, 1, 1], [2, 2, 2], [7, 7, 7]], dtype = flow.float32) >>> target = flow.tensor([[4, 4, 4], [4, 4, 4], [4, 4, 4]], dtype = flow.float32) >>> m = flow.nn.L1Loss(reduction="none") >>> out = m(input, target) >>> out tensor([[3., 3., 3.], [2., 2., 2.], [3., 3., 3.]], dtype=oneflow.float32) >>> m_mean = flow.nn.L1Loss(reduction="mean") >>> out = m_mean(input, target) >>> out tensor(2.6667, dtype=oneflow.float32) >>> m_mean = flow.nn.L1Loss(reduction="sum") >>> out = m_mean(input, target) >>> out tensor(24., dtype=oneflow.float32)
-
class
oneflow.nn.
LayerNorm
(normalized_shape: Union[int, Tuple[int], oneflow.Size], eps: float = 1e-05, elementwise_affine: bool = True)¶ Applies Layer Normalization over a mini-batch of inputs as described in the paper Layer Normalization
\[y = \frac{x - \mathrm{E}[x]}{ \sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta\]The mean and standard-deviation are calculated separately over the last certain number dimensions which have to be of the shape specified by
normalized_shape
. \(\gamma\) and \(\beta\) are learnable affine transform parameters ofnormalized_shape
ifelementwise_affine
isTrue
. The standard-deviation is calculated via the biased estimator.Note
Unlike Batch Normalization and Instance Normalization, which applies scalar scale and bias for each entire channel/plane with the
affine
option, Layer Normalization applies per-element scale and bias withelementwise_affine
.This layer uses statistics computed from input data in both training and evaluation modes.
- Parameters
normalized_shape (int or list or oneflow.Size) –
input shape from an expected input of size
\[[* \times \text{normalized_shape}[0] \times \text{normalized_shape}[1] \times \ldots \times \text{normalized_shape}[-1]]\]If a single integer is used, it is treated as a singleton list, and this module will
normalize over the last dimension which is expected to be of that specific size.
eps – a value added to the denominator for numerical stability. Default: 1e-5
elementwise_affine – a boolean value that when set to
True
, this module has learnable per-element affine parameters initialized to ones (for weights) and zeros (for biases). Default:True
.
- Shape:
Input: \((N, *)\)
Output: \((N, *)\) (same shape as input)
For example:
>>> import numpy as np >>> import oneflow as flow >>> input_arr = np.array( ... [ ... [ ... [[-0.16046895, -1.03667831], [-0.34974465, 0.26505867]], ... [[-1.24111986, -0.53806001], [1.72426331, 0.43572459]], ... ], ... [ ... [[-0.77390957, -0.42610624], [0.16398858, -1.35760343]], ... [[1.07541728, 0.11008703], [0.26361224, -0.48663723]], ... ], ... ], ... dtype=np.float32, ... ) >>> x = flow.Tensor(input_arr) >>> m = flow.nn.LayerNorm(2) >>> y = m(x).numpy() >>> y array([[[[ 0.99997395, -0.99997395], [-0.999947 , 0.999947 ]], [[-0.99995965, 0.9999595 ], [ 0.999988 , -0.999988 ]]], [[[-0.9998348 , 0.99983466], [ 0.9999914 , -0.9999914 ]], [[ 0.9999785 , -0.9999785 ], [ 0.9999645 , -0.9999645 ]]]], dtype=float32)
-
elementwise_affine
: bool¶
-
eps
: float¶
-
extra_repr
() → str¶ Set the extra representation of the module
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
-
normalized_shape
: Tuple[int, …]¶
-
reset_parameters
() → None¶
-
class
oneflow.nn.
LeakyReLU
(negative_slope: float = 0.01, inplace: bool = False)¶ Applies the element-wise function:
\[\begin{split}\text{LeakyRELU}(x) = \begin{cases} x, & \text{ if } x \geq 0 \\ \text{negative_slope} \times x, & \text{ otherwise } \end{cases}\end{split}\]- Parameters
negative_slope – Controls the angle of the negative slope. Default: 1e-2
inplace – can optionally do the operation in-place. Default:
False
- Shape:
Input: \((N, *)\) where * means, any number of additional dimensions
Output: \((N, *)\), same shape as the input
For example:
>>> import numpy as np >>> import oneflow as flow >>> m = flow.nn.LeakyReLU(0.1) >>> arr = np.array([0.2, 0.3, 3.0, 4.0]) >>> x = flow.Tensor(arr) >>> out = m(x) >>> out tensor([0.2000, 0.3000, 3.0000, 4.0000], dtype=oneflow.float32)
-
extra_repr
()¶ Set the extra representation of the module
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
-
class
oneflow.nn.
Linear
(in_features: int, out_features: int, bias: bool = True)¶ Applies a linear transformation to the incoming data: \(y = xA^T + b\)
- Parameters
in_features (-) – size of each input sample
out_features (-) – size of each output sample
bias (-) – If set to
False
, the layer will not learn an additive bias. Default:True
- Shape:
Input: \((N, *, H_{in})\) where \(*\) means any number of additional dimensions and \(H_{in} = {in\_features}\)
Output: \((N, *, H_{out})\) where all but the last dimension are the same shape as the input and \(H_{out} = {out\_features}\).
- Attr:
weight
: the learnable weights of the module of shape \(({out\_features}, {in\_features})\). The values are initialized from \(\mathcal{U}(-\sqrt{k}, \sqrt{k})\), where \((k = 1 / {in\_features})\)bias
: the learnable bias of the module of shape \(({out\_features})\). Ifbias
isTrue
, the values are initialized from \(\mathcal{U}(-\sqrt{k}, \sqrt{k})\) where \((k = 1 / {in\_features})\)
For example:
>>> import numpy as np >>> import oneflow as flow >>> m = flow.nn.Linear(20, 30, False) >>> input = flow.Tensor(np.random.randn(128, 20)) >>> output = m(input) >>> output.size() oneflow.Size([128, 30])
-
extra_repr
() → str¶ Set the extra representation of the module
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
-
reset_parameters
() → None¶
-
class
oneflow.nn.
LogSigmoid
¶ Applies the element-wise function:
\[\text{LogSigmoid}(x) = \log\left(\frac{ 1 }{ 1 + \exp(-x)}\right)\]- Shape:
Input: \((N, *)\) where * means, any number of additional dimensions
Output: \((N, *)\), same shape as the input
For example:
>>> import numpy as np >>> import oneflow as flow >>> x = np.array([-0.5, 0, 0.5]).astype(np.float32) >>> input = flow.Tensor(x) >>> logsigmoid = flow.nn.LogSigmoid() >>> out = logsigmoid(input) >>> out tensor([-0.9741, -0.6931, -0.4741], dtype=oneflow.float32)
-
class
oneflow.nn.
LogSoftmax
(dim: Optional[int] = None)¶ Applies the LogSoftmax function to an n-dimensional input Tensor. The LogSoftmax formulation can be simplified as:
\[\text{LogSoftmax}(x_{i}) = \log\left(\frac{\exp(x_i) }{ \sum_j \exp(x_j)} \right) = x_i - \log({ \sum_j \exp(x_j)})\]- Parameters
dim (int) – A dimension along which LogSoftmax will be computed.
- Shape:
Input: \((N, *)\) where * means, any number of additional dimensions
Output: \((N, *)\), same shape as the input
For example:
>>> import numpy as np >>> import oneflow as flow >>> m = flow.nn.LogSoftmax(dim=1) >>> x = flow.Tensor( ... np.array( ... [[ 0.4296, -1.1957, 2.5463], ... [ 1.2552, -1.5747, 0.6923]] ... ) ... ) >>> out = m(x) >>> out tensor([[-2.2513, -3.8766, -0.1346], [-0.4877, -3.3176, -1.0506]], dtype=oneflow.float32)
-
extra_repr
()¶ Set the extra representation of the module
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
-
class
oneflow.nn.
MSELoss
(reduction: str = 'mean')¶ The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/stable/generated/torch.nn.MSELoss.html?highlight=mseloss#torch.nn.MSELoss
Creates a criterion that measures the mean squared error (squared L2 norm) between each element in the input \(x\) and target \(y\).
The unreduced (i.e. with
reduction
set to'none'
) loss can be described as:\[\ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \quad l_n = \left( x_n - y_n \right)^2,\]where \(N\) is the batch size. If
reduction
is not'none'
(default'mean'
), then:\[\begin{split}\ell(x, y) = \begin{cases} \operatorname{mean}(L), & \text{if reduction} = \text{`mean';}\\ \operatorname{sum}(L), & \text{if reduction} = \text{`sum'.} \end{cases}\end{split}\]\(x\) and \(y\) are tensors of arbitrary shapes with a total of \(n\) elements each.
The mean operation still operates over all the elements, and divides by \(n\).
The division by \(n\) can be avoided if one sets
reduction = 'sum'
.- Parameters
reduction (string, optional) – Specifies the reduction to apply to the output:
'none'
|'mean'
|'sum'
.'none'
: no reduction will be applied,'mean'
: the sum of the output will be divided by the number of elements in the output,'sum'
: the output will be summed. Default:'mean'
- Shape:
Input: \((N, *)\) where \(*\) means, any number of additional dimensions
Target: \((N, *)\), same shape as the input
For example:
>>> import oneflow as flow >>> import numpy as np >>> input = flow.tensor( ... [[-0.02557137, 0.03101675, 1.37493674], ... [0.25599439, -1.08372561, -0.21006816]], dtype=flow.float32) >>> target = flow.tensor( ... [[-1.53105064, -0.68137555, 0.5931354], ... [-0.49158347, 0.93673637, 0.1324141]], dtype=flow.float32) >>> m = flow.nn.MSELoss(reduction="none") >>> out = m(input, target) >>> out tensor([[2.2665, 0.5075, 0.6112], [0.5589, 4.0823, 0.1173]], dtype=oneflow.float32) >>> m = flow.nn.MSELoss(reduction="mean") >>> out = m(input, target) >>> out tensor(1.3573, dtype=oneflow.float32) >>> m = flow.nn.MSELoss(reduction="sum") >>> out = m(input, target) >>> out tensor(8.1436, dtype=oneflow.float32)
-
class
oneflow.nn.
MarginRankingLoss
(margin: float = 0.0, reduction: str = 'mean')¶ Creates a criterion that measures the loss given inputs \(x1\), \(x2\), two 1D mini-batch Tensors, and a label 1D mini-batch tensor \(y\) (containing 1 or -1).
If \(y = 1\) then it assumed the first input should be ranked higher (have a larger value) than the second input, and vice-versa for \(y = -1\).
The loss function for each sample in the mini-batch is:
\[\text{loss}(x1, x2, y) = \max(0, -y * (x1 - x2) + \text{margin})\]- Parameters
margin (float, optional) – Has a default value of \(0\).
reduction (string, optional) – Specifies the reduction to apply to the output:
'none'
|'mean'
|'sum'
.'none'
: no reduction will be applied,'mean'
: the sum of the output will be divided by the number of elements in the output,'sum'
: the output will be summed. Default:'mean'
- Shape:
x1 : \((N, D)\) where N is the batch size and D is the size of a sample.
x2 : \((N, D)\) where N is the batch size and D is the size of a sample.
Target: \((N)\)
Output: scalar. If
reduction
is'none'
, then \((N)\).
For example:
>>> import oneflow as flow >>> import numpy as np >>> x1 = flow.tensor(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), dtype=flow.float32) >>> x2 = flow.tensor(np.array([[2, 2, 2], [2, 2, 2], [2, 2, 2]]), dtype=flow.float32) >>> target = flow.tensor(np.array([[1, -1, 1],[-1, 1, -1], [1, 1, 1]]), dtype=flow.float32) >>> m = flow.nn.MarginRankingLoss(margin =1.0, reduction="none") >>> out = m(x1, x2, target) >>> out tensor([[2., 1., 0.], [3., 0., 5.], [0., 0., 0.]], dtype=oneflow.float32) >>> m = flow.nn.MarginRankingLoss(margin = 0.3, reduction="sum") >>> out = m(x1, x2, target) >>> out tensor(8.2000, dtype=oneflow.float32) >>> m = flow.nn.MarginRankingLoss(margin = 10, reduction="mean") >>> out = m(x1, x2, target) >>> out tensor(8.3333, dtype=oneflow.float32)
-
class
oneflow.nn.
MaxPool1d
(kernel_size: Union[int, Tuple[int]], stride: Optional[Union[int, Tuple[int]]] = None, padding: Union[int, Tuple[int]] = 0, dilation: Union[int, Tuple[int]] = 1, return_indices: bool = False, ceil_mode: bool = False)¶ The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/stable/generated/torch.nn.MaxPool1d.html#torch.nn.MaxPool1d
Applies a 1D max pooling over an input signal composed of several input planes.
In the simplest case, the output value of the layer with input size \((N, C, L)\) and output \((N, C, L_{out})\) can be precisely described as:
\[out(N_i, C_j, k) = \max_{m=0, \ldots, \text{kernel\_size} - 1} input(N_i, C_j, stride \times k + m)\]If
padding
is non-zero, then the input is implicitly padded with minimum value on both sides forpadding
number of points.dilation
is the stride between the elements within the sliding window. This link has a nice visualization of the pooling parameters.Note
When ceil_mode=True, sliding windows are allowed to go off-bounds if they start within the left padding or the input. Sliding windows that would start in the right padded region are ignored.
- Parameters
kernel_size – The size of the sliding window, must be > 0.
stride – The stride of the sliding window, must be > 0. Default value is
kernel_size
.padding – Implicit negative infinity padding to be added on both sides, must be >= 0 and <= kernel_size / 2.
dilation – The stride between elements within a sliding window, must be > 0.
return_indices – If
True
, will return the argmax along with the max values. Useful fortorch.nn.MaxUnpool1d
laterceil_mode – If
True
, will use ceil instead of floor to compute the output shape. This ensures that every element in the input tensor is covered by a sliding window.
- Shape:
Input: \((N, C, L_{in})\)
Output: \((N, C, L_{out})\), where
\[L_{out} = \left\lfloor \frac{L_{in} + 2 \times \text{padding} - \text{dilation} \times (\text{kernel_size} - 1) - 1}{\text{stride}} + 1\right\rfloor\]
For example:
import oneflow as flow import numpy as np of_maxpool1d = flow.nn.MaxPool1d(kernel_size=3, padding=1, stride=1) x = flow.Tensor(np.random.randn(1, 4, 4)) y = of_maxpool1d(x) y.shape oneflow.Size([1, 4, 4])
-
extra_repr
() → str¶ Set the extra representation of the module
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
-
class
oneflow.nn.
MaxPool2d
(kernel_size: Union[int, Tuple[int, int]], stride: Optional[Union[int, Tuple[int, int]]] = None, padding: Union[int, Tuple[int, int]] = 0, dilation: Union[int, Tuple[int, int]] = 1, return_indices: bool = False, ceil_mode: bool = False)¶ The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/stable/generated/torch.nn.MaxPool2d.html#torch.nn.MaxPool2d
Applies a 2D max pooling over an input signal composed of several input planes.
In the simplest case, the output value of the layer with input size \((N, C, H, W)\), output \((N, C, H_{out}, W_{out})\) and
kernel_size
\((kH, kW)\) can be precisely described as:\[\begin{split}\begin{aligned} out(N_i, C_j, h, w) ={} & \max_{m=0, \ldots, kH-1} \max_{n=0, \ldots, kW-1} \\ & \text{input}(N_i, C_j, \text{stride[0]} \times h + m, \text{stride[1]} \times w + n) \end{aligned}\end{split}\]If
padding
is non-zero, then the input is implicitly minimum value padded on both sides forpadding
number of points.dilation
controls the spacing between the kernel points. It is harder to describe, but this link has a nice visualization of whatdilation
does.Note
When ceil_mode=True, sliding windows are allowed to go off-bounds if they start within the left padding or the input. Sliding windows that would start in the right padded region are ignored.
- The parameters
kernel_size
,stride
,padding
,dilation
can either be: a single
int
– in which case the same value is used for the height and width dimensiona
tuple
of two ints – in which case, the first int is used for the height dimension, and the second int for the width dimension
- Parameters
kernel_size – the size of the window to take a max over
stride – the stride of the window. Default value is
kernel_size
padding – implicit minimum value padding to be added on both sides
dilation – a parameter that controls the stride of elements in the window
return_indices – if
True
, will return the max indices along with the outputs. Useful fortorch.nn.MaxUnpool2d
laterceil_mode – when True, will use ceil instead of floor to compute the output shape
- Shape:
Input: \((N, C, H_{in}, W_{in})\)
Output: \((N, C, H_{out}, W_{out})\), where
\[H_{out} = \left\lfloor\frac{H_{in} + 2 * \text{padding[0]} - \text{dilation[0]} \times (\text{kernel_size[0]} - 1) - 1}{\text{stride[0]}} + 1\right\rfloor\]\[W_{out} = \left\lfloor\frac{W_{in} + 2 * \text{padding[1]} - \text{dilation[1]} \times (\text{kernel_size[1]} - 1) - 1}{\text{stride[1]}} + 1\right\rfloor\]
For example:
import oneflow as flow import numpy as np of_maxpool2d = flow.nn.MaxPool2d(kernel_size=3, padding=1, stride=1) x = flow.Tensor(np.random.randn(1, 4, 4, 4)) y = of_maxpool2d(x) y.shape oneflow.Size([1, 4, 4, 4])
-
extra_repr
() → str¶ Set the extra representation of the module
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
- The parameters
-
class
oneflow.nn.
MaxPool3d
(kernel_size: Union[int, Tuple[int, int, int]], stride: Optional[Union[int, Tuple[int, int, int]]] = None, padding: Union[int, Tuple[int, int, int]] = 0, dilation: Union[int, Tuple[int, int, int]] = 1, return_indices: bool = False, ceil_mode: bool = False)¶ The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/stable/generated/torch.nn.MaxPool3d.html#torch.nn.MaxPool3d
Applies a 3D max pooling over an input signal composed of several input planes.
In the simplest case, the output value of the layer with input size \((N, C, D, H, W)\), output \((N, C, D_{out}, H_{out}, W_{out})\) and
kernel_size
\((kD, kH, kW)\) can be precisely described as:\[\begin{split}\begin{aligned} \text{out}(N_i, C_j, d, h, w) ={} & \max_{k=0, \ldots, kD-1} \max_{m=0, \ldots, kH-1} \max_{n=0, \ldots, kW-1} \\ & \text{input}(N_i, C_j, \text{stride[0]} \times d + k, \text{stride[1]} \times h + m, \text{stride[2]} \times w + n) \end{aligned}\end{split}\]If
padding
is non-zero, then the input is implicitly minimum value on both sides forpadding
number of points.dilation
controls the spacing between the kernel points. It is harder to describe, but this link has a nice visualization of whatdilation
does.Note
When ceil_mode=True, sliding windows are allowed to go off-bounds if they start within the left padding or the input. Sliding windows that would start in the right padded region are ignored.
The parameters
kernel_size
,stride
,padding
,dilation
can either be:a single
int
– in which case the same value is used for the depth, height and width dimensiona
tuple
of three ints – in which case, the first int is used for the depth dimension, the second int for the height dimension and the third int for the width dimension
- Parameters
kernel_size – the size of the window to take a max over
stride – the stride of the window. Default value is
kernel_size
padding – implicit minimum value padding to be added on all three sides
dilation – a parameter that controls the stride of elements in the window
return_indices – if
True
, will return the max indices along with the outputs. Useful fortorch.nn.MaxUnpool3d
laterceil_mode – when True, will use ceil instead of floor to compute the output shape
- Shape:
Input: \((N, C, D_{in}, H_{in}, W_{in})\)
Output: \((N, C, D_{out}, H_{out}, W_{out})\), where
\[D_{out} = \left\lfloor\frac{D_{in} + 2 \times \text{padding}[0] - \text{dilation}[0] \times (\text{kernel_size}[0] - 1) - 1}{\text{stride}[0]} + 1\right\rfloor\]\[H_{out} = \left\lfloor\frac{H_{in} + 2 \times \text{padding}[1] - \text{dilation}[1] \times (\text{kernel_size}[1] - 1) - 1}{\text{stride}[1]} + 1\right\rfloor\]\[W_{out} = \left\lfloor\frac{W_{in} + 2 \times \text{padding}[2] - \text{dilation}[2] \times (\text{kernel_size}[2] - 1) - 1}{\text{stride}[2]} + 1\right\rfloor\]
For example:
import oneflow as flow import numpy as np of_maxpool3d = flow.nn.MaxPool3d(kernel_size=3, padding=1, stride=1) x = flow.Tensor(np.random.randn(1, 4, 4, 4, 4)) y = of_maxpool3d(x) y.shape oneflow.Size([1, 4, 4, 4, 4])
-
extra_repr
() → str¶ Set the extra representation of the module
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
-
class
oneflow.nn.
MinMaxObserver
(quantization_formula: str = 'google', quantization_bit: int = 8, quantization_scheme: str = 'symmetric', per_layer_quantization: bool = True)¶ Compute the quantization parameters of the input tensor.
First compute the max and min values of input tensor:
\[ \begin{align}\begin{aligned}& max\_value = max(input)\\& min\_value = min(input)\end{aligned}\end{align} \]Then compute the scale and zero_point with the following equations:
if quantization_scheme == “symmetric”:
\[ \begin{align}\begin{aligned}& denom = 2^{quantization\_to\_bit - 1} - 1\\& scale = max(|max\_value|,|min\_value|) / denom\\& zero\_point = 0\end{aligned}\end{align} \]elif quantization_scheme == “affine”:
\[ \begin{align}\begin{aligned}& denom = 2^{quantization\_to\_bit} - 1\\& scale = (max\_value - min\_value) / denom\\& zero\_point = -min\_value / scale\end{aligned}\end{align} \]If per_layer_quantization is False, then the shape of scale and zero_point will be (input.shape[0],).
- Parameters
quantization_bit (int) – Quantize input to uintX / intX, X can be in range [2, 8]. Defaults to 8.
quantization_scheme (str) – “symmetric” or “affine”, quantize to signed / unsigned integer. Defaults to “symmetric”.
quantization_formula (str) – Support “google” or “cambricon”.
per_layer_quantization (bool) – True or False, means per-layer / per-channel quantization. Defaults to True.
- Returns
The scale and zero_point of input tensor.
- Return type
Tuple[oneflow.Tensor, oneflow.Tensor]
For example:
>>> import numpy as np >>> import oneflow as flow >>> weight = (np.random.random((2, 3, 4, 5)) - 0.5).astype(np.float32) >>> input_tensor = flow.tensor( ... weight, dtype=flow.float32 ... ) >>> quantization_bit = 8 >>> quantization_scheme = "symmetric" >>> quantization_formula = "google" >>> per_layer_quantization = True >>> min_max_observer = flow.nn.MinMaxObserver(quantization_formula=quantization_formula, quantization_bit=quantization_bit, ... quantization_scheme=quantization_scheme, per_layer_quantization=per_layer_quantization) >>> scale, zero_point = min_max_observer( ... input_tensor, )
-
class
oneflow.nn.
Mish
(inplace: bool = False)¶ Applies the element-wise function:
\[\text{Mish}(x) = x * \text{Tanh}(\text{Softplus}(x))\]- Shape:
Input: \((N, *)\) where * means, any number of additional dimensions
Output: \((N, *)\), same shape as the input
For example:
>>> import numpy as np >>> import oneflow as flow >>> x = np.array([1, 2, 3]).astype(np.float32) >>> input = flow.Tensor(x) >>> mish = flow.nn.Mish() >>> out = mish(input) >>> out tensor([0.8651, 1.9440, 2.9865], dtype=oneflow.float32)
-
class
oneflow.nn.
ModuleDict
(modules: Optional[Mapping[str, oneflow.nn.module.Module]] = None)¶
-
class
oneflow.nn.
ModuleList
(modules: Optional[Iterable[oneflow.nn.module.Module]] = None)¶
-
class
oneflow.nn.
MovingAverageMinMaxObserver
(training: bool = False, quantization_formula: str = 'google', stop_update_after_iters: int = 0, quantization_bit: int = 8, quantization_scheme: str = 'symmetric', momentum: float = 0)¶ Compute the quantization parameters based on the moving average of the input tensor’s min and max values.
First compute the moving_max and moving_min value of input tensor:
if quantization_scheme == “symmetric”:
\[ \begin{align}\begin{aligned}& moving\_max = moving\_max * momentum + |max(input)| * (1 - momentum)\\& moving\_min = moving\_max\end{aligned}\end{align} \]elif quantization_scheme == “affine”:
\[ \begin{align}\begin{aligned}& moving\_max = moving\_max * momentum + max(input) * (1 - momentum)\\& moving\_min = moving\_min * momentum + min(input) * (1 - momentum)\end{aligned}\end{align} \]The moving average of min and max values are initialized as the first batch of input Blob’s min and max.
Then compute the scale and zero_point with the following equations:
if quantization_scheme == “symmetric”:
\[ \begin{align}\begin{aligned}& denom = 2^{quantization\_to\_bit - 1} - 1\\& scale = moving\_max / denom\\& zero\_point = 0\end{aligned}\end{align} \]elif quantization_scheme == “affine”:
\[ \begin{align}\begin{aligned}& denom = 2^{quantization\_to\_bit} - 1\\& scale = (moving\_max - moving\_min) / denom\\& zero\_point = -moving\_min / scale\end{aligned}\end{align} \]Note
current_train_step
can be directly assigned to an optimizer(eg.SGD) step.- Parameters
training (bool) – Is the model in training state. Defaults to False.
quantization_bit (int) – Quantize input to uintX / intX, X can be in range [2, 8]. Defaults to 8.
quantization_scheme (str) – “symmetric” or “affine”, quantize to signed / unsigned integer. Defaults to “symmetric”.
quantization_formula (str) – Support “google” or “cambricon”.
momentum (float) – Smoothing parameter for exponential moving average operation. Defaults to 0.95.
- Returns
The scale and zero_point of input tensor.
- Return type
Tuple[oneflow.Tensor, oneflow.Tensor]
For example:
>>> import numpy as np >>> import oneflow as flow >>> weight = (np.random.random((2, 3, 4, 5)) - 0.5).astype(np.float32) >>> input_tensor = flow.tensor( ... weight, dtype=flow.float32 ... ) >>> current_train_step_tensor = flow.tensor( ... np.zeros((1,)).astype(np.float32), ... dtype=flow.int64, ... ) >>> momentum = 0.95 >>> quantization_bit = 8 >>> quantization_scheme = "symmetric" >>> quantization_formula = "google" >>> moving_average_min_max_observer = flow.nn.MovingAverageMinMaxObserver(training=True, quantization_formula=quantization_formula, ... stop_update_after_iters=1, quantization_bit=quantization_bit, ... quantization_scheme=quantization_scheme, momentum=momentum, ... ) >>> (scale, zero_point) = moving_average_min_max_observer( ... input_tensor, ... current_train_step_tensor, ... )
-
reset_running_stats
() → None¶
-
class
oneflow.nn.
NLLLoss
(weight: Optional[oneflow._oneflow_internal.Tensor] = None, ignore_index: int = - 100, reduction: str = 'mean')¶ The negative log likelihood loss. It is useful to train a classification problem with C classes.
The input given through a forward call is expected to contain log-probabilities of each class. input has to be a Tensor of size either \((minibatch, C)\) or \((minibatch, C, d_1, d_2, ..., d_K)\) with \(K \geq 1\) for the K-dimensional case (described later).
Obtaining log-probabilities in a neural network is easily achieved by adding a LogSoftmax layer in the last layer of your network. You may use CrossEntropyLoss instead, if you prefer not to add an extra layer.
The target that this loss expects should be a class index in the range \([0, C-1]\) where C = number of classes;
The unreduced (i.e. with
reduction
set to'none'
) loss can be described as:\[\ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \quad l_n = - w_{y_n} x_{n,y_n}, \quad w_{c} = \mathbb{1},\]where \(x\) is the input, \(y\) is the target, \(w\) is the weight, and \(N\) is the batch size. If
reduction
is not'none'
(default'mean'
), then\[\begin{split}\ell(x, y) = \begin{cases} \sum_{n=1}^N \frac{1}{N} l_n, & \text{if reduction} = \text{`mean';}\\ \sum_{n=1}^N l_n, & \text{if reduction} = \text{`sum'.} \end{cases}\end{split}\]Can also be used for higher dimension inputs, such as 2D images, by providing an input of size \((minibatch, C, d_1, d_2, ..., d_K)\) with \(K \geq 1\), where \(K\) is the number of dimensions, and a target of appropriate shape (see below). In the case of images, it computes NLL loss per-pixel.
- Parameters
reduction (string, optional) – Specifies the reduction to apply to the output:
'none'
|'mean'
|'sum'
.'none'
: no reduction will be applied,'mean'
: the weighted mean of the output is taken,'sum'
: the output will be summed. Default:'mean'
For example:
>>> import oneflow as flow >>> import numpy as np >>> input = flow.tensor( ... [[-0.1664078, -1.7256707, -0.14690138], ... [-0.21474946, 0.53737473, 0.99684894], ... [-1.135804, -0.50371903, 0.7645404]], dtype=flow.float32) >>> target = flow.tensor(np.array([0, 1, 2]), dtype=flow.int32) >>> m = flow.nn.NLLLoss(reduction="none") >>> out = m(input, target) >>> out tensor([ 0.1664, -0.5374, -0.7645], dtype=oneflow.float32) >>> m = flow.nn.NLLLoss(reduction="sum") >>> out = m(input, target) >>> out tensor(-1.1355, dtype=oneflow.float32) >>> m = flow.nn.NLLLoss(reduction="mean") >>> out = m(input, target) >>> out tensor(-0.3785, dtype=oneflow.float32)
-
class
oneflow.nn.
OFRecordBytesDecoder
(blob_name: str, name: Optional[str] = None)¶ This operator reads an tensor as bytes. The output might need
further decoding process like cv2.imdecode() for images and decode(“utf-8”)
for characters,depending on the downstream task.
- Parameters
blob_name – The name of the target feature in OFRecord.
name – The name for this component in the graph.
input – the Tensor which might be provided by an OFRecordReader.
- Returns
The result Tensor encoded with bytes.
For example:
>>> import numpy as np >>> import oneflow as flow >>> def example(): ... batch_size = 16 ... record_reader = flow.nn.OFRecordReader( ... "dataset/", ... batch_size=batch_size, ... part_name_suffix_length=5, ... ) ... val_record = record_reader() ... bytesdecoder_img = flow.nn.OFRecordBytesDecoder("encoded") ... image_bytes_batch = bytesdecoder_img(val_record) ... image_bytes = image_bytes_batch.numpy()[0] ... return image_bytes ... example() array([255 216 255 ... 79 255 217], dtype=uint8)
-
class
oneflow.nn.
OFRecordImageDecoder
(blob_name: str, color_space: str = 'BGR')¶
-
class
oneflow.nn.
OFRecordImageDecoderRandomCrop
(blob_name: str, color_space: str = 'BGR', num_attempts: int = 10, random_seed: Optional[int] = None, random_area: Sequence[float] = [0.08, 1.0], random_aspect_ratio: Sequence[float] = [0.75, 1.333333])¶
-
class
oneflow.nn.
OFRecordRawDecoder
(blob_name: str, shape: Sequence[int], dtype: oneflow._oneflow_internal.dtype, dim1_varying_length: bool = False, truncate: bool = False, auto_zero_padding: bool = False, name: Optional[str] = None)¶
-
class
oneflow.nn.
OFRecordReader
(ofrecord_dir: str, batch_size: int = 1, data_part_num: int = 1, part_name_prefix: str = 'part-', part_name_suffix_length: int = - 1, random_shuffle: bool = False, shuffle_buffer_size: int = 1024, shuffle_after_epoch: bool = False, random_seed: int = - 1, device: Optional[Union[oneflow._oneflow_internal.device, str]] = None, placement: Optional[oneflow._oneflow_internal.placement] = None, sbp: Optional[Union[oneflow._oneflow_internal.sbp.sbp, List[oneflow._oneflow_internal.sbp.sbp]]] = None, name: Optional[str] = None)¶
-
class
oneflow.nn.
PReLU
(num_parameters: int = 1, init: float = 0.25, device=None, dtype=None)¶ Applies the element-wise function:
\[PReLU(x) = \max(0,x) + a * \min(0,x)\]Here \(a\) is a learnable parameter. When called without arguments, nn.PReLU() uses a single parameter \(a\) across all input channels. If called with nn.PReLU(nChannels), a separate \(a\) is used for each input channel.
Note
weight decay should not be used when learning \(a\) for good performance.
Note
Channel dim is the 2nd dim of input. When input has dims < 2, then there is no channel dim and the number of channels = 1.
- Parameters
num_parameters (int) – number of \(a\) to learn. Although it takes an int as input, there is only two values are legitimate: 1, or the number of channels at input. Default: 1
init (float) – the initial value of \(a\). Default: 0.25
- Shape:
Input: \((N, *)\) where * means, any number of additional dimensions
Output: \((N, *)\), same shape as the input
- Attr:
weight (Tensor): the learnable weights of shape (
num_parameters
).
>>> import numpy as np >>> import oneflow as flow >>> m = flow.nn.PReLU() >>> input = flow.tensor(np.asarray([[[[1, -2], [3, 4]]]]), dtype=flow.float32) >>> print(m(input).numpy()) [[[[ 1. -0.5] [ 3. 4. ]]]]
-
extra_repr
() → str¶ Set the extra representation of the module
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
-
class
oneflow.nn.
Parameter
¶
-
class
oneflow.nn.
ParameterDict
(parameters=None)¶
-
class
oneflow.nn.
ParameterList
(parameters=None)¶
-
oneflow.nn.
PixelShuffle
¶ alias of
oneflow.nn.modules.pixelshuffle.PixelShufflev2
-
class
oneflow.nn.
Quantization
(quantization_formula: str = 'google', quantization_bit: int = 8, quantization_scheme: str = 'symmetric')¶ Simulate the quantize operation in inference time.
The output will be computed as:
if quantization_scheme == “symmetric”:
\[ \begin{align}\begin{aligned}& quant\_max = 2^{quantization\_to\_bit - 1} - 1\\& quant\_min = -quant\_max\\& clamp(round(x / scale), quant\_min, quant\_max)\end{aligned}\end{align} \]elif quantization_scheme == “affine”:
\[ \begin{align}\begin{aligned}& quant\_max = 2^{quantization\_to\_bit} - 1\\& quant\_min = 0\\& (clamp(round(x / scale + zero\_point), quant\_min, quant\_max) - zero\_point)\end{aligned}\end{align} \]- Parameters
quantization_bit (int) – Quantize input to uintX / intX, X can be in range [2, 8]. Defaults to 8.
quantization_scheme (str) – “symmetric” or “affine”, quantize to signed / unsigned integer. Defaults to “symmetric”.
quantization_formula (str) – Support “google” or “cambricon”.
- Returns
Input tensor after quantize operation.
- Return type
For example:
>>> import numpy as np >>> import oneflow as flow >>> weight = (np.random.random((2, 3, 4, 5)) - 0.5).astype(np.float32) >>> input_tensor = flow.tensor( ... weight, dtype=flow.float32 ... ) >>> quantization_bit = 8 >>> quantization_scheme = "symmetric" >>> quantization_formula = "google" >>> per_layer_quantization = True >>> min_max_observer = flow.nn.MinMaxObserver(quantization_formula=quantization_formula, quantization_bit=quantization_bit, ... quantization_scheme=quantization_scheme, per_layer_quantization=per_layer_quantization) >>> quantization = flow.nn.Quantization(quantization_formula=quantization_formula, quantization_bit=quantization_bit, ... quantization_scheme=quantization_scheme) >>> scale, zero_point = min_max_observer( ... input_tensor, ... ) >>> output_tensor = quantization( ... input_tensor, ... scale, ... zero_point, ... )
-
class
oneflow.nn.
ReLU
(inplace: bool = False)¶ Applies the rectified linear unit function element-wise:
\(\text{ReLU}(x) = (x)^+ = \max(0, x)\)
- Parameters
inplace – can optionally do the operation in-place. Default:
False
- Shape:
Input: \((N, *)\) where * means, any number of additional dimensions
Output: \((N, *)\), same shape as the input
For example:
>>> import oneflow as flow >>> import numpy as np >>> relu = flow.nn.ReLU() >>> ndarr = np.asarray([1, -2, 3]) >>> x = flow.Tensor(ndarr) >>> relu(x) tensor([1., 0., 3.], dtype=oneflow.float32)
-
extra_repr
()¶ Set the extra representation of the module
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
-
class
oneflow.nn.
ReLU6
(inplace: bool = False)¶ Applies the element-wise function:
\[\begin{split}\text{Relu6}(x) = \begin{cases} 6 & \text{ if } x > 6 \\ 0 & \text{ if } x < 0 \\ x & \text{ otherwise } \\ \end{cases}\end{split}\]- Parameters
inplace – can optionally do the operation in-place. Default:
False
- Shape:
Input: \((N, *)\) where * means, any number of additional dimensions
Output: \((N, *)\), same shape as the input
For example:
>>> import numpy as np >>> import oneflow as flow >>> x = np.array([-0.5, 0, 0.5]).astype(np.float32) >>> input = flow.Tensor(x) >>> relu6 = flow.nn.ReLU6() >>> out = relu6(input) >>> out tensor([0.0000, 0.0000, 0.5000], dtype=oneflow.float32)
-
extra_repr
()¶ Set the extra representation of the module
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
-
class
oneflow.nn.
ReflectionPad2d
(padding: Union[int, Tuple[int, int, int, int]])¶ The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/stable/generated/torch.nn.ReflectionPad2d.html
This operator pads the input tensor using the reflection of the input boundary.
- Parameters
padding (Union[int,tuple]) – The size or bundary of padding, if is int uses the same padding in all dimension; if 4-dims tuple, uses \((\text{padding}_{\text{left}}, \text{padding}_{\text{right}}, \text{padding}_{\text{top}}, \text{padding}_{\text{bottom}} )\)
- Returns
Returns a new tensor which is result of the reflection padding of the input tensor.
- Return type
- Shape:
Input: \((N, C, H_{\text{in}}, W_{\text{in}})\)
Output: \((N, C, H_{\text{out}}, W_{\text{out}})\) where
\(H_{\text{out}} = H_{\text{in}} + \text{padding}_{\text{top}} + \text{padding}_{\text{bottom}}\)
\(W_{\text{out}} = W_{\text{in}} + \text{padding}_{\text{left}} + \text{padding}_{\text{right}}\)
For example:
>>> import oneflow as flow >>> import numpy as np >>> input = flow.tensor(np.arange(18).reshape((1, 2, 3, 3)).astype(np.float32)) >>> m = flow.nn.ReflectionPad2d((2, 2, 1, 1)) >>> out = m(input) >>> out tensor([[[[ 5., 4., 3., 4., 5., 4., 3.], [ 2., 1., 0., 1., 2., 1., 0.], [ 5., 4., 3., 4., 5., 4., 3.], [ 8., 7., 6., 7., 8., 7., 6.], [ 5., 4., 3., 4., 5., 4., 3.]], [[14., 13., 12., 13., 14., 13., 12.], [11., 10., 9., 10., 11., 10., 9.], [14., 13., 12., 13., 14., 13., 12.], [17., 16., 15., 16., 17., 16., 15.], [14., 13., 12., 13., 14., 13., 12.]]]], dtype=oneflow.float32)
-
extra_repr
() → str¶ Set the extra representation of the module
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
-
class
oneflow.nn.
ReplicationPad2d
(padding: Union[int, Tuple[int, int, int, int]])¶ The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/stable/generated/torch.nn.ReplicationPad2d.html?highlight=replicationpad2d#torch.nn.ReplicationPad2d
Pads the input tensor using the replication of the input boundary.
- Parameters
padding (Union[int, tuple, list]) – the size of the padding. If is int, uses the same padding in all boundaries. If a 4-tuple, uses (\(\mathrm{padding_{left}}\), \(\mathrm{padding_{right}}\), \(\mathrm{padding_{top}}\), \(\mathrm{padding_{bottom}}\))
- Shape:
Input: \((N, C, H_{in}, W_{in})\)
Output: \((N, C, H_{out}, W_{out})\) where
\(H_{out} = H_{in} + \mathrm{padding_{top}} + \mathrm{padding_{bottom}}\)
\(W_{out} = W_{in} + \mathrm{padding_{left}} + \mathrm{padding_{right}}\)
For example:
>>> import oneflow as flow >>> import numpy as np >>> m = flow.nn.ReplicationPad2d((2, 2, 1, 1)) >>> input = flow.tensor(np.arange(18).reshape((1, 2, 3, 3)).astype(np.float32)) >>> input_int = flow.tensor(np.arange(18).reshape((1, 2, 3, 3)).astype(np.int32)) >>> output = m(input) >>> output.shape oneflow.Size([1, 2, 5, 7]) >>> output tensor([[[[ 0., 0., 0., 1., 2., 2., 2.], [ 0., 0., 0., 1., 2., 2., 2.], [ 3., 3., 3., 4., 5., 5., 5.], [ 6., 6., 6., 7., 8., 8., 8.], [ 6., 6., 6., 7., 8., 8., 8.]], [[ 9., 9., 9., 10., 11., 11., 11.], [ 9., 9., 9., 10., 11., 11., 11.], [12., 12., 12., 13., 14., 14., 14.], [15., 15., 15., 16., 17., 17., 17.], [15., 15., 15., 16., 17., 17., 17.]]]], dtype=oneflow.float32)
-
extra_repr
() → str¶ Set the extra representation of the module
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
-
class
oneflow.nn.
SELU
(inplace: bool = False)¶ Applies the element-wise function:
The formula is:
\[\text{SELU}(x) = \text{scale} * (\max(0,x) + \min(0, \alpha * (\exp(x) - 1)))\]with \(\alpha = 1.6732632423543772848170429916717\) and
\(\text{scale} = 1.0507009873554804934193349852946\).
Warning
When using
kaiming_normal
orkaiming_normal_
for initialisation,nonlinearity='linear'
should be used instead ofnonlinearity='selu'
in order to get Self-Normalizing Neural Networks. Seetorch.nn.init.calculate_gain()
for more information.More details can be found in the paper Self-Normalizing Neural Networks.
- Shape:
Input: \((N, *)\) where * means, any number of additional dimensions
Output: \((N, *)\), same shape as the input
For example:
>>> import numpy as np >>> import oneflow as flow >>> x = np.array([1, 2, 3]).astype(np.float32) >>> input = flow.Tensor(x) >>> selu = flow.nn.SELU() >>> out = selu(input) >>> out tensor([1.0507, 2.1014, 3.1521], dtype=oneflow.float32)
-
class
oneflow.nn.
Sequential
(*args: Any)¶ A sequential container. Modules will be added to it in the order they are passed in the constructor. Alternatively, an ordered dict of modules can also be passed in.
To make it easier to understand, here is a small example:
>>> import oneflow.nn as nn >>> from collections import OrderedDict >>> nn.Sequential(nn.Conv2d(1,20,5), nn.ReLU(), nn.Conv2d(20,64,5), nn.ReLU()) Sequential( (0): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1)) (1): ReLU() (2): Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1)) (3): ReLU() ) >>> nn.Sequential(OrderedDict([ ... ('conv1', nn.Conv2d(1,20,5)), ... ('relu1', nn.ReLU()), ... ('conv2', nn.Conv2d(20,64,5)), ... ('relu2', nn.ReLU()) ... ])) Sequential( (conv1): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1)) (relu1): ReLU() (conv2): Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1)) (relu2): ReLU() )
-
class
oneflow.nn.
SiLU
(inplace: bool = False)¶ SiLU(Swish) activation:
\[\text{SiLU}(x) = x * sigmoid(x)\]Note
See Gaussian Error Linear Units (GELUs) where the SiLU (Sigmoid Linear Unit) was originally coined, and see Sigmoid-Weighted Linear Units for Neural Network Function Approximation in Reinforcement Learning and Swish: a Self-Gated Activation Function where the SiLU was experimented with later.
- Shape:
Input: \((N, *)\) where * means, any number of additional dimensions
Output: \((N, *)\), same shape as the input
For example:
>>> import numpy as np >>> import oneflow as flow >>> x = np.array([1, 2, 3]).astype(np.float32) >>> input = flow.Tensor(x) >>> silu = flow.nn.SiLU() >>> out = silu(input) >>> out tensor([0.7311, 1.7616, 2.8577], dtype=oneflow.float32)
-
class
oneflow.nn.
Sigmoid
¶ Applies the element-wise function:
\[\text{Sigmoid}(x) = \sigma(x) = \frac{1}{1 + \exp(-x)}\]- Shape:
Input: \((N, *)\) where * means, any number of additional dimensions
Output: \((N, *)\), same shape as the input
For example:
>>> import numpy as np >>> import oneflow as flow >>> x = flow.Tensor(np.array([0.81733328, 0.43621480, 0.10351428])) >>> m = flow.nn.Sigmoid() >>> out = m(x) >>> out tensor([0.6937, 0.6074, 0.5259], dtype=oneflow.float32)
-
class
oneflow.nn.
SmoothL1Loss
(reduction: str = 'mean', beta: float = 1.0)¶ Creates a criterion that uses a squared term if the absolute element-wise error falls below beta and an L1 term otherwise. The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/stable/generated/torch.nn.SmoothL1Loss.html
It is less sensitive to outliers than
torch.nn.MSELoss
and in some cases prevents exploding gradients (e.g. see the paper Fast R-CNN by Ross Girshick)..For a batch of size \(N\), the unreduced loss can be described as:
\[\ell(x, y) = L = \{l_1, ..., l_N\}^T\]with
\[\begin{split}l_n = \begin{cases} 0.5 (x_n - y_n)^2 / beta, & \text{if } |x_n - y_n| < beta \\ |x_n - y_n| - 0.5 * beta, & \text{otherwise } \end{cases}\end{split}\]If reduction is not none, then:
\[\begin{split}\ell(x, y) = \begin{cases} \operatorname{mean}(L), & \text{if reduction} = \text{`mean';}\\ \operatorname{sum}(L), & \text{if reduction} = \text{`sum'.} \end{cases}\end{split}\]Note
Smooth L1 loss can be seen as exactly
L1Loss
, but with the \(|x - y| < beta\) portion replaced with a quadratic function such that its slope is 1 at \(|x - y| = beta\). The quadratic segment smooths the L1 loss near \(|x - y| = 0\).Note
Smooth L1 loss is closely related to
HuberLoss
, being equivalent to \(huber(x, y) / beta\) (note that Smooth L1’s beta hyper-parameter is also known as delta for Huber). This leads to the following differences:As beta -> 0, Smooth L1 loss converges to
L1Loss
, whileHuberLoss
converges to a constant 0 loss.As beta -> \(+\infty\), Smooth L1 loss converges to a constant 0 loss, while
HuberLoss
converges toMSELoss
.For Smooth L1 loss, as beta varies, the L1 segment of the loss has a constant slope of 1. For
HuberLoss
, the slope of the L1 segment is beta.
- Parameters
size_average (bool, optional) – Deprecated (see
reduction
). By default, the losses are averaged over each loss element in the batch. Note that for some losses, there are multiple elements per sample. If the fieldsize_average
is set toFalse
, the losses are instead summed for each minibatch. Ignored whenreduce
isFalse
. Default:True
reduce (bool, optional) – Deprecated (see
reduction
). By default, the losses are averaged or summed over observations for each minibatch depending onsize_average
. Whenreduce
isFalse
, returns a loss per batch element instead and ignoressize_average
. Default:True
reduction (string, optional) – Specifies the reduction to apply to the output:
'none'
|'mean'
|'sum'
.'none'
: no reduction will be applied,'mean'
: the sum of the output will be divided by the number of elements in the output,'sum'
: the output will be summed. Note:size_average
andreduce
are in the process of being deprecated, and in the meantime, specifying either of those two args will overridereduction
. Default:'mean'
beta (float, optional) – Specifies the threshold at which to change between L1 and L2 loss. The value must be non-negative. Default: 1.0
- Shape:
Input: \((N, *)\) where \(*\) means any number of additional dimensions
Target: \((N, *)\); same shape as the input
Output: scalar. If
reduction
is'none'
, then \((N, *)\); same shape as the input
For example:
>>> import oneflow as flow >>> import numpy as np >>> x = flow.tensor(np.array([0.1, 0.4, 0.3, 0.5, 0.9]).astype(np.float32), dtype=flow.float32) >>> y = flow.tensor(np.array([0.3, 0.9, 2.5, 0.4, 0.3]).astype(np.float32), dtype=flow.float32) >>> m = flow.nn.SmoothL1Loss(reduction="none") >>> out = m(x, y) >>> out tensor([0.0200, 0.1250, 1.7000, 0.0050, 0.1800], dtype=oneflow.float32) >>> m = flow.nn.SmoothL1Loss(reduction="mean") >>> out = m(x, y) >>> out tensor(0.4060, dtype=oneflow.float32) >>> m = flow.nn.SmoothL1Loss(reduction="sum") >>> out = m(x, y) >>> out tensor(2.0300, dtype=oneflow.float32)
-
class
oneflow.nn.
Softmax
(dim: Optional[int] = None)¶ Applies the Softmax function to an n-dimensional input Tensor rescaling them so that the elements of the n-dimensional output Tensor lie in the range [0,1] and sum to 1.
Softmax is defined as:
\[\text{Softmax}(x_{i}) = \frac{\exp(x_i)}{\sum_j \exp(x_j)}\]When the input Tensor is a sparse tensor then the unspecifed values are treated as
-inf
.- Shape:
Input: \((*)\) where * means, any number of additional dimensions
Output: \((*)\), same shape as the input
- Returns
a Tensor of the same dimension and shape as the input with values in the range [0, 1]
- Parameters
dim (int) – A dimension along which Softmax will be computed (so every slice along dim will sum to 1).
For example:
>>> import numpy as np >>> import oneflow as flow >>> m = flow.nn.Softmax(dim = 2) >>> x = flow.Tensor( ... np.array( ... [[[-0.46716809, 0.40112534, 0.61984003], ... [-1.31244969, -0.42528763, 1.47953856]]] ... ) ... ) >>> out = m(x) >>> out tensor([[[0.1575, 0.3754, 0.4671], [0.0507, 0.1230, 0.8263]]], dtype=oneflow.float32)
-
extra_repr
()¶ Set the extra representation of the module
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
-
class
oneflow.nn.
Softplus
(beta: int = 1, threshold: int = 20)¶ Applies the element-wise function:
\[\text{Softplus}(x) = \frac{1}{\beta} * \log(1 + \exp(\beta * x))\]SoftPlus is a smooth approximation to the ReLU function and can be used to constrain the output of a machine to always be positive.
For numerical stability the implementation reverts to the linear function when \(input \times \beta > threshold\).
- Parameters
beta – the \(\beta\) value for the Softplus formulation. Default: 1
threshold – values above this revert to a linear function. Default: 20
- Shape:
Input: \((N, *)\) where * means, any number of additional dimensions
Output: \((N, *)\), same shape as the input
For example:
>>> import numpy as np >>> import oneflow as flow >>> x = np.array([-0.5, 0, 0.5]).astype(np.float32) >>> input = flow.Tensor(x) >>> softplus = flow.nn.Softplus() >>> out = softplus(input) >>> out tensor([0.4741, 0.6931, 0.9741], dtype=oneflow.float32)
-
extra_repr
()¶ Set the extra representation of the module
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
-
class
oneflow.nn.
Softsign
(inplace: bool = False)¶ The SoftSign activation.
The formula is:
\[SoftSign(x) = \frac{x}{1 + |x|}\]- Shape:
Input: \((N, *)\) where * means, any number of additional dimensions
Output: \((N, *)\), same shape as the input
For example:
>>> import numpy as np >>> import oneflow as flow >>> x = np.array([1, 2, 3]).astype(np.float32) >>> input = flow.Tensor(x) >>> softsign = flow.nn.Softsign() >>> out = softsign(input) >>> out tensor([0.5000, 0.6667, 0.7500], dtype=oneflow.float32)
-
class
oneflow.nn.
Tanh
¶ This operator computes the hyperbolic tangent value of Tensor.
The equation is:
\[out = \frac{e^x-e^{-x}}{e^x+e^{-x}}\]- Parameters
input (oneflow.Tensor) – A Tensor
- Returns
The result Tensor
- Return type
For example:
>>> import numpy as np >>> import oneflow as flow >>> x = np.array([-1, 0, 1]).astype(np.float32) >>> input = flow.Tensor(x) >>> tanh = flow.nn.Tanh() >>> out = tanh(input) >>> out tensor([-0.7616, 0.0000, 0.7616], dtype=oneflow.float32)
-
class
oneflow.nn.
TripletMarginLoss
(margin: float = 1.0, p: float = 2.0, eps: float = 1e-06, swap: bool = False, size_average=None, reduce=None, reduction: str = 'mean')¶ Creates a criterion that measures the triplet loss given an input tensors \(x1\), \(x2\), \(x3\) and a margin with a value greater than \(0\). This is used for measuring a relative similarity between samples. A triplet is composed by a, p and n (i.e., anchor, positive examples and negative examples respectively). The shapes of all input tensors should be \((N, D)\).
The distance swap is described in detail in the paper Learning shallow convolutional feature descriptors with triplet losses by V. Balntas, E. Riba et al.
The loss function for each sample in the mini-batch is:
\[L(a, p, n) = \max \{d(a_i, p_i) - d(a_i, n_i) + {\rm margin}, 0\}\]where
\[d(x_i, y_i) = \left\lVert {\bf x}_i - {\bf y}_i \right\rVert_p\]- Parameters
margin (float, optional) – Default: \(1\).
p (float, optional) – The norm degree for pairwise distance. Default: \(2.0\).
swap (bool, optional) – The distance swap is described in detail in the paper Learning shallow convolutional feature descriptors with triplet losses by V. Balntas, E. Riba et al. Default:
False
.reduction (string, optional) – Specifies the reduction to apply to the output:
'none'
|'mean'
|'sum'
.'none'
: no reduction will be applied,'mean'
: the sum of the output will be divided by the number of elements in the output,'sum'
: the output will be summed. Note:size_average
andreduce
are in the process of being deprecated, and in the meantime, specifying either of those two args will overridereduction
. Default:'mean'
- Shape:
Input: \((N, D)\) where \(D\) is the vector dimension.
Output: A Tensor of shape \((N)\) if
reduction
is'none'
, or a scalar otherwise.
For example:
>>> import oneflow as flow >>> import numpy as np >>> triplet_loss = flow.nn.TripletMarginLoss(margin=1.0, p=2) >>> anchor = np.array([[1, -1, 1],[-1, 1, -1], [1, 1, 1]]) >>> positive = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> negative = np.array([[2, 2, 2], [2, 2, 2], [2, 2, 2]]) >>> output = triplet_loss(flow.Tensor(anchor), flow.Tensor(positive), flow.Tensor(negative)) >>> output tensor(6.2971, dtype=oneflow.float32)
-
class
oneflow.nn.
Upsample
(size: Optional[Union[int, Tuple[int, …]]] = None, scale_factor: Optional[Union[float, Tuple[float, …]]] = None, mode: str = 'nearest', align_corners: Optional[bool] = None)¶ The interface is consistent with PyTorch.
The documentation is referenced from: https://pytorch.org/docs/1.9.0/_modules/torch/nn/modules/upsampling.html#Upsample
Upsamples a given multi-channel 1D (temporal), 2D (spatial) or 3D (volumetric) data.
The input data is assumed to be of the form minibatch x channels x [optional depth] x [optional height] x width. Hence, for spatial inputs, we expect a 4D Tensor and for volumetric inputs, we expect a 5D Tensor.
The algorithms available for upsampling are nearest neighbor and linear, bilinear, bicubic and trilinear for 3D, 4D and 5D input Tensor, respectively.
One can either give a
scale_factor
or the target outputsize
to calculate the output size. (You cannot give both, as it is ambiguous)- Parameters
size (int or Tuple[int] or Tuple[int, int] or Tuple[int, int, int], optional) – output spatial sizes
scale_factor (float or Tuple[float] or Tuple[float, float] or Tuple[float, float, float], optional) – multiplier for spatial size. Has to match input size if it is a tuple.
mode (str, optional) – the upsampling algorithm: one of
'nearest'
,'linear'
,'bilinear'
,'bicubic'
and'trilinear'
. Default:'nearest'
align_corners (bool, optional) – if
True
, the corner pixels of the input and output tensors are aligned, and thus preserving the values at those pixels. This only has effect whenmode
is'linear'
,'bilinear'
, or'trilinear'
. Default:False
- Shape:
Input: \((N, C, W_{in})\), \((N, C, H_{in}, W_{in})\) or \((N, C, D_{in}, H_{in}, W_{in})\)
Output: \((N, C, W_{out})\), \((N, C, H_{out}, W_{out})\) or \((N, C, D_{out}, H_{out}, W_{out})\), where
\[D_{out} = \left\lfloor D_{in} \times \text{scale_factor} \right\rfloor\]\[H_{out} = \left\lfloor H_{in} \times \text{scale_factor} \right\rfloor\]\[W_{out} = \left\lfloor W_{in} \times \text{scale_factor} \right\rfloor\]Warning
With
align_corners = True
, the linearly interpolating modes (linear, bilinear, bicubic, and trilinear) don’t proportionally align the output and input pixels, and thus the output values can depend on the input size. This was the default behavior for these modes up to version 0.3.1. Since then, the default behavior isalign_corners = False
. See below for concrete examples on how this affects the outputs.Note
If you want downsampling/general resizing, you should use
interpolate()
.For example:
>>> import numpy as np >>> import oneflow as flow >>> input = flow.tensor(np.arange(1, 5).reshape((1, 1, 2, 2)), dtype=flow.float32) >>> input = input.to("cuda") >>> m = flow.nn.Upsample(scale_factor=2.0, mode="nearest") >>> output = m(input) >>> output tensor([[[[1., 1., 2., 2.], ... [3., 3., 4., 4.]]]], device='cuda:0', dtype=oneflow.float32)
-
extra_repr
() → str¶ Set the extra representation of the module
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
-
class
oneflow.nn.
UpsamplingBilinear2d
(size: Optional[Tuple[int, int]] = None, scale_factor: Optional[Tuple[float, float]] = None)¶ Applies a 2D bilinear upsampling to an input signal composed of several input channels.
To specify the scale, it takes either the
size
or thescale_factor
as it’s constructor argument.When
size
is given, it is the output size of the image (h, w).- Parameters
size (int or Tuple[int, int], optional) – output spatial sizes
scale_factor (float or Tuple[float, float], optional) – multiplier for spatial size.
Warning
This class is deprecated in favor of
interpolate()
. It is equivalent tonn.functional.interpolate(..., mode='bilinear', align_corners=True)
.- Shape:
Input: \((N, C, H_{in}, W_{in})\)
Output: \((N, C, H_{out}, W_{out})\) where
\[H_{out} = \left\lfloor H_{in} \times \text{scale_factor} \right\rfloor\]\[W_{out} = \left\lfloor W_{in} \times \text{scale_factor} \right\rfloor\]For example:
>>> import numpy as np >>> import oneflow as flow >>> input = flow.tensor(np.arange(1, 5).reshape((1, 1, 2, 2)), dtype=flow.float32) >>> input = input.to("cuda") >>> m = flow.nn.UpsamplingBilinear2d(scale_factor=2.0) >>> output = m(input) >>> output tensor([[[[1.0000, 1.3333, 1.6667, 2.0000], ... [3.0000, 3.3333, 3.6667, 4.0000]]]], device='cuda:0', dtype=oneflow.float32)
-
class
oneflow.nn.
UpsamplingNearest2d
(size: Optional[Tuple[int, int]] = None, scale_factor: Optional[Tuple[float, float]] = None)¶ Applies a 2D nearest neighbor upsampling to an input signal composed of several input channels.
To specify the scale, it takes either the
size
or thescale_factor
as it’s constructor argument.When
size
is given, it is the output size of the image (h, w).- Parameters
size (int or Tuple[int, int], optional) – output spatial sizes
scale_factor (float or Tuple[float, float], optional) – multiplier for spatial size.
Warning
This class is deprecated in favor of
interpolate()
.- Shape:
Input: \((N, C, H_{in}, W_{in})\)
Output: \((N, C, H_{out}, W_{out})\) where
\[H_{out} = \left\lfloor H_{in} \times \text{scale_factor} \right\rfloor\]\[W_{out} = \left\lfloor W_{in} \times \text{scale_factor} \right\rfloor\]For example:
>>> import numpy as np >>> import oneflow as flow >>> input = flow.tensor(np.arange(1, 5).reshape((1, 1, 2, 2)), dtype=flow.float32) >>> input = input.to("cuda") >>> m = flow.nn.UpsamplingNearest2d(scale_factor=2.0) >>> output = m(input) >>> output tensor([[[[1., 1., 2., 2.], ... [3., 3., 4., 4.]]]], device='cuda:0', dtype=oneflow.float32)
-
class
oneflow.nn.
ZeroPad2d
(padding: Union[int, tuple, list])¶ The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/stable/generated/torch.nn.ZeroPad2d.html
Pads the input tensor boundaries with zero. User can set the amount of padding by setting the parameter paddings.
- Parameters
padding (Union[int, tuple]) – the size of the padding. If is int, uses the same padding in all boundaries. If a 4-tuple, uses (\(\mathrm{padding_{left}}\), \(\mathrm{padding_{right}}\), \(\mathrm{padding_{top}}\), \(\mathrm{padding_{bottom}}\))
- Shape:
Input: \((N, C, H_{in}, W_{in})\)
Output: \((N, C, H_{out}, W_{out})\) where
\(H_{out} = H_{in} + \mathrm{padding_{top}} + \mathrm{padding_{bottom}}\)
\(W_{out} = W_{in} + \mathrm{padding_{left}} + \mathrm{padding_{right}}\)
For example:
>>> import oneflow as flow >>> import numpy as np >>> m1 = flow.nn.ZeroPad2d(2) >>> m2 = flow.nn.ZeroPad2d((1,2,2,0)) >>> input = flow.tensor(np.arange(18).reshape((1, 2, 3, 3)).astype(np.float32)) >>> output = m1(input) >>> output.shape oneflow.Size([1, 2, 7, 7]) >>> output tensor([[[[ 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 1., 2., 0., 0.], [ 0., 0., 3., 4., 5., 0., 0.], [ 0., 0., 6., 7., 8., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0.]], [[ 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 9., 10., 11., 0., 0.], [ 0., 0., 12., 13., 14., 0., 0.], [ 0., 0., 15., 16., 17., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0.]]]], dtype=oneflow.float32) >>> output = m2(input) >>> output tensor([[[[ 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0.], [ 0., 0., 1., 2., 0., 0.], [ 0., 3., 4., 5., 0., 0.], [ 0., 6., 7., 8., 0., 0.]], [[ 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0.], [ 0., 9., 10., 11., 0., 0.], [ 0., 12., 13., 14., 0., 0.], [ 0., 15., 16., 17., 0., 0.]]]], dtype=oneflow.float32)
-
oneflow.nn.parallel.
DistributedDataParallel
(module: oneflow.nn.module.Module, *, broadcast_buffers: bool = True)¶
-
oneflow.nn.utils.
clip_grad_norm_
(parameters: Union[oneflow._oneflow_internal.Tensor, Iterable[oneflow._oneflow_internal.Tensor]], max_norm: float, norm_type: float = 2.0, error_if_nonfinite: bool = True) → oneflow._oneflow_internal.Tensor¶ Clips gradient norm of an iterable of parameters. The norm is computed over all gradients together, as if they were concatenated into a single vector.
- Parameters
parameters (Iterable[Tensor] or Tensor) – an iterable of Tensors or a single Tensor that will have gradients normalized
max_norm (float or int) – max norm of the gradients
norm_type (float or int) – type of the used p-norm. Can be
'inf'
for infinity norm.error_if_nonfinite (bool) – if True, an error is thrown if the total norm of the gradients from :attr:
parameters
isnan
,inf
, or-inf
. Default: True
- Returns
Parameters after cliping gradient norm Total norm of the parameters (viewed as a single vector).
For example:
>>> import oneflow as flow >>> import numpy as np >>> x1 = flow.tensor(np.array([[2, 3, 4], [1.5, 2.6, 3.7]]).astype(np.float32), requires_grad=True) >>> m1 = flow.nn.ReLU() >>> out1 = m1(x1) >>> out1 = out1.sum() >>> out1.backward() >>> norm1 = flow.nn.utils.clip_grad_norm_(x1, 0.6, 1.0) >>> norm1 tensor(6., dtype=oneflow.float32) >>> x1.grad tensor([[0.1000, 0.1000, 0.1000], [0.1000, 0.1000, 0.1000]], dtype=oneflow.float32) >>> x2 = flow.tensor(np.array([[-2, -3, -4], [2.5, 0, 3.2]]).astype(np.float32), requires_grad=True) >>> out2 = flow.atan(x2) >>> out2 = out2.sum() >>> out2.backward() >>> norm2 = flow.nn.utils.clip_grad_norm_(x2, 0.5) >>> norm2 tensor(1.0394, dtype=oneflow.float32) >>> x2.grad tensor([[0.0962, 0.0481, 0.0283], [0.0663, 0.4810, 0.0428]], dtype=oneflow.float32)
-
oneflow.nn.utils.
weight_norm
(module: T_module, name: str = 'weight', dim: int = 0) → T_module¶ Applies weight normalization to a parameter in the given module.
\[\mathbf{w}=g \frac{\mathbf{v}}{\|\mathbf{v}\|}\]Weight normalization is a reparameterization that decouples the magnitude of a weight tensor from its direction. This replaces the parameter specified by
name
(e.g.'weight'
) with two parameters: one specifying the magnitude (e.g.'weight_g'
) and one specifying the direction (e.g.'weight_v'
). Weight normalization is implemented via a hook that recomputes the weight tensor from the magnitude and direction before everyforward()
call.By default, with
dim=0
, the norm is computed independently per output channel/plane. To compute a norm over the entire weight tensor, usedim=None
.See https://arxiv.org/abs/1602.07868
This document description is refereced to the Pytorch document. https://pytorch.org/docs/stable/generated/torch.nn.utils.weight_norm.html
- Parameters
module (Module) – containing module
name (str, optional) – name of weight parameter
dim (int, optional) – dimension over which to compute the norm
- Returns
The original module with the weight norm hook
For example:
>>> import oneflow as flow >>> m = flow.nn.utils.weight_norm(flow.nn.Linear(20, 40), name='weight') >>> m Linear(in_features=20, out_features=40, bias=True) >>> m.weight_g.size() oneflow.Size([40, 1]) >>> m.weight_v.size() oneflow.Size([40, 20])
-
oneflow.nn.utils.
remove_weight_norm
(module: T_module, name: str = 'weight') → T_module¶ Removes the weight normalization reparameterization from a module.
- Parameters
module (Module) – containing module
name (str, optional) – name of weight parameter
For example:
>>> import oneflow as flow >>> m = flow.nn.utils.weight_norm(flow.nn.Linear(20, 40)) >>> flow.nn.utils.remove_weight_norm(m) Linear(in_features=20, out_features=40, bias=True)
-
oneflow.nn.init.
xavier_uniform_
(tensor, gain=1.0, *, data_format='NCHW')¶ The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/stable/nn.init.html.
Fills the input Tensor with values according to the method described in Understanding the difficulty of training deep feedforward neural networks - Glorot, X. & Bengio, Y. (2010), using a uniform distribution. The resulting tensor will have values sampled from \(\mathcal{U}(-a, a)\) where
\[a = \text{gain} \times \sqrt{\frac{6}{\text{fan_in} + \text{fan_out}}}\]Also known as Glorot initialization.
- Parameters
tensor – an n-dimensional flow.Tensor
gain – an optional scaling factor
Examples
>>> w = flow.empty(3, 5) >>> nn.init.xavier_uniform_(w, gain=nn.init.calculate_gain('relu'))
-
oneflow.nn.init.
xavier_normal_
(tensor, gain=1.0, *, data_format='NCHW')¶ The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/stable/nn.init.html.
Fills the input Tensor with values according to the method described in Understanding the difficulty of training deep feedforward neural networks - Glorot, X. & Bengio, Y. (2010), using a normal distribution. The resulting tensor will have values sampled from \(\mathcal{N}(0, \text{std}^2)\) where
\[\text{std} = \text{gain} \times \sqrt{\frac{2}{\text{fan_in} + \text{fan_out}}}\]Also known as Glorot initialization.
- Parameters
tensor – an n-dimensional flow.Tensor
gain – an optional scaling factor
Examples
>>> w = flow.empty(3, 5) >>> nn.init.xavier_normal_(w)
-
oneflow.nn.init.
kaiming_uniform_
(tensor, a=0, mode='fan_in', nonlinearity='leaky_relu', *, data_format='NCHW')¶ The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/stable/nn.init.html.
Fills the input Tensor with values according to the method described in Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification - He, K. et al. (2015), using a uniform distribution. The resulting tensor will have values sampled from \(\mathcal{U}(-\text{bound}, \text{bound})\) where
\[\text{bound} = \text{gain} \times \sqrt{\frac{3}{\text{fan_mode}}}\]Also known as He initialization.
- Parameters
tensor – an n-dimensional flow.Tensor
a – the negative slope of the rectifier used after this layer (only used with
'leaky_relu'
)mode – either
'fan_in'
(default) or'fan_out'
. Choosing'fan_in'
preserves the magnitude of the variance of the weights in the forward pass. Choosing'fan_out'
preserves the magnitudes in the backwards pass.nonlinearity – the non-linear function (nn.functional name), recommended to use only with
'relu'
or'leaky_relu'
(default).
Examples
>>> w = flow.empty(3, 5) >>> nn.init.kaiming_uniform_(w, mode='fan_in', nonlinearity='relu')
-
oneflow.nn.init.
kaiming_normal_
(tensor, a=0, mode='fan_in', nonlinearity='leaky_relu', *, data_format='NCHW')¶ The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/stable/nn.init.html.
Fills the input Tensor with values according to the method described in Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification - He, K. et al. (2015), using a normal distribution. The resulting tensor will have values sampled from \(\mathcal{N}(0, \text{std}^2)\) where
\[\text{std} = \frac{\text{gain}}{\sqrt{\text{fan_mode}}}\]Also known as He initialization.
- Parameters
tensor – an n-dimensional flow.Tensor
a – the negative slope of the rectifier used after this layer (only used with
'leaky_relu'
)mode – either
'fan_in'
(default) or'fan_out'
. Choosing'fan_in'
preserves the magnitude of the variance of the weights in the forward pass. Choosing'fan_out'
preserves the magnitudes in the backwards pass.nonlinearity – the non-linear function (nn.functional name), recommended to use only with
'relu'
or'leaky_relu'
(default).
Examples
>>> w = flow.empty(3, 5) >>> nn.init.kaiming_normal_(w, mode='fan_out', nonlinearity='relu')