Provides functional API for model creation in PyTorch.
Project description
Pytorch Functional
Pytorch Functional is MIT licensed library that adds functional API for model creation to PyTorch.
Defining complex models in PyTorch required creating classes. Defining models in Keras is easier. Pytorch Functional makes it just as easy.
With Pytorch Functional, you can create neural networks without tedious calculations of input shape for each layer.
Features:
- Small extension of PyTorch
- No dependencies besides PyTorch
- Produces models entirely compatible with PyTorch
- Reduces the amount of code that you need to write
- Works well with complex architectures
- Package and documentation automatically tested
Example
To create a functional model, you'll use symbolic tensors and nn.Modules.
You can either call layer(symbolic_tensor)
or symbolic_tensor(layer)
.
You can create functional model just like in Keras: by calling the layer with symbolic tensors as they were normal tensors. Layer will be then automagically registered in your model.
from torch import nn
from pytorch_functional import Input
x = Input(shape=(1, 28, 28)) # Input is a SymbolicTensor
print(type(x))
x = nn.Flatten()(x) # Every layer returns another SymbolicTensor
print(type(x))
<SymbolicTensor at 0x7faf578e2a90; child of 0; parent of 0>
<SymbolicTensor at 0x7faf577fe460; child of 0; parent of 0>
Using symbolic tensors, we can define a working classificator in a few lines of code:
from torch import nn
from pytorch_functional import FunctionalModel, Input
inputs = Input(shape=(1, 28, 28))
x = nn.Flatten()(inputs)
x = nn.Linear(x.shape[1], 10)(x)(nn.ReLU())
model = FunctionalModel(inputs=inputs, outputs=x)
model
FunctionalModel(
(module000_depth001): Flatten(start_dim=1, end_dim=-1)
(module001_depth002): Linear(in_features=784, out_features=10, bias=True)
(module002_depth003): ReLU()
)
See more examples in Quick Start.
Gentle introduction
There's a notebook showing the basic usage of Pytorch Functional. With it you can:
- Learn Pytorch Functional in an interactive way
- See visualizations of graphs that are created under the hood
- Try the package out before installing it on your computer
Installation
Install easily with pip:
pip install pytorch-functional
Links
Create an issue if you have noticed a problem! Send me an e-mail if you want to get involved: sjmikler@gmail.com.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Hashes for pytorch-functional-0.4.16.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b0c3cb8f58b50d1d0beed6027efaa74aacd2e23b4554737b876e1e88e1bb9f6 |
|
MD5 | 8d11960e27495e057c9786519fe18c5a |
|
BLAKE2b-256 | ecfe3cb819da7dcad34255937574832c906b37b65aaa03410ada7f8d7f55df32 |