trw.simple_layers

The purpose to this module is to provide a convenient way to create static neural network

Submodules

Package Contents

Classes

OrderedSet

Ordered set

SimpleOutputBase

Base class to calculate an output

SimpleMergeBase

Base class for nodes with multiple inputs

SimpleLayerBase

Base layer for our simplified network specification

SimpleModule

Generic module

Input

Represent an input (i.e., a feature) to a network

OutputClassification

Output class for classification

Flatten

Generic module

Conv2d

Generic module

ReLU

Generic module

MaxPool2d

Generic module

Linear

Generic module

ConcatChannels

Implement a channel concatenation layer

OutputRecord

Record a field based from the node input values or the batch

OutputEmbedding

Create an embedding for display purposes

ShiftScale

Normalize a tensor with a mean and standard deviation

Functions

denses(parent, sizes, *args, **kwargs)

convs_3d(parent, channels, *args, **kwargs)

convs_2d(parent, channels, *args, **kwargs)

global_average_pooling_2d(parent)

global_average_pooling_3d(parent)

global_max_pooling_2d(parent)

global_max_pooling_3d(parent)

class trw.simple_layers.OrderedSet(iterable=None)

Bases: collections.MutableSet

Ordered set

Implementation based on a doubly linked link and an internal dictionary. This design gives OrderedSet the same big-Oh running times as regular sets including O(1) adds, removes, and lookups as well as O(n) iteration.

__len__(self)
__contains__(self, key)
add(self, key)

Add an element.

discard(self, key)

Remove an element. Do not raise an exception if absent.

__iter__(self)
__reversed__(self)
pop(self, last=True)

Return the popped value. Raise KeyError if empty.

__repr__(self)

Return repr(self).

__eq__(self, other)

Return self==value.

class trw.simple_layers.SimpleOutputBase(node, output_name, shape)

Bases: SimpleLayerBase

Base class to calculate an output

forward(self, inputs, batch)

Create a trw.train.Output from the inputs

Parameters
  • inputs – a list of inputs of the output node

  • batch – the batch of data fed to the network

Returns

a trw.train.Output object

class trw.simple_layers.SimpleMergeBase(parents, shape)

Bases: SimpleLayerBase

Base class for nodes with multiple inputs

class trw.simple_layers.SimpleLayerBase(parents, shape)

Base layer for our simplified network specification

Record the network node by node and keep track of the important information: parents, children, size.

Note

  • nn.Module must be created during the initialization. This is to make sure we can easily share the

    network for different sub-models

get_module(self)

Return a nn.Module

class trw.simple_layers.SimpleModule(node, module, shape=None)

Bases: SimpleLayerBase

Generic module

Module must have a single input and all the module’s parameters should be on the same device.

static calculate_shape(shape, module, parents)
get_module(self)

Return a nn.Module

class trw.simple_layers.Input(shape: list, feature_name: str)

Bases: trw.simple_layers.simple_layers.SimpleLayerBase

Represent an input (i.e., a feature) to a network

get_module(self)

Return a nn.Module

class trw.simple_layers.OutputClassification(node, output_name, classes_name, **kwargs)

Bases: trw.simple_layers.simple_layers.SimpleOutputBase

Output class for classification

forward(self, inputs, batch)

Create a trw.train.Output from the inputs

Parameters
  • inputs – a list of inputs of the output node

  • batch – the batch of data fed to the network

Returns

a trw.train.Output object

get_module(self)

Return a nn.Module

class trw.simple_layers.Flatten(node)

Bases: trw.simple_layers.simple_layers.SimpleModule

Generic module

Module must have a single input and all the module’s parameters should be on the same device.

class trw.simple_layers.Conv2d(node, out_channels, kernel_size, stride=1, padding='same')

Bases: trw.simple_layers.simple_layers.SimpleModule

Generic module

Module must have a single input and all the module’s parameters should be on the same device.

class trw.simple_layers.ReLU(node)

Bases: trw.simple_layers.simple_layers.SimpleModule

Generic module

Module must have a single input and all the module’s parameters should be on the same device.

class trw.simple_layers.MaxPool2d(node, kernel_size, stride=None)

Bases: trw.simple_layers.simple_layers.SimpleModule

Generic module

Module must have a single input and all the module’s parameters should be on the same device.

class trw.simple_layers.Linear(node, out_features)

Bases: trw.simple_layers.simple_layers.SimpleModule

Generic module

Module must have a single input and all the module’s parameters should be on the same device.

class trw.simple_layers.ConcatChannels(nodes, flatten=False)

Bases: trw.simple_layers.simple_layers.SimpleMergeBase

Implement a channel concatenation layer

static calculate_shape(parents)
get_module(self)

Return a nn.Module

class trw.simple_layers.OutputRecord(node, output_name, functor=return_output)

Bases: trw.simple_layers.simple_layers.SimpleOutputBase

Record a field based from the node input values or the batch

forward(self, inputs, batch)

Create a trw.train.Output from the inputs

Parameters
  • inputs – a list of inputs of the output node

  • batch – the batch of data fed to the network

Returns

a trw.train.Output object

get_module(self)

Return a nn.Module

class trw.simple_layers.OutputEmbedding(node, output_name)

Bases: trw.simple_layers.simple_layers.SimpleOutputBase

Create an embedding for display purposes

forward(self, inputs, batch)

Create a trw.train.Output from the inputs

Parameters
  • inputs – a list of inputs of the output node

  • batch – the batch of data fed to the network

Returns

a trw.train.Output object

get_module(self)

Return a nn.Module

trw.simple_layers.denses(parent, sizes, *args, **kwargs)
trw.simple_layers.convs_3d(parent, channels, *args, **kwargs)
trw.simple_layers.convs_2d(parent, channels, *args, **kwargs)
trw.simple_layers.global_average_pooling_2d(parent)
trw.simple_layers.global_average_pooling_3d(parent)
trw.simple_layers.global_max_pooling_2d(parent)
trw.simple_layers.global_max_pooling_3d(parent)
class trw.simple_layers.ShiftScale(node, mean, standard_deviation)

Bases: trw.simple_layers.simple_layers.SimpleModule

Normalize a tensor with a mean and standard deviation

The output tensor will be (x - mean) / standard_deviation

This layer simplify the preprocessing for the trw.simple_layers package