-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #116 from aidotse/single_sm
Single softmax
- Loading branch information
Showing
7 changed files
with
64 additions
and
85 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
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,22 @@ | ||
"""Utility functions for attacks.""" | ||
|
||
import numpy as np | ||
from torch import exp, from_numpy, max, sum | ||
|
||
|
||
def softmax_logits(logits: np.ndarray, temp:float=1.0, dimension:int=-1) -> np.ndarray: | ||
"""Rescale logits to (0, 1). | ||
Args: | ||
---- | ||
logits ( len(dataset) x ... x nb_classes ): Logits to be rescaled. | ||
temp (float): Temperature for softmax. | ||
dimension (int): Dimension to apply softmax. | ||
""" | ||
logits = from_numpy(logits) / temp | ||
logits = logits - max(logits, dim=dimension, keepdim=True).values | ||
logits = exp(logits) | ||
logits = logits/sum(logits, dim=dimension, keepdim=True) | ||
return logits.numpy() | ||
|
File renamed without changes.
File renamed without changes.