RelationModule<T>
Models & Types · AiDotNet.MetaLearning.Modules
Relation module that computes similarity between feature pairs for Relation Networks.
Inheritance: ModelBase<T, Tensor<T>, Tensor<T>> → RelationModule<T>
For Beginners
Instead of using a fixed formula to measure similarity (like Euclidean distance), the relation module is a small neural network that LEARNS how to compare examples. It takes two feature vectors as input (concatenated together) and outputs a number between 0 and 1 indicating how related they are.
How It Works
The relation module g of Sung et al. 2018 maps a pair of embeddings to a relation score in (0, 1). The
paper’s module concatenates the pair and ends with a ReLU layer and a sigmoid unit (Figure 2); see
RelationModuleType for the other architectures. The input is the concatenation of two equally wide
embeddings, the sample’s first, as one vector or one row per pair.
The weights are sized on the first forward pass, when the embedding width is known, and initialised as PyTorch
initialises nn.Linear. This used to be a single dot product with at most
HiddenDimension inputs and a sigmoid - no hidden layer, and the rest of the input ignored.
Constructors
| Constructor | Summary |
|---|---|
RelationModule(int hiddenDimension) |
Initializes a new instance of RelationModule with the paper’s concatenation architecture. |
RelationModule(int hiddenDimension, RelationModuleType relationType) |
Initializes a new instance of RelationModule with the given architecture. |
Properties
| Property | Summary |
|---|---|
ILossFunction<T> DefaultLossFunction { get; } |
|
RelationModuleType RelationType { get; } |
Gets how the module combines the pair. |
int HiddenDimension { get; } |
Gets the width of the pair representation before the sigmoid unit. |
int InputWidth { get; } |
Gets the width of each embedding in a pair; zero until the first forward pass. |
Methods
| Method | Summary |
|---|---|
IFullModel<T, Tensor<T>, Tensor<T>> WithParameters(Vector<T> parameters) |
|
RelationModule<T> Clone() |
Creates a deep copy of the relation module. |
Tensor<T> Forward(Tensor<T> combinedFeatures) |
Performs forward pass through the relation module. |
Tensor<T> Predict(Tensor<T> input) |
|
void SetTrainingMode(bool isTraining) |
Sets the training mode. |
void Train(Tensor<T> input, Tensor<T> expectedOutput) |