Views
No views yet
<SORU> ve <CEVAP> etiketleriyle yapılandırılmış bir biçimde sunulan sorulara yanıt vermek üzere eğitilmiştir. Eğitimde kullanılan cevaplar DeepSeek modeli tarafından üretilmiştir. Amaç, temel modelin belirli bir talimat biçimine uyarak tutarlı ve bağlama uygun cevaplar üretme yeteneğini geliştirmektir..csv dosyasındaki verilerle eğitilmiştir:<SORU> [Soru metni buraya gelecek] </SORU> <CEVAP> [Cevap metni buraya gelecek] </CEVAP><|endoftext|><SORU> ve </SORU>: Sorunun başlangıcını ve bitişini işaretler.<CEVAP> ve </CEVAP>: Cevabın başlangıcını ve bitişini işaretler.<|endoftext|>: GPT-2'nin standart metin sonu (EOS) belirteci olup, her örneğin bittiğini gösterir.transformers ve trl (Transformer Reinforcement Learning) kütüphaneleri kullanılarak SFTTrainer (Supervised Fine-tuning Trainer) ile eğitilmiştir. Eğitimde kullanılan temel hiperparametreler şunlardır:c_attn, c_proj, c_fc (GPT-2 mimarisine uygun dikkat ve feed-forward katmanları)<SORU>, </SORU>, <CEVAP> belirteçleri kayıp (loss) hesaplamasından maskelenmiştir (ignore_index = -100). Yalnızca cevap kısmındaki (</CEVAP> hariç) belirteçler üzerinden öğrenme gerçekleşmiştir.
transformers kütüphanesi ile kolayca kullanabilirsiniz. Model, girdiyi eğitimde kullanılan biçimde beklemektedir (<SORU> ... </SORU> <CEVAP>).1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3# Model ve tokenizer adını belirtin
4model_name = "kayrab/turkish-gpt2-medium-deepseek-qa"
5# Tokenizer'ı yükleyin (use_fast=True önerilir)
6tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
7# Modeli yükleyin (GPU varsa otomatik olarak GPU'ya yükler)
8# Düşük bellekli GPU'lar için dtype=torch.float16 veya torch.bfloat16 kullanabilirsiniz
9model = AutoModelForCausalLM.from_pretrained(
10 model_name,
11 # torch_dtype=torch.float16, # Opsiyonel: fp16 kullanmak için
12 device_map="auto" # Modeli uygun cihaza (GPU/CPU) dağıtır
13)
14# Kullanılacak soruyu tanımlayın
15soru = "Türkiye'nin en kalabalık şehri hangisidir ve neden önemlidir?"
16# Soruyu modelin beklediği biçime getirin
17# Dikkat: Prompt'un sonunda <CEVAP> etiketi ve bir boşluk olmalı!
18prompt = f"<SORU> {soru} </SORU> <CEVAP> "
19# Girdiyi token'lara çevirin ve modelin cihazına gönderin
20inputs = tokenizer(prompt, return_tensors="pt", return_attention_mask=False).to(model.device)
21# Cevap üretme parametreleri
22# </CEVAP> token'ını EOS (End Of Sentence) olarak kullanacağız
23eos_token_id = tokenizer.convert_tokens_to_ids("</CEVAP>")
24if eos_token_id == tokenizer.unk_token_id: # Eğer token eklenmemişse (nadiren olur)
25 eos_token_id = tokenizer.eos_token_id
26# Metin üretme (generate) fonksiyonunu çağırın
27outputs = model.generate(
28 **inputs,
29 max_new_tokens=150, # Üretilecek maksimum yeni token sayısı
30 eos_token_id=eos_token_id, # Bu token üretildiğinde dur
31 pad_token_id=tokenizer.eos_token_id, # Padding için EOS kullan
32 do_sample=True, # Olasılıksal örnekleme yap
33 temperature=0.7, # Daha tutarlı çıktılar için sıcaklığı düşür
34 top_p=0.9, # Nucleus sampling
35 no_repeat_ngram_size=3 # 3-gram tekrarını engelle
36)
37# Üretilen tokenları alın (girdi prompt'u hariç)
38output_tokens = outputs[0, inputs["input_ids"].shape[1]:]
39# Tokenları metne çevirin
40# skip_special_tokens=True, özel token'ları (örn: <|endoftext|>) çıktıdan kaldırır
41cevap = tokenizer.decode(output_tokens, skip_special_tokens=True)
42# </CEVAP> etiketi kalıntılarını temizle (generate bazen tam EOS'ta durmaz)
43cevap_temiz = cevap.split("</CEVAP>")[0].strip()
44print("-" * 20)
45print(f"Soru: {soru}")
46print("-" * 20)
47print(f"Üretilen Cevap: {cevap_temiz}")
48print("-" * 20)
49# Örnek Çıktı (Modele göre değişebilir):
50# --------------------
51# Soru: Türkiye'nin en kalabalık şehri hangisidir ve neden önemlidir?
52# --------------------
53# Üretilen Cevap: Türkiye'nin en kalabalık şehri İstanbul'dur. İstanbul, tarihi, kültürel ve ekonomik açıdan büyük bir öneme sahiptir. İki kıtayı birbirine bağlayan stratejik konumu, zengin tarihi mirası ve Türkiye ekonomisinin merkezi olması nedeniyle önemlidir.
54# --------------------gpt2_medium_deepseek.csv dosyasında yer almaktadır.
gpt2_medium_deepseek.csv dosyasını inceleyerek modelin farklı türdeki sorulara verdiği yanıtların kalitesini görebilirsiniz.<SORU> ... </SORU> <CEVAP> biçimi dışında verilen girdilere beklenmedik veya anlamsız yanıtlar üretebilir.<SORU> and <CEVAP> tags. The answers used during training were generated by the DeepSeek model. The goal is to enhance the base model's ability to produce consistent and contextually appropriate answers following a specific instruction format..csv file with the following structure:<SORU> [Question text here] </SORU> <CEVAP> [Answer text here] </CEVAP><|endoftext|><SORU> and </SORU>: Mark the beginning and end of the question.<CEVAP> and </CEVAP>: Mark the beginning and end of the answer.<|endoftext|>: GPT-2's standard end-of-text (EOS) token, indicating the end of each example.
These special tokens were added to the tokenizer, expanding the model's vocabulary.transformers and trl (Transformer Reinforcement Learning) libraries with the SFTTrainer (Supervised Fine-tuning Trainer). The core hyperparameters used during training are:c_attn, c_proj, c_fc (Attention and feed-forward layers suitable for GPT-2 architecture)<SORU>, </SORU>, <CEVAP> were masked from the loss calculation (ignore_index = -100). Learning occurred only over the tokens in the answer part (excluding </CEVAP>).
transformers library. The model expects the input in the format used during training (<SORU> ... </SORU> <CEVAP>).1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3# Specify the model and tokenizer name
4model_name = "kayrab/turkish-gpt2-medium-deepseek-qa"
5# Load the tokenizer (use_fast=True is recommended)
6tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
7# Load the model (automatically loads to GPU if available)
8# For low-memory GPUs, you can use dtype=torch.float16 or torch.bfloat16
9model = AutoModelForCausalLM.from_pretrained(
10 model_name,
11 # torch_dtype=torch.float16, # Optional: to use fp16
12 device_map="auto" # Distributes the model to the appropriate device (GPU/CPU)
13)
14# Define the question to use
15soru = "Türkiye'nin en kalabalık şehri hangisidir ve neden önemlidir?" # "Which is Turkey's most populous city and why is it important?"
16# Format the question into the format expected by the model
17# Note: The prompt must end with the <CEVAP> tag and a space!
18prompt = f"<SORU> {soru} </SORU> <CEVAP> "
19# Tokenize the input and send it to the model's device
20inputs = tokenizer(prompt, return_tensors="pt", return_attention_mask=False).to(model.device)
21# Answer generation parameters
22# We will use the </CEVAP> token as EOS (End Of Sentence)
23eos_token_id = tokenizer.convert_tokens_to_ids("</CEVAP>")
24if eos_token_id == tokenizer.unk_token_id: # If the token wasn't added (rarely happens)
25 eos_token_id = tokenizer.eos_token_id
26# Call the text generation (generate) function
27outputs = model.generate(
28 **inputs,
29 max_new_tokens=150, # Maximum number of new tokens to generate
30 eos_token_id=eos_token_id, # Stop when this token is generated
31 pad_token_id=tokenizer.eos_token_id, # Use EOS for padding
32 do_sample=True, # Perform probabilistic sampling
33 temperature=0.7, # Lower temperature for more consistent outputs
34 top_p=0.9, # Nucleus sampling
35 no_repeat_ngram_size=3 # Prevent 3-gram repetition
36)
37# Get the generated tokens (excluding the input prompt)
38output_tokens = outputs[0, inputs["input_ids"].shape[1]:]
39# Decode the tokens into text
40# skip_special_tokens=True removes special tokens (e.g., <|endoftext|>) from the output
41cevap = tokenizer.decode(output_tokens, skip_special_tokens=True)
42# Clean up any </CEVAP> tag remnants (generate sometimes doesn't stop exactly at EOS)
43cevap_temiz = cevap.split("</CEVAP>")[0].strip()
44print("-" * 20)
45print(f"Soru (Question): {soru}")
46print("-" * 20)
47print(f"Üretilen Cevap (Generated Answer): {cevap_temiz}")
48print("-" * 20)
49# Example Output (May vary depending on the model):
50# --------------------
51# Soru (Question): Türkiye'nin en kalabalık şehri hangisidir ve neden önemlidir?
52# --------------------
53# Üretilen Cevap (Generated Answer): Türkiye'nin en kalabalık şehri İstanbul'dur. İstanbul, tarihi, kültürel ve ekonomik açıdan büyük bir öneme sahiptir. İki kıtayı birbirine bağlayan stratejik konumu, zengin tarihi mirası ve Türkiye ekonomisinin merkezi olması nedeniyle önemlidir.
54# (English: Turkey's most populous city is Istanbul. Istanbul holds great importance historically, culturally, and economically. It is important due to its strategic location connecting two continents, its rich historical heritage, and being the center of Turkey's economy.)
55# --------------------gpt2_medium_deepseek.csv file.
You can examine the quality of the model's responses to different types of questions by reviewing the gpt2_medium_deepseek.csv file.turkish-gpt2-medium) and the training data (DeepSeek answers).<SORU> ... </SORU> <CEVAP> format.