-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Base model and artifact model are saved together after artifact model…
… training (#149)
- Loading branch information
1 parent
f224112
commit 72b6a08
Showing
9 changed files
with
99 additions
and
47 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
Empty file.
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from typing import Any | ||
|
||
from torch.autograd import Function | ||
|
||
|
||
class GradientReversal(Function): | ||
@staticmethod | ||
def jvp(ctx: Any, *grad_inputs: Any) -> Any: | ||
pass | ||
|
||
@staticmethod | ||
def forward(ctx, x, alpha): | ||
ctx.save_for_backward(x, alpha) | ||
return x | ||
|
||
@staticmethod | ||
def backward(ctx, grad_output): | ||
grad_input = None | ||
_, alpha = ctx.saved_tensors | ||
if ctx.needs_input_grad[0]: | ||
grad_input = - alpha * grad_output | ||
return grad_input, None | ||
|
||
|
||
revgrad = GradientReversal.apply |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from .functional import revgrad | ||
import torch | ||
from torch import nn | ||
|
||
|
||
class GradientReversal(nn.Module): | ||
def __init__(self, alpha): | ||
super().__init__() | ||
self.alpha = torch.tensor(alpha, requires_grad=False) | ||
|
||
def forward(self, x): | ||
return revgrad(x, self.alpha) |
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
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