MCPcopy Create free account
hub / github.com/IBM/Project_CodeNet / train

Function train

model-experiments/gnn-based-experiments/src/main.py:29–59  ·  view source on GitHub ↗
(model, device, loader, optimizer, args, evaluator)

Source from the content-addressed store, hash-verified

27
28
29def train(model, device, loader, optimizer, args, evaluator):
30 model.train()
31
32 y_true = []
33 y_pred = []
34 loss_accum = 0
35 for step, batch in enumerate(tqdm(loader, desc="Iteration")):
36 # one b(atch) per device
37 batch = [b for b in batch if not b.x.shape[0] == 1 and not b.batch[-1] == 0]
38 if batch:
39 pred = model(batch)
40 optimizer.zero_grad()
41
42 trg = torch.cat([b.y.to(device) for b in batch], dim=0)
43 loss = multicls_criterion(pred, trg.to(torch.long).view(-1,))
44 loss.backward()
45 if args.clip > 0:
46 torch.nn.utils.clip_grad_norm(model.parameters(), args.clip)
47 optimizer.step()
48
49 loss_accum += loss.item()
50
51 y_true.append(trg.view(-1,1).detach().cpu())
52 y_pred.append(torch.argmax(pred.detach(), dim=1).view(-1, 1).cpu())
53
54 y_true = torch.cat(y_true, dim=0).numpy()
55 y_pred = torch.cat(y_pred, dim=0).numpy()
56 # print(y_true)
57 # print(y_pred)
58 input_dict = {"y_true": y_true, "y_pred": y_pred}
59 return loss_accum / (step + 1), evaluator.eval(input_dict)
60
61
62def eval(model, device, loader, evaluator):

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected