in models.py [0:0]
def accuracy(self, loader):
nb_groups = loader.dataset.nb_groups
nb_labels = loader.dataset.nb_labels
corrects = torch.zeros(nb_groups * nb_labels)
totals = torch.zeros(nb_groups * nb_labels)
self.eval()
with torch.no_grad():
for i, x, y, g in loader:
predictions = self.predict(x.cuda())
if predictions.squeeze().ndim == 1:
predictions = (predictions > 0).cpu().eq(y).float()
else:
predictions = predictions.argmax(1).cpu().eq(y).float()
groups = (nb_groups * y + g)
for gi in groups.unique():
corrects[gi] += predictions[groups == gi].sum()
totals[gi] += (groups == gi).sum()
corrects, totals = corrects.tolist(), totals.tolist()
self.train()
return sum(corrects) / sum(totals),\
[c/t for c, t in zip(corrects, totals)]