-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
218 lines (171 loc) · 6.88 KB
/
train.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
import os
import logging
import torch
import numpy as np
import pandas as pd
import time
from transformers import AdamW, get_linear_schedule_with_warmup
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from dataloader import Dataloader
# helper func
def calcuate_accu(big_idx, targets):
n_correct = (big_idx == targets).sum().item()
return n_correct
# train func
def train(epoch, train_dataloader, model):
tr_loss = 0
n_correct = 0
nb_tr_steps = 0
nb_tr_examples = 0
model.train()
torch.set_grad_enabled(True)
for step, data in enumerate(train_dataloader, 0):
input_ids = data['input_ids'].to(device)
attention_mask = data['attention_mask'].to(device)
targets = data['labels'].to(device)
model.zero_grad()
outputs = model(input_ids,
attention_mask=attention_mask,
labels=targets)
loss = outputs[0]
tr_loss = tr_loss + loss.item()
big_val, big_idx = torch.max(outputs[1], dim=1)
n_correct += calcuate_accu(big_idx, targets)
nb_tr_steps += 1
nb_tr_examples += targets.size(0)
if step % 100 == 0:
loss_step = tr_loss/nb_tr_steps
accu_step = (n_correct*100)/nb_tr_examples
print(f"Training Loss per 100 steps: {loss_step}")
print(f"Training Accuracy per 100 steps: {accu_step}")
# zero the gradients before backpropagating
optimizer.zero_grad()
# Backpropagation
loss.backward()
# Clip the norm of the gradients to 1.0
# This is to help prevent the "exploding gradients" problem.
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
# # When using GPU
optimizer.step()
# Update the learning rate.
scheduler.step()
print(
f'The Total Accuracy for Epoch {epoch+1}: {(n_correct*100)/nb_tr_examples}')
epoch_loss = tr_loss/nb_tr_steps
epoch_accu = (n_correct*100)/nb_tr_examples
print(f"Training Loss Epoch at {epoch+1}: {epoch_loss}")
print(f"Training Accuracy Epoch at {epoch+1}: {epoch_accu}")
print('=' * 30)
return epoch_loss, epoch_accu
# validation
def evaluation(epoch, val_dataloader, model):
val_loss = 0
n_correct = 0
nb_tr_steps = 0
nb_tr_examples = 0
model.eval()
torch.set_grad_enabled(False)
for step, data in enumerate(val_dataloader, 0):
input_ids = data['input_ids'].to(device)
attention_mask = data['attention_mask'].to(device)
targets = data['labels'].to(device)
outputs = model(
input_ids, attention_mask=attention_mask, labels=targets)
loss = outputs[0]
val_loss = val_loss + loss.item()
big_val, big_idx = torch.max(outputs.logits, dim=1)
n_correct += calcuate_accu(big_idx, targets)
nb_tr_steps += 1
nb_tr_examples += targets.size(0)
if step % 50 == 0:
loss_step = val_loss/nb_tr_steps
accu_step = (n_correct*100)/nb_tr_examples
print(f"Validation Loss per 50 steps: {loss_step}")
print(f"Validation Accuracy per 50 steps: {accu_step}")
print(
f'The Total Accuracy for Epoch {epoch+1}: {(n_correct*100)/nb_tr_examples}')
epoch_loss = val_loss/nb_tr_steps
epoch_accu = (n_correct*100)/nb_tr_examples
print(f"Validation Loss Epoch at {epoch+1}: {epoch_loss}")
print(f"Validation Accuracy Epoch at {epoch+1}: {epoch_accu}")
print('=' * 30)
return epoch_loss, epoch_accu
if __name__ == "__main__":
# reproducability
RANDOM_SEED = 42
np.random.seed(RANDOM_SEED)
torch.manual_seed(RANDOM_SEED)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(device)
logging.basicConfig(level=logging.ERROR)
# dir setup
EXPERIMENT_NAME = "banglabert-auto_5e-5_3eps"
ROOT_DIR = os.path.abspath("/content/drive/MyDrive/Saved Models")
LOG_PATH = os.path.join(ROOT_DIR, "logs", EXPERIMENT_NAME)
if not os.path.exists(os.path.join(ROOT_DIR, "logs")):
os.mkdir(os.path.join(ROOT_DIR, "logs"))
if not os.path.exists(LOG_PATH):
os.mkdir(LOG_PATH)
# Load Pre-Trained Model
PRE_TRAINED_MODEL_NAME = 'csebuetnlp/banglabert'
tokenizer = AutoTokenizer.from_pretrained(PRE_TRAINED_MODEL_NAME)
# Hyper params
MAX_LEN = 200
BATCH_SIZE = 16
EPOCHS = 3
L_RATE = 5e-5
WEIGHT_DECAY = 1e-2
# instantiate dataloaders
df_train = pd.read_csv("/content/10K_Dataset/Final_Train.csv")
df_val = pd.read_csv("/content/10K_Dataset/Final_Val.csv")
train_dataloader = Dataloader(df_train, tokenizer, MAX_LEN, BATCH_SIZE)
val_dataloader = Dataloader(df_val, tokenizer, MAX_LEN, BATCH_SIZE)
# instantiate model
model = AutoModelForSequenceClassification.from_pretrained(PRE_TRAINED_MODEL_NAME,
num_labels=3)
model = model.to(device)
# optimizer and scheduler
optimizer = AdamW(model.parameters(), lr=L_RATE)
total_steps = len(train_dataloader) * EPOCHS
scheduler = get_linear_schedule_with_warmup(optimizer,
num_warmup_steps=220,
num_training_steps=total_steps)
# training
start_time = time.time()
train_loss = []
train_acc = []
val_loss = []
val_acc = []
best_accuracy = 0
for epoch in range(EPOCHS):
training_loss, training_accuracy = train(
epoch, train_dataloader, model)
train_loss.append(training_loss)
train_acc.append(training_accuracy)
validation_loss, validation_accuracy = evaluation(
epoch, val_dataloader, model)
if validation_accuracy > best_accuracy:
print("Saving model at accuracy={:.3f}".format(
validation_accuracy))
torch.save(model.state_dict(),
'{}/{}.pth'.format(LOG_PATH, EXPERIMENT_NAME))
best_accuracy = validation_accuracy
val_loss.append(validation_loss)
val_acc.append(validation_accuracy)
elapsed = (time.time() - start_time)/60
print(f'Time elapsed: {elapsed:.2f} min')
elapsed = (time.time() - start_time)/60
print(f'Total Training Time: {elapsed:.2f} min')
# save results
train_loss = np.array(train_loss)
np.savetxt("{}/{}_train_loss.txt".format(LOG_PATH,
EXPERIMENT_NAME), train_loss, delimiter=",")
val_loss = np.array(val_loss)
np.savetxt("{}/{}_val_loss.txt".format(LOG_PATH,
EXPERIMENT_NAME), val_loss, delimiter=",")
train_acc = np.array(train_acc)
np.savetxt("{}/{}_train_acc.txt".format(LOG_PATH,
EXPERIMENT_NAME), train_acc, delimiter=",")
val_acc = np.array(val_acc)
np.savetxt("{}/{}_val_acc.txt".format(LOG_PATH,
EXPERIMENT_NAME), val_acc, delimiter=",")