Embedding layers

class lasagne.layers.EmbeddingLayer(incoming, input_size, output_size, W=lasagne.init.Normal(), **kwargs)[source]

A layer for word embeddings. The input should be an integer type Tensor variable.

Parameters:

incoming : a Layer instance or a tuple

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

input_size: int

The Number of different embeddings. The last embedding will have index input_size - 1.

output_size : int

The size of each embedding.

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

Initial value, expression or initializer for the embedding matrix. This should be a matrix with shape (input_size, output_size). See lasagne.utils.create_param() for more information.

Examples

>>> from lasagne.layers import EmbeddingLayer, InputLayer, get_output
>>> import theano
>>> x = T.imatrix()
>>> l_in = InputLayer((3, ))
>>> W = np.arange(3*5).reshape((3, 5)).astype('float32')
>>> l1 = EmbeddingLayer(l_in, input_size=3, output_size=5, W=W)
>>> output = get_output(l1, x)
>>> f = theano.function([x], output)
>>> x_test = np.array([[0, 2], [1, 2]]).astype('int32')
>>> f(x_test)
array([[[  0.,   1.,   2.,   3.,   4.],
        [ 10.,  11.,  12.,  13.,  14.]],

       [[  5.,   6.,   7.,   8.,   9.],
        [ 10.,  11.,  12.,  13.,  14.]]], dtype=float32)
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.