Fine-tune EmbeddingGemma  |  Google AI for Developers Skip to main content Models Gemini About Docs API reference Pricing Imagen About Docs Pricing Veo About Docs Pricing Gemma About Docs Gemmaverse Solutions Build with Gemini Gemini API Google AI Studio Customize Gemma open models Gemma open models Multi-framework with Keras Fine-tune in Colab Run on-device Google AI Edge Gemini Nano on Android Chrome built-in web APIs Build responsibly Responsible GenAI Toolkit Secure AI Framework Code assistance Android Studio Chrome DevTools Colab Firebase Google Cloud JetBrains Jules VS Code Community Google AI Forum Gemini for Research / English Deutsch Español – América Latina Français Indonesia Italiano Polski Português – Brasil Shqip Tiếng Việt Türkçe Русский עברית العربيّة فارسی हिंदी বাংলা ภาษาไทย 中文 – 简体 中文 – 繁體 日本語 한국어 Sign in Gemma Gemma Docs Models More Gemma Docs Solutions More Code assistance More Community More Overview Get started Releases Models Core Gemma Overview Gemma 4 model card Gemma 3 model card Gemma 2 model card Gemma 1 model card Core Variants Gemma 3n Overview Model card DiffusionGemma Overview Model card Diffusion Explained Generate Output FunctionGemma Overview Model card Formatting and best practices Function calling with Hugging Face Transformers Full function calling sequence with FunctionGemma Fine-tune FunctionGemma EmbeddingGemma Overview Model card Generate embeddings with Sentence Transformers Fine-tune EmbeddingGemma PaliGemma Overview v2 model card v1 model card Generate output with Keras Fine-tune with JAX and Flax Prompt and system instructions ShieldGemma Overview ShieldGemma 2 Model card ShieldGemma 1 Model card Run Gemma Fundamentals Overview Prompt Formatting Legacy Gemma setup [Gemma 1, 2, and 3] Legacy Prompt and system instructions [Gemma 1, 2, and 3] Run locally with a Chat UI or integrate via API LM Studio Ollama Run efficiently on Edge LiteRT-LM Llama.cpp MLX Build/Train in Python Tunix (Tune-in-JAX) Hugging Face Transformers Keras Unsloth Deploy to Production / Enterprise Gemini API Google Cloud Cloud GKE Multi-Token Prediction (MTP) Overview Hugging Face Transformers Core Capabilities Text Basic and multi-turn chat Function calling Visual data Overview Image understanding Video understanding Audio data Thinking Tuning guides Overview Tune using Hugging Face Transformers and QLoRA Vision Tune using Hugging Face Transformers and QLoRA Full model fine-tune using Hugging Face Transformers Tune using Gemma library Research and tools RecurrentGemma Overview Inference using JAX and Flax Fine-tune using JAX and Flax Model card DataGemma Gemma Scope Gemma-APS Community Gemmaverse Discord Legal Terms of use Gemma 4 license Prohibited use Intended use statement Gemini About Docs API reference Pricing Imagen About Docs Pricing Veo About Docs Pricing Gemma About Docs Gemmaverse Build with Gemini Gemini API Google AI Studio Customize Gemma open models Gemma open models Multi-framework with Keras Fine-tune in Colab Run on-device Google AI Edge Gemini Nano on Android Chrome built-in web APIs Build responsibly Responsible GenAI Toolkit Secure AI Framework Android Studio Chrome DevTools Colab Firebase Google Cloud JetBrains Jules VS Code Google AI Forum Gemini for Research Gemma 4 released with text, audio and image input and long up to 256K context window! Learn more Home Gemma Models Docs Send feedback Fine-tune EmbeddingGemma View on ai.google.dev Run in Google Colab Run in Kaggle Open in Vertex AI View source on GitHub Fine-tuning helps close the gap between a model's general-purpose understanding and the specialized, high-performance accuracy that your application requires. Since no single model is perfect for every task, fine-tuning adapts it to your specific domain. Imagine your company, "Shibuya Financial" offers various complex financial products like investment trusts, NISA accounts (a tax-advantaged savings account), and home loans. Your customer support team uses an internal knowledge base to quickly find answers to customer questions. Setup Before starting this tutorial, complete the following steps: Get access to EmbeddingGemma by logging into Hugging Face and selecting Acknowledge license for a Gemma model. Generate a Hugging Face Access Token and use it to login from Colab. This notebook will run on either CPU or GPU. Install Python packages Install the libraries required for running the EmbeddingGemma model and generating embeddings. Sentence Transformers is a Python framework for text and image embeddings. For more information, see the Sentence Transformers documentation. pip install -U sentence-transformers git+https://github.com/huggingface/transformers@v4.56.0-Embedding-Gemma-preview After you have accepted the license, you need a valid Hugging Face Token to access the model. # Login into Hugging Face Hub from huggingface_hub import login login() Load Model Use the sentence-transformers libraries to create an instance of a model class with EmbeddingGemma. import torch from sentence_transformers import SentenceTransformer device = "cuda" if torch.cuda.is_available() else "cpu" model_id = "google/embeddinggemma-300M" model = SentenceTransformer(model_id).to(device=device) print(f"Device: {model.device}") print(model) print("Total number of parameters in the model:", sum([p.numel() for _, p in model.named_parameters()])) Device: cuda:0 SentenceTransformer( (0): Transformer({'max_seq_length': 2048, 'do_lower_case': False, 'architecture': 'Gemma3TextModel'}) (1): Pooling({'word_embedding_dimension': 768, 'pooling_mode_cls_token': False, 'pooling_mode_mean_tokens': True, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False, 'pooling_mode_weightedmean_tokens': False, 'pooling_mode_lasttoken': False, 'include_prompt': True}) (2): Dense({'in_features': 768, 'out_features': 3072, 'bias': False, 'activation_function': 'torch.nn.modules.linear.Identity'}) (3): Dense({'in_features': 3072, 'out_features': 768, 'bias': False, 'activation_function': 'torch.nn.modules.linear.Identity'}) (4): Normalize() ) Total number of parameters in the model: 307581696 Prepare the Fine-Tuning Dataset This is the most crucial part. You need to create a dataset that teaches the model what "similar" means in your specific context. This data is often structured as triplets: (anchor, positive, negative) Anchor: The original query or sentence. Positive: A sentence that is semantically very similar or identical to the anchor. Negative: A sentence that is on a related topic but semantically distinct. In this example, we only prepared 3 triplets, but for a real application, you would need a much larger dataset to perform well. from datasets import Dataset dataset = [ ["How do I open a NISA account?", "What is the procedure for starting a new tax-free investment account?", "I want to check the balance of my regular savings account."], ["Are there fees for making an early repayment on a home loan?", "If I pay back my house loan early, will there be any costs?", "What is the management fee for this investment trust?"], ["What is the coverage for medical insurance?", "Tell me about the benefits of the health insurance plan.", "What is the cancellation policy for my life insurance?"], ] # Convert the list-based dataset into a list of dictionaries. data_as_dicts = [ {"anchor": row[0], "positive": row[1], "negative": row[2]} for row in dataset ] # Create a Hugging Face `Dataset` object from the list of dictionaries. train_dataset = Dataset.from_list(data_as_dicts) print(train_dataset) Dataset({ features: ['anchor', 'positive', 'negative'], num_rows: 3 }) Before Fine-Tuning A search for "tax-free investment" might have given the following results, with similarity scores: Document: Opening a NISA account (Score: 0.51) Document: Opening a Regular Saving Account (Score: 0.50) <- Similar score, potentially confusing Document: Home Loan Application Guide (Score: 0.44) Note: To generate optimal embeddings with EmbeddingGemma, you should add an "instructional prompt" or "task" to the beginning of your input text. You will use STS for sentence similarity. For details on all available EmbeddingGemma prompts, see the model card. task_name = "STS" def get_scores(query, documents): # Calculate embeddings by calling model.encode() query_embeddings = model.encode(query, prompt_name=task_name) doc_embeddings = model.encode(documents, prompt_name=task_name) # Calculate the embedding similarities similarities = model.similarity(query_embeddings, doc_embeddings) for idx, doc in enumerate(documents): print("Document: ", doc, "-> 🤖 Score: ", similarities.numpy()[0][idx]) query = "I want to start a tax-free installment investment, what should I do?" documents = ["Opening a NISA Account", "Opening a Regular Savings Account", "Home Loan Application Guide"] get_scores(query, documents) Document: Opening a NISA Account -> 🤖 Score: 0.51571906 Document: Opening a Regular Savings Account -> 🤖 Score: 0.5035889 Document: Home Loan Application Guide -> 🤖 Score: 0.4406476 Training Using a framework like sentence-transformers in Python, the base model gradually learns the subtle distinctions in your financial vocabulary. from sentence_transformers import SentenceTransformerTrainer, SentenceTransformerTrainingArguments from sentence_transformers.losses import MultipleNegativesRankingLoss from transformers import TrainerCallback loss = MultipleNegativesRankingLoss(model) args = SentenceTransformerTrainingArguments( # Required parameter: output_dir="my-embedding-gemma", # Optional training parameters: prompts=model.prompts[task_name], # use model's prompt to train num_train_epochs=5, per_device_train_batch_size=1, learning_rate=2e-5, warmup_ratio=0.1, # Optional tracking/debugging parameters: logging_steps=train_dataset.num_rows, report_to="none", ) class MyCallback(TrainerCallback): "A callback that evaluates the model at the end of eopch" def __init__(self, evaluate): self.evaluate = evaluate # evaluate function def on_log(self, args, state, control, **kwargs): # Evaluate the model using text generation print(f"Step {state.global_step} finished. Running evaluation:") self.evaluate() def evaluate(): get_scores(query, documents) trainer = SentenceTransformerTrainer( model=model, args=args, train_dataset=train_dataset, loss=loss, callbacks=[MyCallback(evaluate)] ) trainer.train() Step 3 finished. Running evaluation: Document: Opening a NISA Account -> 🤖 Score: 0.6459116 Document: Opening a Regular Savings Account -> 🤖 Score: 0.42690125 Document: Home Loan Application Guide -> 🤖 Score: 0.40419024 Step 6 finished. Running evaluation: Document: Opening a NISA Account -> 🤖 Score: 0.68530923 Document: Opening a Regular Savings Account -> 🤖 Score: 0.3611964 Document: Home Loan Application Guide -> 🤖 Score: 0.40812016 Step 9 finished. Running evaluation: Document: Opening a NISA Account -> 🤖 Score: 0.7168733 Document: Opening a Regular Savings Account -> 🤖 Score: 0.3449782 Document: Home Loan Application Guide -> 🤖 Score: 0.44477722 Step 12 finished. Running evaluation: Document: Opening a NISA Account -> 🤖 Score: 0.73008573 Document: Opening a Regular Savings Account -> 🤖 Score: 0.34124148 Document: Home Loan Application Guide -> 🤖 Score: 0.4676212 Step 15 finished. Running evaluation: Document: Opening a NISA Account -> 🤖 Score: 0.73378766 Document: Opening a Regular Savings Account -> 🤖 Score: 0.34055778 Document: Home Loan Application Guide -> 🤖 Score: 0.47503752 Step 15 finished. Running evaluation: Document: Opening a NISA Account -> 🤖 Score: 0.73378766 Document: Opening a Regular Savings Account -> 🤖 Score: 0.34055778 Document: Home Loan Application Guide -> 🤖 Score: 0.47503752 TrainOutput(global_step=15, training_loss=0.009651267528511198, metrics={'train_runtime': 195.3004, 'train_samples_per_second': 0.077, 'train_steps_per_second': 0.077, 'total_flos': 0.0, 'train_loss': 0.009651267528511198, 'epoch': 5.0}) After Fine-Tuning The same search now yields much clearer results: Document: Opening a NISA account (Score: 0.73) <- Much more confident Document: Opening a Regular Saving Account (Score: 0.34) <- Clearly less relevant Document: Home Loan Application Guide (Score: 0.47) get_scores(query, documents) Document: Opening a NISA Account -> 🤖 Score: 0.73378766 Document: Opening a Regular Savings Account -> 🤖 Score: 0.34055778 Document: Home Loan Application Guide -> 🤖 Score: 0.47503752 To upload your model to the Hugging Face Hub, you can use the push_to_hub method from the Sentence Transformers library. Uploading your model makes it easy to access for inference directly from the Hub, share with others, and version your work. Once uploaded, anyone can load your model with a single line of code, simply by referencing its unique model ID <username>/my-embedding-gemma # Push to Hub model.push_to_hub("my-embedding-gemma") Summary and next steps You have now learned how to adapt an EmbeddingGemma model for a specific domain by fine-tuning it with the Sentence Transformers library. Explore what more you can do with EmbeddingGemma: Training Overview in Sentence Transformers Documentation Generate embeddings with Sentence Transformers Simple RAG example in the Gemma Cookbook Send feedback Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Last updated 2026-04-22 UTC. Need to tell us more? [[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2026-04-22 UTC."],[],[]] Terms Privacy Manage cookies English Deutsch Español – América Latina Français Indonesia Italiano Polski Português – Brasil Shqip Tiếng Việt Türkçe Русский עברית العربيّة فارسی हिंदी বাংলা ภาษาไทย 中文 – 简体 中文 – 繁體 日本語 한국어