-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added
tanh-relu
activation fn and input noise options (#77)
* Still need to pip-install from GitHub hufy implementation. * Added support for `tanh_sae`. * Added notebook for loading the `tanh_sae` * tweaking config options to be more declarating / composable * testing adding noise to SAE forward pass * updating notebook --------- Co-authored-by: David Chanin <chanindav@gmail.com>
- Loading branch information
Showing
8 changed files
with
698 additions
and
13 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,18 @@ | ||
from typing import Callable | ||
|
||
import torch | ||
|
||
|
||
def get_activation_fn(activation_fn: str) -> Callable[[torch.Tensor], torch.Tensor]: | ||
if activation_fn == "relu": | ||
return torch.nn.ReLU() | ||
elif activation_fn == "tanh-relu": | ||
return tanh_relu | ||
else: | ||
raise ValueError(f"Unknown activation function: {activation_fn}") | ||
|
||
|
||
def tanh_relu(input: torch.Tensor) -> torch.Tensor: | ||
input = torch.relu(input) | ||
input = torch.tanh(input) | ||
return input |
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
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,21 @@ | ||
import pytest | ||
import torch | ||
|
||
from sae_lens.training.activation_functions import get_activation_fn | ||
|
||
|
||
def test_get_activation_fn_tanh_relu(): | ||
tanh_relu = get_activation_fn("tanh-relu") | ||
assert tanh_relu(torch.tensor([-1.0, 0.0])).tolist() == [0.0, 0.0] | ||
assert tanh_relu(torch.tensor(1e10)).item() == pytest.approx(1.0) | ||
|
||
|
||
def test_get_activation_fn_relu(): | ||
relu = get_activation_fn("relu") | ||
assert relu(torch.tensor([-1.0, 0.0])).tolist() == [0.0, 0.0] | ||
assert relu(torch.tensor(999.9)).item() == pytest.approx(999.9) | ||
|
||
|
||
def test_get_activation_fn_error_for_unknown_values(): | ||
with pytest.raises(ValueError): | ||
get_activation_fn("unknown") |
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.