![]() |
![]() |
![]() |
|
![]() |
İnce ayar, bir modelin genel amaçlı anlayışı ile uygulamanızın gerektirdiği uzmanlaşmış, yüksek performanslı doğruluk arasındaki farkı kapatmaya yardımcı olur. Hiçbir model her görev için mükemmel olmadığından ince ayar, modeli alanınıza göre uyarlar.
"Shibuya Financial" adlı şirketinizin yatırım fonları, NISA hesapları (vergi avantajlı bir tasarruf hesabı) ve konut kredileri gibi çeşitli karmaşık mali ürünler sunduğunu düşünün. Müşteri desteği ekibiniz, müşteri sorularının yanıtlarını hızlıca bulmak için dahili bir bilgi bankası kullanıyor.
Kurulum
Bu eğitime başlamadan önce aşağıdaki adımları tamamlayın:
- Hugging Face'e giriş yapıp bir Gemma modeli için Lisansı onayla'yı seçerek EmbeddingGemma'ya erişin.
- Hugging Face Erişim Jetonu oluşturun ve Colab'den giriş yapmak için kullanın.
Bu not defteri CPU veya GPU'da çalıştırılabilir.
Python paketlerini yükleme
EmbeddingGemma modelini çalıştırmak ve yerleştirmeler oluşturmak için gereken kitaplıkları yükleyin. Sentence Transformers, metin ve resim yerleştirmeleri için kullanılan bir Python çerçevesidir. Daha fazla bilgi için Sentence Transformers belgelerini inceleyin.
pip install -U sentence-transformers git+https://github.com/huggingface/transformers@v4.56.0-Embedding-Gemma-preview
Lisansı kabul ettikten sonra modele erişmek için geçerli bir Hugging Face jetonuna ihtiyacınız vardır.
# Login into Hugging Face Hub
from huggingface_hub import login
login()
Modeli Yükle
EmbeddingGemma ile bir model sınıfının örneğini oluşturmak için sentence-transformers
kitaplıklarını kullanın.
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
İnce ayar veri kümesini hazırlama
Bu en önemli kısımdır. Modeli, kendi bağlamınızda "benzer"in ne anlama geldiği konusunda eğiten bir veri kümesi oluşturmanız gerekir. Bu veriler genellikle üçlü olarak yapılandırılır: (bağlantı, pozitif, negatif)
- Çapa: Orijinal sorgu veya cümle.
- Olumlu: Anlama göre, çapa ile çok benzer veya aynı olan bir cümle.
- Olumsuz: Anlamsal olarak farklı ancak ilgili bir konudaki cümle.
Bu örnekte yalnızca 3 üçlü hazırladık ancak gerçek bir uygulamada iyi performans göstermek için çok daha büyük bir veri kümesine ihtiyacınız olacaktır.
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 })
İnce Ayardan Önce
"Vergisiz yatırım" araması, benzerlik puanlarıyla birlikte aşağıdaki sonuçları vermiş olabilir:
- Belge: NISA hesabı açma (Puan: 0,45)
- Belge: Normal Tasarruf Hesabı Açma (Puan: 0,48) <- Benzer puan, kafa karıştırabilir
- Belge: Ev Kredisi Başvuru Kılavuzu (Puan: 0,42)
task_name = "STS"
def get_scores(query, documents):
# Calculate embeddings by calling model.encode()
query_embeddings = model.encode(query, prompt=task_name)
doc_embeddings = model.encode(documents, prompt=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.45698774 Document: Opening a Regular Savings Account -> 🤖 Score: 0.48092696 Document: Home Loan Application Guide -> 🤖 Score: 0.42127067
Eğitim
Python'da sentence-transformers
gibi bir çerçeve kullanıldığında temel model, finansal kelime dağarcığınızdaki ince ayrımları yavaş yavaş öğrenir.
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.6449194 Document: Opening a Regular Savings Account -> 🤖 Score: 0.44123 Document: Home Loan Application Guide -> 🤖 Score: 0.46752414 Step 6 finished. Running evaluation: Document: Opening a NISA Account -> 🤖 Score: 0.68873787 Document: Opening a Regular Savings Account -> 🤖 Score: 0.34069622 Document: Home Loan Application Guide -> 🤖 Score: 0.50065553 Step 9 finished. Running evaluation: Document: Opening a NISA Account -> 🤖 Score: 0.7148906 Document: Opening a Regular Savings Account -> 🤖 Score: 0.30480516 Document: Home Loan Application Guide -> 🤖 Score: 0.52454984 Step 12 finished. Running evaluation: Document: Opening a NISA Account -> 🤖 Score: 0.72614634 Document: Opening a Regular Savings Account -> 🤖 Score: 0.29255486 Document: Home Loan Application Guide -> 🤖 Score: 0.5370023 Step 15 finished. Running evaluation: Document: Opening a NISA Account -> 🤖 Score: 0.7294032 Document: Opening a Regular Savings Account -> 🤖 Score: 0.2893038 Document: Home Loan Application Guide -> 🤖 Score: 0.54087913 Step 15 finished. Running evaluation: Document: Opening a NISA Account -> 🤖 Score: 0.7294032 Document: Opening a Regular Savings Account -> 🤖 Score: 0.2893038 Document: Home Loan Application Guide -> 🤖 Score: 0.54087913 TrainOutput(global_step=15, training_loss=0.009651281436261646, metrics={'train_runtime': 63.2486, 'train_samples_per_second': 0.237, 'train_steps_per_second': 0.237, 'total_flos': 0.0, 'train_loss': 0.009651281436261646, 'epoch': 5.0})
İnce Ayardan Sonra
Aynı arama artık çok daha net sonuçlar veriyor:
- Belge: NISA hesabı açma (Puan: 0,72) <- Çok daha fazla güven
- Belge: Normal Tasarruf Hesabı Açma (Puan: 0,28) <- Açıkça daha az alakalı
- Belge: Ev Kredisi Başvuru Kılavuzu (Puan: 0,54)
get_scores(query, documents)
Document: Opening a NISA Account -> 🤖 Score: 0.7294032 Document: Opening a Regular Savings Account -> 🤖 Score: 0.2893038 Document: Home Loan Application Guide -> 🤖 Score: 0.54087913
Modelinizi Hugging Face Hub'a yüklemek için Sentence Transformers kitaplığındaki push_to_hub
yöntemini kullanabilirsiniz.
Modelinizi yükleyerek doğrudan Hub'dan çıkarım için erişebilir, başkalarıyla paylaşabilir ve çalışmanızın farklı sürümlerini oluşturabilirsiniz. Yüklendikten sonra herkes, benzersiz model kimliğine <username>/my-embedding-gemma
referans vererek tek bir kod satırıyla modelinizi yükleyebilir.
# Push to Hub
model.push_to_hub("my-embedding-gemma")
Özet ve sonraki adımlar
Artık EmbeddingGemma modelini Sentence Transformers kitaplığıyla ince ayar yaparak belirli bir alana nasıl uyarlayacağınızı öğrendiniz.
EmbeddingGemma ile yapabileceklerinizi keşfedin:
- Sentence Transformers belgelerindeki Training Overview (Eğitime Genel Bakış)
- Sentence Transformers ile yerleştirilmiş öğeler oluşturma
- Gemma Cookbook'taki basit RAG örneği