Skip to main content

Collection of functions and modules to help development in PyTorch.

Project description

torchoutil

Python PyTorch Code style: black

Collection of functions and modules to help development in PyTorch.

Installation

pip install torchoutil

The only requirements are python>=3.8 and torch>=1.10.

Usage

Batch of padded sequences

import torch
from torchoutil import masked_mean

x = torch.as_tensor([1, 2, 3, 4])
mask = torch.as_tensor([True, True, False, False])
result = masked_mean(x, mask)
# result contains the mean of the values marked as True: 1.5
import torch
from torchoutil import lengths_to_non_pad_mask

x = torch.as_tensor([3, 1, 2])
pad_mask = lengths_to_non_pad_mask(x, max_len=4)
# tensor([[True, True, True, False],
#         [True, False, False, False],
#         [True, True, False, False]])

Multilabel conversions

import torch
from torchoutil import probs_to_names

probs = torch.as_tensor([[0.9, 0.1], [0.6, 0.9]])
names = probs_to_names(probs, threshold=0.5, idx_to_name={0: "Cat", 1: "Dog"})
# [["Cat"], ["Cat", "Dog"]]
import torch
from torchoutil import multihot_to_indices

multihot = torch.as_tensor([[1, 0, 0], [0, 1, 1], [0, 0, 0]])
indices = multihot_to_indices(multihot)
# [[0], [1, 2], []]

...and more tensor manipulations!

import torch
from torchoutil import insert_at_indices

x = torch.as_tensor([1, 2, 3, 4])
result = insert_at_indices(x, indices=[0, 2], values=5)
# result contains tensor with inserted values: tensor([5, 1, 2, 5, 3, 4])
import torch
from torchoutil import get_inverse_perm

perm = torch.randperm(10)
inv_perm = get_inverse_perm(perm)

x1 = torch.rand(10)
x2 = x1[perm]
x3 = x2[inv_perm]
# inv_perm are indices that allow us to get x1 from x3, i.e. x1 == x3 here

Contact

Maintainer:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torchoutil-0.1.0.tar.gz (24.6 kB view details)

Uploaded Source

File details

Details for the file torchoutil-0.1.0.tar.gz.

File metadata

  • Download URL: torchoutil-0.1.0.tar.gz
  • Upload date:
  • Size: 24.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for torchoutil-0.1.0.tar.gz
Algorithm Hash digest
SHA256 fc95b9c7c73bc6ca6dfa741954f88be873aa4146033586aea9a069484c3c9589
MD5 bafd899e2c86caa3880c7885df24cdd5
BLAKE2b-256 ef5323bc7be10417852af6a289c2132d511bfdb95dc41448a19307cc006b3746

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page