Welcome to a step-by-step guide on how to harness the power of the fine-tuned Turkish GPT-2 model! This blog will provide you with insights on setting up and using this remarkable language generation model.
Model Overview
This model is based on GPT-2-Small and has been fine-tuned using articles from Turkish Wikipedia as of October 28, 2020. With its current accuracy of 33% and perplexity of 51.88, this model is equipped to generate Turkish text efficiently. You can check out a live demo of this model at www.metayazar.com.
Getting Started
To get started, you will need to set up your environment and install the necessary libraries.
Installation Steps
- Make sure you have Python installed on your machine.
- Install necessary libraries using pip:
- transformers
- torch
Setting Up the Model
Begin by importing the required libraries and loading the fine-tuned model as follows:
from transformers import AutoTokenizer, AutoModelWithLMHead
import torch
# Load the tokenizer and model
tokenizer = AutoTokenizer.from_pretrained("gorkemgoknar/gpt2-small-turkish")
model = AutoModelWithLMHead.from_pretrained("gorkemgoknar/gpt2-small-turkish")
# Set maximum sequence length
tokenizer.model_max_length = 1024
model.eval() # Disable dropout
Generating Text
Once the model is set up, you can start generating text! Let’s dive into two methods: generating a single word and generating a full sequence of text.
1. Generate a Single Word
Here’s how to generate a single word using your input:
# Input sequence
text = "Bu yazıyı bilgisayar yazdı."
inputs = tokenizer(text, return_tensors="pt")
# Model output
outputs = model(**inputs, labels=inputs["input_ids"])
loss, logits = outputs[:2]
predicted_index = torch.argmax(logits[0, -1, :]).item()
predicted_text = tokenizer.decode(predicted_index)
# Output results
print("Input text:", text)
print("Predicted text:", predicted_text)
2. Generate a Full Sequence
If you want a complete sentence or paragraph, you can do so with the following code:
# Input sequence
text = "Bu yazıyı bilgisayar yazdı."
inputs = tokenizer(text, return_tensors="pt")
# Model output using Top-k sampling method
sample_outputs = model.generate(
inputs.input_ids,
pad_token_id=50256,
do_sample=True,
max_length=50,
top_k=40,
num_return_sequences=1
)
# Generated sequences
for i, sample_output in enumerate(sample_outputs):
print("Generated text {}: {}".format(i + 1, tokenizer.decode(sample_output.tolist())))
Troubleshooting
While working with this model, you may encounter some common issues. Here are a few troubleshooting tips:
- Model not loading: Ensure that you have an active internet connection and the correct model path in your code.
- Memory issues: If you run out of memory, consider reducing the max length of the sequences or using a smaller model.
- No output generated: Check your input strings. The model needs some context to generate meaningful output.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
Using the fine-tuned Turkish GPT-2 model opens doors to many fascinating applications, like content creation, language support, and much more!
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.