File size: 6,469 Bytes
95456ed | 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 | import torch
import torch.nn as nn
import numpy as np
import transformers
from typing import Optional
class EarlyStopping:
def __init__(
self,
patience: int,
path: str,
delta: Optional[int] = 0,
):
self.patience = patience
self.delta = delta
self.best_score = None
self.early_stop = False
self.counter = 0
self.best_loss = np.inf
self.path = path
def __call__(
self,
val_loss,
model,
):
score = -val_loss
if self.best_score is None:
self.best_score = score
self.save_checkpoint(val_loss, model)
elif score < self.best_score + self.delta:
self.counter += 1
print(f"EarlyStopping counter: {self.counter} out of {self.patience}")
if self.counter >= self.patience:
self.early_stop = True
else:
self.best_score = score
self.save_checkpoint(val_loss, model)
self.counter = 0
def save_checkpoint(
self,
val_loss,
model,
):
"""Saves model when validation loss decreases."""
print(
f"Validation loss decreased ({self.best_loss:.6f} --> {val_loss:.6f}). Saving model..."
)
torch.save(model.state_dict(), self.path)
self.best_loss = val_loss
def train(
model,
num_epochs: int,
train_loader: torch.utils.data.DataLoader,
val_loader: torch.utils.data.DataLoader,
criterion: nn.MSELoss,
optimizer: transformers.AdamW,
early_stopping: EarlyStopping,
scheduler: transformers.get_linear_schedule_with_warmup,
device: torch.device,
fix_dur_colname: str,
output_attentions: Optional[bool] = None,
use_attention_mask: Optional[bool] = None,
):
"""
Train loop to train the Seq2Seq model.
:param model: the model to train
:param num_epochs: number of epochs to train
:param train_loader: the training data loader
:param val_loader: the validation data loader
:param criterion: the loss function (MSE Loss)
:param optimizer: the optimizer (AdamW)
:param early_stopping: the early stopping object
:param scheduler: the learning rate scheduler
:param device: the device to train on
:param fix_dur_colname: the name of the column containing the fixations durations
"""
for epoch in range(num_epochs):
model.train()
for batch_idx, train_batch in enumerate(train_loader):
optimizer.zero_grad()
sp_embeddings = train_batch["sp_embeddings"].to(device)
attention_mask = train_batch["attention_masks"].to(device)
fix_durs = train_batch[fix_dur_colname].to(device)
# forward pass
if use_attention_mask:
if output_attentions:
out, _ = model(
sp_embeddings=sp_embeddings,
attention_mask=attention_mask,
output_attentions=output_attentions,
)
else:
out = model(
sp_embeddings=sp_embeddings,
attention_mask=attention_mask,
output_attentions=output_attentions,
)
else:
if output_attentions:
out, _ = model(
sp_embeddings=sp_embeddings,
output_attentions=output_attentions,
)
else:
out = model(
sp_embeddings=sp_embeddings,
output_attentions=output_attentions,
)
# train_loss = criterion(out, fix_durs)
# mask the padding in the loss computation
loss_mask = (fix_durs != 0).float()
# train_loss = criterion(out * loss_mask, fix_durs * loss_mask)
train_loss = criterion(out, fix_durs)
train_loss.backward()
optimizer.step()
scheduler.step()
print(f"\t epoch {epoch+1}, batch {batch_idx+1}, loss: {train_loss.item():.4f}")
# validation
model.eval()
val_loss = 0.0
with torch.no_grad():
for val_batch in val_loader:
sp_embeddings = val_batch["sp_embeddings"].to(device)
attention_mask = val_batch["attention_masks"].to(device)
fix_durs = val_batch["fix_durs"].to(device)
if use_attention_mask:
if output_attentions:
out, attentions = model(
sp_embeddings=sp_embeddings,
attention_mask=attention_mask,
output_attentions=output_attentions,
)
else:
out = model(
sp_embeddings=sp_embeddings,
attention_mask=attention_mask,
output_attentions=output_attentions,
)
else:
# forward pass
if output_attentions:
out, attentions = model(
sp_embeddings=sp_embeddings,
output_attentions=output_attentions,
)
else:
out = model(
sp_embeddings=sp_embeddings,
output_attentions=output_attentions,
)
val_loss_mask = (fix_durs != 0).float()
# val_loss += criterion(out * val_loss_mask, fix_durs * val_loss_mask).item()
val_loss += criterion(out, fix_durs).item()
# average the losses
val_loss /= len(val_loader)
train_loss /= len(train_loader)
print(f"Epoch {epoch+1}, Train Loss: {train_loss:.4f}, Val Loss: {val_loss:.4f}")
# check for early stopping
early_stopping(val_loss, model)
if early_stopping.early_stop:
print("Early stopping")
break
|