Skip to main content

Provides symbolic API for model creation in PyTorch.

Project description

Pytorch Symbolic

PyPi version PyPI license Documentation Status Notebook Python 3.7 Python 3.10

Pytorch Symbolic is MIT licensed library that adds symbolic API for model creation to PyTorch.

Defining complex models in PyTorch requires creating classes and writing boilerplate code.

Defining models in Keras is easier. Pytorch Symbolic makes it just as easy.

Features:

  • Small extension of PyTorch
  • No dependencies besides PyTorch
  • Produces models entirely compatible with PyTorch
  • Overhead free, tested in benchmarks
  • Reduces the amount of code you write
  • Works well with complex architectures
  • Code and documentation is automatically tested

Example

To create a symbolic model, create Symbolic Tensors and nn.Modules. Add layers by calling layer(symbolic_tensor) or equivalently symbolic_tensor(layer). That's all!

Layers will be automagically registered in your model.

from torch import nn
from pytorch_symbolic import Input

inputs = Input(shape=(1, 28, 28))  # Input is a SymbolicTensor
print(inputs)

x = nn.Flatten()(inputs)  # Every layer operation returns another SymbolicTensor
x = inputs(nn.Flatten())  # This is equivalent to previous line
print(x)
<SymbolicTensor at 0x7faf5779d130; child of 0; parent of 0>
<SymbolicTensor at 0x7fafea899f10; child of 1; parent of 0>

Using symbolic tensors, we can define a working classifier in a few lines of code:

from torch import nn
from pytorch_symbolic import Input, SymbolicModel

inputs = Input(shape=(1, 28, 28))
x = nn.Flatten()(inputs)
x = nn.Linear(x.shape[1], 10)(x)(nn.ReLU())
model = SymbolicModel(inputs=inputs, outputs=x)
model
SymbolicModel(
  (module0_depth1): Flatten(start_dim=1, end_dim=-1)
  (module1_depth2): Linear(in_features=784, out_features=10, bias=True)
  (module2_depth3): ReLU()
)

See more examples in Documentation Quick Start.

Gentle introduction

There's a jupyter notebook showing the basic usage of Pytorch Symbolic. You will:

  • Learn Pytorch Symbolic in an interactive way
  • Try the package before installing it on your computer
  • See visualizations of graphs that are created under the hood

Click: Open In Colab

Installation

Install Pytorch Symbolic easily with pip:

pip install pytorch-symbolic

Links

Create an issue if you noticed a problem!

Send me an e-mail if you want to get involved: sjmikler@gmail.com.

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

pytorch-symbolic-0.7.5.tar.gz (17.7 kB view hashes)

Uploaded Source

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