Layer base classes

class lasagne.layers.Layer(incoming, name=None)[source]

The Layer class represents a single layer of a neural network. It should be subclassed when implementing new types of layers.

Because each layer can keep track of the layer(s) feeding into it, a network’s output Layer instance can double as a handle to the full network.

Parameters:

incoming : a Layer instance or a tuple

The layer feeding into this layer, or the expected input shape.

name : a string or None

An optional name to attach to this layer.

add_param(spec, shape, name=None, **tags)[source]

Register and possibly initialize a parameter tensor for the layer.

When defining a layer class, this method is called in the constructor to define which parameters the layer has, what their shapes are, how they should be initialized and what tags are associated with them. This allows layer classes to transparently support parameter initialization from numpy arrays and callables, as well as setting parameters to existing Theano shared variables or Theano expressions.

All registered parameters are stored along with their tags in the ordered dictionary Layer.params, and can be retrieved with Layer.get_params(), optionally filtered by their tags.

Parameters:

spec : Theano shared variable, expression, numpy array or callable

initial value, expression or initializer for this parameter. See lasagne.utils.create_param() for more information.

shape : tuple of int

a tuple of integers representing the desired shape of the parameter tensor.

name : str (optional)

a descriptive name for the parameter variable. This will be passed to theano.shared when the variable is created, prefixed by the layer’s name if any (in the form 'layer_name.param_name'). If spec is already a shared variable or expression, this parameter will be ignored to avoid overwriting an existing name.

**tags (optional)

tags associated with the parameter can be specified as keyword arguments. To associate the tag tag1 with the parameter, pass tag1=True.

By default, the tags regularizable and trainable are associated with the parameter. Pass regularizable=False or trainable=False respectively to prevent this.

Returns:

Theano shared variable or Theano expression

the resulting parameter variable or parameter expression

Notes

It is recommended to assign the resulting parameter variable/expression to an attribute of the layer for easy access, for example:

>>> self.W = self.add_param(W, (2, 3), name='W')  
get_output_for(input, **kwargs)[source]

Propagates the given input through this layer (and only this layer).

Parameters:

input : Theano expression

The expression to propagate through this layer.

Returns:

output : Theano expression

The output of this layer given the input to this layer.

Notes

This is called by the base lasagne.layers.get_output() to propagate data through a network.

This method should be overridden when implementing a new Layer class. By default it raises NotImplementedError.

get_output_shape_for(input_shape)[source]

Computes the output shape of this layer, given an input shape.

Parameters:

input_shape : tuple

A tuple representing the shape of the input. The tuple should have as many elements as there are input dimensions, and the elements should be integers or None.

Returns:

tuple

A tuple representing the shape of the output of this layer. The tuple has as many elements as there are output dimensions, and the elements are all either integers or None.

Notes

This method will typically be overridden when implementing a new Layer class. By default it simply returns the input shape. This means that a layer that does not modify the shape (e.g. because it applies an elementwise operation) does not need to override this method.

get_params(unwrap_shared=True, **tags)[source]

Returns a list of Theano shared variables or expressions that parameterize the layer.

By default, all shared variables that participate in the forward pass will be returned (in the order they were registered in the Layer’s constructor via add_param()). The list can optionally be filtered by specifying tags as keyword arguments. For example, trainable=True will only return trainable parameters, and regularizable=True will only return parameters that can be regularized (e.g., by L2 decay).

If any of the layer’s parameters was set to a Theano expression instead of a shared variable, unwrap_shared controls whether to return the shared variables involved in that expression (unwrap_shared=True, the default), or the expression itself (unwrap_shared=False). In either case, tag filtering applies to the expressions, considering all variables within an expression to be tagged the same.

Parameters:

unwrap_shared : bool (default: True)

Affects only parameters that were set to a Theano expression. If True the function returns the shared variables contained in the expression, otherwise the Theano expression itself.

**tags (optional)

tags can be specified to filter the list. Specifying tag1=True will limit the list to parameters that are tagged with tag1. Specifying tag1=False will limit the list to parameters that are not tagged with tag1. Commonly used tags are regularizable and trainable.

Returns:

list of Theano shared variables or expressions

A list of variables that parameterize the layer

Notes

For layers without any parameters, this will return an empty list.

class lasagne.layers.MergeLayer(incomings, name=None)[source]

This class represents a layer that aggregates input from multiple layers. It should be subclassed when implementing new types of layers that obtain their input from multiple layers.

Parameters:

incomings : a list of Layer instances or tuples

The layers feeding into this layer, or expected input shapes.

name : a string or None

An optional name to attach to this layer.

get_output_for(inputs, **kwargs)[source]

Propagates the given inputs through this layer (and only this layer).

Parameters:

inputs : list of Theano expressions

The Theano expressions to propagate through this layer.

Returns:

Theano expressions

The output of this layer given the inputs to this layer.

Notes

This is called by the base lasagne.layers.get_output() to propagate data through a network.

This method should be overridden when implementing a new Layer class with multiple inputs. By default it raises NotImplementedError.

get_output_shape_for(input_shapes)[source]

Computes the output shape of this layer, given a list of input shapes.

Parameters:

input_shape : list of tuple

A list of tuples, with each tuple representing the shape of one of the inputs (in the correct order). These tuples should have as many elements as there are input dimensions, and the elements should be integers or None.

Returns:

tuple

A tuple representing the shape of the output of this layer. The tuple has as many elements as there are output dimensions, and the elements are all either integers or None.

Notes

This method must be overridden when implementing a new Layer class with multiple inputs. By default it raises NotImplementedError.