Skip to main content

Useful additional layers for PyTorch.

Project description

Torchmore

The torchmore library is a small library of layers and utilities for writing PyTorch models for image recognition, OCR, and other applications.

Flex

The flex library performs simple size inference. It does so by wrapping up individual layers in a wrapper that instantiates the layer only when dimensional data is available. The wrappers can be removed later and the model turned into one with only completely standard modules. That looks like this:

from torch import nn
from torchmore import layers, flex

noutput = 10

model = nn.Sequential(
    layers.Input("BDHW"),
    flex.Conv2d(100),
    flex.BatchNorm(),
    nn.ReLU(),
    flex.Conv2d(100),
    flex.BatchNorm(),
    nn.ReLU(),
    layers.Reshape([1, [2, 3, 4]]),
    flex.Full(100),
    flex.BatchNorm(),
    nn.ReLU(),
    flex.Full(noutput)
)

flex.shape_inference(model, (1, 1, 28, 28))

The flex library provides wrappers for the following layers right now:

  • Linear
  • Conv1d, Conv2d, Conv3d
  • ConvTranspose1d, ConvTranspose2d, ConvTranspose3d
  • LSTM, BDL_LSTM, BDHW_LSTM
  • BatchNorm1d, BatchNorm2d, BatchNorm3d
  • BatchNorm

You can use Flex directly. The following two layers are identical:

layer1 = flex.Conv2d(100)
layer2 = flex.Flex(lambda x: nn.Conv2d(x.size(1), 100))

That is, you can easily turn any layer into a Flex layer that way even if it isn't in the library.

Layers

layers.Input

The Input layer is a handy little layer that reorders input dimensions, checks size ranges and value ranges, and automatically transfers data to the current device on which the model runs.

For example, consider the following Input layer:

    layers.Input("BHWD", "BDHW", range=(0, 1), sizes=[None, 1, None, None]),

This says:

  • the input is in "BHWD" order and will get reordered to "BDHW"
  • input values must be in the interval $[0, 1]$
  • input tensors must have $D=1$
  • input tensors are transferred to the same device as weights for the model

The .order Attribute

Note that if the input tensor has a .order attribute, that will be used to reorder the input dimensions into the desired dimensions. This allows the model to accept inputs in multiple orders. Consider

model = nn.Sequential(
    layers.Input("BHWD", "BDHW", range=(0, 1), sizes=[None, 1, None, None]),
    ...
)
a = torch.rand((1, 100, 150, 1))
b = a.permute(0, 3, 1, 2)
b.order = "BDHW"

assert model(a) == model(b)

layers.Reorder

The Reorder layer reorders axes just like Tensor.permute does, but it does so in a way that documents better what is going on. Consider the following code fragment:

    layers.Reorder("BDL", "LBD"),
    flex.LSTM(100, bidirectional=True),
    layers.Reorder("LBD", "BDL"),
    flex.Conv1d(noutput, 1),
    layers.Reorder("BDL", "BLD")

The letters themselves are arbitrary, but common choices are "BDLHW". This is likely clearer than a sequence of permutations.

layers.Fun

For module-based networks, it's convenient to add functions. The Fun layer permits that, as in:

    layers.Fun("lambda x: x.permute(2, 0, 1)")

Note that since functions are specified as strings, this can be pickled.

LSTM layers

  • layers.LSTM: a trivial LSTM layer that simply dicards the state output
  • layers.BDL_LSTM: an LSTM variant that is a drop-in replacement for a Conv1d layer
  • layers.BDHW_LSTM: an MDLSTM variant that is a drop-in replacement for a Conv2d layer
  • layers.BDHW_LSTM_to_BDH: a rowwise LSTM, reducing dimension by 1

Other Layers

These may be occasionally useful:

  • layers.Info(info="", every=1000000): prints info about the activations
  • layers.CheckSizes(...): checks the sizes of tensors propagated through
  • layers.CheckRange(...): checks the ranges of values
  • layers.Permute(...): axis permutation (like x.permute)
  • layers.Reshape(...): tensor reshaping, with the option of combining axes
  • layers.View(...): equivalent of x.view
  • layers.Parallel: run two modules in parallel and stack the results
  • layers.SimplePooling2d: wrapped up max pooling/unpooling
  • layers.AcrossPooling2d: wrapped up max pooling/unpooling with convolution

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

torchmore-0.0.0.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

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

torchmore-0.0.0-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file torchmore-0.0.0.tar.gz.

File metadata

  • Download URL: torchmore-0.0.0.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.7.5

File hashes

Hashes for torchmore-0.0.0.tar.gz
Algorithm Hash digest
SHA256 474d79d188cd6ba1fcf079ca97fde6dcf3be786aca6f0162ef9a9bd168c41d5c
MD5 a23393b33d6a4b5e77919227f388c5c3
BLAKE2b-256 662735ebdd07056c166e5f5c55f3d6f60d2e1733c1a6b9a77497c9fe405bcb42

See more details on using hashes here.

File details

Details for the file torchmore-0.0.0-py3-none-any.whl.

File metadata

  • Download URL: torchmore-0.0.0-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.7.5

File hashes

Hashes for torchmore-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 062030ee71c33e33dbfcae4c93c0f007834c9fabc90d76a342741669021bd24a
MD5 f6af28fbbe19d02182000815195ccca9
BLAKE2b-256 153b2e4e99e7650ee27170ad42809d64bcd65f8813bfed5b41ab68971dac7735

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