MatchingNetworksAlgorithm<T, TInput, TOutput>

Models & Types · AiDotNet.MetaLearning.Algorithms

Implementation of Matching Networks for few-shot learning.

Inheritance: ModelBase<T, TInput, TOutput> → MetaLearnerBase<T, TInput, TOutput> → MatchingNetworksAlgorithm<T, TInput, TOutput>

Implements: IGeneratedParameterRegistrar<T>

For Beginners

Matching Networks label a new example by comparing it with every labelled example of the task and letting the most similar ones vote. Training shapes the feature space so that the vote is right.

How It Works

Matching Networks (Vinyals et al. 2016) classify a query x by attention over the labelled support set: P(y | x, S) = sum_i a(x, x_i) y_i with a(x, x_i) = softmax_i c(f(x), g(x_i)), where c is the cosine similarity and y_i the one-hot support labels (eq. 1 and section 2.1.1). Training maximises log P(y | x, S) of the query labels over episodes (eq. 2).

Full context embeddings (section 2.1.2 and appendix A) make both embeddings depend on the support set. g(x_i, S) = h_fwd_i + h_bwd_i + g'(x_i) runs a bidirectional LSTM over the support set (A.2); and f(x, S) = attLSTM(f'(x), g(S), K) runs K steps of an LSTM that reads g(S) by content-based attention, h_k = LSTM(f'(x), [h_(k-1), r_(k-1)], c_(k-1)) + f'(x) with r_(k-1) = sum_i softmax(h_(k-1)' g(x_i)) g(x_i) (A.1). Here f' and g' are the one embedding network the options hold.

Exact gradients. Support and query examples go through the embedding network together and the whole episode - context embeddings, attention and class probabilities - is rebuilt from those embeddings on the tape, so the embedding network, the context LSTMs and a learned kernel all receive the exact gradient of the episode loss. This replaced a forward-difference estimate over at most 100 evenly spaced parameters, scaled up as if it were an unbiased estimate of the rest.

Reference: Vinyals, O., Blundell, C., Lillicrap, T., Kavukcuoglu, K., & Wierstra, D. (2016). Matching Networks for One Shot Learning. NeurIPS.

Constructors

Constructor Summary
MatchingNetworksAlgorithm(MatchingNetworksOptions<T, TInput, TOutput> options) Initializes a new instance of the MatchingNetworksAlgorithm class.

Properties

Property Summary
MetaLearningAlgorithmType AlgorithmType { get; } Gets the algorithm type identifier for this meta-learner.

Methods

Method Summary
IModel<TInput, TOutput, ModelMetadata<T>> Adapt(IMetaLearningTask<T, TInput, TOutput> task) Adapts to a new task by embedding its support set.
ModelOptions GetOptions()
T MetaTrain(TaskBatch<T, TInput, TOutput> taskBatch) Performs one meta-training step: for each episode, the attention loss of its query examples, differentiated exactly into the embedding network and every learned part of the matching function.