Skip to main content

PyWarm - A cleaner way to build neural networks for PyTorch

PyWarm

A cleaner way to build neural networks for PyTorch.

PyPI Python Version PyPI Version License

Examples | Tutorial | API reference


Introduction

PyWarm is a lightweight, high-level neural network construction API for PyTorch. It enables defining all parts of NNs in the functional way.

With PyWarm, you can put all network data flow logic in the forward() method of your model, without the need to define children modules in the __init__() method and then call it again in the forward(). This result in a much more readable model definition in fewer lines of code.

PyWarm only aims to simplify the network definition, and does not attempt to cover model training, validation or data handling.


For example, a convnet for MNIST: (If needed, click the tabs to switch between Warm and Torch versions)

# powered by PyWarm
import torch.nn as nn
import torch.nn.functional as F
import warm
import warm.functional as W


class ConvNet(nn.Module):

    def __init__(self):
        super().__init__()
        warm.up(self, [2, 1, 28, 28])

    def forward(self, x):
        x = W.conv(x, 20, 5, activation='relu')
        x = F.max_pool2d(x, 2)
        x = W.conv(x, 50, 5, activation='relu')
        x = F.max_pool2d(x, 2)
        x = x.view(-1, 800)
        x = W.linear(x, 500, activation='relu')
        x = W.linear(x, 10)
        return F.log_softmax(x, dim=1)
# vanilla PyTorch version, taken from
# pytorch tutorials/beginner_source/blitz/neural_networks_tutorial.py 
import torch.nn as nn
import torch.nn.functional as F


class ConvNet(nn.Module):

    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(1, 20, 5, 1)
        self.conv2 = nn.Conv2d(20, 50, 5, 1)
        self.fc1 = nn.Linear(4*4*50, 500)
        self.fc2 = nn.Linear(500, 10)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        x = F.max_pool2d(x, 2, 2)
        x = F.relu(self.conv2(x))
        x = F.max_pool2d(x, 2, 2)
        x = x.view(-1, 4*4*50)
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        return F.log_softmax(x, dim=1)

A couple of things you may have noticed:

  • First of all, in the PyWarm version, the entire network definition and data flow logic resides in the forward() method. You don't have to look up and down repeatedly to understand what self.conv1, self.fc1 etc. is doing.

  • You do not need to track and specify in_channels (or in_features, etc.) for network layers. PyWarm can infer the information for you. e.g.

# Warm
x = W.conv(x, 20, 5, activation='relu')
x = W.conv(x, 50, 5, activation='relu')


# Torch
self.conv1 = nn.Conv2d(1, 20, 5, 1)
self.conv2 = nn.Conv2d(20, 50, 5, 1)
  • One unified W.conv for all 1D, 2D, and 3D cases. Fewer things to keep track of!

  • activation='relu'. All warm.functional APIs accept an optional activation keyword, which is basically equivalent to F.relu(W.conv(...)).

For deeper neural networks, see additional examples.


Installation

pip3 install pywarm

Quick start: 30 seconds to PyWarm

If you already have experinces with PyTorch, using PyWarm is very straightforward:

  • First, import PyWarm in you model file:
import warm
import warm.functional as W
  • Second, remove child module definitions in the model's __init__() method. In stead, use W.conv, W.linear ... etc. in the model's forward() method, just like how you would use torch nn functional F.max_pool2d, F.relu ... etc.

    For example, instead of writing:

# Torch
class MyModule(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size)
        # other child module definitions
    def forward(self, x):
        x = self.conv1(x)
        # more forward steps
  • You can now write in the warm way:
# Warm
class MyWarmModule(nn.Module):
    def __init__(self):
        super().__init__()
        warm.up(self, input_shape_or_data)
    def forward(self, x):
        x = W.conv(x, out_channels, kernel_size) # no in_channels needed
        # more forward steps
  • Finally, don't forget to warmify the model by adding

    warm.up(self, input_shape_or_data)

    at the end of the model's __init__() method. You need to supply input_shape_or_data, which is either a tensor of input data, or just its shape, e.g. [2, 1, 28, 28] for MNIST inputs.

    The model is now ready to use, just like any other PyTorch models.

Check out the tutorial and examples if you want to learn more!


Testing

Clone the repository first, then

cd pywarm
pytest -v

Documentation

Documentations are generated using the excellent Portray package.

Metadata

Release files for pywarm 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pywarm 0.4.1
File Size Uploaded Provenance Actions
PyWarm-0.4.1.tar.gz 14.3 kB Without provenance Details for PyWarm-0.4.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pywarm 0.4.1
File Interpreter ABI Platform Actions
PyWarm-0.4.1-py3-none-any.whl Python 3 none any Details for PyWarm-0.4.1-py3-none-any.whl

Total release size: 27.6 kB

Release files / PyWarm-0.4.1.tar.gz

Download URL PyWarm-0.4.1.tar.gz
Size 14.3 kB
Tags Source
SHA-256 checksum
How to use checksums
bbfe5753cf44cfdb994369c2c2343787daec0dfdde26c61daf0c2dd17aaaa99f
BLAKE2b-256 checksum
How to use checksums
eacaa6e774eb305dd880c6d7d942f1aebc0a47eacd6f386f5df64dd5e5cf0bb5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/0.12.17 CPython/3.6.8 Linux/5.0.0-27-generic

Release files / PyWarm-0.4.1-py3-none-any.whl

Download URL PyWarm-0.4.1-py3-none-any.whl
Size 13.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8fa3ef1c59bc3e52d18f653d7eb164ac9f877174954de7ee0f33e23d2b5827d7
BLAKE2b-256 checksum
How to use checksums
5f40f3b27d611ec6502dcda24d57cef96898ebd864fa5a0bc827b4c7bd615329
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/0.12.17 CPython/3.6.8 Linux/5.0.0-27-generic

Security for pywarm 0.4.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page