Converts data to a numpy array of dtype theano.config.floatX.


arr : array_like

The data to be converted.


numpy ndarray

The input array in the floatX dtype configured for Theano. If arr is an ndarray of correct dtype, it is returned as is.

lasagne.utils.shared_empty(dim=2, dtype=None)[source]

Creates empty Theano shared variable.

Shortcut to create an empty Theano shared variable with the specified number of dimensions.


dim : int, optional

The number of dimensions for the empty variable, defaults to 2.

dtype : a numpy data-type, optional

The desired dtype for the variable. Defaults to the Theano floatX dtype.


Theano shared variable

An empty Theano shared variable of dtype dtype with dim dimensions.


Wrap as Theano expression.

Wraps the given input as a Theano constant if it is not a valid Theano expression already. Useful to transparently handle numpy arrays and Python scalars, for example.


input : number, numpy array or Theano expression

Expression to be converted to a Theano constant.


Theano symbolic constant

Theano constant version of input.


Returns all shared variables the given expression(s) depend on.


expressions : Theano expression or iterable of Theano expressions

The expressions to collect shared variables from.


list of Theano shared variables

All shared variables the given expression(s) depend on, in fixed order (as found by a left-recursive depth-first search). If some expressions are shared variables themselves, they are included in the result.

lasagne.utils.one_hot(x, m=None)[source]

One-hot representation of integer vector.

Given a vector of integers from 0 to m-1, returns a matrix with a one-hot representation, where each row corresponds to an element of x.


x : integer vector

The integer vector to convert to a one-hot representation.

m : int, optional

The number of different columns for the one-hot representation. This needs to be strictly greater than the maximum value of x. Defaults to max(x) + 1.


Theano tensor variable

A Theano tensor variable of shape (n, m), where n is the length of x, with the one-hot representation of x.


If your integer vector represents target class memberships, and you wish to compute the cross-entropy between predictions and the target class memberships, then there is no need to use this function, since the function lasagne.objectives.categorical_crossentropy() can compute the cross-entropy from the integer vector directly.


Filters duplicates of iterable.

Create a new list from l with duplicate entries removed, while preserving the original order.


l : iterable

Input iterable to filter of duplicates.



A list of elements of l without duplicates and in the same order.

lasagne.utils.compute_norms(array, norm_axes=None)[source]

Compute incoming weight vector norms.


array : numpy array or Theano expression

Weight or bias.

norm_axes : sequence (list or tuple)

The axes over which to compute the norm. This overrides the default norm axes defined for the number of dimensions in array. When this is not specified and array is a 2D array, this is set to (0,). If array is a 3D, 4D or 5D array, it is set to a tuple listing all axes but axis 0. The former default is useful for working with dense layers, the latter is useful for 1D, 2D and 3D convolutional layers. Finally, in case array is a vector, norm_axes is set to an empty tuple, and this function will simply return the absolute value for each element. This is useful when the function is applied to all parameters of the network, including the bias, without distinction. (Optional)


norms : 1D array or Theano vector (1D)

1D array or Theano vector of incoming weight/bias vector norms.


>>> array = np.random.randn(100, 200)
>>> norms = compute_norms(array)
>>> norms.shape
>>> norms = compute_norms(array, norm_axes=(1,))
>>> norms.shape
lasagne.utils.create_param(spec, shape, name=None)[source]

Helper method to create Theano shared variables for layer parameters and to initialize them.


spec : scalar number, numpy array, Theano expression, or callable

Either of the following:

  • a scalar or a numpy array with the initial parameter values
  • a Theano expression or shared variable representing the parameters
  • a function or callable that takes the desired shape of the parameter array as its single argument and returns a numpy array, a Theano expression, or a shared variable representing the parameters.

shape : iterable of int

a tuple or other iterable of integers representing the desired shape of the parameter array.

name : string, optional

The name to give to the parameter variable. Ignored if spec is or returns a Theano expression or shared variable that already has a name.


Theano shared variable or Theano expression

A Theano shared variable or expression representing layer parameters. If a scalar or a numpy array was provided, a shared variable is initialized to contain this array. If a shared variable or expression was provided, it is simply returned. If a callable was provided, it is called, and its output is used to initialize a shared variable.


This function is called by Layer.add_param() in the constructor of most Layer subclasses. This enables those layers to support initialization with scalars, numpy arrays, existing Theano shared variables or expressions, and callables for generating initial parameter values, Theano expressions, or shared variables.