[0eda78]: / src / utils / training.py

Download this file

170 lines (115 with data), 6.0 kB

  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
from utils.dataloader import Dataloader
from utils.BertArchitecture import BertNER
from utils.BertArchitecture import BioBertNER
from utils.metric_tracking import MetricsTracking
import torch
from torch.optim import SGD
from torch.utils.data import DataLoader
import numpy as np
import pandas as pd
from tqdm import tqdm
def train_loop(model, train_dataset, eval_dataset, optimizer, batch_size, epochs, type, train_sampler=None, eval_sampler=None, verbose=True):
"""
Usual training loop, including training and evaluation.
Parameters:
model (BertNER | BioBertNER): Model to be trained.
train_dataset (Custom_Dataset): Dataset used for training.
eval_dataset (Custom_Dataset): Dataset used for testing.
optimizer (torch.optim): Optimizer used, usually SGD or Adam.
batch_size (int): Batch size used during training.
epochs (int): Number of epochs used for training.
train_sampler (SubsetRandomSampler): Sampler used during hyperparameter-tuning.
val_subsampler (SubsetRandomSampler): Sampler used during hyperparameter-tuning.
verbose (bool): Whether the model should be evaluated after each epoch or not.
Returns:
tuple:
- train_res (dict): A dictionary containing the results obtained during training.
- test_res (dict): A dictionary containing the results obtained during testing.
"""
if train_sampler == None or eval_sampler == None:
train_dataloader = DataLoader(train_dataset, batch_size = batch_size, shuffle = False, sampler=train_sampler)
eval_dataloader = DataLoader(eval_dataset, batch_size = batch_size, shuffle = False, sampler=eval_sampler)
else:
train_dataloader = DataLoader(train_dataset, batch_size = batch_size, shuffle = False)
eval_dataloader = DataLoader(eval_dataset, batch_size = batch_size, shuffle = False)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device)
#training
for epoch in range(epochs):
train_metrics = MetricsTracking(type)
model.train() #train mode
for train_data in tqdm(train_dataloader):
train_label = train_data['entity'].to(device)
mask = train_data['attention_mask'].squeeze(1).to(device)
input_id = train_data['input_ids'].squeeze(1).to(device)
optimizer.zero_grad()
output = model(input_id, mask, train_label)
loss, logits = output.loss, output.logits
predictions = logits.argmax(dim=-1)
#compute metrics
train_metrics.update(predictions, train_label, loss.item())
loss.backward()
optimizer.step()
if verbose:
model.eval() #evaluation mode
eval_metrics = MetricsTracking(type)
with torch.no_grad():
for eval_data in eval_dataloader:
eval_label = eval_data['entity'].to(device)
mask = eval_data['attention_mask'].squeeze(1).to(device)
input_id = eval_data['input_ids'].squeeze(1).to(device)
output = model(input_id, mask, eval_label)
loss, logits = output.loss, output.logits
predictions = logits.argmax(dim=-1)
eval_metrics.update(predictions, eval_label, loss.item())
train_results = train_metrics.return_avg_metrics(len(train_dataloader))
eval_results = eval_metrics.return_avg_metrics(len(eval_dataloader))
print(f"Epoch {epoch+1} of {epochs} finished!")
print(f"TRAIN\nMetrics {train_results}\n")
print(f"VALIDATION\nMetrics {eval_results}\n")
if not verbose:
model.eval() #evaluation mode
eval_metrics = MetricsTracking(type)
with torch.no_grad():
for eval_data in eval_dataloader:
eval_label = eval_data['entity'].to(device)
mask = eval_data['attention_mask'].squeeze(1).to(device)
input_id = eval_data['input_ids'].squeeze(1).to(device)
output = model(input_id, mask, eval_label)
loss, logits = output.loss, output.logits
predictions = logits.argmax(dim=-1)
eval_metrics.update(predictions, eval_label, loss.item())
train_results = train_metrics.return_avg_metrics(len(train_dataloader))
eval_results = eval_metrics.return_avg_metrics(len(eval_dataloader))
print(f"Epoch {epoch+1} of {epochs} finished!")
print(f"TRAIN\nMetrics {train_results}\n")
print(f"VALIDATION\nMetrics {eval_results}\n")
return train_results, eval_results
def testing(model, test_dataset, batch_size, type):
"""
Function for testing a trained model.
Parameters:
model (BertNER | BioBertNER): Model to be tested
train_dataset (Custom_Dataset): Dataset used for testing
batch_size (int): Batch size used during training.
Returns:
tuple:
- test_res (dict): A dictionary containing the results obtained during testing.
"""
test_dataloader = DataLoader(test_dataset, batch_size = batch_size, shuffle = False)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device)
model.eval() #evaluation mode
test_metrics = MetricsTracking(type)
with torch.no_grad():
for test_data in test_dataloader:
test_label = test_data['entity'].to(device)
mask = test_data['attention_mask'].squeeze(1).to(device)
input_id = test_data['input_ids'].squeeze(1).to(device)
output = model(input_id, mask, test_label)
loss, logits = output.loss, output.logits
predictions = logits.argmax(dim=-1)
test_metrics.update(predictions, test_label, loss.item())
test_results = test_metrics.return_avg_metrics(len(test_dataloader))
print(f"TEST\nMetrics {test_results}\n")
return test_results