forked from hpcaitech/ColossalAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update vit example for new API (hpcaitech#98) (hpcaitech#99)
- Loading branch information
Showing
3 changed files
with
19 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,21 @@ | ||
import torch.nn as nn | ||
from colossalai.registry import LOSSES | ||
import torch | ||
|
||
|
||
@LOSSES.register_module | ||
class MixupLoss(nn.Module): | ||
def __init__(self, loss_fn_cls): | ||
super().__init__() | ||
self.loss_fn = loss_fn_cls() | ||
|
||
def forward(self, inputs, *args): | ||
targets_a, targets_b, lam = args | ||
def forward(self, inputs, targets_a, targets_b, lam): | ||
return lam * self.loss_fn(inputs, targets_a) + (1 - lam) * self.loss_fn(inputs, targets_b) | ||
|
||
|
||
class MixupAccuracy(nn.Module): | ||
def forward(self, logits, targets): | ||
targets = targets['targets_a'] | ||
preds = torch.argmax(logits, dim=-1) | ||
correct = torch.sum(targets == preds) | ||
return correct |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters