Args: mod : It is the mod we have to train criterion : Loss function, her we have Cross entropy loss optimizer : object of torch.optim class call: call is the COMMAND to be excuted to run th
(mod, criterion, optimizer, call, train_loader, valid_loader, fold, e=500, patience=5, report=False)
| 178 | |
| 179 | |
| 180 | def training(mod, criterion, optimizer, call, train_loader, valid_loader, fold, e=500, patience=5, report=False): |
| 181 | """Args: |
| 182 | mod : |
| 183 | It is the mod we have to train |
| 184 | criterion : |
| 185 | Loss function, her we have Cross entropy loss |
| 186 | optimizer : |
| 187 | object of torch.optim class |
| 188 | call: |
| 189 | call is the COMMAND to be excuted to run the forward method of the model |
| 190 | it changed as per the modality and other possible input |
| 191 | train_loader: |
| 192 | It is a instance of train dataloader |
| 193 | valid_loader: |
| 194 | It is a instance of validation dataloader, it is given as a input to evaluation class |
| 195 | fold: |
| 196 | 5 FOLD {0,1,2,3,4} |
| 197 | e: |
| 198 | maximum epoch |
| 199 | patience: |
| 200 | how many epoch to wait after the early stopping condition in satisfied |
| 201 | report: |
| 202 | It True then the classification report for the validation set is printed, it is given as a input to evaluation class |
| 203 | save: |
| 204 | If true then best model for each fold is saved |
| 205 | |
| 206 | """ |
| 207 | |
| 208 | print('-'*100) |
| 209 | train_losses = [0] |
| 210 | valid_losses = [0] |
| 211 | max_f1 = 0 |
| 212 | patience_flag = 1 |
| 213 | best_epooch = 0 |
| 214 | print(fold, e, patience) |
| 215 | |
| 216 | while e > 0: |
| 217 | total_loss = [] |
| 218 | seed() |
| 219 | for batch in train_loader: |
| 220 | uText = batch[0].float().to(device) |
| 221 | cText = batch[1].float().to(device) |
| 222 | uAudio = batch[2].float().to(device) |
| 223 | cAudio = batch[3].float().to(device) |
| 224 | uVideo = batch[4].float().to(device) |
| 225 | cVideo = batch[5].float().to(device) |
| 226 | speaker = batch[6].float().to(device) |
| 227 | y_true = batch[7].long().to(device) |
| 228 | del batch |
| 229 | output = eval(call) |
| 230 | loss = criterion(output, y_true) |
| 231 | del uText, cText, uAudio, cAudio, uVideo, cVideo, speaker |
| 232 | optimizer.zero_grad() |
| 233 | total_loss.append(loss.detach().item()) |
| 234 | loss.backward() |
| 235 | optimizer.step() |
| 236 | with torch.no_grad(): |
| 237 | valid_f1, valid_loss = evaluation( |
no test coverage detected