-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnlp_internship.py
327 lines (252 loc) · 11.2 KB
/
nlp_internship.py
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# -*- coding: utf-8 -*-
"""Nlp internship.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1QStZaOSnlBo2ct3p9gPAmtW4ehPWqkdK
"""
from google.colab import drive
drive.mount('/content/drive')
import pandas as pd
train_df = pd.read_excel("/content/drive/MyDrive/Books/train.xlsx")
eval_df = pd.read_excel("/content/drive/MyDrive/Books/evaluation.xlsx")
!pip install transformers
!pip install datasets
import numpy as np
import pandas as pd
from transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer, TrainingArguments
from transformers import EvalPrediction
from sklearn.metrics import precision_recall_fscore_support, classification_report
from datasets import Dataset
import random
# ... (the rest of the functions remain the same)
# Replace these lines with the actual paths to your train and eval CSV files
# train_df = pd.read_csv("train.csv")
# eval_df = pd.read_csv("eval.csv")
def generate_negatives(df, multiplier=1):
negative_df = df.copy()
for _ in range(multiplier):
negative_df['reason'] = negative_df['reason'].apply(lambda x: ' '.join(random.sample(x.split(), len(x.split()))))
negative_df['label'] = 0
return pd.concat([df, negative_df], ignore_index=True)
def preprocess_dataset(df, tokenizer):
def encode(example):
inputs = tokenizer(example['text'], example['reason'], padding=True, truncation=True, max_length=512, return_tensors='pt')
return {k: v.squeeze(0) for k, v in inputs.items()}
dataset = Dataset.from_pandas(df)
dataset = dataset.map(encode, batched=True)
dataset.set_format(type='torch', columns=['input_ids', 'attention_mask', 'label'])
return dataset
def compute_metrics(eval_pred: EvalPrediction):
predictions = eval_pred.predictions
labels = eval_pred.label_ids
preds = np.argmax(predictions, axis=1)
precision, recall, f1, _ = precision_recall_fscore_support(labels, preds, average='binary')
return {'precision': precision, 'recall': recall, 'f1': f1}
train_df
eval_df
train_df = generate_negatives(train_df, multiplier=1)
models = ['bert-base-uncased', 'distilbert-base-uncased', 'roberta-base']
model_results = {}
for model_name in models:
print(f"Training and evaluating {model_name}")
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=2)
train_dataset = preprocess_dataset(train_df, tokenizer)
eval_dataset = preprocess_dataset(eval_df, tokenizer)
training_args = TrainingArguments(
output_dir=f'./results/{model_name}',
num_train_epochs=3,
per_device_train_batch_size=8,
per_device_eval_batch_size=8,
evaluation_strategy='epoch',
save_strategy='epoch',
logging_dir=f'./logs/{model_name}',
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
tokenizer=tokenizer,
compute_metrics=compute_metrics,
)
trainer.train() # This line was missing in your code
# Error Analysis
predictions = trainer.predict(eval_dataset)
preds = np.argmax(predictions.predictions, axis=1)
report = classification_report(eval_dataset['label'], preds, output_dict=True)
model_results[model_name] = report
print("Error analysis:")
for model_name, report in model_results.items():
print(f"Model: {model_name}")
print(report)
import numpy as np
import pandas as pd
from transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer, TrainingArguments
from transformers import EvalPrediction
from sklearn.metrics import precision_recall_fscore_support, classification_report
from datasets import Dataset
import random
# ... (the rest of the functions remain the same)
def generate_negatives(df, multiplier=1):
positive_df = df[df['label'] == 1]
negative_df = df[df['label'] == 0].sample(len(positive_df), replace=True)
balanced_df = pd.concat([positive_df, negative_df], ignore_index=True)
return balanced_df.sample(frac=1).reset_index(drop=True)
def preprocess_dataset(df, tokenizer):
def encode(example):
inputs = tokenizer(example['text'], example['reason'], padding=True, truncation=True, max_length=512, return_tensors='pt')
return {k: v.squeeze(0) for k, v in inputs.items()}
dataset = Dataset.from_pandas(df)
dataset = dataset.map(encode, batched=True)
dataset.set_format(type='torch', columns=['input_ids', 'attention_mask', 'label'])
return dataset
def compute_metrics(eval_pred: EvalPrediction):
predictions = eval_pred.predictions
labels = eval_pred.label_ids
preds = np.argmax(predictions, axis=1)
precision, recall, f1, _ = precision_recall_fscore_support(labels, preds, average='binary')
return {'precision': precision, 'recall': recall, 'f1': f1}
# ... (preprocess_dataset and compute_metrics functions remain the same)
train_df
eval_df
train_df = generate_negatives(train_df, multiplier=1)
models = ['bert-base-uncased', 'distilbert-base-uncased', 'roberta-base']
model_results = {}
for model_name in models:
print(f"Training and evaluating {model_name}")
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=2)
train_dataset = preprocess_dataset(train_df, tokenizer)
eval_dataset = preprocess_dataset(eval_df, tokenizer)
num_pos = len(train_df[train_df['label'] == 1])
num_neg = len(train_df[train_df['label'] == 0])
pos_weight = num_neg / num_pos
training_args = TrainingArguments(
output_dir=f'./results/{model_name}',
num_train_epochs=3,
per_device_train_batch_size=8,
per_device_eval_batch_size=8,
evaluation_strategy='epoch',
save_strategy='epoch',
logging_dir=f'./logs/{model_name}',
report_to="none",
)
model.config.loss_function = 'CrossEntropyLoss'
model.config.pos_weight = pos_weight
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
tokenizer=tokenizer,
compute_metrics=compute_metrics,
)
trainer.train()
# Error Analysis
predictions = trainer.predict(eval_dataset)
preds = np.argmax(predictions.predictions, axis=1)
report = classification_report(eval_dataset['label'], preds, output_dict=True)
model_results[model_name] = report
print("Error analysis:")
for model_name, report in model_results.items():
print(f"Model: {model_name}")
print(report)
#This is to address the issue of poor class balance
import numpy as np
import pandas as pd
from transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer, TrainingArguments
from transformers import EvalPrediction
from sklearn.metrics import precision_recall_fscore_support, classification_report
from datasets import Dataset
import random
# ... (generate_negatives, preprocess_dataset, and compute_metrics functions remain the same)
def generate_negatives(df, multiplier=1):
positive_df = df[df['label'] == 1]
negative_df = df[df['label'] == 0].sample(len(positive_df), replace=True)
balanced_df = pd.concat([positive_df, negative_df], ignore_index=True)
return balanced_df.sample(frac=1).reset_index(drop=True)
def preprocess_dataset(df, tokenizer):
def encode(example):
inputs = tokenizer(example['text'], example['reason'], padding=True, truncation=True, max_length=512, return_tensors='pt')
return {k: v.squeeze(0) for k, v in inputs.items()}
dataset = Dataset.from_pandas(df)
dataset = dataset.map(encode, batched=True)
dataset.set_format(type='torch', columns=['input_ids', 'attention_mask', 'label'])
return dataset
def compute_metrics(eval_pred: EvalPrediction):
predictions = eval_pred.predictions
labels = eval_pred.label_ids
preds = np.argmax(predictions, axis=1)
precision, recall, f1, _ = precision_recall_fscore_support(labels, preds, average='binary')
return {'precision': precision, 'recall': recall, 'f1': f1}
train_df
eval_df
train_df = generate_negatives(train_df, multiplier=1)
models = ['bert-base-uncased', 'distilbert-base-uncased', 'roberta-base', 'sentence-transformers/bert-base-nli-mean-tokens']
model_results = {}
for model_name in models:
print(f"Training and evaluating {model_name}")
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=2)
train_dataset = preprocess_dataset(train_df, tokenizer)
eval_dataset = preprocess_dataset(eval_df, tokenizer)
num_pos = len(train_df[train_df['label'] == 1])
num_neg = len(train_df[train_df['label'] == 0])
pos_weight = num_neg / num_pos
training_args = TrainingArguments(
output_dir=f'./results/{model_name}',
num_train_epochs=5, # Increase the number of epochs
learning_rate=2e-5, # Fine-tune the learning rate
per_device_train_batch_size=8,
per_device_eval_batch_size=8,
evaluation_strategy='epoch',
save_strategy='epoch',
logging_dir=f'./logs/{model_name}',
report_to="none",
lr_scheduler_type='cosine', # Use cosine learning rate scheduler
)
model.config.loss_function = 'CrossEntropyLoss'
model.config.pos_weight = pos_weight
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
tokenizer=tokenizer,
compute_metrics=compute_metrics,
)
trainer.train()
# Error Analysis
predictions = trainer.predict(eval_dataset)
preds = np.argmax(predictions.predictions, axis=1)
report = classification_report(eval_dataset['label'], preds, output_dict=True)
model_results[model_name] = report
print("Error analysis:")
for model_name, report in model_results.items():
print(f"Model: {model_name}")
print(report)
import pandas as pd
# Prepare the data for the table
data = []
for model_name, report in model_results.items():
precision = report['1']['precision']
recall = report['1']['recall']
f1_score = report['1']['f1-score']
data.append([model_name, precision, recall, f1_score])
# Create the DataFrame and set the column names
results_df = pd.DataFrame(data, columns=['Model', 'Precision', 'Recall', 'F1 Score'])
# Display the DataFrame
print(results_df)
"""To analyze the errors made by each model and discuss possible reasons for these errors, you can start by examining the misclassified examples. This can be done by comparing the ground truth labels with the model's predictions. You can use the following code snippet to get the misclassified examples for each model:
## Error Analysis
"""
misclassified_examples = {}
for model_name in models:
predictions = trainer.predict(eval_dataset)
preds = np.argmax(predictions.predictions, axis=1)
misclassified_indices = np.where(eval_dataset['label'] != preds)[0]
misclassified_examples[model_name] = eval_df.iloc[misclassified_indices]
for model_name, misclassified_df in misclassified_examples.items():
print(f"\nMisclassified examples for {model_name}:")
print(misclassified_df)