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 GPT-4o 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
4# Model ve tokenizer adını belirtin
5model_name = "kayrab/turkish-gpt2-medium-gpt4o-qa"
6
7# Tokenizer'ı yükleyin (use_fast=True önerilir)
8tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
9
10# Modeli yükleyin (GPU varsa otomatik olarak GPU'ya yükler)
11# Düşük bellekli GPU'lar için dtype=torch.float16 veya torch.bfloat16 kullanabilirsiniz
12model = AutoModelForCausalLM.from_pretrained(
13 model_name,
14 # torch_dtype=torch.float16, # Opsiyonel: fp16 kullanmak için
15 device_map="auto" # Modeli uygun cihaza (GPU/CPU) dağıtır
16)
17
18# Kullanılacak soruyu tanımlayın
19soru = "Türkiye'nin en kalabalık şehri hangisidir ve neden önemlidir?"
20
21# Soruyu modelin beklediği biçime getirin
22# Dikkat: Prompt'un sonunda <CEVAP> etiketi ve bir boşluk olmalı!
23prompt = f"<SORU> {soru} </SORU> <CEVAP> "
24
25# Girdiyi token'lara çevirin ve modelin cihazına gönderin
26inputs = tokenizer(prompt, return_tensors="pt", return_attention_mask=False).to(model.device)
27
28# Cevap üretme parametreleri
29# </CEVAP> token'ını EOS (End Of Sentence) olarak kullanacağız
30eos_token_id = tokenizer.convert_tokens_to_ids("</CEVAP>")
31if eos_token_id == tokenizer.unk_token_id: # Eğer token eklenmemişse (nadiren olur)
32 eos_token_id = tokenizer.eos_token_id
33
34# Metin üretme (generate) fonksiyonunu çağırın
35outputs = model.generate(
36 **inputs,
37 max_new_tokens=150, # Üretilecek maksimum yeni token sayısı
38 eos_token_id=eos_token_id, # Bu token üretildiğinde dur
39 pad_token_id=tokenizer.eos_token_id, # Padding için EOS kullan
40 do_sample=True, # Olasılıksal örnekleme yap
41 temperature=0.7, # Daha tutarlı çıktılar için sıcaklığı düşür
42 top_p=0.9, # Nucleus sampling
43 no_repeat_ngram_size=3 # 3-gram tekrarını engelle
44)
45
46# Üretilen tokenları alın (girdi prompt'u hariç)
47output_tokens = outputs[0, inputs["input_ids"].shape[1]:]
48
49# Tokenları metne çevirin
50# skip_special_tokens=True, özel token'ları (örn: <|endoftext|>) çıktıdan kaldırır
51cevap = tokenizer.decode(output_tokens, skip_special_tokens=True)
52
53# </CEVAP> etiketi kalıntılarını temizle (generate bazen tam EOS'ta durmaz)
54cevap_temiz = cevap.split("</CEVAP>")[0].strip()
55
56print("-" * 20)
57print(f"Soru: {soru}")
58print("-" * 20)
59print(f"Üretilen Cevap: {cevap_temiz}")
60print("-" * 20)
61
62# Örnek Çıktı (Modele göre değişebilir):
63# --------------------
64# Soru: Türkiye'nin en kalabalık şehri hangisidir ve neden önemlidir?
65# --------------------
66# Ü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.
67# --------------------gpt2_medium_gpt4o.csv dosyasında yer almaktadır.
gpt2_medium_gpt4o.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 GPT-4o 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
4# Specify the model and tokenizer name
5model_name = "kayrab/turkish-gpt2-medium-gpt4o-qa"
6
7# Load the tokenizer (use_fast=True is recommended)
8tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
9
10# Load the model (automatically loads to GPU if available)
11# For low-memory GPUs, you can use dtype=torch.float16 or torch.bfloat16
12model = AutoModelForCausalLM.from_pretrained(
13 model_name,
14 # torch_dtype=torch.float16, # Optional: to use fp16
15 device_map="auto" # Distributes the model to the appropriate device (GPU/CPU)
16)
17
18# Define the question to use
19soru = "Türkiye'nin en kalabalık şehri hangisidir ve neden önemlidir?" # "Which is Turkey's most populous city and why is it important?"
20
21# Format the question into the format expected by the model
22# Note: The prompt must end with the <CEVAP> tag and a space!
23prompt = f"<SORU> {soru} </SORU> <CEVAP> "
24
25# Tokenize the input and send it to the model's device
26inputs = tokenizer(prompt, return_tensors="pt", return_attention_mask=False).to(model.device)
27
28# Answer generation parameters
29# We will use the </CEVAP> token as EOS (End Of Sentence)
30eos_token_id = tokenizer.convert_tokens_to_ids("</CEVAP>")
31if eos_token_id == tokenizer.unk_token_id: # If the token wasn't added (rarely happens)
32 eos_token_id = tokenizer.eos_token_id
33
34# Call the text generation (generate) function
35outputs = model.generate(
36 **inputs,
37 max_new_tokens=150, # Maximum number of new tokens to generate
38 eos_token_id=eos_token_id, # Stop when this token is generated
39 pad_token_id=tokenizer.eos_token_id, # Use EOS for padding
40 do_sample=True, # Perform probabilistic sampling
41 temperature=0.7, # Lower temperature for more consistent outputs
42 top_p=0.9, # Nucleus sampling
43 no_repeat_ngram_size=3 # Prevent 3-gram repetition
44)
45
46# Get the generated tokens (excluding the input prompt)
47output_tokens = outputs[0, inputs["input_ids"].shape[1]:]
48
49# Decode the tokens into text
50# skip_special_tokens=True removes special tokens (e.g., <|endoftext|>) from the output
51cevap = tokenizer.decode(output_tokens, skip_special_tokens=True)
52
53# Clean up any </CEVAP> tag remnants (generate sometimes doesn't stop exactly at EOS)
54cevap_temiz = cevap.split("</CEVAP>")[0].strip()
55
56print("-" * 20)
57print(f"Soru (Question): {soru}")
58print("-" * 20)
59print(f"Üretilen Cevap (Generated Answer): {cevap_temiz}")
60print("-" * 20)
61
62# Example Output (May vary depending on the model):
63# --------------------
64# Soru (Question): Türkiye'nin en kalabalık şehri hangisidir ve neden önemlidir?
65# --------------------
66# Ü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.
67# (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.)
68# --------------------gpt2_medium_gpt4o.csv file.
You can examine the quality of the model's responses to different types of questions by reviewing the gpt2_medium_gpt4o.csv file.turkish-gpt2-medium) and the training data (GPT-4o answers).<SORU> ... </SORU> <CEVAP> format.