Skip to main content

batchify anything

Project description

batchify: structured data in pytorch

PyTorch can already batchify tensors (and tuples of tensors), but what about arbitrary classes? If your neural network is dealing with complex datatypes, structuring your data in classes is the solution. With batchify, you can seemlessly return classes in a pytorch dataset.

As an example, let's say you want your neural network do something with people. People, as we all know, are faces and names

import torch
from batchify import Batch, Batchable

MAX_NAME_LEN = 128
IMG_SIZE = 256
class Person(Batchable):

    face: torch.Tensor
    name: torch.Tensor

    def __init__(self): # not a very interesting person
        self.face = torch.zeros((3, IMG_SIZE, IMG_SIZE))
        self.name = torch.zeros((MAX_NAME_LEN,))

Now here's the fun part: we can make a batch of people. This automatically batchifies both the face and the name

dave = Person()
rhonda = Person()
batch = Batch([dave, rhonda])
print(len(batch))
print(dave.name.shape)
print(batch.name.shape) # notice the extra batch dimension
print(batch[0].name.shape) # un-batchification
2
torch.Size([128])
torch.Size([2, 128])
torch.Size([128])

But what about a custom person dataset? Pretty easy with the batchify dataloader

from batchify import DataLoader

class PersonDataset(torch.utils.data.Dataset):

    def __len__(self):
        return 16

    def __getitem__(self, index):
        return Person()

batch_size = 8
dataset = PersonDataset()
loader = DataLoader(dataset, batch_size=batch_size)
for batch in loader:
    print(batch.face.shape)
torch.Size([8, 3, 256, 256])
torch.Size([8, 3, 256, 256])

This is all great if you want to input a Person into your network. But what if you want to output a person?

(warning: this functionality only works if you have correct type annotations on your Batchable classes)

out_batch = Batch(Person,
                  face=torch.zeros((batch_size, 3, IMG_SIZE, IMG_SIZE)),
                  name=torch.zeros((batch_size, MAX_NAME_LEN)))
print(len(out_batch))
print(out_batch[0].name.shape)
8
torch.Size([128])

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

batchify-0.0.1.tar.gz (3.4 kB view details)

Uploaded Source

Built Distributions

batchify-0.0.1-py3.8.egg (6.0 kB view details)

Uploaded Source

batchify-0.0.1-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file batchify-0.0.1.tar.gz.

File metadata

  • Download URL: batchify-0.0.1.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.8.5

File hashes

Hashes for batchify-0.0.1.tar.gz
Algorithm Hash digest
SHA256 73f5e7ca83e3bfdffc4e06a8fe964863c3090e71942dc1780da34e526299c1fd
MD5 e6450a751367ab93a2dc2f293759396e
BLAKE2b-256 6721e11c86cb5a3d9dfae7e5c2b0f3a354377611b7900c3bf782d79403868632

See more details on using hashes here.

File details

Details for the file batchify-0.0.1-py3.8.egg.

File metadata

  • Download URL: batchify-0.0.1-py3.8.egg
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.8.5

File hashes

Hashes for batchify-0.0.1-py3.8.egg
Algorithm Hash digest
SHA256 816126f9dc0e910de8b0bd43c29cc900522d2a4261063ff466fb992d343dd520
MD5 e427d173ad513ef7194b98ee68564f37
BLAKE2b-256 8e2b358022d1208cc903e37e0b975fbbce7820db5d0ebfe6671b87ec2c85b90d

See more details on using hashes here.

File details

Details for the file batchify-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: batchify-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.8.5

File hashes

Hashes for batchify-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fbc6780aa6596ee79efd4b23bb4b085ada5a7f17e6797cb415429496a5a29afb
MD5 e59a486efbe66bd52df66cb3fccba7f6
BLAKE2b-256 9532d183df870dab4c8b87c601563a1964e3405a891f8b3c5d44d780c6d7980b

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