torchluent

Contains the FluentModule class

class torchluent.fluent_module.FluentModule(shape: Tuple[int], assume_wrapped: bool = False)[source]

This constructs torch modules in a fluent-style interface.

Example

from torchluent import FluentModule
net = (
    FluentModule(28*28)
    .dense(128)
    .operator('ReLU')
    .dense(10)
    .operator('ReLU')
    .build()
)

Note

This modules shape and all shape arguments are in practice prefixed by a batch dimension. The batch dimension is not altered by any of these calls, including reshaping, unless otherwise specified.

Variables
  • sequence (list[nn.Module]) – the actual sequence of modules that we have constructed so far.

  • shape (tuple[int]) – the current feature shape

  • is_verbose (bool) – if we are currently outputting each function call and the corresponding effects

  • wrapped (bool) – if we are currently storing a list of hidden states

build(with_stripped=False) → torch.nn.modules.module.Module[source]

Constructs the actual torch module created through other invocations to this instance.

Parameters

with_stripped (bool) – if True, wrap() must have been called and the output changes to (net, stripped_net).

Returns

a ready-to-use torch module

Return type

nn.Module

conv1d(*args, **kwargs) → torchluent.fluent_module.FluentModule[source]

Applies a 1d convolution to the current data. The current shape should be in the form (channels, length). This accepts all the same arguments as nn.Conv1d exception for in_channels which it will calculate from the current shape.

See also

torch.nn.Conv1d

Returns

self

Return type

FluentModule

conv2d(*args, **kwargs) → torchluent.fluent_module.FluentModule[source]

Applies a convolution to the current data. The current shape should be in the form (channels, height, width). This accepts all the same arguments as nn.Conv2d except for in_channels, which it will calculate from the current shape.

See also

torch.nn.Conv2d

Returns

self

Return type

FluentModule

conv3d(*args, **kwargs) → torchluent.fluent_module.FluentModule[source]

Applies a convolution to the current data. The current shape should be in the form (channels, depth, height, width). This accepts all the same arguments as nn.Conv3d except for in_channels, which it will calculate from the current shape.

See also

torch.nn.Conv3d

Returns

self

Return type

FluentModule

dense(out_features: int, bias: bool = True) → torchluent.fluent_module.FluentModule[source]

A dense layer, also known as a linear layer or a fully connected layer. A dense layer requires that this already be in flattened form, i.e., len(self.shape) == 1.

Parameters
  • out_features (int) – the number of neurons to project to

  • bias (bool) – determines if a bias (additive) term is applied to each of the output features

Returns

self

Return type

FluentModule

flatten() → torchluent.fluent_module.FluentModule[source]

Reshapes this such that the data has only one dimension.

Note

The batch dimension is preserved.

Returns

self

Return type

FluentModule

maxpool1d(*args, **kwargs) → torchluent.fluent_module.FluentModule[source]

The arguments and keyword arguments are identical to MaxPool1d

Returns

self

Return type

FluentModule

maxpool2d(*args, **kwargs) → torchluent.fluent_module.FluentModule[source]

The arguments and keyword arguments are identical to MaxPool2d

Returns

self

Return type

FluentModule

maxpool3d(*args, **kwargs) → torchluent.fluent_module.FluentModule[source]

The arguments and keyword arguments are identical to MaxPool3d

Returns

self

Return type

FluentModule

operator(oper, *args, **kwargs) → torchluent.fluent_module.FluentModule[source]

An operator is some operation which does not change the shape of the data. The operator may be specified as a string, in which it should be a module in torch.nn, or it may be the module itself which has not yet be initialized (i.e. ‘ReLU’ or nn.ReLU but not nn.ReLU())

Example

from torchluent import FluentModule
net = (
    FluentModule(28*28)
    .dense(10)
    .operator('LeakyReLU', negative_slope=0.05)
    .build()
)
Parameters
  • oper – the name of the operator or a callable which returns one

  • args – passed to the operator

  • kwargs – passed to the operator

Returns

self

Return type

FluentModule

reshape(shape: Tuple[int]) → torchluent.fluent_module.FluentModule[source]

Reshapes the data to the specified shape. Must correspond to the same total number of features.

Note

The batch dimension is preserved.

Parameters

shape (tuple[int]) – the new shape for the data

Returns

self

Return type

FluentModule

save_state()[source]

Stores the current state into the list for the result. Requires that wrap() has already been called.

Returns

self

Return type

FluentModule

silent() → torchluent.fluent_module.FluentModule[source]

Disables verbose mode

Returns

self

Return type

FluentModule

then(module, *args, **kwargs) → torchluent.fluent_module.FluentModule[source]

Applies a generic torch module transformation. To determine the output shape, this just runs some data through the module. If the module is a string then it it is assumed to be the name of an attribute in torch.nn, and it is initialized with the specified arguments.

Parameters

module – the module that should modify the data

Rtype module

union[nn.Module, str, type]

Returns

self

Return type

FluentModule

then_with(dims, mod, *args, **kwargs) → torchluent.fluent_module.FluentModule[source]

This applies the given nn.Module or string for an attribute in nn with the given dimensions passed as inputs. dims should either be a single number, which is treated like a tuple of a single element, or a tuple of numbers, which is treated as if each element is (i, num) where i is the index, or a tuple of (arg_index, num).

Our current shape is injected into args such that for each pair (arg_index, num) in dims, args[arg_index] = self.shape[num]. This allows for an extremely generic interface for modules which do not have a dedicated function for them.

Example

from torchluent import FluentModule

net = (
    FluentModule((1, 7, 7))
    .verbose()
    .then_with(0, 'ConvTranspose2d', 16,
               kernel_size=2, stride=2, padding=2)
    .operator('LeakyReLU')
    .then_with(0, 'ConvTranspose2d', 32,
               kernel_size=2, stride=2, padding=2)
    .operator('LeakyReLU')
    .then_with(0, 'ConvTranspose2d', 1,
               kernel_size=3, stride=2, padding=2)
    .operator('LeakyReLU')
    .build()
)
Variables
  • dims – one of int, tuple[int], and tuple[tuple[int, int]]. each element is treated as if by (arg_index, num) where num is the dimension in self.shape that corresponds to args[arg_index]

  • mod – either a str (for an attribute in nn) or a callable which returns a module.

Returns

self

Return type

FluentModule

transpose(dim1: int, dim2: int) → torchluent.fluent_module.FluentModule[source]

Transposes the two specified dimensions, where dimension 0 is the first dimension after the batch dimension (i.e., really index 0 in self.shape).

Example

from torchluent import FluentModule import torch

net = FluentModule((1, 12, 24)).transpose(0, 2).build() inp = torch.randn((5, 1, 12, 24)) out = net(inp) print(out.shape) # torch.Size[5, 12, 24, 1]

Returns

self

Return type

FluentModule

verbose() → torchluent.fluent_module.FluentModule[source]

Turns on verbose mode, which cases this to output every function call and the resulting shape.

Returns

self

Return type

FluentModule

wrap(with_input: bool = False) → torchluent.fluent_module.FluentModule[source]

Changes the output to the form (x, arr) where an arr is a list of states stored in locations specified with save_state()

Parameters

with_input (bool) – if True we immediately save_state()

Returns

self

Return type

FluentModule

class torchluent.fluent_module.InitListModule(include_first: bool)[source]

Initializes a list of states, optionally with the state its passed in.

Variables

include_first (bool) – True to include x in the list, False to make an empty list.

extra_repr()[source]

Set the extra representation of the module

To print customized extra information, you should reimplement this method in your own modules. Both single-line and multi-line strings are acceptable.

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class torchluent.fluent_module.Reshape(*args)[source]

Reshapes the input to match the given shape, using view. This preserves the first dimension which is assumed to be the batch dimension.

Example

import torchluent
import torch

a = torchluent.Reshape(28*28)

data = torch.randn(5, 28, 28)
reshaped = a(data)
print(reshaped.shape) # torch.Size[5, 784]
Variables

shape (tuple[int]) – the new shape for the input

extra_repr()[source]

Set the extra representation of the module

To print customized extra information, you should reimplement this method in your own modules. Both single-line and multi-line strings are acceptable.

forward(x)[source]

Changes the view of x to the desired shape

class torchluent.fluent_module.SaveStateModule[source]

Stores the state into the array.

forward(x_and_arr)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class torchluent.fluent_module.StrippingModule(child: torch.nn.modules.module.Module)[source]

Strips the array from the output of the child

Variables

child (nn.Module) – the child who we are stripping

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class torchluent.fluent_module.Transpose(dim1: int, dim2: int)[source]

Transposes two dimensions. Does not effect the batch dimension.

Example

import torchluent
import torch

transposer = torchluent.Transpose(0, 1)

data = torch.randn(5, 100, 50)
newdata = transposer(data)
print(newdata.shape) # torch.Size[5, 50, 100]
Variables
  • dim1 (int) – the first dimension to transpose

  • dim2 (int) – the second dimension to transpose

extra_repr()[source]

Set the extra representation of the module

To print customized extra information, you should reimplement this method in your own modules. Both single-line and multi-line strings are acceptable.

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class torchluent.fluent_module.WrapModule(child: torch.nn.modules.module.Module)[source]

Wraps a module which is expecting just x, passing the list through it

Variables

child (nn.Module) – the wrapped module

forward(x_and_arr)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Indices and tables