SGPT<T>

Models & Types · AiDotNet.NeuralNetworks

SGPT (Sentence GPT) neural network implementation using decoder-only transformer architectures.

Inheritance: NeuralNetworkBase<T> → DeclaredModelLayoutBase<T> → TextEmbeddingModelLayoutBase<T> → TransformerEmbeddingNetwork<T> → SGPT<T>

For Beginners

Most AI models are like “readers” who read a whole sentence and then think about it. SGPT is like a “writer.” Because it’s trained to write sentences one word at a time, it has a very deep understanding of how sentences are built. When it finishes a sentence, the very last word it would have written contains a “mental summary” of everything that came before it. SGPT uses that summary as the coordinate (embedding) for the whole sentence.

How It Works

SGPT leverages large-scale decoder-only models (like GPT-2 or GPT-Neo) to generate high-quality sentence embeddings. By focusing on the last token of a sequence, the model utilizes the unidirectional context to summarize the entire sentence’s meaning.

Example

var architecture = new NeuralNetworkArchitecture<float>(inputFeatures: 8, outputSize: 4);
var input = Tensor<float>.CreateRandom(new[] { 1, 512 });
var trainX = Tensor<float>.CreateRandom(4, 8);
var trainY = Tensor<float>.CreateRandom(4, 2);
var result = new AiModelBuilder<float, Tensor<float>, Tensor<float>>()
    .ConfigureModel(new SGPT<float>(architecture))
    .Build(trainX, trainY);
var embedding = result.Predict(input);

Constructors

Constructor Summary
SGPT() Initializes a new instance of the SGPT model.
SGPT(NeuralNetworkArchitecture<T> architecture, SGPTOptions options, ITokenizer tokenizer, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction) Initializes a new instance of the SGPT model.

Methods

Method Summary
ModelMetadata<T> GetModelMetadata() Retrieves metadata about the SGPT model.
Task<Matrix<T>> EmbedBatchAsync(IEnumerable<string> texts)
Task<Vector<T>> EmbedAsync(string text)
Vector<T> Embed(string text)