Skip to main content

Training neural networks in PyTorch

Project description


Python package PyPI PyPI - Python Version License

torchtuples is a small python package for training PyTorch models. It works equally well for numpy arrays and torch tensors. One of the main benefits of torchtuples is that it handles data in the form of nested tuples (see example below).


Requires python 3.6 or 3.7.

torchtuples depends on PyTorch which should be installed from HERE.

Next, torchtuples can be installed using pip:

pip install git+git://

or by cloning the repo:

git clone
cd torchtuples
python install


import torch
from torch import nn
from torchtuples import Model, optim

Make a data set with three sets of covariates x0, x1 and x2, and a target y. The covariates are structured in a nested tuple x.

n = 500
x0, x1, x2 = [torch.randn(n, 3) for _ in range(3)]
y = torch.randn(n, 1)
x = (x0, (x0, x1, x2))

Create a simple ReLU net that takes as input the tensor x_tensor and the tuple x_tuple. Note that x_tuple can be of arbitrary length. The tensors in x_tuple are passed through a layer lin_tuple, averaged, and concatenated with x_tensor. We then pass our new tensor through the layer lin_cat.

class Net(nn.Module):
    def __init__(self):
        self.lin_tuple = nn.Linear(3, 2)
        self.lin_cat = nn.Linear(5, 1)
        self.relu = nn.ReLU()

    def forward(self, x_tensor, x_tuple):
        x = [self.relu(self.lin_tuple(xi)) for xi in x_tuple]
        x = torch.stack(x).mean(0)
        x =[x, x_tensor], dim=1)
        return self.lin_cat(x)

We can now fit the model with

model = Model(Net(), nn.MSELoss(), optim.SGD(0.01))
log =, y, batch_size=64, epochs=5)

and make predictions with

preds = model.predict(x)

Project details

Download files

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

Files for torchtuples, version 0.1.2
Filename, size File type Python version Upload date Hashes
Filename, size torchtuples-0.1.2-py3-none-any.whl (40.5 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size torchtuples-0.1.2.tar.gz (36.9 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page