ding.torch_utils.network.merge¶
ding.torch_utils.network.merge
¶
This file provides an implementation of several different neural network modules that are used for merging and transforming input data in various ways. The following components can be used when we are dealing with data from multiple modes, or when we need to merge multiple intermediate embedded representations in the forward process of a model.
The main classes defined in this code are:
- BilinearGeneral: This class implements a bilinear transformation layer that applies a bilinear transformation to
incoming data, as described in the "Multiplicative Interactions and Where to Find Them", published at ICLR 2020,
https://openreview.net/forum?id=rylnK6VtDH. The transformation involves two input features and an output
feature, and also includes an optional bias term.
- TorchBilinearCustomized: This class implements a bilinear layer similar to the one provided by PyTorch
(torch.nn.Bilinear), but with additional customizations. This class can be used as an alternative to the
BilinearGeneral class.
- TorchBilinear: This class is a simple wrapper around the PyTorch's built-in nn.Bilinear module. It provides the
same functionality as PyTorch's nn.Bilinear but within the structure of the current module.
- FiLM: This class implements a Feature-wise Linear Modulation (FiLM) layer. FiLM layers apply an affine
transformation to the input data, conditioned on some additional context information.
- GatingType: This is an enumeration class that defines different types of gating mechanisms that can be used in
the modules.
- SumMerge: This class provides a simple summing mechanism to merge input streams.
- VectorMerge: This class implements a more complex merging mechanism for vector streams.
The streams are first transformed using layer normalization, a ReLU activation, and a linear layer.
Then they are merged either by simple summing or by using a gating mechanism.
The implementation of these classes involves PyTorch and Numpy libraries, and the classes use PyTorch's nn.Module as the base class, making them compatible with PyTorch's neural network modules and functionalities. These modules can be useful building blocks in more complex deep learning architectures.
BilinearGeneral
¶
Bases: Module
Overview
Bilinear implementation as in: Multiplicative Interactions and Where to Find Them, ICLR 2020, https://openreview.net/forum?id=rylnK6VtDH.
Interfaces:
__init__, forward
__init__(in1_features, in2_features, out_features)
¶
Overview
Initialize the Bilinear layer.
Arguments:
- in1_features (:obj:int): The size of each first input sample.
- in2_features (:obj:int): The size of each second input sample.
- out_features (:obj:int): The size of each output sample.
reset_parameters()
¶
Overview
Initialize the parameters of the Bilinear layer.
forward(x, z)
¶
Overview
compute the bilinear function.
Arguments:
- x (:obj:torch.Tensor): The first input tensor.
- z (:obj:torch.Tensor): The second input tensor.
TorchBilinearCustomized
¶
Bases: Module
Overview
Customized Torch Bilinear implementation.
Interfaces:
__init__, forward
__init__(in1_features, in2_features, out_features)
¶
Overview
Initialize the Bilinear layer.
Arguments:
- in1_features (:obj:int): The size of each first input sample.
- in2_features (:obj:int): The size of each second input sample.
- out_features (:obj:int): The size of each output sample.
reset_parameters()
¶
Overview
Initialize the parameters of the Bilinear layer.
forward(x, z)
¶
Overview
Compute the bilinear function.
Arguments:
- x (:obj:torch.Tensor): The first input tensor.
- z (:obj:torch.Tensor): The second input tensor.
FiLM
¶
Bases: Module
Overview
Feature-wise Linear Modulation (FiLM) Layer. This layer applies feature-wise affine transformation based on context.
Interfaces:
__init__, forward
__init__(feature_dim, context_dim)
¶
Overview
Initialize the FiLM layer.
Arguments:
- feature_dim (:obj:int). The dimension of the input feature vector.
- context_dim (:obj:int). The dimension of the input context vector.
forward(feature, context)
¶
Overview
Forward propagation.
Arguments:
- feature (:obj:torch.Tensor). The input feature, shape (batch_size, feature_dim).
- context (:obj:torch.Tensor). The input context, shape (batch_size, context_dim).
Returns:
- conditioned_feature : torch.Tensor. The output feature after FiLM, shape (batch_size, feature_dim).
GatingType
¶
Bases: Enum
Overview
Enum class defining different types of tensor gating and aggregation in modules.
SumMerge
¶
Bases: Module
Overview
A PyTorch module that merges a list of tensors by computing their sum. All input tensors must have the same size. This module can work with any type of tensor (vector, units or visual).
Interfaces:
__init__, forward
forward(tensors)
¶
Overview
Forward pass of the SumMerge module, which sums the input tensors.
Arguments:
- tensors (:obj:List[Tensor]): List of input tensors to be summed. All tensors must have the same size.
Returns:
- summed (:obj:Tensor): Tensor resulting from the sum of all input tensors.
VectorMerge
¶
Bases: Module
Overview
Merges multiple vector streams. Streams are first transformed through layer normalization, relu, and linear layers, then summed. They don't need to have the same size. Gating can also be used before the sum.
Interfaces:
__init__, encode, _compute_gate, forward
.. note:: For more details about the gating types, please refer to the GatingType enum class.
__init__(input_sizes, output_size, gating_type=GatingType.NONE, use_layer_norm=True)
¶
Overview
Initialize the VectorMerge module.
Arguments:
- input_sizes (:obj:Dict[str, int]): A dictionary mapping input names to their sizes. The size is a single integer for 1D inputs, or None for 0D inputs. If an input size is None, we assume it's ().
- output_size (:obj:int): The size of the output vector.
- gating_type (:obj:GatingType): The type of gating mechanism to use. Default is GatingType.NONE.
- use_layer_norm (:obj:bool): Whether to use layer normalization. Default is True.
encode(inputs)
¶
Overview
Encode the input tensors using layer normalization, relu, and linear transformations.
Arguments:
- inputs (:obj:Dict[str, Tensor]): The input tensors.
Returns:
- gates (:obj:List[Tensor]): The gate tensors after transformations.
- outputs (:obj:List[Tensor]): The output tensors after transformations.
forward(inputs)
¶
Overview
Forward pass through the VectorMerge module.
Arguments:
- inputs (:obj:Dict[str, Tensor]): The input tensors.
Returns:
- output (:obj:Tensor): The output tensor after passing through the module.
Full Source Code
../ding/torch_utils/network/merge.py