Views
No views yet
| Family | Use Case | Presets |
|---|---|---|
| all-MiniLM | General-purpose sentence embeddings | all_minilm_l6_v2_en, all_minilm_l6_v1_en, all_minilm_l12_v2_en |
| paraphrase-MiniLM | Paraphrase detection & semantic similarity | paraphrase_minilm_l3_v2_en, paraphrase_minilm_l6_v2_en, paraphrase_minilm_l12_v2_en |
| multi-qa-MiniLM | Question answering & semantic search | multi_qa_minilm_l6_cos_v1_en, multi_qa_minilm_l6_dot_v1_en |
| msmarco-MiniLM | Information retrieval & passage ranking | msmarco_minilm_l6_cos_v5_en, msmarco_minilm_l12_cos_v5_en |
all-*, multi-qa-*-cos, and msmarco-*-cos families produce L2-normalized embeddings, enabling fast cosine similarity via dot product.pip install -U -q keras-hub
pip install -U -q keras
encode_text() for queries and encode_documents() for documents, then compute similarity:1import keras_hub
2# Load a pre-trained sentence embedder.
3embedder = keras_hub.models.BertTextEmbedder.from_preset(
4 "multi_qa_minilm_l6_dot_v1_en",
5)
6# Encode a query and a set of documents.
7query = "How do I get a replacement Medicare card?"
8documents = [
9 "Medicare replacement cards can be requested online or by phone.",
10 "The longest river in the world is the Nile.",
11 "You can apply for a new Medicare card at ssa.gov.",
12]
13query_embedding = embedder.encode_text(query)
14document_embeddings = embedder.encode_documents(documents)
15# Compute cosine similarity (embeddings are L2-normalized by default).
16similarities = embedder.similarity(query_embedding, document_embeddings)
17print(similarities)
18# Best match:
19best_idx = int(similarities[0].argmax())
20print(f"Best match: {documents[best_idx]}")
211import keras_hub
2
3embedder = keras_hub.models.TextEmbedder.from_preset(
4 "multi_qa_minilm_l6_dot_v1_en",
5)
6
7# Encode a batch of sentences.
8sentences = [
9 "That is a happy person",
10 "That is a very happy person",
11 "Today is a sunny day",
12]
13embeddings = embedder.predict(sentences)
14print(embeddings.shape) # (3, 384)1import keras_hub
2import numpy as np
3
4embedder = keras_hub.models.BertTextEmbedder.from_preset(
5 "multi_qa_minilm_l6_dot_v1_en",
6)
7
8# Two groups of semantically related sentences.
9group_a = [
10 "The cat sits on the mat.",
11 "A kitten is resting on a rug.",
12]
13group_b = [
14 "The stock market rallied today.",
15 "Financial markets saw significant gains.",
16]
17
18all_sentences = group_a + group_b
19embeddings = embedder.encode_text(all_sentences)
20print(embeddings.shape) # (4, 384)
21
22# Compute pairwise cosine similarity across all sentences.
23# This produces a (4, 4) matrix where entry [i][j] is the
24# similarity between sentence i and sentence j.
25similarity_matrix = embedder.similarity(embeddings, embeddings)
26print(similarity_matrix)
27# Sentences 0-1 (cat/kitten) will have high mutual similarity.
28# Sentences 2-3 (markets) will have high mutual similarity.
29# Cross-group similarity (e.g., 0 vs 2) will be low.1
2import keras_hub
3import numpy as np
4
5# Load without preprocessor.
6embedder = keras_hub.models.BertTextEmbedder.from_preset(
7 "multi_qa_minilm_l6_dot_v1_en",
8 preprocessor=None,
9)
10
11# Provide pre-tokenized inputs.
12features = {
13 "token_ids": np.ones(shape=(2, 12), dtype="int32"),
14 "segment_ids": np.array(
15 [[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0]] * 2
16 ),
17 "padding_mask": np.array(
18 [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0]] * 2
19 ),
20}
21embeddings = embedder.predict(features)
22print(embeddings.shape) # (2, 384)
231import keras_hub
2
3# CLS pooling (use [CLS] token representation).
4embedder = keras_hub.models.BertTextEmbedder.from_preset(
5 "multi_qa_minilm_l6_dot_v1_en",
6 pooling_mode="cls",
7)
8
9# Max pooling.
10embedder = keras_hub.models.BertTextEmbedder.from_preset(
11 "multi_qa_minilm_l6_dot_v1_en",
12 pooling_mode="max",
13)
14
15# Disable L2 normalization.
16embedder = keras_hub.models.BertTextEmbedder.from_preset(
17 "multi_qa_minilm_l6_dot_v1_en",
18 normalize=False,
19)
20encode_text() for queries and encode_documents() for documents, then compute similarity:1import keras_hub
2# Load a pre-trained sentence embedder.
3embedder = keras_hub.models.BertTextEmbedder.from_preset(
4 "hf://keras/multi_qa_minilm_l6_dot_v1_en",
5)
6# Encode a query and a set of documents.
7query = "How do I get a replacement Medicare card?"
8documents = [
9 "Medicare replacement cards can be requested online or by phone.",
10 "The longest river in the world is the Nile.",
11 "You can apply for a new Medicare card at ssa.gov.",
12]
13query_embedding = embedder.encode_text(query)
14document_embeddings = embedder.encode_documents(documents)
15# Compute cosine similarity (embeddings are L2-normalized by default).
16similarities = embedder.similarity(query_embedding, document_embeddings)
17print(similarities)
18# Best match:
19best_idx = int(similarities[0].argmax())
20print(f"Best match: {documents[best_idx]}")
211import keras_hub
2
3embedder = keras_hub.models.TextEmbedder.from_preset(
4 "hf://keras/multi_qa_minilm_l6_dot_v1_en",
5)
6
7# Encode a batch of sentences.
8sentences = [
9 "That is a happy person",
10 "That is a very happy person",
11 "Today is a sunny day",
12]
13embeddings = embedder.predict(sentences)
14print(embeddings.shape) # (3, 384)1import keras_hub
2import numpy as np
3
4embedder = keras_hub.models.BertTextEmbedder.from_preset(
5 "hf://keras/multi_qa_minilm_l6_dot_v1_en",
6)
7
8# Two groups of semantically related sentences.
9group_a = [
10 "The cat sits on the mat.",
11 "A kitten is resting on a rug.",
12]
13group_b = [
14 "The stock market rallied today.",
15 "Financial markets saw significant gains.",
16]
17
18all_sentences = group_a + group_b
19embeddings = embedder.encode_text(all_sentences)
20print(embeddings.shape) # (4, 384)
21
22# Compute pairwise cosine similarity across all sentences.
23# This produces a (4, 4) matrix where entry [i][j] is the
24# similarity between sentence i and sentence j.
25similarity_matrix = embedder.similarity(embeddings, embeddings)
26print(similarity_matrix)
27# Sentences 0-1 (cat/kitten) will have high mutual similarity.
28# Sentences 2-3 (markets) will have high mutual similarity.
29# Cross-group similarity (e.g., 0 vs 2) will be low.1
2import keras_hub
3import numpy as np
4
5# Load without preprocessor.
6embedder = keras_hub.models.BertTextEmbedder.from_preset(
7 "hf://keras/multi_qa_minilm_l6_dot_v1_en",
8 preprocessor=None,
9)
10
11# Provide pre-tokenized inputs.
12features = {
13 "token_ids": np.ones(shape=(2, 12), dtype="int32"),
14 "segment_ids": np.array(
15 [[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0]] * 2
16 ),
17 "padding_mask": np.array(
18 [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0]] * 2
19 ),
20}
21embeddings = embedder.predict(features)
22print(embeddings.shape) # (2, 384)
231import keras_hub
2
3# CLS pooling (use [CLS] token representation).
4embedder = keras_hub.models.BertTextEmbedder.from_preset(
5 "hf://keras/multi_qa_minilm_l6_dot_v1_en",
6 pooling_mode="cls",
7)
8
9# Max pooling.
10embedder = keras_hub.models.BertTextEmbedder.from_preset(
11 "hf://keras/multi_qa_minilm_l6_dot_v1_en",
12 pooling_mode="max",
13)
14
15# Disable L2 normalization.
16embedder = keras_hub.models.BertTextEmbedder.from_preset(
17 "hf://keras/multi_qa_minilm_l6_dot_v1_en",
18 normalize=False,
19)
20