-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enable custom activation functions (#65)
* enable custom activation functions * add file; add default; rm branch * updt set_grad_enabled in activation fn
- Loading branch information
Showing
4 changed files
with
120 additions
and
61 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from typing import Callable | ||
|
||
import torch | ||
import stk | ||
|
||
|
||
def act_fn(x: stk.Matrix, function: Callable, return_grad_fn: bool = False, **kwargs): | ||
assert isinstance(x, stk.Matrix) | ||
with torch.set_grad_enabled(torch.is_grad_enabled() or return_grad_fn): | ||
if return_grad_fn: | ||
x.data.requires_grad = True | ||
out = function(x.data, **kwargs) | ||
y = stk.Matrix( | ||
x.size(), | ||
out, | ||
x.row_indices, | ||
x.column_indices, | ||
x.offsets, | ||
x.column_indices_t, | ||
x.offsets_t, | ||
x.block_offsets_t) | ||
if return_grad_fn: | ||
return y, out.backward | ||
return y |
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
Oops, something went wrong.