Skip to main content

Symbolic API for model creation in PyTorch.

Project description

Pytorch Symbolic

PyPi version PyPI license Notebook

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

Pytorch Symbolic makes it easier and faster to define complex models. It spares you writing boilerplate code. It aims to be PyTorch equivalent for Keras Functional API.

Features:

  • Small extension of PyTorch
  • No dependencies besides PyTorch
  • Produces models entirely compatible with PyTorch
  • Works with torch.compile and torch.export
  • Overhead free as tested in benchmarks
  • Reduces the amount of boilerplate code
  • Works well with complex architectures
  • Code and documentation is automatically tested

Example

To create a symbolic model, you need Symbolic Tensors and torch.nn.Module. Register layers and operations in your model by calling layer(inputs) or equivalently inputs(layer). Layers will be automagically added to your model and all operations will be replayed on the real data. That's all!

Using Pytorch Symbolic, 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.Softmax(1))
model = SymbolicModel(inputs=inputs, outputs=x)
model.summary()
_______________________________________________________
     Layer       Output shape        Params   Parent   
=======================================================
1    Input_1     (None, 1, 28, 28)   0                 
2    Flatten_1   (None, 784)         0        1        
3    Linear_1    (None, 10)          7850     2        
4*   Softmax_1   (None, 10)          0        3        
=======================================================
Total params: 7850
Trainable params: 7850
Non-trainable params: 0
_______________________________________________________

See more examples in Documentation Quick Start.

Symbolic models are plain torch.nn.Module objects, so they work with modern PyTorch tooling:

model = SymbolicModel(inputs=inputs, outputs=x)
compiled_model = torch.compile(model)
exported_program = torch.export.export(model, (example_input,))
# ...

How to start

See Jupyter Notebook showing the basic usage of Pytorch Symbolic:

  • 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.

Open in Colab: Open In Colab

Installation

Install Pytorch Symbolic easily with pip:

pip install pytorch-symbolic

Troubleshooting

Please create an issue if you notice a problem!

Links

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-1.2.1.tar.gz (989.2 kB view details)

Uploaded Source

Built Distribution

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

pytorch_symbolic-1.2.1-py3-none-any.whl (23.9 kB view details)

Uploaded Python 3

File details

Details for the file pytorch_symbolic-1.2.1.tar.gz.

File metadata

  • Download URL: pytorch_symbolic-1.2.1.tar.gz
  • Upload date:
  • Size: 989.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.14

File hashes

Hashes for pytorch_symbolic-1.2.1.tar.gz
Algorithm Hash digest
SHA256 60b6eac6083409bc0feb7d7f5607b94250ed078377d1b1910732e6d7add3b786
MD5 33777444e82a05954863d6fffe433f6c
BLAKE2b-256 ed22bd61f90af6db165d19bd5114cee0a320b20e11af9db60a60e78b0d90ddc5

See more details on using hashes here.

File details

Details for the file pytorch_symbolic-1.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pytorch_symbolic-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 008b306e5525cc9c9489a3c91f91b15251ea775cd1ba7b6b69b7855fd60a9762
MD5 2f936ba79ae03772c0b2b27cd02ab06e
BLAKE2b-256 3760c18ce080cf14d9923ba16342d1e9fa30466f7a55bddb4ebac56bc43c6c20

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