Skip to main content

Provides functional API for model creation in PyTorch.

Project description

Pytorch Functional

PyPi version PyPI license Documentation Status Python 3.7 Python 3.8 Python 3.9 Python 3.10

Pytorch Functional is a MIT licensed library that adds functional API for model creation to PyTorch.

Defining complex models in PyTorch requires creating classes. Defining models in tensorflow is easier. This makes it just as easy in PyTorch. With Pytorch Functional, you can create neural networks without tedious calculations of input shapes for each layer.

Features:

  • Small extension to 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
  • Adds no overhead

New in 0.3.0

In the new experimental API you can create functional model just like in tensorflow, by calling the layer with a placeholder as an argument. Works with multiple arguments as well! It's 100% backward compatibile, you can even mix new and old API.

>>> from torch import nn
>>> from pytorch_functional import Input, FunctionalModel

>>> import pytorch_functional.enable_experimental_api  # JUST IMPORT THIS TO ENABLE NEW API

>>> inputs = Input(shape=(1, 28, 28))
>>> x = nn.Flatten()(inputs)
>>> x = nn.Linear(x.shape[1], 10)(x)
>>> outputs = nn.ReLU()(x)
>>> model = FunctionalModel(inputs, outputs)
>>> 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()
)

If you want this new API to be further developed, let me know!

Example

To create a functional model, call a placeholder with the layer as an argument. This will return another placeholder, which you can use.

>>> from torch import nn
>>> from pytorch_functional import Input, FunctionalModel
>>> inputs = Input(shape=(1, 28, 28))
>>> x = inputs(nn.Flatten())
>>> outputs = x(nn.Linear(x.shape[1], 10))(nn.ReLU())
>>> model = FunctionalModel(inputs, outputs)
>>> 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.

Installation

Install easily with pip:

pip install pytorch-functional

Links

Contact

Send me an e-mail: 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-functional-0.3.1.tar.gz (11.5 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