InstructorEmbedding<T>
Models & Types · AiDotNet.NeuralNetworks
Instructor/E5 (Instruction-Tuned) embedding model implementation. Uses task-specific instructions to adapt embeddings for different use cases.
Inheritance: NeuralNetworkBase<T> → DeclaredModelLayoutBase<T> → TextEmbeddingModelLayoutBase<T> → TransformerEmbeddingNetwork<T> → InstructorEmbedding<T>
For Beginners
Most AI models read every sentence the same way. “Instructor” models are like specialized scholars. If you tell them “read this like a doctor looking for a diagnosis,” they will focus on medical terms. If you tell them “read this like a poet,” they will focus on the mood. It makes the “coordinates” (embeddings) much more useful for your specific goal.
How It Works
Instructor models are transformer-based encoders trained with instructions. By prepending a task description (e.g., “Represent the Wikipedia sentence for retrieval:”), the model learns to produce embeddings that are optimized for that specific task.
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 InstructorEmbedding<float>(architecture))
.Build(trainX, trainY);
var embedding = result.Predict(input);
Constructors
| Constructor | Summary |
|---|---|
InstructorEmbedding() |
Initializes a new instance with default architecture settings. |
InstructorEmbedding(NeuralNetworkArchitecture<T> architecture, InstructorEmbeddingOptions options, ITokenizer tokenizer, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction) |
Initializes a new instance of the InstructorEmbedding model. |
Methods
| Method | Summary |
|---|---|
ModelMetadata<T> GetModelMetadata() |
Retrieves metadata about the Instructor model, including its default instruction. |
Task<Matrix<T>> EmbedBatchAsync(IEnumerable<string> texts) |
|
Task<Vector<T>> EmbedAsync(string text) |
|
Vector<T> Embed(string text) |
|
Vector<T> EmbedWithInstruction(string text, string instruction) |
Encodes text into a normalized embedding vector using a task-specific instruction. |
void SetDefaultInstruction(string instruction) |
Sets the default instruction used for general embedding generation. |