-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mazharul Islam Rakib_ID-20101408.py
213 lines (145 loc) · 6.74 KB
/
Mazharul Islam Rakib_ID-20101408.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
# -*- coding: utf-8 -*-
"""iSarcasmRevisited.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1waGnG5CcX0kdf1xLl11RpqwjpjFXB4oj
"""
!pip install transformers
!pip install seaborn matplotlib
import pandas as pd
from transformers import BertTokenizer, BertForSequenceClassification
from torch.utils.data import DataLoader, TensorDataset, RandomSampler
import torch
from sklearn.model_selection import train_test_split
from sklearn.metrics import precision_score, recall_score, confusion_matrix
"""Dyanmically Importing Data From Drive"""
# from google.colab import drive
# drive.mount('/content/drive')
"""My Drive Link -- https://drive.google.com/drive/folders/1YNYYFPXxMwGHmPl03Ede033DT5Op76Nk?usp=sharing
Reading Train Data
"""
train_data = pd.read_csv('/content/train.En.csv')
print(train_data.columns)
texts = train_data['tweet'].tolist()
labels = train_data['sarcastic'].tolist()
"""Reading Test Data"""
# test_data = pd.read_csv('/content/drive/MyDrive/CSE_440/Test Data/task_A_En_test.csv')
# test_data = pd.read_csv('/content/drive/MyDrive/CSE_440/Test Data/task_B_En_test.csv')
# test_data = pd.read_csv('/content/drive/MyDrive/CSE_440/Test Data/task_C_En_test.csv')
# If uploading to Colab Uncomment those as required
test_data = pd.read_csv('/content/task_A_En_test.csv')
# test_data = pd.read_csv('/content/task_B_En_test.csv')
# test_data = pd.read_csv('/content/task_C_En_test.csv')
print(test_data.columns)
# For Test Data "A"
test_texts = test_data['text'].tolist()
test_labels = test_data['sarcastic'].tolist()
# For Test Data "B"
# test_texts = test_data['text'].tolist()
# test_labels = test_data['sarcasm'].tolist()
# For Test Data "C"
# Combining two Texts
# test_data['combined_text'] = test_data['text_0'] + ' ' + test_data['text_1']
# test_texts = test_data['combined_text'].tolist()
# test_labels = test_data['sarcastic_id'].tolist()
print(test_texts)
"""Preprocessing using BERT tokenizer"""
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
# Remove NaN Values: Before tokenizing, ensure that your texts list doesn't have any nan values. We filter out these values.d
texts = [text for text in texts if pd.notna(text)]
# # Replace NaN Values: Alternatively, you can replace nan values with a placeholder string (like "[UNK]" which stands for "unknown" in BERT's vocabulary)
# texts = [text if pd.notna(text) else "[UNK]" for text in texts]
input_ids = [tokenizer.encode(text, add_special_tokens=True) for text in texts]
"""Padding input_ids"""
max_len = max([len(ids) for ids in input_ids])
input_ids = [ids + [0] * (max_len - len(ids)) for ids in input_ids]
input_ids = torch.tensor(input_ids)
"""Attention masks"""
attention_masks = [[float(id != 0) for id in ids] for ids in input_ids]
"""Equalize the lengths of input_ids and labels (OpenAI)"""
min_length = min(len(input_ids), len(labels))
input_ids = input_ids[:min_length]
labels = labels[:min_length]
"""Split Data (OpenAi)"""
from sklearn.model_selection import train_test_split
train_inputs, val_inputs, train_labels, val_labels, train_masks, val_masks = train_test_split(
input_ids, labels, attention_masks, test_size=0.2, random_state=42)
"""Attention Mask (OpenAi)"""
attention_masks = [[1 if token_id > 0 else 0 for token_id in input_id] for input_id in input_ids]
"""Splitting into training and validation sets"""
train_inputs, val_inputs, train_labels, val_labels = train_test_split(input_ids, labels, test_size=0.2)
train_masks, val_masks = train_test_split(attention_masks, test_size=0.2)
"""Convert to PyTorch data types"""
train_inputs, val_inputs = torch.tensor(train_inputs), torch.tensor(val_inputs)
train_labels, val_labels = torch.tensor(train_labels), torch.tensor(val_labels)
train_masks, val_masks = torch.tensor(train_masks), torch.tensor(val_masks)
"""Create DataLoader"""
train_data = TensorDataset(train_inputs, train_masks, train_labels)
train_loader = DataLoader(train_data, sampler=RandomSampler(train_data), batch_size=32)
val_data = TensorDataset(val_inputs, val_masks, val_labels)
val_loader = DataLoader(val_data, batch_size=32)
"""Model"""
model = BertForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=2)
optimizer = torch.optim.Adam(model.parameters(), lr=1e-5)
"""Training"""
model.train()
for epoch in range(3): # Number of epochs
for batch in train_loader:
inputs, masks, labels = batch
optimizer.zero_grad()
outputs = model(inputs, attention_mask=masks, labels=labels)
loss = outputs.loss
loss.backward()
optimizer.step()
"""Preprocessing test data"""
# Ensure no NaN values in test_texts before encoding
test_texts = [text for text in test_texts if pd.notna(text)]
# Encode, truncate to max_len if longer, or pad with 0's if shorter
test_input_ids = [tokenizer.encode(text, add_special_tokens=True) for text in test_texts]
test_input_ids = [ids[:max_len] + [0] * (max_len - len(ids)) for ids in test_input_ids]
# Convert to torch tensor
test_input_ids = torch.tensor(test_input_ids)
"""Attention masks for test data"""
test_attention_masks = [[float(id != 0) for id in ids] for ids in test_input_ids]
"""Convert to PyTorch data types"""
test_inputs = torch.tensor(test_input_ids)
test_labels = torch.tensor(test_labels)
test_masks = torch.tensor(test_attention_masks)
"""Create DataLoader for test data"""
test_data = TensorDataset(test_inputs, test_masks, test_labels)
test_loader = DataLoader(test_data, batch_size=32)
"""Evaluation on test data"""
model.eval()
true_labels = []
predicted_labels = []
correct = 0
with torch.no_grad():
for batch in test_loader:
inputs, masks, labels = batch
outputs = model(inputs, attention_mask=masks)
predictions = torch.argmax(outputs.logits, dim=1)
correct += (predictions == labels).sum().item()
# Append true and predicted labels
true_labels.extend(labels.tolist())
predicted_labels.extend(predictions.tolist())
print(f'Test Accuracy: {correct / len(test_labels)}')
"""Calculate precision, recall, and confusion matrix"""
# true_labels = []
# predicted_labels = []
precision = precision_score(true_labels, predicted_labels)
recall = recall_score(true_labels, predicted_labels)
conf_matrix = confusion_matrix(true_labels, predicted_labels)
print(f'Precision: {precision}')
print(f'Recall: {recall}')
print(f'Confusion Matrix:\n{conf_matrix}')
import matplotlib.pyplot as plt
import seaborn as sns
# Plotting the confusion matrix
plt.figure(figsize=(8, 6))
sns.set(font_scale=1.2)
sns.heatmap(conf_matrix, annot=True, fmt='g', cmap='Blues', cbar=False,
xticklabels=['Non-Sarcastic', 'Sarcastic'], yticklabels=['Non-Sarcastic', 'Sarcastic'])
plt.xlabel('Predicted Labels')
plt.ylabel('True Labels')
plt.title('Confusion Matrix')
plt.show()