Tensor manipulation#

Basic operation#

shape(x)

Return the shape of x.

shape.shape_tuple(x)

Get a tuple of symbolic shape values.

Casting#

cast(x, dtype)

Symbolically cast x to a Tensor of type dtype.

real

Return real component of complex-valued tensor z Generalizes a scalar Op to tensors.

imag

Return imaginary component of complex-valued tensor z Generalizes a scalar Op to tensors.

Updating elements#

Attention

Index assignment is not supported in Aesara. If you want to do the equivalent of a[5] = b or a[5]+=b you will need to use the set_subtensor or inc_subtensor operator respectively.

set_subtensor(x, y[, inplace, ...])

Return x with the given subtensor overwritten by y.

inc_subtensor(x, y[, inplace, ...])

Update the value of an indexed array by a given amount.

fill_diagonal_offset(a, val, offset)

Returns a copy of an array with all elements of the main diagonal set to a specified scalar value.

Changing tensor shape#

Specific to Aesara

The specify_shape operator is specific to Aesara.

reshape(x, newshape[, ndim])

flatten(x[, ndim])

Return a copy of the array collapsed into one dimension.

specify_shape(x, shape)

Specify a fixed shape for a Variable.

Transpose-like operations#

moveaxis(a, source, destination)

Move axes of a TensorVariable to new positions.

swapaxes(y, axis1, axis2)

Swap the axes of a tensor.

roll(x, shift[, axis])

Convenience function to roll TensorTypes along the given axis.

TensorVariable.T

transpose(x[, axes])

Reorder the dimensions of x.

Changing number of dimensions#

atleast_1d(*arys[, n, left])

Convert inputs to arrays with at least n dimensions.

atleast_2d(*arys[, n, left])

Convert inputs to arrays with at least n dimensions.

atleast_3d(*arys[, n, left])

Convert inputs to arrays with at least n dimensions.

broadcast_to(x, shape)

Broadcast an array to a new shape.

broadcast_arrays(*args)

Broadcast any number of arrays against each other.

squeeze(x[, axis])

Remove broadcastable (length 1) dimensions from the shape of an array.

Joining tensors#

join(axis, *tensors_list)

Convenience function to concatenate TensorTypes along the given axis.

stack(*tensors, **kwargs)

Stack tensors in sequence on given axis (default is 0).

stacklists(arg)

Recursively stack lists of tensors to maintain similar structure.

horizontal_stack(*args)

Stack arrays in sequence horizontally (column wise).

vertical_stack(*args)

Stack arrays in sequence vertically (row wise).

concatenate(tensor_list[, axis])

Alias for `join`(axis, *tensor_list).

aesara.tensor.stack(tensors, axis=0)[source]#

Stack tensors in sequence on given axis (default is 0).

Take a sequence of tensors and stack them on given axis to make a single tensor. The size in dimension axis of the result will be equal to the number of tensors passed.

Parameters:
  • tensors – a list or a tuple of one or more tensors of the same rank.

  • axis – the axis along which the tensors will be stacked. Default value is 0.

Returns:

A tensor such that rval[0] == tensors[0], rval[1] == tensors[1], etc.

Examples:

>>> a = aesara.tensor.type.scalar()
>>> b = aesara.tensor.type.scalar()
>>> c = aesara.tensor.type.scalar()
>>> x = aesara.tensor.stack([a, b, c])
>>> x.ndim # x is a vector of length 3.
1
>>> a = aesara.tensor.type.tensor4()
>>> b = aesara.tensor.type.tensor4()
>>> c = aesara.tensor.type.tensor4()
>>> x = aesara.tensor.stack([a, b, c])
>>> x.ndim # x is a 5d tensor.
5
>>> rval = x.eval(dict((t, np.zeros((2, 2, 2, 2))) for t in [a, b, c]))
>>> rval.shape # 3 tensors are stacked on axis 0
(3, 2, 2, 2, 2)

We can also specify different axis than default value 0:

>>> x = aesara.tensor.stack([a, b, c], axis=3)
>>> x.ndim
5
>>> rval = x.eval(dict((t, np.zeros((2, 2, 2, 2))) for t in [a, b, c]))
>>> rval.shape # 3 tensors are stacked on axis 3
(2, 2, 2, 3, 2)
>>> x = aesara.tensor.stack([a, b, c], axis=-2)
>>> x.ndim
5
>>> rval = x.eval(dict((t, np.zeros((2, 2, 2, 2))) for t in [a, b, c]))
>>> rval.shape # 3 tensors are stacked on axis -2
(2, 2, 2, 3, 2)
aesara.tensor.stack(*tensors)[source]

Warning

The interface stack(*tensors) is deprecated! Use stack(tensors, axis=0) instead.

Stack tensors in sequence vertically (row wise).

Take a sequence of tensors and stack them vertically to make a single tensor.

param tensors:

one or more tensors of the same rank

returns:

A tensor such that rval[0] == tensors[0], rval[1] == tensors[1], etc.

>>> x0 = at.scalar()
>>> x1 = at.scalar()
>>> x2 = at.scalar()
>>> x = at.stack(x0, x1, x2)
>>> x.ndim # x is a vector of length 3.
1
aesara.tensor.concatenate(tensor_list, axis=0)[source]#
Parameters:
  • tensor_list (a list or tuple of Tensors that all have the same shape in the axes not specified by the axis argument.) – one or more Tensors to be concatenated together into one.

  • axis (literal or symbolic integer) – Tensors will be joined along this axis, so they may have different shape[axis]

>>> x0 = at.fmatrix()
>>> x1 = at.ftensor3()
>>> x2 = at.fvector()
>>> x = at.concatenate([x0, x1[0], at.shape_padright(x2)], axis=1)
>>> x.ndim
2
aesara.tensor.stacklists(tensor_list)[source]#
Parameters:

tensor_list (an iterable that contains either tensors or other iterables of the same type as tensor_list (in other words, this is a tree whose leaves are tensors).) – tensors to be stacked together.

Recursively stack lists of tensors to maintain similar structure.

This function can create a tensor from a shaped list of scalars:

>>> from aesara.tensor import stacklists, scalars, matrices
>>> from aesara import function
>>> a, b, c, d = scalars('abcd')
>>> X = stacklists([[a, b], [c, d]])
>>> f = function([a, b, c, d], X)
>>> f(1, 2, 3, 4)
array([[ 1.,  2.],
       [ 3.,  4.]])

We can also stack arbitrarily shaped tensors. Here we stack matrices into a 2 by 2 grid:

>>> from numpy import ones
>>> a, b, c, d = matrices('abcd')
>>> X = stacklists([[a, b], [c, d]])
>>> f = function([a, b, c, d], X)
>>> x = ones((4, 4), 'float32')
>>> f(x, x, x, x).shape
(2, 2, 4, 4)

Splitting tensors#

split(x, splits_size, n_splits[, axis])

Tiling tensors#

tile(x, reps[, ndim])

Tile input array x according to reps.

repeat(x, repeats[, axis])

Repeat elements of an array.

Adding and removing elements#

unique(ar[, return_index, return_inverse, ...])

Find the unique elements of an array.

Rearranging elements#

reshape(x, newshape[, ndim])

flatten(x[, ndim])

Return a copy of the array collapsed into one dimension.

permute_row_elements(x, y[, inverse])

inverse_permutation(perm)

Computes the inverse of permutations.

Padding tensors#

shape_padleft(t[, n_ones])

Reshape t by left-padding the shape with n_ones 1s.

shape_padright(t[, n_ones])

Reshape t by right-padding the shape with n_ones 1s.

shape_padaxis(t, axis)

Reshape t by inserting 1 at the dimension axis.