momentumnet.MomentumNet

class momentumnet.MomentumNet(functions, gamma, init_speed=False, init_function=None, use_backprop=True, is_residual=True)

Create a Momentum Residual Network.

It iterates

v_{t + 1} = gamma * v_t + (1 - gamma) * f_t(x_t)
x_{t + 1} = x_t + v_{t + 1}

where the f_t are stored in functions. These forward equations can be reversed in closed-form, enabling learning without backpropagation. This process trades memory for computations.

Parameters
functionslist of modules, list of Sequential or Sequential

a list of Sequential to define the transformation at each layer. Each function in the list can take additional inputs. ‘x’ is assumed to be the first input.

gammafloat

the momentum term

init_speedbool (default: False)

if init_speed is True then specify an init_function for the velocity v

init_functionSequential (default: None)

to initialize the velocity to init_function(x) before the forward pass

use_backpropbool (default: True)

if True then standard backpropagation is used, if False activations are not saved during the forward pass allowing memory savings

is_residualBool (default: True)

if True then the update rule is v_{t + 1} = (1 - gamma) * v_t + gamma * f_t(x_t) if False then the update rule is v_{t + 1} = (1 - gamma) * v_t + gamma * (f_t(x_t) - x_t)

Notes

Implementation based on Michael E. Sander, Pierre Ablin, Mathieu Blondel, Gabriel Peyre. Momentum Residual Neural Networks. Proceedings of the 38th International Conference on Machine Learning, PMLR 139:9276-9287

Examples

>>> import torch
>>> from torch import nn
>>> from momentumnet import MomentumNet
>>> hidden = 8
>>> d = 50
>>> function = nn.Sequential(nn.Linear(d, hidden),
...                           nn.Tanh(), nn.Linear(hidden, d))
>>> mresnet = MomentumNet([function,] * 10, gamma=0.99)
>>> x = torch.randn(10, d)
>>> mresnet(x).shape
torch.Size([10, 50])
Attributes
functions
init_function

Methods

forward(x, *args, **kwargs)

maps x to the output of the network

__init__(functions, gamma, init_speed=False, init_function=None, use_backprop=True, is_residual=True)

Initializes internal Module state, shared by both nn.Module and ScriptModule.

Methods

__init__(functions, gamma[, init_speed, ...])

Initializes internal Module state, shared by both nn.Module and ScriptModule.

add_module(name, module)

Adds a child module to the current module.

apply(fn)

Applies fn recursively to every submodule (as returned by .children()) as well as self.

bfloat16()

Casts all floating point parameters and buffers to bfloat16 datatype.

buffers([recurse])

Returns an iterator over module buffers.

children()

Returns an iterator over immediate children modules.

cpu()

Moves all model parameters and buffers to the CPU.

cuda([device])

Moves all model parameters and buffers to the GPU.

double()

Casts all floating point parameters and buffers to double datatype.

eval()

Sets the module in evaluation mode.

extra_repr()

Set the extra representation of the module

float()

Casts all floating point parameters and buffers to float datatype.

forward(x, *args, **kwargs)

Defines the computation performed at every call.

get_buffer(target)

Returns the buffer given by target if it exists, otherwise throws an error.

get_extra_state()

Returns any extra state to include in the module's state_dict.

get_parameter(target)

Returns the parameter given by target if it exists, otherwise throws an error.

get_submodule(target)

Returns the submodule given by target if it exists, otherwise throws an error.

half()

Casts all floating point parameters and buffers to half datatype.

load_state_dict(state_dict[, strict])

Copies parameters and buffers from state_dict into this module and its descendants.

modules()

Returns an iterator over all modules in the network.

named_buffers([prefix, recurse])

Returns an iterator over module buffers, yielding both the name of the buffer as well as the buffer itself.

named_children()

Returns an iterator over immediate children modules, yielding both the name of the module as well as the module itself.

named_modules([memo, prefix, remove_duplicate])

Returns an iterator over all modules in the network, yielding both the name of the module as well as the module itself.

named_parameters([prefix, recurse])

Returns an iterator over module parameters, yielding both the name of the parameter as well as the parameter itself.

parameters([recurse])

Returns an iterator over module parameters.

register_backward_hook(hook)

Registers a backward hook on the module.

register_buffer(name, tensor[, persistent])

Adds a buffer to the module.

register_forward_hook(hook)

Registers a forward hook on the module.

register_forward_pre_hook(hook)

Registers a forward pre-hook on the module.

register_full_backward_hook(hook)

Registers a backward hook on the module.

register_parameter(name, param)

Adds a parameter to the module.

requires_grad_([requires_grad])

Change if autograd should record operations on parameters in this module.

set_extra_state(state)

This function is called from load_state_dict() to handle any extra state found within the state_dict.

share_memory()

See torch.Tensor.share_memory_()

state_dict([destination, prefix, keep_vars])

Returns a dictionary containing a whole state of the module.

to(*args, **kwargs)

Moves and/or casts the parameters and buffers.

to_empty(*, device)

Moves the parameters and buffers to the specified device without copying storage.

train([mode])

Sets the module in training mode.

type(dst_type)

Casts all parameters and buffers to dst_type.

xpu([device])

Moves all model parameters and buffers to the XPU.

zero_grad([set_to_none])

Sets gradients of all model parameters to zero.

Attributes

T_destination

dump_patches

This allows better BC support for load_state_dict().

functions

init_function

Fork me on GitHub