Shape layers

class lasagne.layers.ReshapeLayer(incoming, shape, **kwargs)[source]

A layer reshaping its input tensor to another tensor of the same total number of elements.

Parameters:
incoming : a Layer instance or a tuple

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

shape : tuple

The target shape specification. Each element can be one of:

  • i, a positive integer directly giving the size of the dimension
  • [i], a single-element list of int, denoting to use the size of the i th input dimension
  • -1, denoting to infer the size for this dimension to match the total number of elements in the input tensor (cannot be used more than once in a specification)
  • TensorVariable directly giving the size of the dimension

Notes

The tensor elements will be fetched and placed in C-like order. That is, reshaping [1,2,3,4,5,6] to shape (2,3) will result in a matrix [[1,2,3],[4,5,6]], not in [[1,3,5],[2,4,6]] (Fortran-like order), regardless of the memory layout of the input tensor. For C-contiguous input, reshaping is cheap, for others it may require copying the data.

Examples

>>> from lasagne.layers import InputLayer, ReshapeLayer
>>> l_in = InputLayer((32, 100, 20))
>>> l1 = ReshapeLayer(l_in, ((32, 50, 40)))
>>> l1.output_shape
(32, 50, 40)
>>> l_in = InputLayer((None, 100, 20))
>>> l1 = ReshapeLayer(l_in, ([0], [1], 5, -1))
>>> l1.output_shape
(None, 100, 5, 4)
lasagne.layers.reshape[source]

alias of ReshapeLayer

class lasagne.layers.FlattenLayer(incoming, outdim=2, **kwargs)[source]

A layer that flattens its input. The leading outdim-1 dimensions of the output will have the same shape as the input. The remaining dimensions are collapsed into the last dimension.

Parameters:
incoming : a Layer instance or a tuple

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

outdim : int

The number of dimensions in the output.

See also

flatten
Shortcut
lasagne.layers.flatten[source]

alias of FlattenLayer

class lasagne.layers.DimshuffleLayer(incoming, pattern, **kwargs)[source]

A layer that rearranges the dimension of its input tensor, maintaining the same same total number of elements.

Parameters:
incoming : a Layer instance or a tuple

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

pattern : tuple

The new dimension order, with each element giving the index of the dimension in the input tensor or ‘x’ to broadcast it. For example (3,2,1,0) will reverse the order of a 4-dimensional tensor. Use ‘x’ to broadcast, e.g. (3,2,1,’x’,0) will take a 4 tensor of shape (2,3,5,7) as input and produce a tensor of shape (7,5,3,1,2) with the 4th dimension being broadcast-able. In general, all dimensions in the input tensor must be used to generate the output tensor. Omitting a dimension attempts to collapse it; this can only be done to broadcast-able dimensions, e.g. a 5-tensor of shape (7,5,3,1,2) with the 4th being broadcast-able can be shuffled with the pattern (4,2,1,0) collapsing the 4th dimension resulting in a tensor of shape (2,3,5,7).

Examples

>>> from lasagne.layers import InputLayer, DimshuffleLayer
>>> l_in = InputLayer((2, 3, 5, 7))
>>> l1 = DimshuffleLayer(l_in, (3, 2, 1, 'x', 0))
>>> l1.output_shape
(7, 5, 3, 1, 2)
>>> l2 = DimshuffleLayer(l1, (4, 2, 1, 0))
>>> l2.output_shape
(2, 3, 5, 7)
lasagne.layers.dimshuffle[source]

alias of DimshuffleLayer

class lasagne.layers.PadLayer(incoming, width, val=0, batch_ndim=2, **kwargs)[source]

Pad all dimensions except the first batch_ndim with width zeros on both sides, or with another value specified in val. Individual padding for each dimension or edge can be specified using a tuple or list of tuples for width.

Parameters:
incoming : a Layer instance or a tuple

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

width : int, iterable of int, or iterable of tuple

Padding width. If an int, pads each axis symmetrically with the same amount in the beginning and end. If an iterable of int, defines the symmetric padding width separately for each axis. If an iterable of tuples of two ints, defines a seperate padding width for each beginning and end of each axis.

val : float

Value used for padding

batch_ndim : int

Dimensions up to this value are not padded. For padding convolutional layers this should be set to 2 so the sample and filter dimensions are not padded

lasagne.layers.pad[source]

alias of PadLayer

class lasagne.layers.SliceLayer(incoming, indices, axis=-1, **kwargs)[source]

Slices the input at a specific axis and at specific indices.

Parameters:
incoming : a Layer instance or a tuple

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

indices : int or slice instance

If an int, selects a single element from the given axis, dropping the axis. If a slice, selects all elements in the given range, keeping the axis.

axis : int

Specifies the axis from which the indices are selected.

Examples

>>> from lasagne.layers import SliceLayer, InputLayer
>>> l_in = InputLayer((2, 3, 4))
>>> SliceLayer(l_in, indices=0, axis=1).output_shape
... # equals input[:, 0]
(2, 4)
>>> SliceLayer(l_in, indices=slice(0, 1), axis=1).output_shape
... # equals input[:, 0:1]
(2, 1, 4)
>>> SliceLayer(l_in, indices=slice(-2, None), axis=-1).output_shape
... # equals input[..., -2:]
(2, 3, 2)