Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,125 @@
|
|
1 |
-
---
|
2 |
-
license: apache-2.0
|
3 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: apache-2.0
|
3 |
+
---
|
4 |
+
# Fine-Tuning Pre-Trained Model for English and Albanian
|
5 |
+
|
6 |
+
This project demonstrates the process of fine-tuning a pre-trained model for language tasks in both **English** and **Albanian**. We utilize transfer learning with a pre-trained model (e.g., BERT or multilingual BERT) to adapt it for specific tasks in these two languages, such as text classification, named entity recognition (NER), or sentiment analysis.
|
7 |
+
|
8 |
+
## Requirements
|
9 |
+
|
10 |
+
### Prerequisites
|
11 |
+
- Python 3.7+
|
12 |
+
- TensorFlow or PyTorch
|
13 |
+
- Hugging Face Transformers library
|
14 |
+
- CUDA-enabled GPU (recommended for faster training)
|
15 |
+
|
16 |
+
### Dependencies
|
17 |
+
Install the following Python libraries using `pip`:
|
18 |
+
|
19 |
+
```bash
|
20 |
+
pip install torch transformers datasets
|
21 |
+
pip install tensorflow # If using TensorFlow
|
22 |
+
pip install tqdm
|
23 |
+
pip install scikit-learn
|
24 |
+
Model Overview
|
25 |
+
We 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.
|
26 |
+
|
27 |
+
Example Pre-Trained Models:
|
28 |
+
bert-base-multilingual-cased
|
29 |
+
xlm-roberta-base
|
30 |
+
Fine-Tuning Process
|
31 |
+
1. Load the Pre-Trained Model and Tokenizer
|
32 |
+
python
|
33 |
+
Copy code
|
34 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
35 |
+
|
36 |
+
# Load the pre-trained multilingual model
|
37 |
+
model_name = 'bert-base-multilingual-cased'
|
38 |
+
tokenizer = BertTokenizer.from_pretrained(model_name)
|
39 |
+
model = BertForSequenceClassification.from_pretrained(model_name, num_labels=2) # Adjust num_labels based on your task
|
40 |
+
2. Prepare the Dataset
|
41 |
+
You 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.
|
42 |
+
|
43 |
+
Example:
|
44 |
+
|
45 |
+
python
|
46 |
+
Copy code
|
47 |
+
from datasets import load_dataset
|
48 |
+
|
49 |
+
# Load the dataset (replace with your own dataset)
|
50 |
+
dataset = load_dataset('csv', data_files='path_to_your_data.csv')
|
51 |
+
3. Preprocess the Data
|
52 |
+
Use the tokenizer to preprocess the dataset, converting text into token IDs compatible with the pre-trained model.
|
53 |
+
|
54 |
+
python
|
55 |
+
Copy code
|
56 |
+
def preprocess_function(examples):
|
57 |
+
return tokenizer(examples['text'], padding='max_length', truncation=True)
|
58 |
+
|
59 |
+
# Apply preprocessing
|
60 |
+
tokenized_datasets = dataset.map(preprocess_function, batched=True)
|
61 |
+
4. Fine-Tuning the Model
|
62 |
+
Train the model on your dataset using either PyTorch or TensorFlow. Here's an example using PyTorch:
|
63 |
+
|
64 |
+
python
|
65 |
+
Copy code
|
66 |
+
from torch.utils.data import DataLoader
|
67 |
+
from transformers import AdamW
|
68 |
+
|
69 |
+
# Set training parameters
|
70 |
+
train_dataset = tokenized_datasets['train']
|
71 |
+
train_dataloader = DataLoader(train_dataset, batch_size=16, shuffle=True)
|
72 |
+
|
73 |
+
# Set optimizer
|
74 |
+
optimizer = AdamW(model.parameters(), lr=2e-5)
|
75 |
+
|
76 |
+
# Training loop
|
77 |
+
model.train()
|
78 |
+
for epoch in range(3):
|
79 |
+
for batch in train_dataloader:
|
80 |
+
optimizer.zero_grad()
|
81 |
+
input_ids = batch['input_ids'].to(device)
|
82 |
+
labels = batch['labels'].to(device)
|
83 |
+
outputs = model(input_ids, labels=labels)
|
84 |
+
loss = outputs.loss
|
85 |
+
loss.backward()
|
86 |
+
optimizer.step()
|
87 |
+
print(f"Epoch {epoch}, Loss: {loss.item()}")
|
88 |
+
5. Evaluate the Model
|
89 |
+
After training, evaluate the model’s performance using the validation or test dataset.
|
90 |
+
|
91 |
+
python
|
92 |
+
Copy code
|
93 |
+
from sklearn.metrics import accuracy_score
|
94 |
+
|
95 |
+
model.eval()
|
96 |
+
# Example evaluation loop
|
97 |
+
predictions = []
|
98 |
+
labels = []
|
99 |
+
for batch in eval_dataloader:
|
100 |
+
with torch.no_grad():
|
101 |
+
input_ids = batch['input_ids'].to(device)
|
102 |
+
labels.append(batch['labels'].numpy())
|
103 |
+
outputs = model(input_ids)
|
104 |
+
preds = torch.argmax(outputs.logits, dim=-1)
|
105 |
+
predictions.append(preds.numpy())
|
106 |
+
|
107 |
+
accuracy = accuracy_score(labels, predictions)
|
108 |
+
print(f"Accuracy: {accuracy}")
|
109 |
+
Languages Supported
|
110 |
+
English: The model is fine-tuned on English text for the task at hand (e.g., text classification, sentiment analysis, etc.).
|
111 |
+
Albanian: 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.
|
112 |
+
Results
|
113 |
+
This 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.
|
114 |
+
|
115 |
+
Example Results:
|
116 |
+
|
117 |
+
Accuracy: 85% on English dataset
|
118 |
+
Accuracy: 80% on Albanian dataset
|
119 |
+
Conclusion
|
120 |
+
By 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.
|
121 |
+
|
122 |
+
License
|
123 |
+
This project is licensed under the MIT License - see the LICENSE file for details.
|
124 |
+
|
125 |
+
|