Dense Tensor (pyttb.tensor
)
Note
Classes and functions defined in tensor.py
have been promoted to the pyttb
namespace.
For all examples in this document, the following module imports are assumed:
>>> import pyttb as ttb
>>> import numpy as np
Class for dense tensors.
- pyttb.tensor.data
Data of the tensor
- Type:
Instances of pyttb.tensor
can be created using
pyttb.tensor.tensor.__init__()
or the following methods:
from_function()
- Create a tensor from a function
copy()
- Make a deep copy of a tensor
tenones()
- Create an all ones tensor of a specified size
tenzeros()
- Create an all zeros tensor of a specified size
tenrand()
- Create a random tensor of a specified size
tendiag()
- Create a tensor with a specified diagonal
teneye()
- Create an identity tensor
pyttb.sptensor.to_tensor()
- Convert a sparse tensor to a dense tensor
pyttb.ktensor.to_tensor()
- Convert a Kruskal tensor to a dense tensor
pyttb.ttensor.to_tensor()
- Convert a Tucker tensor to a dense tensor
pyttb.tenmat.to_tensor()
- Convert a tenmat to a dense tensor
See Tensors for getting started with the tensor class.
- pyttb.tensor.__delattr__(self, name, /)
Implement delattr(self, name).
- pyttb.tensor.__dir__(self, /)
Default dir() implementation.
- pyttb.tensor.__format__(self, format_spec, /)
Default object formatter.
- pyttb.tensor.__getattribute__(self, name, /)
Return getattr(self, name).
- pyttb.tensor.__init_subclass__()
This method is called when a class is subclassed.
The default implementation does nothing. It may be overridden to extend subclasses.
- pyttb.tensor.__new__(*args, **kwargs)
Create and return a new object. See help(type) for accurate signature.
- pyttb.tensor.__reduce__(self, /)
Helper for pickle.
- pyttb.tensor.__reduce_ex__(self, protocol, /)
Helper for pickle.
- pyttb.tensor.__setattr__(self, name, value, /)
Implement setattr(self, name, value).
- pyttb.tensor.__sizeof__(self, /)
Size of object in memory, in bytes.
- pyttb.tensor.__subclasshook__()
Abstract classes can override this to customize issubclass().
This is invoked early on by abc.ABCMeta.__subclasscheck__(). It should return True, False or NotImplemented. If it returns NotImplemented, the normal algorithm is used. Otherwise, it overrides the normal algorithm (and the outcome is cached).
- pyttb.tenones(shape: int | Iterable[int], order: Literal['F', 'C'] = 'F') tensor [source]
Create a tensor of all ones.
- Parameters:
shape – Shape of resulting tensor.
order – Memory layout for resulting tensor.
- Returns:
Constructed tensor.
Examples
>>> T = ttb.tenones((3,)) >>> T tensor of shape (3,) with order F data[:] = [1. 1. 1.] >>> T = ttb.tenones((3, 3)) >>> T tensor of shape (3, 3) with order F data[:, :] = [[1. 1. 1.] [1. 1. 1.] [1. 1. 1.]]
- pyttb.tenzeros(shape: int | Iterable[int], order: Literal['F', 'C'] = 'F') tensor [source]
Create a tensor of all zeros.
- Parameters:
shape – Shape of resulting tensor.
order – Memory layout for resulting tensor.
- Returns:
Constructed tensor.
Examples
>>> T = ttb.tenzeros((3,)) >>> T tensor of shape (3,) with order F data[:] = [0. 0. 0.] >>> T = ttb.tenzeros((3, 3)) >>> T tensor of shape (3, 3) with order F data[:, :] = [[0. 0. 0.] [0. 0. 0.] [0. 0. 0.]]
- pyttb.tenrand(shape: int | Iterable[int], order: Literal['F', 'C'] = 'F') tensor [source]
Create a tensor with entries drawn from a uniform distribution on [0, 1].
- Parameters:
shape – Shape of resulting tensor.
order – Memory layout for resulting tensor.
- Returns:
Constructed tensor.
Examples
>>> np.random.seed(1) >>> T = ttb.tenrand((3,)) >>> T tensor of shape (3,) with order F data[:] = [4.170...e-01 7.203...e-01 1.143...e-04]
- pyttb.tendiag(elements: OneDArray, shape: Shape | None = None, order: MemoryLayout = 'F') tensor [source]
Create a tensor with elements along super diagonal.
If provided shape is too small the tensor will be enlarged to accommodate.
- Parameters:
elements – Elements to set along the diagonal.
shape – Shape of resulting tensor.
order – Memory layout for resulting tensor.
- Returns:
Constructed tensor.
Examples
>>> shape = (3,) >>> values = np.ones(shape) >>> T1 = ttb.tendiag(values) >>> T2 = ttb.tendiag(values, (3, 3, 3)) >>> T1.isequal(T2) True
- pyttb.teneye(ndims: int, size: int, order: Literal['F', 'C'] = 'F') tensor [source]
Create identity tensor of specified shape.
T is an “identity tensor if T.ttsv(x, skip_dim=0) = x for all x such that norm(x) == 1.
An identity tensor only exists if order is even. This method is resource intensive for even moderate orders or sizes (>=6).
- Parameters:
ndims (Number of dimensions of tensor.)
size (Number of elements in any dimension of the tensor.)
order – Memory layout for resulting tensor.
Examples
>>> ttb.teneye(2, 3) tensor of shape (3, 3) with order F data[:, :] = [[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]] >>> x = np.ones((5,)) >>> x /= np.linalg.norm(x) >>> T = ttb.teneye(4, 5) >>> np.allclose(T.ttsv(x, 0), x) True
- Returns:
Identity tensor.