Skip to main content

A small pytorch package for efficiently running pair-wise operations such as distances on the batch-level.

Project description

batchdist

This is a small PyTorch-based package which allows for efficient batched operations, e.g. for computing distances without having to slowly loop over all instance pairs of a batch of data.

After having encountered mulitple instances of torch modules/methods promising to handling batches while only returning a vector of pairwise results (see example below) instead of the full matrix, this package serves as a tool to wrap such methods in order to return full matrices (e.g. distance matrices) using fast, batched operations (without loops).

Example

First, let's define a custom distance function that only computes pair-wise distances for batches, so two batches of each 10 samples are converted to a distance vector of shape (10,).

>>> def dummy_distance(x,y):
        """
        This is a dummy distance d which allows for a batch dimension 
        (say with n instances in a batch), but does not return the full 
        n x n distance matrix but only a n-dimensional vector of the 
        pair-wise distances d(x_i,y_i) for all i in (1,...,n). 
        """
        x_ = x.sum(axis=[1,2])
        y_ = y.sum(axis=[1,2])
        return x_ + y_

# batchdist wraps a torch module around this callable to compute 
# the full n x n matrix with batched operations (no loops). 

>>> import batchdist as bd
>>> batched = bd.BatchDistance(dummy_distance)

# generate data (two batches of 256 samples of dimension [4,3])

>>> x1 = torch.rand(256,4,3)
>>> x2 = torch.rand(256,4,3)

>>> out1 = batched(x1, x2) # distance matrix of shape [256,256]

For more details, consult the included examples.

Installation

With poetry:
$ poetry add batchdist

With pip:
$ pip install batchdist

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

batchdist-0.1.3.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

batchdist-0.1.3-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file batchdist-0.1.3.tar.gz.

File metadata

  • Download URL: batchdist-0.1.3.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.0b2 CPython/3.8.6 Darwin/19.6.0

File hashes

Hashes for batchdist-0.1.3.tar.gz
Algorithm Hash digest
SHA256 6934bb2fa93ef94c4b9eaa5c688c75c36f795f9abcf4c0d5d1626224b5b1e268
MD5 afa501957de0e2ed3d036c98f5231dc8
BLAKE2b-256 0e11416cd818ec974da6c7a3b0e283be2d7983d11b4c5da2aa1fc51179e17ae9

See more details on using hashes here.

File details

Details for the file batchdist-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: batchdist-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.0b2 CPython/3.8.6 Darwin/19.6.0

File hashes

Hashes for batchdist-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ed3f9a801d8181ef852fdf4b25244ae983b5c305a90ac74c263ce93d8d892895
MD5 d1827f3844ada457d60b61236f341214
BLAKE2b-256 4fa0ba69e73ede282bef1637640951b0bd73f9985ba824027c2f16dca63871de

See more details on using hashes here.

Supported by

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