Views
No views yet
pip:1pip install torch transformers datasets
2pip install tensorflow # If using TensorFlow
3pip install tqdm
4pip install scikit-learn
5Model Overview
6We fine-tuned a pre-trained multilingual model (e.g., BERT Multilingual, mBERT, or XLM-RoBERTa) to perform NLP tasks in both English and Albanian. These models are pre-trained on multiple languages, including English and Albanian, and are then fine-tuned on a custom dataset tailored to your task.
7
8Example Pre-Trained Models:
9bert-base-multilingual-cased
10xlm-roberta-base
11Fine-Tuning Process
121. Load the Pre-Trained Model and Tokenizer
13python
14Copy code
15from transformers import BertTokenizer, BertForSequenceClassification
16
17# Load the pre-trained multilingual model
18model_name = 'bert-base-multilingual-cased'
19tokenizer = BertTokenizer.from_pretrained(model_name)
20model = BertForSequenceClassification.from_pretrained(model_name, num_labels=2) # Adjust num_labels based on your task
212. Prepare the Dataset
22You can fine-tune the model on your own dataset (in English and Albanian) using Hugging Face’s datasets library, or prepare your own dataset in CSV or JSON format.
23
24Example:
25
26python
27Copy code
28from datasets import load_dataset
29
30# Load the dataset (replace with your own dataset)
31dataset = load_dataset('csv', data_files='path_to_your_data.csv')
323. Preprocess the Data
33Use the tokenizer to preprocess the dataset, converting text into token IDs compatible with the pre-trained model.
34
35python
36Copy code
37def preprocess_function(examples):
38 return tokenizer(examples['text'], padding='max_length', truncation=True)
39
40# Apply preprocessing
41tokenized_datasets = dataset.map(preprocess_function, batched=True)
424. Fine-Tuning the Model
43Train the model on your dataset using either PyTorch or TensorFlow. Here's an example using PyTorch:
44
45python
46Copy code
47from torch.utils.data import DataLoader
48from transformers import AdamW
49
50# Set training parameters
51train_dataset = tokenized_datasets['train']
52train_dataloader = DataLoader(train_dataset, batch_size=16, shuffle=True)
53
54# Set optimizer
55optimizer = AdamW(model.parameters(), lr=2e-5)
56
57# Training loop
58model.train()
59for epoch in range(3):
60 for batch in train_dataloader:
61 optimizer.zero_grad()
62 input_ids = batch['input_ids'].to(device)
63 labels = batch['labels'].to(device)
64 outputs = model(input_ids, labels=labels)
65 loss = outputs.loss
66 loss.backward()
67 optimizer.step()
68 print(f"Epoch {epoch}, Loss: {loss.item()}")
695. Evaluate the Model
70After training, evaluate the model’s performance using the validation or test dataset.
71
72python
73Copy code
74from sklearn.metrics import accuracy_score
75
76model.eval()
77# Example evaluation loop
78predictions = []
79labels = []
80for batch in eval_dataloader:
81 with torch.no_grad():
82 input_ids = batch['input_ids'].to(device)
83 labels.append(batch['labels'].numpy())
84 outputs = model(input_ids)
85 preds = torch.argmax(outputs.logits, dim=-1)
86 predictions.append(preds.numpy())
87
88accuracy = accuracy_score(labels, predictions)
89print(f"Accuracy: {accuracy}")
90Languages Supported
91English: The model is fine-tuned on English text for the task at hand (e.g., text classification, sentiment analysis, etc.).
92Albanian: The same model can be used for Albanian text, leveraging multilingual pre-trained weights. The performance may vary depending on the dataset, but mBERT and XLM-R are known to perform well for Albanian.
93Results
94This fine-tuned model provides state-of-the-art performance on both English and Albanian tasks. Results on the validation/test set should demonstrate good generalization across these two languages.
95
96Example Results:
97
98Accuracy: 85% on English dataset
99Accuracy: 80% on Albanian dataset
100Conclusion
101By fine-tuning a pre-trained multilingual model, we significantly reduce the time and computational resources required for training a model from scratch. This approach leverages transfer learning, where the model has already learned general linguistic patterns from a wide variety of languages, allowing it to adapt to specific tasks in both English and Albanian.
102
103License
104This project is licensed under the MIT License - see the LICENSE file for details.
105
106