Skip to main content

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

Release files for torchmore 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for torchmore 0.1.0
File Size Uploaded
torchmore-0.1.0.tar.gz 13.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for torchmore 0.1.0
File Interpreter ABI Platform
torchmore-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size:26.5 kB

Release files / torchmore-0.1.0.tar.gz

Download URL torchmore-0.1.0.tar.gz
Size 13.4 kB
Tags Source
SHA-256 checksum
How to use checksums
e4f656693dd668747f39919de61be2ecbb849cde2c6a22adfb9530cf65e38654
BLAKE2b-256 checksum
How to use checksums
a4a7a245952774f7962d131e2ac89dbd34dc4955323c5978602b4b6a037c8d5b
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / torchmore-0.1.0-py3-none-any.whl

Download URL torchmore-0.1.0-py3-none-any.whl
Size 13.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6987868e92414b0d0b629a5cf46cce3f46a0d16feefe244a063c9d1385c8a3fc
BLAKE2b-256 checksum
How to use checksums
9ff94cf8d04eb675bc3b730e053ffdfc821fe70d58316de6763deb9110ff9e9e
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

0.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page