Edit model card

Model Card for Model ID

This is an idiomatic expression classifier based on the BERT model from Hugging Face's Transformers library. The model is trained to classify sentences containing idiomatic expressions as either figurative or literal.

Model Details

  • Developed by: Abdallah Ashraf
  • Language(s) (NLP): english
  • Finetuned from model: bert-base-uncased

Uses

Direct Use

The model can be used directly for classifying idiomatic expressions in text data.

Downstream Use

The model can also be fine-tuned for specific downstream tasks, such as sentiment analysis or natural language understanding, by incorporating it into larger NLP pipelines.

Out-of-Scope Use

The model may not perform well on non-idiomatic text or text in languages other than English.

Bias, Risks, and Limitations

The model's performance may be influenced by biases present in the training data, such as cultural or linguistic biases. Additionally, the model's accuracy may vary depending on the complexity and context of the idiomatic expressions.

How to Get Started with the Model

To use the model, instantiate the IdiomClassifier class and load the pre-trained weights. Then, tokenize the input text using the BERT tokenizer and pass it through the model for classification.

from transformers import BertModel, BertTokenizer
import torch.nn as nn

# Load the BERT model and tokenizer
bert_model = BertModel.from_pretrained('abdallahashrafx/Bert_idiom_classifier')
tokenizer = BertTokenizer.from_pretrained('abdallahashrafx/Bert_idiom_classifier')

# Define the IdiomClassifier class
class IdiomClassifier(nn.Module):
    def __init__(self):
        super(IdiomClassifier, self).__init__()
        self.bert = bert_model
        self.drop = nn.Dropout(p=0.4)
        self.out = nn.Linear(self.bert.config.hidden_size, 1)

    def forward(self, input_ids, attention_mask):
        _, pooled_output = self.bert(input_ids=input_ids, attention_mask=attention_mask)
        output = self.drop(pooled_output)
        return self.out(output)

# Instantiate the model and move it to the GPU
model = IdiomClassifier().to(device)

How to run inference (predict on raw text)

sentence = "A little bird told me it was your birthday, he said."

# Tokenize and encode the sentence
encoded_sentence = tokenizer.encode_plus(
    sentence,
    max_length=MAX_LEN,
    add_special_tokens=True,
    return_token_type_ids=False,
    pad_to_max_length=True,
    return_attention_mask=True,
    return_tensors='pt',
)

input_ids = encoded_sentence['input_ids'].to(device)
attention_mask = encoded_sentence['attention_mask'].to(device)

output = model(input_ids, attention_mask)
# Apply sigmoid to convert logits to probabilities
probs = torch.sigmoid(output)

# Round probabilities to get predictions
prediction = (probs > 0.5).int()

print(f'sentence: {sentence}')
print(f'class  : {class_names[prediction]}')

Training Details

Training Procedure

The model was trained using the AdamW optimizer with a learning rate of 2e-6 and weight decay of 0.01. A linear scheduler with no warmup was used to adjust the learning rate during training.

Training Hyperparameters

  • Training Hyperparameters
  • Training regime: Full fine-tuning
  • Optimizer: AdamW
  • Learning rate: 2e-6
  • momentum: 90-95
  • Weight decay: 0.01
  • loss function : Binary cross entropy loss

Evaluation

Testing Data, Factors & Metrics

Testing Data

The model was evaluated on a separate test dataset containing sentences with idiomatic expressions and their ground truth classifications.

Metrics

The evaluation metrics used to assess the model's performance include accuracy, precision, recall, and F1-score for both figurative and literal classifications.

Results

The model achieved an overall accuracy of 90% on the test dataset, with balanced precision, recall, and F1-score for both figurative and literal classifications.

Downloads last month
23
Safetensors
Model size
109M params
Tensor type
F32
·
Inference Examples
This model does not have enough activity to be deployed to Inference API (serverless) yet. Increase its social visibility and check back later, or deploy to Inference Endpoints (dedicated) instead.

Dataset used to train abdallahashrafx/Bert_idiom_classifier