ding.torch_utils.network.nn_module¶
ding.torch_utils.network.nn_module
¶
ChannelShuffle
¶
Bases: Module
Overview
Apply channel shuffle to the input tensor. For more details about the channel shuffle, please refer to the 'ShuffleNet' paper: https://arxiv.org/abs/1707.01083
Interfaces:
__init__, forward
__init__(group_num)
¶
Overview
Initialize the ChannelShuffle class.
Arguments:
- group_num (:obj:int): The number of groups to exchange.
forward(x)
¶
Overview
Forward pass through the ChannelShuffle module.
Arguments:
- x (:obj:torch.Tensor): The input tensor.
Returns:
- x (:obj:torch.Tensor): The shuffled input tensor.
NearestUpsample
¶
Bases: Module
Overview
This module upsamples the input to the given scale_factor using the nearest mode.
Interfaces:
__init__, forward
__init__(scale_factor)
¶
Overview
Initialize the NearestUpsample class.
Arguments:
- scale_factor (:obj:Union[float, List[float]]): The multiplier for the spatial size.
forward(x)
¶
Overview
Return the upsampled input tensor.
Arguments:
- x (:obj:torch.Tensor): The input tensor.
Returns:
- upsample(:obj:torch.Tensor): The upsampled input tensor.
BilinearUpsample
¶
Bases: Module
Overview
This module upsamples the input to the given scale_factor using the bilinear mode.
Interfaces:
__init__, forward
__init__(scale_factor)
¶
Overview
Initialize the BilinearUpsample class.
Arguments:
- scale_factor (:obj:Union[float, List[float]]): The multiplier for the spatial size.
forward(x)
¶
Overview
Return the upsampled input tensor.
Arguments:
- x (:obj:torch.Tensor): The input tensor.
Returns:
- upsample(:obj:torch.Tensor): The upsampled input tensor.
NoiseLinearLayer
¶
Bases: Module
Overview
This is a linear layer with random noise.
Interfaces:
__init__, reset_noise, reset_parameters, forward
__init__(in_channels, out_channels, sigma0=0.4)
¶
Overview
Initialize the NoiseLinearLayer class. The 'enable_noise' attribute enables external control over whether noise is applied. - If enable_noise is True, the layer adds noise even if the module is in evaluation mode. - If enable_noise is False, no noise is added regardless of self.training.
Arguments:
- in_channels (:obj:int): Number of channels in the input tensor.
- out_channels (:obj:int): Number of channels in the output tensor.
- sigma0 (:obj:int, optional): Default noise volume when initializing NoiseLinearLayer. Default is 0.4.
reset_noise()
¶
Overview
Reset the noise settings in the layer.
reset_parameters()
¶
Overview
Reset the parameters in the layer.
forward(x)
¶
Overview
Perform the forward pass with noise.
Arguments:
- x (:obj:torch.Tensor): The input tensor.
Returns:
- output (:obj:torch.Tensor): The output tensor with noise.
NaiveFlatten
¶
Bases: Module
Overview
This module is a naive implementation of the flatten operation.
Interfaces:
__init__, forward
__init__(start_dim=1, end_dim=-1)
¶
Overview
Initialize the NaiveFlatten class.
Arguments:
- start_dim (:obj:int, optional): The first dimension to flatten. Default is 1.
- end_dim (:obj:int, optional): The last dimension to flatten. Default is -1.
forward(x)
¶
Overview
Perform the flatten operation on the input tensor.
Arguments:
- x (:obj:torch.Tensor): The input tensor.
Returns:
- output (:obj:torch.Tensor): The flattened output tensor.
weight_init_(weight, init_type='xavier', activation=None)
¶
Overview
Initialize weight according to the specified type.
Arguments:
- weight (:obj:torch.Tensor): The weight that needs to be initialized.
- init_type (:obj:str, optional): The type of initialization to implement, supports ["xavier", "kaiming", "orthogonal"].
- activation (:obj:str, optional): The activation function name. Recommended to use only with ['relu', 'leaky_relu'].
sequential_pack(layers)
¶
Overview
Pack the layers in the input list to a nn.Sequential module.
If there is a convolutional layer in module, an extra attribute out_channels will be added
to the module and set to the out_channel of the conv layer.
Arguments:
- layers (:obj:List[nn.Module]): The input list of layers.
Returns:
- seq (:obj:nn.Sequential): Packed sequential container.
conv1d_block(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, activation=None, norm_type=None)
¶
Overview
Create a 1-dimensional convolution layer with activation and normalization.
Arguments:
- in_channels (:obj:int): Number of channels in the input tensor.
- out_channels (:obj:int): Number of channels in the output tensor.
- kernel_size (:obj:int): Size of the convolving kernel.
- stride (:obj:int, optional): Stride of the convolution. Default is 1.
- padding (:obj:int, optional): Zero-padding added to both sides of the input. Default is 0.
- dilation (:obj:int, optional): Spacing between kernel elements. Default is 1.
- groups (:obj:int, optional): Number of blocked connections from input channels to output channels. Default is 1.
- activation (:obj:nn.Module, optional): The optional activation function.
- norm_type (:obj:str, optional): Type of the normalization.
Returns:
- block (:obj:nn.Sequential): A sequential list containing the torch layers of the 1-dimensional convolution layer.
.. note:: Conv1d (https://pytorch.org/docs/stable/generated/torch.nn.Conv1d.html#torch.nn.Conv1d)
conv2d_block(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, pad_type='zero', activation=None, norm_type=None, num_groups_for_gn=1, bias=True)
¶
Overview
Create a 2-dimensional convolution layer with activation and normalization.
Arguments:
- in_channels (:obj:int): Number of channels in the input tensor.
- out_channels (:obj:int): Number of channels in the output tensor.
- kernel_size (:obj:int): Size of the convolving kernel.
- stride (:obj:int, optional): Stride of the convolution. Default is 1.
- padding (:obj:int, optional): Zero-padding added to both sides of the input. Default is 0.
- dilation (:obj:int): Spacing between kernel elements.
- groups (:obj:int, optional): Number of blocked connections from input channels to output channels. Default is 1.
- pad_type (:obj:str, optional): The way to add padding, include ['zero', 'reflect', 'replicate']. Default is 'zero'.
- activation (:obj:nn.Module): the optional activation function.
- norm_type (:obj:str): The type of the normalization, now support ['BN', 'LN', 'IN', 'GN', 'SyncBN'], default set to None, which means no normalization.
- num_groups_for_gn (:obj:int): Number of groups for GroupNorm.
- bias (:obj:bool): whether to add a learnable bias to the nn.Conv2d. Default is True.
Returns:
- block (:obj:nn.Sequential): A sequential list containing the torch layers of the 2-dimensional convolution layer.
.. note:: Conv2d (https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html#torch.nn.Conv2d)
deconv2d_block(in_channels, out_channels, kernel_size, stride=1, padding=0, output_padding=0, groups=1, activation=None, norm_type=None)
¶
Overview
Create a 2-dimensional transpose convolution layer with activation and normalization.
Arguments:
- in_channels (:obj:int): Number of channels in the input tensor.
- out_channels (:obj:int): Number of channels in the output tensor.
- kernel_size (:obj:int): Size of the convolving kernel.
- stride (:obj:int, optional): Stride of the convolution. Default is 1.
- padding (:obj:int, optional): Zero-padding added to both sides of the input. Default is 0.
- output_padding (:obj:int, optional): Additional size added to one side of the output shape. Default is 0.
- groups (:obj:int, optional): Number of blocked connections from input channels to output channels. Default is 1.
- activation (:obj:int, optional): The optional activation function.
- norm_type (:obj:int, optional): Type of the normalization.
Returns:
- block (:obj:nn.Sequential): A sequential list containing the torch layers of the 2-dimensional transpose convolution layer.
.. note::
ConvTranspose2d (https://pytorch.org/docs/master/generated/torch.nn.ConvTranspose2d.html)
fc_block(in_channels, out_channels, activation=None, norm_type=None, use_dropout=False, dropout_probability=0.5)
¶
Overview
Create a fully-connected block with activation, normalization, and dropout. Optional normalization can be done to the dim 1 (across the channels). x -> fc -> norm -> act -> dropout -> out
Arguments:
- in_channels (:obj:int): Number of channels in the input tensor.
- out_channels (:obj:int): Number of channels in the output tensor.
- activation (:obj:nn.Module, optional): The optional activation function.
- norm_type (:obj:str, optional): Type of the normalization.
- use_dropout (:obj:bool, optional): Whether to use dropout in the fully-connected block. Default is False.
- dropout_probability (:obj:float, optional): Probability of an element to be zeroed in the dropout. Default is 0.5.
Returns:
- block (:obj:nn.Sequential): A sequential list containing the torch layers of the fully-connected block.
.. note::
You can refer to nn.linear (https://pytorch.org/docs/master/generated/torch.nn.Linear.html).
normed_linear(in_features, out_features, bias=True, device=None, dtype=None, scale=1.0)
¶
Overview
Create a nn.Linear module but with normalized fan-in init.
Arguments:
- in_features (:obj:int): Number of features in the input tensor.
- out_features (:obj:int): Number of features in the output tensor.
- bias (:obj:bool, optional): Whether to add a learnable bias to the nn.Linear. Default is True.
- device (:obj:torch.device, optional): The device to put the created module on. Default is None.
- dtype (:obj:torch.dtype, optional): The desired data type of created module. Default is None.
- scale (:obj:float, optional): The scale factor for initialization. Default is 1.0.
Returns:
- out (:obj:nn.Linear): A nn.Linear module with normalized fan-in init.
normed_conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', device=None, dtype=None, scale=1)
¶
Overview
Create a nn.Conv2d module but with normalized fan-in init.
Arguments:
- in_channels (:obj:int): Number of channels in the input tensor.
- out_channels (:obj:int): Number of channels in the output tensor.
- kernel_size (:obj:Union[int, Tuple[int, int]]): Size of the convolving kernel.
- stride (:obj:Union[int, Tuple[int, int]], optional): Stride of the convolution. Default is 1.
- padding (:obj:Union[int, Tuple[int, int]], optional): Zero-padding added to both sides of the input. Default is 0.
- dilation (:Union[int, Tuple[int, int]], optional): Spacing between kernel elements. Default is 1.
- groups (:obj:int, optional): Number of blocked connections from input channels to output channels. Default is 1.
- bias (:obj:bool, optional): Whether to add a learnable bias to the nn.Conv2d. Default is True.
- padding_mode (:obj:str, optional): The type of padding algorithm to use. Default is 'zeros'.
- device (:obj:torch.device, optional): The device to put the created module on. Default is None.
- dtype (:obj:torch.dtype, optional): The desired data type of created module. Default is None.
- scale (:obj:float, optional): The scale factor for initialization. Default is 1.
Returns:
- out (:obj:nn.Conv2d): A nn.Conv2d module with normalized fan-in init.
MLP(in_channels, hidden_channels, out_channels, layer_num, layer_fn=None, activation=None, norm_type=None, use_dropout=False, dropout_probability=0.5, output_activation=True, output_norm=True, last_linear_layer_init_zero=False)
¶
Overview
Create a multi-layer perceptron using fully-connected blocks with activation, normalization, and dropout, optional normalization can be done to the dim 1 (across the channels). x -> fc -> norm -> act -> dropout -> out
Arguments:
- in_channels (:obj:int): Number of channels in the input tensor.
- hidden_channels (:obj:int): Number of channels in the hidden tensor.
- out_channels (:obj:int): Number of channels in the output tensor.
- layer_num (:obj:int): Number of layers.
- layer_fn (:obj:Callable, optional): Layer function.
- activation (:obj:nn.Module, optional): The optional activation function.
- norm_type (:obj:str, optional): The type of the normalization.
- use_dropout (:obj:bool, optional): Whether to use dropout in the fully-connected block. Default is False.
- dropout_probability (:obj:float, optional): Probability of an element to be zeroed in the dropout. Default is 0.5.
- output_activation (:obj:bool, optional): Whether to use activation in the output layer. If True, we use the same activation as front layers. Default is True.
- output_norm (:obj:bool, optional): Whether to use normalization in the output layer. If True, we use the same normalization as front layers. Default is True.
- last_linear_layer_init_zero (:obj:bool, optional): Whether to use zero initializations for the last linear layer (including w and b), which can provide stable zero outputs in the beginning, usually used in the policy network in RL settings.
Returns:
- block (:obj:nn.Sequential): A sequential list containing the torch layers of the multi-layer perceptron.
.. note:: you can refer to nn.linear (https://pytorch.org/docs/master/generated/torch.nn.Linear.html).
one_hot(val, num, num_first=False)
¶
Overview
Convert a torch.LongTensor to one-hot encoding. This implementation can be slightly faster than
torch.nn.functional.one_hot.
Arguments:
- val (:obj:torch.LongTensor): Each element contains the state to be encoded, the range should be [0, num-1]
- num (:obj:int): Number of states of the one-hot encoding
- num_first (:obj:bool, optional): If False, the one-hot encoding is added as the last dimension; otherwise, it is added as the first dimension. Default is False.
Returns:
- one_hot (:obj:torch.FloatTensor): The one-hot encoded tensor.
Example:
>>> one_hot(2torch.ones([2,2]).long(),3)
tensor([[[0., 0., 1.],
[0., 0., 1.]],
[[0., 0., 1.],
[0., 0., 1.]]])
>>> one_hot(2torch.ones([2,2]).long(),3,num_first=True)
tensor([[[0., 0.], [1., 0.]],
[[0., 1.], [0., 0.]],
[[1., 0.], [0., 1.]]])
binary_encode(y, max_val)
¶
Overview
Convert elements in a tensor to its binary representation.
Arguments:
- y (:obj:torch.Tensor): The tensor to be converted into its binary representation.
- max_val (:obj:torch.Tensor): The maximum value of the elements in the tensor.
Returns:
- binary (:obj:torch.Tensor): The input tensor in its binary representation.
Example:
>>> binary_encode(torch.tensor([3,2]),torch.tensor(8))
tensor([[0, 0, 1, 1],[0, 0, 1, 0]])
noise_block(in_channels, out_channels, activation=None, norm_type=None, use_dropout=False, dropout_probability=0.5, sigma0=0.4)
¶
Overview
Create a fully-connected noise layer with activation, normalization, and dropout. Optional normalization can be done to the dim 1 (across the channels).
Arguments:
- in_channels (:obj:int): Number of channels in the input tensor.
- out_channels (:obj:int): Number of channels in the output tensor.
- activation (:obj:str, optional): The optional activation function. Default is None.
- norm_type (:obj:str, optional): Type of normalization. Default is None.
- use_dropout (:obj:bool, optional): Whether to use dropout in the fully-connected block.
- dropout_probability (:obj:float, optional): Probability of an element to be zeroed in the dropout. Default is 0.5.
- sigma0 (:obj:float, optional): The sigma0 is the default noise volume when initializing NoiseLinearLayer. Default is 0.4.
Returns:
- block (:obj:nn.Sequential): A sequential list containing the torch layers of the fully-connected block.
Full Source Code
../ding/torch_utils/network/nn_module.py