Pytorch Symbolic: an equivalent of Keras Functional API


I just hit 1.0.0 version of a library I’ve been developing for the past months as a side project.

Pytorch Symbolic

A library that aims to provide a concise API for neural network creation in PyTorch. The API and the inner workings are similar to Keras / TensorFlow2 Functional API which I always enjoyed using. I decided to go with “Symbolic” in the name instead of “Functional” because I believe it better represents how the library works (also “functional” is kind of taken by torch.nn.functional).

I did my best to prepare a useful documentation, so if you are interested, please check it out!
It is filled with examples, best practices, benchmarks, explanations of the inner workings and more.


This example shows how to create a multiple inputs neural network:

from torch import nn
from pytorch_symbolic import Input, SymbolicModel

input1 = Input(shape=(3, 32, 32))
input2 = Input(shape=(3, 32, 32))

output1 = nn.Conv2d(input1.C, 16, 3)(input1)
output2 = nn.Conv2d(input2.C, 16, 3)(input2)
final_output = output1 + output2

model = SymbolicModel(inputs=(input1, input2), outputs=final_output)

Keras-like summary is available as well:

     Layer          Output shape         Params   Parent   
1    Input_1        (None, 3, 32, 32)    0                 
2    Input_2        (None, 3, 32, 32)    0                 
3    Conv2d_1       (None, 16, 30, 30)   448      1        
4    Conv2d_2       (None, 16, 30, 30)   448      2        
5*   AddOpLayer_1   (None, 16, 30, 30)   0        3,4      
Total params: 896
Trainable params: 896
Non-trainable params: 0

Pytorch Symbolic is actually more powerful than Keras Functional API, because it can be used to define graphs of operations over arbitrary Python objects. So if your torch.nn.Module operates on dictionaries, you can still use it in Pytorch Symbolic. This is not just a gimmick, as it is necessary to be compatible with all the Modules provided by torch.nn and Modules commonly used by the community.
You can read more in Advanced Topics section of the documentation.


Installation is easy with pip and there are no dependencies besides PyTorch (torch>=1.12.0):

pip install pytorch-symbolic

It is a small package, easy to install and uninstall, if you don’t like it. :slight_smile:

There’s an introduction in form of Jupyter Notebook, if you prefer it.
Go to GitHub to see it or run in Colab.

This library does not compete with the existing:

  • FastAI
  • PyTorch Lightning
  • PyTorch Ignite

All of which are great libraries for data processing and neural network training. Pytorch Symbolic provides API solely for neural network creation and produces models entirely compatible with PyTorch, which means you can train the models created with Pytorch Symbolic using one of the above libraries!

Whether you try it or not, I am excited to hear your feedback on this library. Do you have any suggestions, questions, critique? Please share all your thoughts! :slight_smile:

Contributions are welcomed too!