MCPcopy Create free account
hub / github.com/alexrame/fishr / SD

Class SD

domainbed/algorithms.py:906–929  ·  view source on GitHub ↗

Gradient Starvation: A Learning Proclivity in Neural Networks Equation 25 from [https://arxiv.org/pdf/2011.09468.pdf]

Source from the content-addressed store, hash-verified

904
905
906class SD(ERM):
907 """
908 Gradient Starvation: A Learning Proclivity in Neural Networks
909 Equation 25 from [https://arxiv.org/pdf/2011.09468.pdf]
910 """
911
912 def __init__(self, input_shape, num_classes, num_domains, hparams):
913 super(SD, self).__init__(input_shape, num_classes, num_domains, hparams)
914 self.sd_reg = hparams["sd_reg"]
915
916 def update(self, minibatches, unlabeled=None):
917 all_x = torch.cat([x for x, y in minibatches])
918 all_y = torch.cat([y for x, y in minibatches])
919 all_p = self.predict(all_x)
920
921 loss = F.cross_entropy(all_p, all_y)
922 penalty = (all_p**2).mean()
923 objective = loss + self.sd_reg * penalty
924
925 self.optimizer.zero_grad()
926 objective.backward()
927 self.optimizer.step()
928
929 return {'loss': loss.item(), 'penalty': penalty.item()}
930
931
932class ANDMask(ERM):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected