NumpyTensorSpace
- class odl.backends.arrays.npy_tensors.NumpyTensorSpace(shape, dtype='float64', device='cpu', **kwargs)[source]
Bases:
TensorSpaceSet 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 with1.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,normorinner. It also cannot be used in case of non-numericdtype.- distcallable, optional
Distance function defining a metric on the space. It must accept two
NumpyTensorarguments and return a non-negative real number. SeeNotesfor mathematical requirements.By default,
dist(x, y)is calculated asnorm(x - y).This option cannot be combined with
weight,normorinner. It also cannot be used in case of non-numericdtype.- normcallable, optional
The norm implementation. It must accept a
NumpyTensorargument, return a non-negative real number. SeeNotesfor mathematical requirements.By default,
norm(x)is calculated asinner(x, x).This option cannot be combined with
weight,distorinner. It also cannot be used in case of non-numericdtype.- innercallable, optional
The inner product implementation. It must accept two
NumpyTensorarguments and return an element of the field of the space (usually real or complex number). SeeNotesfor mathematical requirements.This option cannot be combined with
weight,distornorm. It also cannot be used in case of non-numericdtype.- 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,normorinneris given, or ifdtypeis 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
is a mapping
satisfying the following conditions for all space elements
:
,
,
,
.
A norm on a space
is a mapping
satisfying the following conditions for all
space elements
: and scalars
:
,
,
,
.
An inner product on a space
over a field
or
is a
mapping
satisfying the following conditions for all
space elements
: and scalars
:
,
,
.
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')