Table of Contents

Class RelationNetworkOptions<T, TInput, TOutput>

Namespace
AiDotNet.MetaLearning.Options
Assembly
AiDotNet.dll

Configuration options for Relation Networks algorithm.

public class RelationNetworkOptions<T, TInput, TOutput> : IMetaLearnerOptions<T>

Type Parameters

T

The numeric data type used for calculations (e.g., float, double).

TInput

The input data type (e.g., Matrix<T>, Tensor<T>).

TOutput

The output data type (e.g., Vector<T>, Tensor<T>).

Inheritance
RelationNetworkOptions<T, TInput, TOutput>
Implements
Inherited Members

Remarks

Relation Networks learn to compare query examples with class examples by learning a relation function that measures similarity. Unlike metric learning approaches that use fixed distance functions, Relation Networks learn the relation function end-to-end.

For Beginners: Relation Networks learns how to compare examples:

  1. Encode all examples (support and query) with a feature encoder
  2. For each query, concatenate with each support example's features
  3. Pass concatenated features through a relation module (neural network)
  4. The relation module outputs a similarity score
  5. Apply softmax to get class probabilities

Instead of using predefined distances (like Euclidean), it learns a neural network to measure "how related" two examples are.

Constructors

RelationNetworkOptions(IFullModel<T, TInput, TOutput>)

Initializes a new instance of the RelationNetworkOptions class with the required meta-model.

public RelationNetworkOptions(IFullModel<T, TInput, TOutput> metaModel)

Parameters

metaModel IFullModel<T, TInput, TOutput>

The feature encoder to be trained (required).

Exceptions

ArgumentNullException

Thrown when metaModel is null.

Properties

AdaptationSteps

Gets or sets the number of adaptation steps.

public int AdaptationSteps { get; set; }

Property Value

int

Default is 1 (Relation Networks uses feed-forward comparison).

AggregationMethod

Gets or sets the aggregation method for combining multiple support example scores.

public RelationAggregationMethod AggregationMethod { get; set; }

Property Value

RelationAggregationMethod

Default is Mean.

ApplyFeatureTransform

Gets or sets whether to apply feature transformation before relation.

public bool ApplyFeatureTransform { get; set; }

Property Value

bool

Default is false.

CheckpointFrequency

Gets or sets how often to save checkpoints.

public int CheckpointFrequency { get; set; }

Property Value

int

ConcatenationDimension

Gets or sets the dimension for feature concatenation.

public int ConcatenationDimension { get; set; }

Property Value

int

Default is 0 (first feature dimension).

DataLoader

Gets or sets the episodic data loader for sampling tasks. Default: null (tasks must be provided manually to MetaTrain).

public IEpisodicDataLoader<T, TInput, TOutput>? DataLoader { get; set; }

Property Value

IEpisodicDataLoader<T, TInput, TOutput>

EnableCheckpointing

Gets or sets whether to save model checkpoints.

public bool EnableCheckpointing { get; set; }

Property Value

bool

EvaluationFrequency

Gets or sets how often to evaluate the meta-learner.

public int EvaluationFrequency { get; set; }

Property Value

int

EvaluationTasks

Gets or sets the number of tasks to use for evaluation.

public int EvaluationTasks { get; set; }

Property Value

int

FeatureEncoderL2Reg

Gets or sets the L2 regularization strength for the feature encoder.

public double FeatureEncoderL2Reg { get; set; }

Property Value

double

Default is 0.0 (no regularization).

GradientClipThreshold

Gets or sets the maximum gradient norm for gradient clipping.

public double? GradientClipThreshold { get; set; }

Property Value

double?

Default is 10.0.

InnerLearningRate

Gets or sets the learning rate for the inner loop (not used in Relation Networks).

public double InnerLearningRate { get; set; }

Property Value

double

Default is 0.01.

InnerOptimizer

Gets or sets the optimizer for inner loop updates. Default: null (Relation Networks uses feed-forward comparison, not gradient-based inner loop).

public IGradientBasedOptimizer<T, TInput, TOutput>? InnerOptimizer { get; set; }

Property Value

IGradientBasedOptimizer<T, TInput, TOutput>

LossFunction

Gets or sets the loss function for training. Default: null (uses cross-entropy loss internally).

public ILossFunction<T>? LossFunction { get; set; }

Property Value

ILossFunction<T>

MetaBatchSize

Gets or sets the number of tasks to sample per meta-training iteration.

public int MetaBatchSize { get; set; }

Property Value

int

Default is 4.

MetaModel

Gets or sets the meta-model (feature encoder) to be trained. This is the only required property.

public IFullModel<T, TInput, TOutput> MetaModel { get; set; }

Property Value

IFullModel<T, TInput, TOutput>

Remarks

The meta-model should be a feature encoder that maps inputs to an embedding space. The relation module will then learn to compare embeddings from this encoder.

For Beginners: This is typically a CNN for images or an MLP for tabular data. The encoder learns to produce embeddings that the relation module can effectively compare.

MetaOptimizer

Gets or sets the optimizer for network updates. Default: null (uses built-in Adam optimizer with OuterLearningRate).

public IGradientBasedOptimizer<T, TInput, TOutput>? MetaOptimizer { get; set; }

Property Value

IGradientBasedOptimizer<T, TInput, TOutput>

NumClasses

Gets or sets the number of output classes.

public int NumClasses { get; set; }

Property Value

int

Default is 5.

NumHeads

Gets or sets the number of heads for multi-head relation.

public int NumHeads { get; set; }

Property Value

int

Default is 4.

NumMetaIterations

Gets or sets the total number of meta-training iterations.

public int NumMetaIterations { get; set; }

Property Value

int

Default is 1000.

OuterLearningRate

Gets or sets the learning rate for the outer loop (encoder and relation module training).

public double OuterLearningRate { get; set; }

Property Value

double

Default is 0.001.

RandomSeed

Gets or sets the random seed for reproducibility.

public int? RandomSeed { get; set; }

Property Value

int?

RelationDropout

Gets or sets the dropout rate for the relation module.

public double RelationDropout { get; set; }

Property Value

double

Default is 0.0 (no dropout).

RelationHiddenDimension

Gets or sets the hidden dimension for the relation module.

public int RelationHiddenDimension { get; set; }

Property Value

int

Default is 64.

RelationModuleL2Reg

Gets or sets the L2 regularization strength for the relation module.

public double RelationModuleL2Reg { get; set; }

Property Value

double

Default is 0.0 (no regularization).

RelationType

Gets or sets the type of relation module architecture.

public RelationModuleType RelationType { get; set; }

Property Value

RelationModuleType

Default is Concatenate.

UseFirstOrder

Gets or sets whether to use first-order approximation.

public bool UseFirstOrder { get; set; }

Property Value

bool

Default is true since Relation Networks doesn't use gradient-based inner loop.

UseMultiHeadRelation

Gets or sets whether to use multi-head relation.

public bool UseMultiHeadRelation { get; set; }

Property Value

bool

Default is false.

Methods

Clone()

Creates a deep copy of the Relation Network options.

public IMetaLearnerOptions<T> Clone()

Returns

IMetaLearnerOptions<T>

A new RelationNetworkOptions instance with the same configuration.

IsValid()

Validates that all Relation Network configuration options are properly set.

public bool IsValid()

Returns

bool

True if the configuration is valid; otherwise, false.