Created
January 31, 2024 18:17
-
-
Save 0x00b1/32a747a031a789dce9fc4dfd4a02ae0e to your computer and use it in GitHub Desktop.
PyTorch `iota` operator
This file contains hidden or 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
from torch import Tensor | |
import torch | |
def iota(shape: tuple[int, ...], dim: int = 0, **kwargs) -> Tensor: | |
dimensions = [] | |
for index, _ in enumerate(shape): | |
if index != dim: | |
dimension = 1 | |
else: | |
dimension = shape[index] | |
dimensions = [*dimensions, dimension] | |
return torch.arange(shape[dim], **kwargs).view(*dimensions).expand(*shape) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.