Platforms to show: All Mac Windows Linux Cross-Platform

NLContextualEmbeddingMBS class

Type Topic Plugin Version macOS Windows Linux iOS Targets
class Natural Language MBS MacFrameworks Plugin 26.2 ✅ Yes ❌ No ❌ No ✅ Yes All
A model that computes sequences of embedding vectors for natural language utterances.
Example
// compare the meaning of two sentences with contextual embeddings
Var embedding As NLContextualEmbeddingMBS = NLContextualEmbeddingMBS.contextualEmbeddingWithLanguage("en")
If embedding = Nil Then Return
If Not embedding.hasAvailableAssets Then
embedding.requestEmbeddingAssets // downloads the model, try again later
MessageBox "Downloading the model, please try again later."
Return
End If

Var error As NSErrorMBS
If Not embedding.Load(error) Then
MessageBox error.localizedDescription
Return
End If

Var a As NLContextualEmbeddingResultMBS = embedding.embeddingResultForString("The cat sleeps on the sofa.", "en", error)
Var b As NLContextualEmbeddingResultMBS = embedding.embeddingResultForString("A kitten is napping on the couch.", "en", error)
If a <> Nil And b <> Nil Then
MessageBox "Similarity: " + Format(a.averageToken.cosineSimilarity(b.averageToken), "0.00")
End If

NLContextualEmbeddingMBS provides a dense vector representation of natural language utterances using models trained with contextualized language understanding. Unlike static word embeddings provided by NLEmbeddingMBS, contextual embeddings dynamically adjust based on surrounding words, enabling deeper language comprehension.

If you are building text classification or word tagging models, you can use NLContextualEmbeddingMBS with Create ML by selecting MLTextClassifier.FeatureExtractorType.bertEmbedding as the feature extraction component in your model. For more information on how to create your own text classification model refer to Creating a text classifier model.

Note
For semantic similarity tasks, consider using NLEmbeddingMBS. To learn more on how to implement semantic similarity, refer to Finding similarities between pieces of text.

Before configuring your contextual embedding:

  • Select a model that’s appropriate for your target language when initializing the embedding.
  • Call requestEmbeddingAssetsWithCompletionHandler: before computing the embedding to confirm assets are available.
Some models support multiple languages; for example, you can use the same object to compute embeddings for both English and Spanish. However, this isn’t always the case — languages like English and Chinese may require separate models. For more information about supported languages, refer to languages.
This is an abstract class. You can't create an instance, but you can get one from various plugin functions.

RequestEmbeddingAssets Results

Constant Value Description
NLContextualEmbeddingAssetsResultAvailable 0 A result that indicates that the assets are present on-device.
NLContextualEmbeddingAssetsResultError 2 A result that indicates the framework encounters an error.
NLContextualEmbeddingAssetsResultNotAvailable 1 A result that indicates that the assets aren’t present on-device.

This class has no subclasses.

Blog Entries

Release notes

Some related classes:


The items on this page are in the following plugins: MBS MacFrameworks Plugin.


NikonSizeMBS   -   NLContextualEmbeddingResultMBS


The biggest plugin in space...