-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
executable file
·287 lines (251 loc) · 11.3 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
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
import torch
from tqdm import tqdm
import evaluate
from utils.train import get_decoded_preds_and_labels
from torch.utils.tensorboard import SummaryWriter
def train_loop(
model,
optimizer,
criterion,
early_stopping,
loader_train,
loader_val,
epochs,
device,
step_lr=None,
cfg='',
cfg_name = 'gen_dump.txt',
gen_metrics=None,
tokenizer=None
):
"""
This function excecute the full training procedure for a specified model. It logs all the results obtained during training using a SummaryWriter.
Parameters
----------
model: models.StancePredictionModule
The stance prediction model to be trained.
optimizer: optim.Optimizer
The optimizer object.
criterion: nn.Module
The loss function of the classification task.
early_stopping: utils.early_stopping.EarlyStopping
The early stopping regularization object.
loader_train: torch.utils.data.DataLoader
The data loader of the training dataset.
loader_val: torch.utils.data.DataLoader
The data loader of the validation dataset.
epochs: int
The number of epochs to be excecuted.
device: str
The name of the device where to excecure the training procedure.
step_lr: optim.Optimizer
The learning rate scheduler. Default None.
cfg: yacs.config.CfgNode or str
The configuration file or a description containing all the (hyper)parameters of the model. Default ''.
"""
# Push the model to the GPU if available
if device == 'cuda':
model.cuda()
# Initialize the SummaryWriter
writer = SummaryWriter('log/', flush_secs=1)
writer.add_text('CFG', text_string=str(cfg))
for i in range(epochs):
torch.cuda.empty_cache()
# Execute a training step and store its results
train_results = train(model, optimizer, criterion, loader_train, device)
# Execute a validation step and store its results
val_results = validate(model, criterion, loader_val, device, cfg_name=cfg_name, gen_metrics=gen_metrics, tokenizer=tokenizer)
# Log the results
"""
writer.add_scalar('Train loss', train_results['train_loss'], i)
writer.add_scalar('Train accuracy', train_results['train_accuracy'], i)
writer.add_scalar('Val loss', val_results['val_loss'], i)
writer.add_scalar('Val accuracy', val_results['val_accuracy'], i)"""
# Check if the procedure must terminate due to early stopping
if early_stopping:
if early_stopping(val_results['val_accuracy']):
print('Early stopping triggered, best score: ', early_stopping.best_score)
model.load_state_dict(early_stopping.best_weights)
break
# Take a learining rate step
if step_lr:
step_lr.step()
def train(model, optimizer, criterion, data_loader, device):
"""
This function excecute a single training step.
Parameters
----------
model: models.StancePredictionModule
The stance prediction model to be trained.
optimizer: optim.Optimizer
The optimizer object.
criterion: nn.Module
The loss function of the classification task.
data_loader: torch.utils.data.DataLoader
The data loader of the training dataset.
device: str
The name of the device where to excecure the training procedure.
Returns
----------
results: dict
A dictionary containing the loss and accuracy of this procedure.
"""
model.train()
total_loss = 0.0
total_acc = 0.0
total_cls_loss = 0.0
total_gen_loss = 0.0
total = 0
results = {}
model_name = model.__class__.__name__
for data in tqdm(data_loader):
# Prepare the input according to the type of the model and propagate it to the network
if model_name == 'TextModel':
input_dict = data[0]
input_dict = {k:input_dict[k].to(device) for k in input_dict.keys()}
labels = data[1].to(device)
output = model(**input_dict)
elif model_name == 'AudioModel':
waves = data[0].to(device)
labels = data[1].to(device)
output = model(waves)
elif model_name == 'MultimodalModel':
input_dict = data[0]
input_dict = {k:input_dict[k].to(device) for k in input_dict.keys()}
waves = data[1].to(device)
labels = data[2].to(device)
output = model(input_dict, waves)
elif model_name == 'TextGenerationModel':
input_dict = data['text']
input_dict = {k:input_dict[k].to(device) for k in input_dict.keys()}
waves = None
motion = None
if model.use_audio:
waves = data['audio'].to(device)
if model.generate_motion:
motion = data['motion'].to(device)
labels = data['labels'].to(device)
loss_lm, loss_cls, output = model(input_dict['input_ids'], input_dict['attention_mask'], waves, motion, labels_cls=labels, return_dict=False)
# Compute the loss between the output and the target labels
output = output.squeeze()
if model_name != 'TextGenerationModel':
loss = criterion(output, labels)
else:
if model.generate_motion:
total_cls_loss += loss_cls.detach().item()
total_gen_loss += loss_lm.detach().item()
loss = loss_lm + loss_cls
else:
loss = loss_cls
total_loss += loss.detach().item()
acc = ((output > 0).float() == labels).sum().detach().item()
total_acc += acc
total += labels.size(0)
# Backpropagate and update the optimizer
loss.backward()
optimizer.step()
optimizer.zero_grad(set_to_none=True)
#print('train_loss:', total_loss / len(data_loader), 'train_accuracy:', total_acc / total, end='\t')
results['train_loss'] = total_loss / len(data_loader)
results['train_accuracy'] = total_acc / total
if model_name == 'TextGenerationModel':
if model.generate_motion:
results['train_loss_gen'] = total_gen_loss / len(data_loader)
results['train_loss_cls'] = total_cls_loss / len(data_loader)
print('\t'.join([f'{key}: {results[key]}' for key in results.keys()]), end='\t')
return results
def validate(model, criterion, data_loader, device, cfg_name='gen_dump.txt', gen_metrics=None, tokenizer=None):
"""
This function excecute a single validation step.
Parameters
----------
model: models.StancePredictionModule
The stance prediction model to be validated.
criterion: nn.Module
The loss function of the classification task.
data_loader: torch.utils.data.DataLoader
The data loader of the validation dataset.
device: str
The name of the device where to excecure the validation procedure.
Returns
----------
results: dict
A dictionary containing the loss and accuracy of this procedure.
"""
model.eval()
with torch.no_grad():
gen_metrics = [evaluate.load(metric_name) for metric_name in gen_metrics]
total_loss = 0.0
total_acc = 0.0
total_cls_loss = 0.0
total_gen_loss = 0.0
total = 0
results = {}
model_name = model.__class__.__name__
for data in data_loader:
# Prepare the input according to the type of the model and propagate it to the network
if model_name == 'TextModel':
input_dict = data[0]
input_dict = {k:input_dict[k].to(device) for k in input_dict.keys()}
labels = data[1].to(device)
output = model(**input_dict)
elif model_name == 'AudioModel':
waves = data[0].to(device)
labels = data[1].to(device)
output = model(waves)
elif model_name == 'MultimodalModel':
input_dict = data[0]
input_dict = {k:input_dict[k].to(device) for k in input_dict.keys()}
waves = data[1].to(device)
labels = data[2].to(device)
output = model(input_dict, waves)
elif model_name == 'TextGenerationModel':
input_dict = data['text']
input_dict = {k:input_dict[k].to(device) for k in input_dict.keys()}
waves = None
motion = None
if model.use_audio:
waves = data['audio'].to(device)
if model.generate_motion:
motion = data['motion'].to(device)
labels = data['labels'].to(device)
loss_lm, loss_cls, output = model(input_dict['input_ids'], input_dict['attention_mask'], waves, motion, labels_cls=labels, return_dict=False)
output = output.squeeze()
# Compute the loss between the output and the target labels
if model_name != 'TextGenerationModel':
loss = criterion(output, labels).detach().item()
else:
if model.generate_motion:
total_cls_loss += loss_cls.detach().item()
total_gen_loss += loss_lm.detach().item()
loss = (loss_lm + loss_cls).detach().item()
else:
loss = loss_cls.detach().item()
total_loss += loss
total += labels.size(0)
if model.generate_motion:
# Compute text generation metrics
if gen_metrics is not None:
preds, motions = get_decoded_preds_and_labels(**input_dict, audio=waves, labels=motion, model=model, tokenizer=tokenizer)
with open(cfg_name, 'a', encoding='utf-8') as f:
f.write('\n' + '\n'.join(preds) + '\n')
for metric in gen_metrics:
metric.add_batch(predictions=preds, references=motions)
del preds, motions
acc = ((output > 0).float() == labels).sum().detach().item()
total_acc += acc
results['val_loss'] = total_loss / len(data_loader)
results['val_accuracy'] = total_acc / total
if model_name == 'TextGenerationModel':
if model.generate_motion:
results['val_loss_gen'] = total_gen_loss / len(data_loader)
results['val_loss_cls'] = total_cls_loss / len(data_loader)
if gen_metrics is not None:
for metric in gen_metrics:
try:
results.update(metric.compute())
except ZeroDivisionError:
results[metric.__class__.__name__] = 0.0
print('\t'.join([f'{key}: {results[key] if "rouge" not in key else results[key].mid}' for key in results.keys()]))
del gen_metrics
return results