Collection of functions and modules to help development in PyTorch.
Project description
torchoutil
Collection of functions and modules to help development in PyTorch.
Installation
pip install torchoutil
The only requirements are pytorch and typing_extensions.
To check if the package is installed and show the package version, you can use the following command:
torchoutil-info
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)
# Each row i contains x[i] True values for non-padding mask
# 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 x3 from x2, i.e. x1 == x3 here
Extras
torchoutil
also provides additional modules when some specific package are already installed in your environment.
All extras can be installed with pip install torchoutil[extras]
If tensorboard
is installed, the function load_event_file
can be used.
If numpy
is installed, the classes FromNumpy
and ToNumpy
can be used and their related function.
If h5py
is installed, the function pack_to_hdf
and class HDFDataset
can be used.
Contact
Maintainer:
- Étienne Labbé "Labbeti": labbeti.pub@gmail.com
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
File details
Details for the file torchoutil-0.2.0.tar.gz
.
File metadata
- Download URL: torchoutil-0.2.0.tar.gz
- Upload date:
- Size: 39.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 64dedf05e1f9c43d961ff4397caa42233a0c17428e57580c284f35334fd10ec4 |
|
MD5 | c9f19b0eebd5ee819d0d294e8a2fa11b |
|
BLAKE2b-256 | ee14866b86142d0c628d75d48a768566905c76bfa5c9bfc2ff8e8c544fdda039 |