This repository contains a fine-tuned Sentence Transformer model trained on the "Omartificial-Intelligence-Space/Arabic-NLi-Triplet" dataset. The model is trained to generate 384-dimensional embeddings for semantic similarity tasks like paraphrase mining, sentence similarity, and clustering in Arabic.
The dataset contains triplets of sentences in Arabic: an anchor sentence, a positive sentence (semantically similar to the anchor), and a negative sentence (semantically dissimilar to the anchor). The dataset is designed for learning sentence representations through triplet margin loss.
Below is the training loss recorded at various steps during the training process:
The model was trained using the following code (without resuming from checkpoints):
1from datasets import load_dataset
2from transformers import AutoTokenizer, AutoModel, TrainingArguments, Trainer
3from torch.nn import TripletMarginLoss
4
5# Load dataset
6dataset = load_dataset("Omartificial-Intelligence-Space/Arabic-NLi-Triplet")
7
8# Load tokenizer
9tokenizer = AutoTokenizer.from_pretrained("intfloat/multilingual-e5-small")
10
11# Tokenize function
12def tokenize_function(examples):
13 anchor_encodings = tokenizer(examples['anchor'], truncation=True, padding='max_length', max_length=128)
14 positive_encodings = tokenizer(examples['positive'], truncation=True, padding='max_length', max_length=128)
15 negative_encodings = tokenizer(examples['negative'], truncation=True, padding='max_length', max_length=128)
16
17 return {
18 'anchor_input_ids': anchor_encodings['input_ids'],
19 'anchor_attention_mask': anchor_encodings['attention_mask'],
20 'positive_input_ids': positive_encodings['input_ids'],
21 'positive_attention_mask': positive_encodings['attention_mask'],
22 'negative_input_ids': negative_encodings['input_ids'],
23 'negative_attention_mask': negative_encodings['attention_mask'],
24 }
25
26tokenized_datasets = dataset.map(tokenize_function, batched=True, remove_columns=dataset["train"].column_names)
27
28# Define triplet loss
29triplet_loss = TripletMarginLoss(margin=1.0)
30
31def compute_loss(anchor_embedding, positive_embedding, negative_embedding):
32 return triplet_loss(anchor_embedding, positive_embedding, negative_embedding)
33
34# Load model
35model = AutoModel.from_pretrained("intfloat/multilingual-e5-small")
36
37class TripletTrainer(Trainer):
38 def compute_loss(self, model, inputs, return_outputs=False):
39 anchor_input_ids = inputs['anchor_input_ids'].to(self.args.device)
40 anchor_attention_mask = inputs['anchor_attention_mask'].to(self.args.device)
41 positive_input_ids = inputs['positive_input_ids'].to(self.args.device)
42 positive_attention_mask = inputs['positive_attention_mask'].to(self.args.device)
43 negative_input_ids = inputs['negative_input_ids'].to(self.args.device)
44 negative_attention_mask = inputs['negative_attention_mask'].to(self.args.device)
45
46 anchor_embeds = model(input_ids=anchor_input_ids, attention_mask=anchor_attention_mask).last_hidden_state.mean(dim=1)
47 positive_embeds = model(input_ids=positive_input_ids, attention_mask=positive_attention_mask).last_hidden_state.mean(dim=1)
48 negative_embeds = model(input_ids=negative_input_ids, attention_mask=negative_attention_mask).last_hidden_state.mean(dim=1)
49
50 return compute_loss(anchor_embeds, positive_embeds, negative_embeds)
51
52# Training arguments
53training_args = TrainingArguments(
54 output_dir="/content/drive/MyDrive/results",
55 learning_rate=2e-5,
56 per_device_train_batch_size=16,
57 num_train_epochs=3,
58 weight_decay=0.01,
59 logging_dir='/content/drive/MyDrive/logs',
60 remove_unused_columns=False,
61 fp16=True,
62 save_total_limit=3,
63)
64
65# Initialize trainer
66trainer = TripletTrainer(
67 model=model,
68 args=training_args,
69 train_dataset=tokenized_datasets['train'],
70)
71
72# Start training
73trainer.train()
74
75# Save model and evaluate
76trainer.save_model("/content/drive/MyDrive/fine-tuned-multilingual-e5")
77results = trainer.evaluate()
78print(results)
To use the model, install the required libraries and load the model with the following code:
1from sentence_transformers import SentenceTransformer
2
3# Load the fine-tuned model
4model = SentenceTransformer("gimmeursocks/ara-e5-small")
5
6# Run inference
7sentences = ['أنا سعيد', 'الجو جميل اليوم', 'هذا كلب كبير']
8embeddings = model.encode(sentences)
9print(embeddings.shape)
If you use this model or dataset, please cite the corresponding paper or dataset source.