NumpyTensorSpace

class odl.backends.arrays.npy_tensors.NumpyTensorSpace(shape, dtype='float64', device='cpu', **kwargs)[source]

Bases: TensorSpace

Set of tensors of arbitrary data type, implemented with NumPy.

A tensor is, in the most general sense, a multi-dimensional array that allows operations per entry (keep the rank constant), reductions / contractions (reduce the rank) and broadcasting (raises the rank). For non-numeric data type like object, the range of valid operations is rather limited since such a set of tensors does not define a vector space. Any numeric data type, on the other hand, is considered valid for a tensor space, although certain operations - like division with integer dtype - are not guaranteed to yield reasonable results.

Under these restrictions, all basic vector space operations are supported by this class, along with reductions based on arithmetic or comparison, and element-wise mathematical functions (“ufuncs”).

This class is implemented using numpy.ndarray’s as back-end.

See the Wikipedia article on tensors for further details. See also [Hac2012] “Part I Algebraic Tensors” for a rigorous treatment of tensors with a definition close to this one.

Note also that this notion of tensors is the same as in popular Deep Learning frameworks.

References

[Hac2012] Hackbusch, W. Tensor Spaces and Numerical Tensor Calculus. Springer, 2012.

__init__(shape, dtype='float64', device='cpu', **kwargs)[source]

Initialize a new instance.

Parameters

shapepositive int or sequence of positive ints

Number of entries per axis for elements in this space. A single integer results in a space with rank 1, i.e., 1 axis.

dtype (str): optional

Data type of each element. Defaults to ‘float64’

device (str):

Device on which the data is. For Numpy, it must be ‘cpu’.

Other Parameters

weightingoptional

Use weighted inner product, norm, and dist. The following types are supported as weighting:

None: no weighting, i.e. weighting with 1.0 (default).

Weighting: Use this weighting as-is. Compatibility with this space’s elements is not checked during init.

float: Weighting by a constant.

array-like: Pointwise weighting by an array.

This option cannot be combined with dist, norm or inner. It also cannot be used in case of non-numeric dtype.

distcallable, optional

Distance function defining a metric on the space. It must accept two NumpyTensor arguments and return a non-negative real number. See Notes for mathematical requirements.

By default, dist(x, y) is calculated as norm(x - y).

This option cannot be combined with weight, norm or inner. It also cannot be used in case of non-numeric dtype.

normcallable, optional

The norm implementation. It must accept a NumpyTensor argument, return a non-negative real number. See Notes for mathematical requirements.

By default, norm(x) is calculated as inner(x, x).

This option cannot be combined with weight, dist or inner. It also cannot be used in case of non-numeric dtype.

innercallable, optional

The inner product implementation. It must accept two NumpyTensor arguments and return an element of the field of the space (usually real or complex number). See Notes for mathematical requirements.

This option cannot be combined with weight, dist or norm. It also cannot be used in case of non-numeric dtype.

exponentpositive float, optional

Exponent of the norm. For values other than 2.0, no inner product is defined.

This option has no impact if either dist, norm or inner is given, or if dtype is non-numeric.

Default: 2.0

kwargs :

Further keyword arguments are passed to the weighting classes.

See Also

odl.core.space.space_utils.rn : constructor for real tensor spaces odl.core.space.space_utils.cn : constructor for complex tensor spaces odl.core.space.space_utils.tensor_space :

constructor for tensor spaces of arbitrary scalar data type

Notes

  • A distance function or metric on a space \mathcal{X} is a mapping d:\mathcal{X} \times \mathcal{X} \to \mathbb{R} satisfying the following conditions for all space elements x, y, z:

    • d(x, y) \geq 0,

    • d(x, y) = 0 \Leftrightarrow x = y,

    • d(x, y) = d(y, x),

    • d(x, y) \leq d(x, z) + d(z, y).

  • A norm on a space \mathcal{X} is a mapping \| \cdot \|:\mathcal{X} \to \mathbb{R} satisfying the following conditions for all space elements x, y: and scalars s:

    • \| x\| \geq 0,

    • \| x\| = 0 \Leftrightarrow x = 0,

    • \| sx\| = |s| \cdot \| x \|,

    • \| x+y\| \leq \| x\| +
\| y\|.

  • An inner product on a space \mathcal{X} over a field \mathbb{F} = \mathbb{R} or \mathbb{C} is a mapping \langle\cdot, \cdot\rangle: \mathcal{X} \times
\mathcal{X} \to \mathbb{F} satisfying the following conditions for all space elements x, y, z: and scalars s:

    • \langle x, y\rangle =
\overline{\langle y, x\rangle},

    • \langle sx + y, z\rangle = s \langle x, z\rangle +
\langle y, z\rangle,

    • \langle x, x\rangle = 0 \Leftrightarrow x = 0.

Examples

Explicit initialization with the class constructor:

>>> space = NumpyTensorSpace(3, float)
>>> space
rn(3)
>>> space.shape
(3,)
>>> space.dtype
dtype('float64')

A more convenient way is to use factory functions:

>>> space = odl.rn(3, weighting=[1, 2, 3])
>>> space
rn(3, weighting=[1, 2, 3])
>>> space = odl.tensor_space((2, 3), dtype=int)
>>> space
tensor_space((2, 3), 'int32')