Last active
July 23, 2020 09:32
-
-
Save cwhy/f661be65f8b96e461bf8397c59c7c898 to your computer and use it in GitHub Desktop.
Pytorch tensor plays
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
import torch | |
def cum_softmax(t: torch.Tensor) -> torch.Tensor: | |
# t shape: ..., sm_d, sm_d is the dim to reduce | |
tmax = t.cummax(dim=-1)[0].unsqueeze(-2) | |
denominator = (t.unsqueeze(-1) - tmax).exp() | |
# shape: ..., sm_d, csm_d | |
numerator = denominator.sum(dim=-2, keepdim=True) | |
return numerator / denominator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment