tensor.extra_ops – Tensor Extra Ops
- class
theano.tensor.extra_ops.
CpuContiguous
[source] - Check to see if the input is c-contiguous,if it is, do nothing, else return a contiguous array.
- class
theano.tensor.extraops.
SearchsortedOp
(_side='left')[source] - Wrapper of numpy.searchsorted.
For full documentation, see searchsorted()
.
See also
searchsorted
- numpy-like function to use the SearchsortedOp
- class
theano.tensor.extraops.
Unique
(_return_index=False, return_inverse=False, return_counts=False)[source] - Wraps numpy.unique. This op is not implemented on the GPU.
Examples
- >>> import numpy as np
- >>> import theano
- >>> x = theano.tensor.vector()
- >>> f = theano.function([x], Unique(True, True, False)(x))
- >>> f([1, 2., 3, 4, 3, 2, 1.])
- [array([ 1., 2., 3., 4.]), array([0, 1, 2, 3]), array([0, 1, 2, 3, 2, 1, 0])]
- >>> y = theano.tensor.matrix()
- >>> g = theano.function([y], Unique(True, True, False)(y))
- >>> g([[1, 1, 1.0], (2, 3, 3.0)])
- [array([ 1., 2., 3.]), array([0, 3, 4]), array([0, 0, 0, 1, 2, 2])]
theano.tensor.extraops.
bartlett
(_M)[source]- An instance of this class returns the Bartlett spectral window in thetime-domain. The Bartlett window is very similar to a triangular window,except that the end points are at zero. It is often used in signalprocessing for tapering a signal, without generating too much ripple inthe frequency domain.
New in version 0.6.
Parameters:M (integer scalar) – Number of points in the output window. If zero or less,an empty vector is returned.Returns:The triangular window, with the maximum value normalized to one(the value one appears only if the number of samples is odd), withthe first and last samples equal to zero.Return type:vector of doubles
theano.tensor.extraops.
bincount
(_x, weights=None, minlength=None, assert_nonneg=False)[source]- Count number of occurrences of each value in array of ints.
The number of bins (of size 1) is one larger than the largestvalue in x. If minlength is specified, there will be at leastthis number of bins in the output array (though it will be longerif necessary, depending on the contents of x). Each bin gives thenumber of occurrences of its index value in x. If weights isspecified the input array is weighted by it, i.e. if a value nis found at position i, out[n] += weight[i] instead of out[n] += 1.
Parameters:
- x (1 dimension, nonnegative ints) –
- weights (array of the same shape as x with corresponding weights.) – Optional.
- minlength (A minimum number of bins for the output array.) – Optional.
- assert_nonneg (A flag that inserts an assert_op to check if) – every input x is nonnegative.Optional.
New in version 0.6.
theano.tensor.extraops.
compress
(_condition, x, axis=None)[source]- Return selected slices of an array along given axis.
It returns the input tensor, but with selected slices along a given axisretained. If no axis is provided, the tensor is flattened.Corresponds to numpy.compress
New in version 0.7.
Parameters:
- x – Input data, tensor variable.
- condition – 1 dimensional array of non-zero and zero valuescorresponding to indices of slices along a selected axis.Returns: x with selected slices. Return type: object
theano.tensor.extraops.
cumprod
(_x, axis=None)[source]- Return the cumulative product of the elements along a given axis.
Wraping of numpy.cumprod.
Parameters:
- x – Input tensor variable.
- axis – The axis along which the cumulative product is computed.The default (None) is to compute the cumprod over the flattened array.
New in version 0.7.
theano.tensor.extraops.
cumsum
(_x, axis=None)[source]- Return the cumulative sum of the elements along a given axis.
Wraping of numpy.cumsum.
Parameters:
- x – Input tensor variable.
- axis – The axis along which the cumulative sum is computed.The default (None) is to compute the cumsum over the flattened array.
New in version 0.7.
theano.tensor.extraops.
diff
(_x, n=1, axis=-1)[source]- Calculate the n-th order discrete difference along given axis.
The first order difference is given by out[i] = a[i + 1] - a[i]along the given axis, higher order differences are calculated byusing diff recursively. Wraping of numpy.diff.
Parameters:
- x – Input tensor variable.
- n – The number of times values are differenced, default is 1.
- axis – The axis along which the difference is taken, default is the last axis.
New in version 0.6.
theano.tensor.extraops.
fill_diagonal
(_a, val)[source]- Returns a copy of an array with allelements of the main diagonal set to a specified scalar value.
New in version 0.6.
Parameters:
- a – Rectangular array of at least two dimensions.
- val – Scalar value to fill the diagonal whose type must becompatible with that of array ‘a’ (i.e. ‘val’ cannot be viewedas an upcast of ‘a’).Returns:
- array – An array identical to ‘a’ except that its main diagonalis filled with scalar ‘val’. (For an array ‘a’ with a.ndim >=2, the main diagonal is the list of locations ai, i, …, i.)
- Support rectangular matrix and tensor with more than 2 dimensions
- if the later have all dimensions are equals.
theano.tensor.extraops.
fill_diagonal_offset
(_a, val, offset)[source]- Returns a copy of an array with allelements of the main diagonal set to a specified scalar value.
Parameters:
- a – Rectangular array of two dimensions.
- val – Scalar value to fill the diagonal whose type must becompatible with that of array ‘a’ (i.e. ‘val’ cannot be viewedas an upcast of ‘a’).
- offset – Scalar value Offset of the diagonal from the maindiagonal. Can be positive or negative integer.Returns: An array identical to ‘a’ except that its offset diagonalis filled with scalar ‘val’. The output is unwrapped. Return type: array
theano.tensor.extraops.
ravel_multi_index
(_multi_index, dims, mode='raise', order='C')[source]- Converts a tuple of index arrays into an array of flatindices, applying boundary modes to the multi-index.
Parameters:
- multi_index (tuple of Theano or NumPy arrays) – A tuple of integer arrays, one array for each dimension.
- dims (tuple of ints) – The shape of array into which the indices from
multi_index
apply. - mode ({'raise', 'wrap', 'clip'}, optional) – Specifies how out-of-bounds indices are handled. Can specifyeither one mode or a tuple of modes, one mode per index. ‘raise’ – raise an error (default) ‘wrap’ – wrap around* ‘clip’ – clip to the rangeIn ‘clip’ mode, a negative index which would normallywrap will clip to 0 instead.
- order ({'C', 'F'}, optional) – Determines whether the multi-index should be viewed asindexing in row-major (C-style) or column-major(Fortran-style) order.Returns:
raveled_indices – An array of indices into the flattened version of an arrayof dimensions
dims
. Return type: Theano array
See also
theano.tensor.extraops.
repeat
(_x, repeats, axis=None)[source]- Repeat elements of an array.
It returns an array which has the same shape as x, exceptalong the given axis. The axis is used to speficy along whichaxis to repeat values. By default, use the flattened inputarray, and return a flat output array.
The number of repetitions for each element is repeat.repeats is broadcasted to fit the length of the given axis.
Parameters:
- x – Input data, tensor variable.
- repeats – int, scalar or tensor variable
- axis (int, optional) –
See also
tensor.tile()
, ()
theano.tensor.extraops.
searchsorted
(_x, v, side='left', sorter=None)[source]- Find indices where elements should be inserted to maintain order.
Wrapping of numpy.searchsorted. Find the indices into a sorted arrayx such that, if the corresponding elements in v were insertedbefore the indices, the order of x would be preserved.
Parameters:
- x (1-D tensor (array-like__)) – Input array. If sorter is None, then it must be sorted inascending order, otherwise sorter must be an array of indiceswhich sorts it.
- v (tensor (array-like)) – Contains the values to be inserted into x.
- side ({'left', 'right'}, optional.) – If ‘left’ (default), the index of the first suitablelocation found is given. If ‘right’, return the last such index. Ifthere is no suitable index, return either 0 or N (where N is the lengthof x).
- sorter (1-D tensor of integers (array-like), __optional) – Contains indices that sort array x into ascending order.They are typically the result of argsort.Returns: indices – Array of insertion points with the same shape as v. Return type: tensor of integers (int64)
See also
Notes
- Binary search is used to find the required insertion points.
- This Op is working only on CPU currently. Examples
- >>> from theano import tensor
- >>> x = tensor.dvector()
- >>> idx = x.searchsorted(3)
- >>> idx.eval({x: [1,2,3,4,5]})
- array(2)
- >>> tensor.extra_ops.searchsorted([1,2,3,4,5], 3).eval()
- array(2)
- >>> tensor.extra_ops.searchsorted([1,2,3,4,5], 3, side='right').eval()
- array(3)
- >>> tensor.extra_ops.searchsorted([1,2,3,4,5], [-10, 10, 2, 3]).eval()
- array([0, 5, 1, 2])
New in version 0.9.
theano.tensor.extraops.
squeeze
(_x)[source]- Remove broadcastable dimensions from the shape of an array.
It returns the input array, but with thebroadcastable dimensions removed. This isalways x itself or a view into x.
New in version 0.6.
Parameters:x – Input data, tensor variable.Returns:x without its broadcastable dimensions.Return type:object
theano.tensor.extraops.
to_one_hot
(_y, nb_class, dtype=None)[source]- Return a matrix where each row correspond to the one hotencoding of each element in y.
Parameters:
- y – A vector of integer value between 0 and nb_class - 1.
- nb_class (int) – The number of class in y.
- dtype (data-type) – The dtype of the returned matrix. Default floatX.Returns:
A matrix of shape (y.shape[0], nb_class), where each row
i
isthe one hot encoding of the correspondingy[i]
value. Return type: object
theano.tensor.extraops.
unravel_index
(_indices, dims, order='C', ndim=None)[source]- Converts a flat index or array of flat indices into a tupleof coordinate arrays.
This method is similar to the NumPy version, except for theadditional ndim
parameter. This parameter is required ifthe length of dims
cannot be determined automatically.
Parameters:
- indices (Theano or NumPy array) – An integer array whose elements are indices into the flattenedversion of an array of dimensions
dims
. - dims (tuple of ints) – The shape of the array to use for unraveling
indices
. - order ({'C', 'F'}, optional) – Determines whether the indices should be viewed as indexing inrow-major (C-style) or column-major (Fortran-style) order.
- ndim (int, optional) – Specifies the number of dimensions, i.e., the length of
dims
. This is required if the dimensions cannot be determinedautomatically fromdims
itself.Returns: unraveled_coords – Each array in the tuple has the same shape as theindices
array. Return type: tuple of ndarray
See also