Pytorch Symbolic: an equivalent of Keras Functional API

gahaalt: <p>Hello!</p>
<p>I just hit 1.0.0 version of a library I’ve been developing for the past months as a side project.</p>
<h1>
<a name="pytorch-symbolic-1" class="anchor" href="#pytorch-symbolic-1"></a>Pytorch Symbolic</h1>
<p>A library that aims to provide a concise API for neural network creation in PyTorch. The API and the inner workings are similar to <a href="https://www.tensorflow.org/guide/keras/functional" rel="noopener nofollow ugc">Keras / TensorFlow2 Functional API</a> 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 <code>torch.nn.functional</code>).</p>
<p>I did my best to prepare a useful documentation, so if you are interested, please check it out!<br>
It is filled with examples, best practices, benchmarks, explanations of the inner workings and more.</p>
<ul>
<li><a href="https://pytorch-symbolic.readthedocs.io/en/latest/quick_start" rel="noopener nofollow ugc"><strong>See Documentation</strong></a></li>
<li><a href="https://github.com/gahaalt/pytorch-symbolic/" rel="noopener nofollow ugc">See on GitHub</a></li>
<li><a href="https://pypi.org/project/pytorch-symbolic/" rel="noopener nofollow ugc">See on PyPI</a></li>
</ul>
<h2>
<a name="example-2" class="anchor" href="#example-2"></a>Example</h2>
<p>This example shows how to create a multiple inputs neural network:</p>
<pre><code class="lang-auto">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)
model.summary()
</code></pre>
<p>Keras-like summary is available as well:</p>
<pre><code class="lang-auto">___________________________________________________________
     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
___________________________________________________________
</code></pre>
<p>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 <code>torch.nn.Module</code> 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 <code>torch.nn</code> and Modules commonly used by the community.<br>
You can read more in <a href="https://pytorch-symbolic.readthedocs.io/en/latest/advanced_topics/" rel="noopener nofollow ugc">Advanced Topics</a> section of the documentation.</p>
<h2>
<a name="installation-3" class="anchor" href="#installation-3"></a>Installation</h2>
<p>Installation is easy with pip and there are no dependencies besides PyTorch (<code>torch&gt;=1.12.0</code>):</p>
<pre><code class="lang-auto">pip install pytorch-symbolic
</code></pre>
<p>It is a small package, easy to install and uninstall, if you don’t like it. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<p>There’s an introduction in form of Jupyter Notebook, if you prefer it.<br>
<a href="https://github.com/gahaalt/pytorch-symbolic/" rel="noopener nofollow ugc">Go to GitHub</a> to see it or run in Colab.</p>
<hr>
<p>This library does not compete with the existing:</p>
<ul>
<li>FastAI</li>
<li>PyTorch Lightning</li>
<li>PyTorch Ignite</li>
</ul>
<p>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!</p>
<p>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! <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<p>Contributions are welcomed too!</p>