NavyaNayer commited on
Commit
3a42b37
·
verified ·
1 Parent(s): 613ca7d

Delete complexity_Score_finetuned.py

Browse files
Files changed (1) hide show
  1. complexity_Score_finetuned.py +0 -273
complexity_Score_finetuned.py DELETED
@@ -1,273 +0,0 @@
1
- <<<<<<< HEAD
2
- import torch
3
- import random
4
- import numpy as np
5
- from tqdm import tqdm
6
- from datasets import load_dataset
7
- from transformers import DistilBertTokenizerFast, DistilBertForSequenceClassification
8
- from torch.utils.data import DataLoader
9
- from transformers import AdamW
10
- from sklearn.metrics import r2_score, f1_score, mean_absolute_error
11
-
12
- # Set random seed for reproducibility
13
- torch.manual_seed(42)
14
- np.random.seed(42)
15
- random.seed(42)
16
-
17
- # Load DEITA-Complexity dataset
18
- dataset = load_dataset("hkust-nlp/deita-complexity-scorer-data")
19
- val_data = dataset["validation"]
20
-
21
- # Initialize tokenizer
22
- tokenizer = DistilBertTokenizerFast.from_pretrained("distilbert-base-uncased")
23
-
24
- # Preprocessing function
25
- def preprocess_function(examples):
26
- return tokenizer(examples["input"], truncation=True, padding="max_length", max_length=128)
27
-
28
- # Tokenize validation dataset
29
- val_encodings = val_data.map(preprocess_function, batched=True)
30
-
31
- # Inspect the structure of val_encodings
32
- print("Validation Encodings Structure:")
33
- print(val_encodings)
34
-
35
- # Convert dataset to PyTorch format
36
- class ComplexityDataset(torch.utils.data.Dataset):
37
- def __init__(self, encodings):
38
- self.encodings = encodings
39
-
40
- def __len__(self):
41
- return len(self.encodings['input_ids'])
42
-
43
- def __getitem__(self, idx):
44
- # Create a dictionary for the inputs
45
- item = {
46
- "input_ids": torch.tensor(self.encodings['input_ids'][idx]),
47
- "attention_mask": torch.tensor(self.encodings['attention_mask'][idx]),
48
- # Convert target to float if it's a string
49
- "labels": torch.tensor(float(self.encodings['target'][idx]), dtype=torch.float) # Ensure 'target' is numeric
50
- }
51
- return item
52
-
53
- val_dataset = ComplexityDataset(val_encodings)
54
-
55
- # Load pre-trained DistilBERT model
56
- model = DistilBertForSequenceClassification.from_pretrained("distilbert-base-uncased", num_labels=1)
57
-
58
- # Freeze first 4 transformer layers
59
- for layer in model.distilbert.transformer.layer[:4]:
60
- for param in layer.parameters():
61
- param.requires_grad = False
62
-
63
- # Define optimizer
64
- optimizer = AdamW(model.parameters(), lr=2e-5)
65
-
66
- # Use GPU if available
67
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
68
- model.to(device)
69
-
70
- # DataLoader for batching
71
- val_loader = DataLoader(val_dataset, batch_size=8, shuffle=False)
72
-
73
- # Evaluation function
74
- def evaluate_model(model, val_loader):
75
- model.eval()
76
- val_loss = 0.0
77
- total_mae = 0.0
78
- all_predictions = []
79
- all_labels = []
80
-
81
- with torch.no_grad():
82
- for batch in tqdm(val_loader, desc="Evaluating", leave=False):
83
- batch = {key: val.to(device) for key, val in batch.items()}
84
- outputs = model(**batch)
85
- loss = torch.nn.functional.mse_loss(outputs.logits.squeeze(), batch["labels"])
86
-
87
- val_loss += loss.item()
88
- total_mae += torch.nn.functional.l1_loss(outputs.logits.squeeze(), batch["labels"], reduction="sum").item()
89
-
90
- all_predictions.extend(outputs.logits.squeeze().cpu().numpy())
91
- all_labels.extend(batch["labels"].cpu().numpy())
92
-
93
- avg_val_loss = val_loss / len(val_loader)
94
- avg_val_mae = total_mae / len(val_loader.dataset)
95
-
96
- # Calculate additional metrics
97
- r2 = r2_score(all_labels, all_predictions)
98
- f1 = f1_score(np.round(all_labels), np.round(all_predictions), average='weighted')
99
-
100
- return avg_val_loss, avg_val_mae, r2, f1, all_predictions, all_labels
101
-
102
- # Evaluate the model
103
- val_loss, val_mae, r2, f1, predictions, labels = evaluate_model(model, val_loader)
104
-
105
- print(f"Validation Loss = {val_loss:.4f}, Validation MAE = {val_mae:.4f}, R² Score = {r2:.4f}, F1 Score = {f1:.4f}")
106
-
107
- # Testing the model (inference on the validation set)
108
- def test_model(model, val_loader):
109
- model.eval()
110
- all_predictions = []
111
- all_labels = []
112
-
113
- with torch.no_grad():
114
- for batch in tqdm(val_loader, desc="Testing", leave=False):
115
- batch = {key: val.to(device) for key, val in batch.items()}
116
- outputs = model(**batch)
117
-
118
- all_predictions.extend(outputs.logits.squeeze().cpu().numpy())
119
- all_labels.extend(batch["labels"].cpu().numpy())
120
-
121
- return np.array(all_predictions), np.array(all_labels)
122
-
123
- # Get predictions and labels from the test function
124
- test_predictions, test_labels = test_model(model, val_loader)
125
-
126
- # You can also calculate the evaluation metrics on the test predictions
127
- test_r2 = r2_score(test_labels, test_predictions)
128
- test_f1 = f1_score(np.round(test_labels), np.round(test_predictions), average='weighted')
129
-
130
- print(f"Test R² Score = {test_r2:.4f}, Test F1 Score = {test_f1:.4f}")
131
-
132
- # Save the fine-tuned model
133
- model.save_pretrained("fine_tuned_deita_model")
134
- tokenizer.save_pretrained("fine_tuned_deita_model")
135
-
136
- print("✅ Evaluation and testing complete! Model saved at 'fine_tuned_deita_model'.")
137
- =======
138
- import torch
139
- import random
140
- import numpy as np
141
- from tqdm import tqdm
142
- from datasets import load_dataset
143
- from transformers import DistilBertTokenizerFast, DistilBertForSequenceClassification
144
- from torch.utils.data import DataLoader
145
- from transformers import AdamW
146
- from sklearn.metrics import r2_score, f1_score, mean_absolute_error
147
-
148
- # Set random seed for reproducibility
149
- torch.manual_seed(42)
150
- np.random.seed(42)
151
- random.seed(42)
152
-
153
- # Load DEITA-Complexity dataset
154
- dataset = load_dataset("hkust-nlp/deita-complexity-scorer-data")
155
- val_data = dataset["validation"]
156
-
157
- # Initialize tokenizer
158
- tokenizer = DistilBertTokenizerFast.from_pretrained("distilbert-base-uncased")
159
-
160
- # Preprocessing function
161
- def preprocess_function(examples):
162
- return tokenizer(examples["input"], truncation=True, padding="max_length", max_length=128)
163
-
164
- # Tokenize validation dataset
165
- val_encodings = val_data.map(preprocess_function, batched=True)
166
-
167
- # Inspect the structure of val_encodings
168
- print("Validation Encodings Structure:")
169
- print(val_encodings)
170
-
171
- # Convert dataset to PyTorch format
172
- class ComplexityDataset(torch.utils.data.Dataset):
173
- def __init__(self, encodings):
174
- self.encodings = encodings
175
-
176
- def __len__(self):
177
- return len(self.encodings['input_ids'])
178
-
179
- def __getitem__(self, idx):
180
- # Create a dictionary for the inputs
181
- item = {
182
- "input_ids": torch.tensor(self.encodings['input_ids'][idx]),
183
- "attention_mask": torch.tensor(self.encodings['attention_mask'][idx]),
184
- # Convert target to float if it's a string
185
- "labels": torch.tensor(float(self.encodings['target'][idx]), dtype=torch.float) # Ensure 'target' is numeric
186
- }
187
- return item
188
-
189
- val_dataset = ComplexityDataset(val_encodings)
190
-
191
- # Load pre-trained DistilBERT model
192
- model = DistilBertForSequenceClassification.from_pretrained("distilbert-base-uncased", num_labels=1)
193
-
194
- # Freeze first 4 transformer layers
195
- for layer in model.distilbert.transformer.layer[:4]:
196
- for param in layer.parameters():
197
- param.requires_grad = False
198
-
199
- # Define optimizer
200
- optimizer = AdamW(model.parameters(), lr=2e-5)
201
-
202
- # Use GPU if available
203
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
204
- model.to(device)
205
-
206
- # DataLoader for batching
207
- val_loader = DataLoader(val_dataset, batch_size=8, shuffle=False)
208
-
209
- # Evaluation function
210
- def evaluate_model(model, val_loader):
211
- model.eval()
212
- val_loss = 0.0
213
- total_mae = 0.0
214
- all_predictions = []
215
- all_labels = []
216
-
217
- with torch.no_grad():
218
- for batch in tqdm(val_loader, desc="Evaluating", leave=False):
219
- batch = {key: val.to(device) for key, val in batch.items()}
220
- outputs = model(**batch)
221
- loss = torch.nn.functional.mse_loss(outputs.logits.squeeze(), batch["labels"])
222
-
223
- val_loss += loss.item()
224
- total_mae += torch.nn.functional.l1_loss(outputs.logits.squeeze(), batch["labels"], reduction="sum").item()
225
-
226
- all_predictions.extend(outputs.logits.squeeze().cpu().numpy())
227
- all_labels.extend(batch["labels"].cpu().numpy())
228
-
229
- avg_val_loss = val_loss / len(val_loader)
230
- avg_val_mae = total_mae / len(val_loader.dataset)
231
-
232
- # Calculate additional metrics
233
- r2 = r2_score(all_labels, all_predictions)
234
- f1 = f1_score(np.round(all_labels), np.round(all_predictions), average='weighted')
235
-
236
- return avg_val_loss, avg_val_mae, r2, f1, all_predictions, all_labels
237
-
238
- # Evaluate the model
239
- val_loss, val_mae, r2, f1, predictions, labels = evaluate_model(model, val_loader)
240
-
241
- print(f"Validation Loss = {val_loss:.4f}, Validation MAE = {val_mae:.4f}, R² Score = {r2:.4f}, F1 Score = {f1:.4f}")
242
-
243
- # Testing the model (inference on the validation set)
244
- def test_model(model, val_loader):
245
- model.eval()
246
- all_predictions = []
247
- all_labels = []
248
-
249
- with torch.no_grad():
250
- for batch in tqdm(val_loader, desc="Testing", leave=False):
251
- batch = {key: val.to(device) for key, val in batch.items()}
252
- outputs = model(**batch)
253
-
254
- all_predictions.extend(outputs.logits.squeeze().cpu().numpy())
255
- all_labels.extend(batch["labels"].cpu().numpy())
256
-
257
- return np.array(all_predictions), np.array(all_labels)
258
-
259
- # Get predictions and labels from the test function
260
- test_predictions, test_labels = test_model(model, val_loader)
261
-
262
- # You can also calculate the evaluation metrics on the test predictions
263
- test_r2 = r2_score(test_labels, test_predictions)
264
- test_f1 = f1_score(np.round(test_labels), np.round(test_predictions), average='weighted')
265
-
266
- print(f"Test R² Score = {test_r2:.4f}, Test F1 Score = {test_f1:.4f}")
267
-
268
- # Save the fine-tuned model
269
- model.save_pretrained("fine_tuned_deita_model")
270
- tokenizer.save_pretrained("fine_tuned_deita_model")
271
-
272
- print("✅ Evaluation and testing complete! Model saved at 'fine_tuned_deita_model'.")
273
- >>>>>>> b1313c5d084e410cadf261f2fafd8929cb149a4f