Convolutional layers

class lasagne.layers.Conv1DLayer(incoming, num_filters, filter_size, stride=1, pad=0, untie_biases=False, W=lasagne.init.GlorotUniform(), b=lasagne.init.Constant(0.), nonlinearity=lasagne.nonlinearities.rectify, convolution=lasagne.theano_extensions.conv.conv1d_mc0, **kwargs)[source]

1D convolutional layer

Performs a 1D convolution on its input and optionally adds a bias and applies an elementwise nonlinearity.

Parameters:

incoming : a Layer instance or a tuple

The layer feeding into this layer, or the expected input shape. The output of this layer should be a 3D tensor, with shape (batch_size, num_input_channels, input_length).

num_filters : int

The number of learnable convolutional filters this layer has.

filter_size : int or iterable of int

An integer or a 1-element tuple specifying the size of the filters.

stride : int or iterable of int

An integer or a 1-element tuple specifying the stride of the convolution operation.

pad : int, iterable of int, ‘full’, ‘same’ or ‘valid’ (default: 0)

By default, the convolution is only computed where the input and the filter fully overlap (a valid convolution). When stride=1, this yields an output that is smaller than the input by filter_size - 1. The pad argument allows you to implicitly pad the input with zeros, extending the output size.

An integer or a 1-element tuple results in symmetric zero-padding of the given size on both borders.

'full' pads with one less than the filter size on both sides. This is equivalent to computing the convolution wherever the input and the filter overlap by at least one position.

'same' pads with half the filter size on both sides (one less on the second side for an even filter size). When stride=1, this results in an output size equal to the input size.

'valid' is an alias for 0 (no padding / a valid convolution).

untie_biases : bool (default: False)

If False, the layer will have a bias parameter for each channel, which is shared across all positions in this channel. As a result, the b attribute will be a vector (1D).

If True, the layer will have separate bias parameters for each position in each channel. As a result, the b attribute will be a matrix (2D).

W : Theano shared variable, numpy array or callable

An initializer for the weights of the layer. This should initialize the layer weights to a 3D array with shape (num_filters, num_input_channels, filter_length). See lasagne.utils.create_param() for more information.

b : Theano shared variable, numpy array, callable or None

An initializer for the biases of the layer. If None is provided, the layer will have no biases. This should initialize the layer biases to a 1D array with shape (num_filters,) if untied_biases is set to False. If it is set to True, its shape should be (num_filters, input_length) instead. See lasagne.utils.create_param() for more information.

nonlinearity : callable or None

The nonlinearity that is applied to the layer activations. If None is provided, the layer will be linear.

convolution : callable

The convolution implementation to use. The lasagne.theano_extensions.conv module provides some alternative implementations for 1D convolutions, because the Theano API only features a 2D convolution implementation. Usually it should be fine to leave this at the default value.

**kwargs

Any additional keyword arguments are passed to the Layer superclass.

Notes

Theano’s underlying convolution (theano.tensor.nnet.conv.conv2d()) only supports pad=0 and pad='full'. This layer emulates other modes by cropping a full convolution or explicitly padding the input with zeros.

Attributes

W (Theano shared variable) Variable representing the filter weights.
b (Theano shared variable) Variable representing the biases.
get_W_shape()[source]

Get the shape of the weight matrix W.

Returns:

tuple of int

The shape of the weight matrix.

class lasagne.layers.Conv2DLayer(incoming, num_filters, filter_size, stride=(1, 1), pad=0, untie_biases=False, W=lasagne.init.GlorotUniform(), b=lasagne.init.Constant(0.), nonlinearity=lasagne.nonlinearities.rectify, convolution=theano.tensor.nnet.conv2d, **kwargs)[source]

2D convolutional layer

Performs a 2D convolution on its input and optionally adds a bias and applies an elementwise nonlinearity.

Parameters:

incoming : a Layer instance or a tuple

The layer feeding into this layer, or the expected input shape. The output of this layer should be a 4D tensor, with shape (batch_size, num_input_channels, input_rows, input_columns).

num_filters : int

The number of learnable convolutional filters this layer has.

filter_size : int or iterable of int

An integer or a 2-element tuple specifying the size of the filters.

stride : int or iterable of int

An integer or a 2-element tuple specifying the stride of the convolution operation.

pad : int, iterable of int, ‘full’, ‘same’ or ‘valid’ (default: 0)

By default, the convolution is only computed where the input and the filter fully overlap (a valid convolution). When stride=1, this yields an output that is smaller than the input by filter_size - 1. The pad argument allows you to implicitly pad the input with zeros, extending the output size.

A single integer results in symmetric zero-padding of the given size on all borders, a tuple of two integers allows different symmetric padding per dimension.

'full' pads with one less than the filter size on both sides. This is equivalent to computing the convolution wherever the input and the filter overlap by at least one position.

'same' pads with half the filter size on both sides (one less on the second side for an even filter size). When stride=1, this results in an output size equal to the input size.

'valid' is an alias for 0 (no padding / a valid convolution).

Note that 'full' and 'same' can be faster than equivalent integer values due to optimizations by Theano.

untie_biases : bool (default: False)

If False, the layer will have a bias parameter for each channel, which is shared across all positions in this channel. As a result, the b attribute will be a vector (1D).

If True, the layer will have separate bias parameters for each position in each channel. As a result, the b attribute will be a 3D tensor.

W : Theano shared variable, numpy array or callable

An initializer for the weights of the layer. This should initialize the layer weights to a 4D array with shape (num_filters, num_input_channels, filter_rows, filter_columns). See lasagne.utils.create_param() for more information.

b : Theano shared variable, numpy array, callable or None

An initializer for the biases of the layer. If None is provided, the layer will have no biases. This should initialize the layer biases to a 1D array with shape (num_filters,) if untied_biases is set to False. If it is set to True, its shape should be (num_filters, input_rows, input_columns) instead. See lasagne.utils.create_param() for more information.

nonlinearity : callable or None

The nonlinearity that is applied to the layer activations. If None is provided, the layer will be linear.

convolution : callable

The convolution implementation to use. Usually it should be fine to leave this at the default value.

**kwargs

Any additional keyword arguments are passed to the Layer superclass.

Notes

Theano’s underlying convolution (theano.tensor.nnet.conv.conv2d()) only supports pad=0 and pad='full'. This layer emulates other modes by cropping a full convolution or explicitly padding the input with zeros.

Attributes

W (Theano shared variable) Variable representing the filter weights.
b (Theano shared variable) Variable representing the biases.
get_W_shape()[source]

Get the shape of the weight matrix W.

Returns:

tuple of int

The shape of the weight matrix.

Note

For experts: Conv2DLayer will create a convolutional layer using T.nnet.conv2d, Theano’s default convolution. On compilation for GPU, Theano replaces this with a cuDNN-based implementation if available, otherwise falls back to a gemm-based implementation. For details on this, please see the Theano convolution documentation.

Lasagne also provides convolutional layers directly enforcing a specific implementation: lasagne.layers.dnn.Conv2DDNNLayer to enforce cuDNN, lasagne.layers.corrmm.Conv2DMMLayer to enforce the gemm-based one, lasagne.layers.cuda_convnet.Conv2DCCLayer for Krizhevsky’s cuda-convnet.