MCPcopy Create free account
hub / github.com/thuml/Time-Series-Library / vali

Method vali

exp/exp_classification.py:47–77  ·  view source on GitHub ↗
(self, vali_data, vali_loader, criterion)

Source from the content-addressed store, hash-verified

45 return criterion
46
47 def vali(self, vali_data, vali_loader, criterion):
48 total_loss = []
49 preds = []
50 trues = []
51 self.model.eval()
52 with torch.no_grad():
53 for i, (batch_x, label, padding_mask) in enumerate(vali_loader):
54 batch_x = batch_x.float().to(self.device)
55 padding_mask = padding_mask.float().to(self.device)
56 label = label.to(self.device)
57
58 outputs = self.model(batch_x, padding_mask, None, None)
59
60 pred = outputs.detach()
61 loss = criterion(pred, label.long().squeeze())
62 total_loss.append(loss.item())
63
64 preds.append(outputs.detach())
65 trues.append(label)
66
67 total_loss = np.average(total_loss)
68
69 preds = torch.cat(preds, 0)
70 trues = torch.cat(trues, 0)
71 probs = torch.nn.functional.softmax(preds) # (total_samples, num_classes) est. prob. for each class and sample
72 predictions = torch.argmax(probs, dim=1).cpu().numpy() # (total_samples,) int class index for each sample
73 trues = trues.flatten().cpu().numpy()
74 accuracy = cal_accuracy(predictions, trues)
75
76 self.model.train()
77 return total_loss, accuracy
78
79 def train(self, setting):
80 train_data, train_loader = self._get_data(flag='TRAIN')

Callers 1

trainMethod · 0.95

Calls 2

cal_accuracyFunction · 0.90
trainMethod · 0.45

Tested by

no test coverage detected