Mixup of minibatches from different domains https://arxiv.org/pdf/2001.00677.pdf https://arxiv.org/pdf/1912.01805.pdf
| 406 | |
| 407 | |
| 408 | class Mixup(ERM): |
| 409 | """ |
| 410 | Mixup of minibatches from different domains |
| 411 | https://arxiv.org/pdf/2001.00677.pdf |
| 412 | https://arxiv.org/pdf/1912.01805.pdf |
| 413 | """ |
| 414 | |
| 415 | def __init__(self, input_shape, num_classes, num_domains, hparams): |
| 416 | super(Mixup, self).__init__(input_shape, num_classes, num_domains, hparams) |
| 417 | |
| 418 | def update(self, minibatches, unlabeled=None): |
| 419 | objective = 0 |
| 420 | |
| 421 | for (xi, yi), (xj, yj) in random_pairs_of_minibatches(minibatches): |
| 422 | lam = np.random.beta(self.hparams["mixup_alpha"], self.hparams["mixup_alpha"]) |
| 423 | |
| 424 | x = lam * xi + (1 - lam) * xj |
| 425 | predictions = self.predict(x) |
| 426 | |
| 427 | objective += lam * F.cross_entropy(predictions, yi) |
| 428 | objective += (1 - lam) * F.cross_entropy(predictions, yj) |
| 429 | |
| 430 | objective /= len(minibatches) |
| 431 | |
| 432 | self.optimizer.zero_grad() |
| 433 | objective.backward() |
| 434 | self.optimizer.step() |
| 435 | |
| 436 | return {'loss': objective.item()} |
| 437 | |
| 438 | |
| 439 | class GroupDRO(ERM): |
nothing calls this directly
no outgoing calls
no test coverage detected