MCPcopy Create free account
hub / github.com/cfiltnlp/MUStARD_Plus_Plus / training

Function training

MPP_Code/training/execute_classification_implicit.py:198–281  ·  view source on GitHub ↗

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,save=False)

Source from the content-addressed store, hash-verified

196
197
198def training(mod, criterion, optimizer, call, train_loader, valid_loader, fold, e=500, patience=5, report=False,save=False):
199 """Args:
200 mod :
201 It is the mod we have to train
202 criterion :
203 Loss function, her we have Cross entropy loss
204 optimizer :
205 object of torch.optim class
206 call:
207 call is the COMMAND to be excuted to run the forward method of the model
208 it changed as per the modality and other possible input
209 train_loader:
210 It is a instance of train dataloader
211 valid_loader:
212 It is a instance of validation dataloader, it is given as a input to evaluation class
213 fold:
214 5 FOLD {0,1,2,3,4}
215 e:
216 maximum epoch
217 patience:
218 how many epoch to wait after the early stopping condition in satisfied
219 report:
220 It True then the classification report for the validation set is printed, it is given as a input to evaluation class
221 save:
222 If true then best model for each fold is saved
223
224 """
225
226 print('-'*100)
227 train_losses = [0]
228 valid_losses = [0]
229 max_f1 = 0
230 patience_flag = 1
231 best_epooch = 0
232 print(fold, e, patience)
233
234 while e > 0:
235 total_loss = []
236 seed()
237 for batch in train_loader:
238 uText = batch[0].float().to(device)
239 cText = batch[1].float().to(device)
240 uAudio = batch[2].float().to(device)
241 cAudio = batch[3].float().to(device)
242 uVideo = batch[4].float().to(device)
243 cVideo = batch[5].float().to(device)
244 speaker = batch[6].float().to(device)
245 y_true = batch[7].long().to(device)
246 del batch
247 # call is the command to be executed, since we have different combination of input modality, this decides the input by default
248 output = eval(call)
249 loss = criterion(output, y_true)
250 del uText, cText, uAudio, cAudio, uVideo, cVideo, speaker
251 # with torch.cuda.device(device):
252 # torch.cuda.empty_cache()
253 optimizer.zero_grad()
254 total_loss.append(loss.detach().item())
255 loss.backward()

Calls 2

seedFunction · 0.70
evaluationFunction · 0.70

Tested by

no test coverage detected