Downloads · 30 days
4
11% of all-time downloads
keras/bge_m3
bge_m3 is a machine learning model from keras. Use it for the machine learning task on the model card, and read the license before you ship it in a product. It is set up for keras-hub.
BGE (BAAI General Embedding) models for dense text retrieval and semantic similarity tasks, implemented in Keras.
Downloads · 30 days
4
11% of all-time downloads
All-time downloads
37
Public
Repo size
2.3 GB
Likes
0
Public
Click a slice to open those files.
.h52.3 GB · 100%
From the Hugging Face model README
BGE (BAAI General Embedding) models for dense text retrieval and semantic similarity tasks, implemented in Keras.
BGE (BAAI General Embedding) is a family of bi-directional, transformer-based text embedding models developed by the Beijing Academy of Artificial Intelligence (BAAI). Built on the BERT encoder architecture, BGE models are fine-tuned specifically for dense retrieval, semantic similarity, and clustering tasks.
For embedding generation, the model outputs L2-normalized embeddings of the [CLS] token's hidden state, producing fixed-dimensional dense vectors suitable for cosine similarity comparisons.
These models can be used with KerasHub through the BgeTextEmbedder task API.
BGE models follow the standard BERT encoder architecture:
Tokenizer: WordPiece tokenizer with BERT-compatible special tokens ([CLS], [SEP], [PAD]).
Encoder: Multi-layer bi-directional Transformer encoder.
Embedding output: L2-normalized [CLS] token hidden states.
BGE models are trained on large-scale text pair datasets for contrastive learning. See the original paper and BAAI's Hugging Face page for full training details.
Keras and KerasHub can be installed with:
pip install -U -q keras-hub
pip install -U -q keras
Jax, TensorFlow, and Torch come preinstalled in Kaggle Notebooks. For instructions on installing them in another environment see the Keras Getting Started page.
| Preset | Architecture | Pooling | Normalize | Languages |
|---|---|---|---|---|
bge_small_en | BERT | CLS | L2 | English |
bge_base_en | BERT | CLS | L2 | English |
bge_large_en | BERT | CLS | L2 | English |
bge_small_v1.5_en | BERT | CLS | L2 | English |
bge_base_v1.5_en | BERT | CLS | L2 | English |
bge_large_v1.5_en | BERT | CLS | L2 | English |
bge_base_zh | BERT | CLS | L2 | Chinese |
bge_large_zh | BERT | CLS | L2 | Chinese |
bge_small_v1.5_zh | BERT | CLS | L2 | Chinese |
bge_base_v1.5_zh | BERT | CLS | L2 | Chinese |
bge_large_v1.5_zh | BERT | CLS | L2 | Chinese |
bge_llm_embedder | BERT | CLS | L2 | English |
bge_m3 | XLM-RoBERTa | CLS | L2 | 100+ |
# Install and setup
!pip install -q keras-hub
import os
os.environ["KERAS_BACKEND"] = "jax" # or "tensorflow" or "torch"
import keras_hub
import numpy as np
# Load a BGE model from the Kaggle preset
embedder = keras_hub.models.BertTextEmbedder.from_preset("bge_m3")
# Encode text into embeddings
embeddings = embedder.encode_text(["The weather is lovely today."])
print(f"Shape: {embeddings.shape}") # (1, 384)
# Compute similarity between sentences
query = ["What is deep learning?"]
passages = [
"Deep learning is a subset of machine learning using neural networks with many layers.",
"The Eiffel Tower is located in Paris, France.",
"Neural networks learn representations of data through backpropagation.",
]
# Encode the texts into embeddings before passing to similarity
query_embeddings = embedder.encode_text(query)
passage_embeddings = embedder.encode_documents(passages)
# Calculate similarity
scores = embedder.similarity(query_embeddings, passage_embeddings)
print("Similarity scores:")
# Access the first row of scores [0] since we have 1 query
for passage, score in zip(passages, np.array(scores)[0]):
print(f" {float(score):.4f} → {passage[:60]}...")
# Install and setup
!pip install -q keras-hub
import os
os.environ["KERAS_BACKEND"] = "jax" # or "tensorflow" or "torch"
import keras_hub
import numpy as np
# Load a BGE model from the Kaggle preset
embedder = keras_hub.models.BertTextEmbedder.from_preset("hf://keras/bge_m3")
# Encode text into embeddings
embeddings = embedder.encode_text(["The weather is lovely today."])
print(f"Shape: {embeddings.shape}") # (1, 384)
# Compute similarity between sentences
query = ["What is deep learning?"]
passages = [
"Deep learning is a subset of machine learning using neural networks with many layers.",
"The Eiffel Tower is located in Paris, France.",
"Neural networks learn representations of data through backpropagation.",
]
# Encode the texts into embeddings before passing to similarity
query_embeddings = embedder.encode_text(query)
passage_embeddings = embedder.encode_documents(passages)
# Calculate similarity
scores = embedder.similarity(query_embeddings, passage_embeddings)
print("Similarity scores:")
# Access the first row of scores [0] since we have 1 query
for passage, score in zip(passages, np.array(scores)[0]):
print(f" {float(score):.4f} → {passage[:60]}...")