File size: 4,308 Bytes
df70cdf
 
817aed5
 
 
 
 
 
 
 
 
 
 
df70cdf
 
 
 
23ec75d
 
df70cdf
 
 
 
23ec75d
4b1eabe
23ec75d
df70cdf
 
 
 
 
23ec75d
df70cdf
 
4b1eabe
df70cdf
23ec75d
df70cdf
 
 
 
23ec75d
df70cdf
 
 
 
23ec75d
df70cdf
 
 
 
23ec75d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df70cdf
 
 
 
 
23ec75d
df70cdf
 
 
4b1eabe
 
 
 
 
 
 
df70cdf
 
 
 
 
 
 
 
23ec75d
df70cdf
 
 
23ec75d
df70cdf
 
 
23ec75d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
---
library_name: transformers
tags:
- idioms
- idiom recognition
license: apache-2.0
datasets:
- gsarti/magpie
language:
- en
metrics:
- accuracy
pipeline_tag: feature-extraction
---

# 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.

```python
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)

```python
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

<!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the 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.