torchwrench

Collection of functions and modules to help development in PyTorch.

https://github.com/labbeti/torchwrench

Science Score: 44.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (13.4%) to scientific vocabulary

Keywords

deep-learning pytorch utilities
Last synced: 7 months ago · JSON representation ·

Repository

Collection of functions and modules to help development in PyTorch.

Basic Info
Statistics
  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • Open Issues: 0
  • Releases: 5
Topics
deep-learning pytorch utilities
Created 10 months ago · Last pushed 8 months ago
Metadata Files
Readme Changelog License Citation

README.md

torchwrench

Python Build Documentation Status PyTorch Collection of functions and modules to help development in PyTorch.

Installation

With pip: bash pip install torchwrench

With uv: bash uv add torchwrench

The main requirement is PyTorch.

To check if the package is installed and show the package version, you can use the following command in your terminal: bash torchwrench-info

This library has been tested on all Python versions 3.8 - 3.13, all PyTorch versions 1.10 - 2.6, and on Linux, Mac and Windows systems.

Examples

torchwrench functions and modules can be used like torch ones. The default acronym for torchwrench is tw.

Label conversions

Supports multiclass labels conversions between probabilities, classes indices, classes names and onehot encoding.

```python import torchwrench as tw

probs = tw.astensor([[0.9, 0.1], [0.4, 0.6]]) names = tw.probstoname(probs, idxto_name={0: "Cat", 1: "Dog"})

["Cat", "Dog"]

```

This package also supports multilabel labels conversions between probabilities, classes multi-indices, classes multi-names and multihot encoding.

```python import torchwrench as tw

multihot = tw.astensor([[1, 0, 0], [0, 1, 1], [0, 0, 0]]) indices = tw.multihotto_indices(multihot)

[[0], [1, 2], []]

```

Finally, this packages includes the powerset multilabel conversions :

```python import torchwrench as tw

multihot = tw.astensor([[1, 0, 0], [0, 1, 1], [0, 0, 0]]) indices = tw.multilabeltopowerset(multihot, numclasses=3, maxsetsize=2)

tensor([[0, 1, 0, 0, 0, 0, 0],

[0, 0, 0, 0, 0, 0, 1],

[1, 0, 0, 0, 0, 0, 0]])

```

Typing

Typing with number of dimensions :

```python import torchwrench as tw

x1 = tw.astensor([1, 2]) print(isinstance(x1, tw.Tensor2D)) # False x2 = tw.astensor([[1, 2], [3, 4]]) print(isinstance(x2, tw.Tensor2D)) # True ```

Typing with tensor dtype :

```python import torchwrench as tw

x1 = tw.as_tensor([1, 2], dtype=tw.int) print(isinstance(x1, tw.SignedIntegerTensor)) # True

x2 = tw.as_tensor([1, 2], dtype=tw.long) print(isinstance(x2, tw.SignedIntegerTensor1D)) # True

x3 = tw.as_tensor([1, 2], dtype=tw.float) print(isinstance(x3, tw.SignedIntegerTensor)) # False ```

Padding & cropping

Pad a specific dimension :

```python import torchwrench as tw

x = tw.rand(10, 3, 1) padded = tw.paddim(x, targetlength=5, dim=1, pad_value=-1)

x2 has shape (10, 5, 1), padded with -1

```

Pad nested list of tensors to a single one :

```python import torchwrench as tw

tensors = [tw.rand(10, 2), [tw.rand(3)] * 5, tw.rand(0, 5)] padded = tw.padandstackrec(tensors, padvalue=0)

padded has shape (3, 10, 5), padded with 0

```

Remove values at a specific dimension :

```python import torchwrench as tw

x = tw.rand(10, 5, 3) cropped = tw.cropdim(x, dim=1, targetlength=2)

cropped has shape (10, 2, 3)

```

Masking

```python import torchwrench as tw

x = tw.astensor([3, 1, 2]) mask = tw.lengthstononpadmask(x, maxlen=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]])

```

```python import torchwrench as tw

x = tw.astensor([1, 2, 3, 4]) mask = tw.astensor([True, True, False, False]) result = tw.masked_mean(x, mask)

result contains the mean of the values marked as True: 1.5

```

Others tensors manipulations!

```python import torchwrench as tw

x = tw.astensor([1, 2, 3, 4]) result = tw.insertat_indices(x, indices=[0, 2], values=5)

result contains tensor with inserted values: tensor([5, 1, 2, 5, 3, 4])

```

```python import torchwrench as tw

perm = tw.randperm(10) invperm = tw.getinverse_perm(perm)

x1 = tw.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

```

Extra: pre-compute datasets to HDF files

Here is an example of pre-computing spectrograms of torchaudio SPEECHCOMMANDS dataset, using pack_dataset function:

```python from torchaudio.datasets import SPEECHCOMMANDS from torchaudio.transforms import Spectrogram from torchwrench import nn from torchwrench.extras.hdf import packtohdf

speechcommandsroot = "path/to/speechcommands" packedroot = "path/to/packed_dataset.hdf"

dataset = SPEECHCOMMANDS(speechcommandsroot, download=True, subset="validation")

dataset[0] is a tuple, contains waveform and other metadata

class MyTransform(nn.Module): def init(self) -> None: super().init() self.spectrogram_extractor = Spectrogram()

def forward(self, item):
    waveform = item[0]
    spectrogram = self.spectrogram_extractor(waveform)
    return (spectrogram,) + item[1:]

packtohdf(dataset, packed_root, MyTransform()) ```

Then you can load the pre-computed dataset using HDFDataset: ```python from torchwrench.extras.hdf import HDFDataset

packedroot = "path/to/packeddataset.hdf" packeddataset = HDFDataset(packedroot) packed_dataset[0] # == first transformed item, i.e. transform(dataset[0]) ```

Contact

Maintainer: - Étienne Labbé "Labbeti": labbeti.pub@gmail.com

Owner

  • Name: Labbeti
  • Login: Labbeti
  • Kind: user
  • Location: Toulouse, France
  • Company: IRIT

PhD student at IRIT (Institut de Recherche en Informatique de Toulouse), working mainly on Automated Audio Captioning.

Citation (CITATION.cff)

# -*- coding: utf-8 -*-

cff-version: 1.2.0
title: torchwrench
message: 'If you use this software, please cite it as below.'
type: software
authors:
  - given-names: Étienne
    family-names: Labbé
    email: labbeti.pub@gmail.com
    affiliation: IRIT
    orcid: 'https://orcid.org/0000-0002-7219-5463'
repository-code: 'https://github.com/Labbeti/torchwrench/'
abstract: Collection of functions and modules to help development in PyTorch.
keywords:
  - pytorch
  - deep-learning
  - utilities
license: MIT
version: 0.7.4
date-released: '2025-07-24'

GitHub Events

Total
  • Release event: 3
  • Public event: 1
  • Push event: 65
  • Pull request event: 5
  • Create event: 7
Last Year
  • Release event: 3
  • Public event: 1
  • Push event: 65
  • Pull request event: 5
  • Create event: 7

Issues and Pull Requests

Last synced: 9 months ago

All Time
  • Total issues: 0
  • Total pull requests: 1
  • Average time to close issues: N/A
  • Average time to close pull requests: 16 minutes
  • Total issue authors: 0
  • Total pull request authors: 1
  • Average comments per issue: 0
  • Average comments per pull request: 0.0
  • Merged pull requests: 1
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 1
  • Average time to close issues: N/A
  • Average time to close pull requests: 16 minutes
  • Issue authors: 0
  • Pull request authors: 1
  • Average comments per issue: 0
  • Average comments per pull request: 0.0
  • Merged pull requests: 1
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
  • Labbeti (5)
Top Labels
Issue Labels
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • pypi 245 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 5
  • Total maintainers: 1
pypi.org: torchwrench

Collection of functions and modules to help development in PyTorch.

  • Homepage: https://pypi.org/project/torchwrench/
  • Documentation: https://torchwrench.readthedocs.io/
  • License: MIT License Copyright (c) 2025 Labbeti Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  • Latest release: 0.7.4
    published 9 months ago
  • Versions: 5
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 245 Last month
Rankings
Dependent packages count: 8.9%
Average: 29.7%
Dependent repos count: 50.4%
Maintainers (1)
Last synced: 8 months ago

Dependencies

docs/requirements.txt pypi
  • sphinx-press-theme >=0.8.0
pyproject.toml pypi
  • pythonwrench *
  • torch >=1.10.0
  • typing-extensions >=4.10.0
.github/workflows/test.yaml actions
  • actions/checkout v4 composite
  • astral-sh/setup-uv v5 composite
setup.py pypi
uv.lock pypi
  • 124 dependencies