Tensor

# Import statement

from hottbox.core import Tensor
class Tensor(array, custom_state=None, mode_names=None)[source]

This class describes multidimensional data.

All its methods implement all common operation on a tensor alone

Parameters
arraynp.ndarray

N-dimensional array

custom_statedict

Provides flexibility to create a Tensor object in folded, unfolded or rotated state. Use with caution. If provided, then should include only the following keys:

‘mode_order’ -> tuple of lists

‘normal_shape’ -> tuple

‘rtype’ -> str

mode_nameslist[str]

Description of the tensor modes. If nothing is specified then all modes of the created Tensor get generic names ‘mode-0’, ‘mode-1’ etc.

Attributes

_data

(np.ndarray) N-dimensional array.

_modes

(list[Mode]) Description of the tensor modes in form of a list where each element is object of Mode class. Meta information.

_state

(State) Reference to meta information about transformations applied to a tensor, such as unfolding, vectorising etc. Meta information.

Raises
TypeError

If parameter array is not of np.ndarray type.

StateError

If parameter custom_state is inconsistent with provided array or does not meet requirements for creating State of the tensor.

ModeError

If parameter mode_names is inconsistent with provided array and custom_state or does not meet requirements for creating list of Mode of the tensor.

Examples

  1. Creating a tensor with default parameters for meta data

>>> import numpy as np
>>> from hottbox.core import Tensor
>>> data = np.arange(24).reshape(2, 3, 4)
>>> tensor = Tensor(data)
>>> print(tensor)
    This tensor is of order 3 and consists of 24 elements.
    Sizes and names of its modes are (2, 3, 4) and ['mode-0', 'mode-1', 'mode-2'] respectively.
  1. Creating a tensor with custom mode names

>>> import numpy as np
>>> from hottbox.core import Tensor
>>> data = np.arange(24).reshape(2, 3, 4)
>>> tensor = Tensor(data, mode_names=["Year", "Month", "Day"])
>>> tensor.show_state()
    State(normal_shape=(2, 3, 4), rtype='Init', mode_order=([0], [1], [2]))
>>> print(tensor)
    This tensor is of order 3 and consists of 24 elements.
    Sizes and names of its modes are (2, 3, 4) and ['Year', 'Month', 'Day'] respectively.
  1. Creating a tensor in custom state

>>> import numpy as np
>>> from hottbox.core import Tensor
>>> data = np.arange(24).reshape(2, 3*4)
>>> tensor = Tensor(data, custom_state={"normal_shape": (2, 3, 4),
...                                     "mode_order": ([0], [1, 2]),
...                                     "rtype": "T"}
>>> data.shape
    (2, 12)
>>> tensor.shape
    (2, 12)
>>> tensor.show_state()
    State(normal_shape=(2, 3, 4), rtype='T', mode_order=([0], [1, 2]))
>>> print(tensor)
    This tensor is of order 2 and consists of 24 elements.
    Sizes and names of its modes are (2, 12) and ['mode-0', 'mode-1_mode-2'] respectively.

Methods

access(self, inds, mode)

Equivalent to multidimnesional slicing

copy(self)

Produces a copy of itself as a new object

copy_modes(self, tensor)

Copy modes meta from tensor representation

describe(self)

Expose some metrics

fold(self[, inplace])

Fold to the original shape

mode_n_product(self, matrix, mode[, …])

Mode-n product of a tensor with a matrix

reset_meta(self)

Set all meta information with respect to the current shape of data array

reset_mode_index(self[, mode])

Drop index for the specified mode number

reset_mode_name(self[, mode])

Set default name for the specified mode number

set_mode_index(self, mode_index)

Set index for specified mode

set_mode_names(self, mode_names)

Rename modes of a tensor

show_state(self)

Show the current state of the Tensor

unfold(self, mode[, rtype, inplace])

Perform mode-n unfolding to a matrix

vectorise(self[, rtype, inplace])

Perform vectorisation of a tensor

write_subtensor(self, inds, mode, overwrite)

Works in the same way as access but permits changing of the tensor data

access(self, inds, mode)[source]

Equivalent to multidimnesional slicing

Parameters
indsint

The index of the axis. e.g [:,:,0] will be at mode=2, inds=0

modeint

The axis to access

overwriteTensor

Overwrite slice with a subtensor

Returns
——-

Numpy array with the formulated subtensor

copy(self)[source]

Produces a copy of itself as a new object

Returns
new_objectTensor

New object of Tensor class with attributes having the same values, but no memory space is shared

Notes

Only the last transformation will be copied if tensor is not in normal state

copy_modes(self, tensor)[source]

Copy modes meta from tensor representation

Parameters
tensor{Tensor, TensorCPD, TensorTKD, TensorTT}
Returns
selfTensor
property data

N-dimensional array with data values

Returns
arraynp.ndarray
describe(self)[source]

Expose some metrics

fold(self, inplace=True)[source]

Fold to the original shape

Parameters
inplacebool

If True, then modifies itself. If False, then creates new object (copy)

Returns
tensorTensor

Tensor of original shape (self.ft_shape)

Raises
TensorStateError

If tensor is in normal state: self.in_normal_state is True.

Notes

Basically, this method can be called in order to undo both self.unfold and self.vectorise

property frob_norm

Frobenious norm of a tensor

Returns
float
property ft_shape

Shape of the a tensor in normal format (without being in unfolded or folded state)

Returns
shapetuple
property in_normal_state

Checks state of a tensor

Returns
bool

Notes

If returns True, then can call unfold and mode_n_product.

If returns False, then can call fold

mode_n_product(self, matrix, mode, new_name=None, inplace=True)[source]

Mode-n product of a tensor with a matrix

Parameters
matrix{Tensor, np.ndarray}

2D array

modeint

Specifies mode along which a tensor is multiplied by a matrix

inplacebool

If True, then modifies itself. If False, then creates new object (copy)

new_namestr

New name for the corresponding mode after computing this product. See Notes-3 for more info

Returns
tensorTensor

The result of the mode-n product of a tensor with a matrix along specified mode.

Raises
TensorStateError

If tensor is not in normal state: self.in_normal_state is False.

ModeError

If there is uncertainty with assigning new name for the tensor.modes[mode].

Notes

  1. Mode-n product operation changes the self._state._normal_shape attribute

  2. Remember that mode-n product changes the shape of the tensor. Presumably, it also changes the interpretation of that mode depending on the matrix

  3. If matrix is an object of Tensor class then you shouldn’t specify new_name, since it will be changed to matrix.mode_names[0]

  4. If matrix.mode_names[0] == "mode-0" then no changes to tensor.mode_names will be made

property mode_names

Description of the tensor modes in current state

Returns
nameslist[str]
property modes

Meta data for the modes of a tensor

Returns
list[Mode]
property order

Order of a tensor

Returns
int
reset_meta(self)[source]

Set all meta information with respect to the current shape of data array

Returns
selfTensor
reset_mode_index(self, mode=None)[source]

Drop index for the specified mode number

Parameters
modeint

Mode number which index to be dropped By default resets all indices

Returns
selfTensor
reset_mode_name(self, mode=None)[source]

Set default name for the specified mode number

Parameters
modeint

Mode number which name to be set to default value By default resets names of all modes

Returns
selfTensor
set_mode_index(self, mode_index)[source]

Set index for specified mode

Parameters
mode_indexdict

New indices for the tensor modes in form of a dictionary. Key defines the mode whose index to be changed. Value contains a list of new indices for this mode.

Returns
selfTensor
Raises
ModeError

If parameter mode_index is inconsistent with self.modes.

set_mode_names(self, mode_names)[source]

Rename modes of a tensor

Parameters
mode_namesdict

New names for the tensor modes in form of a dictionary The name of the mode defined by the Key of the dict will be renamed to the corresponding Value

Returns
selfTensor

Return self so that methods could be chained

Raises
ModeError

If parameter mode_names is inconsistent with self.modes.

property shape

Shape of a tensor in current state

Returns
tuple

Sizes of all dimensions of a tensor

show_state(self)[source]

Show the current state of the Tensor

property size

Number of elements in a tensor

Returns
int
unfold(self, mode, rtype='T', inplace=True)[source]

Perform mode-n unfolding to a matrix

Parameters
modeint

Specifies a mode along which a tensor will be unfolded

rtypestr

Defines an unfolding convention.

inplacebool

If True, then modifies itself. If False, then creates new object (copy)

Returns
tensorTensor

Unfolded version of a tensor

Raises
TensorStateError

If tensor is not in normal state: self.in_normal_state is False.

ValueError

If parameter rtype is not one of {‘T’, ‘K’}.

vectorise(self, rtype='T', inplace=True)[source]

Perform vectorisation of a tensor

Parameters
rtypestr

Defines a vectorisation convention.

inplacebool

If True, then modifies itself. If False, then creates new object (copy)

Returns
tensorTensor

Vectorised version of a tensor

Raises
TensorStateError

If tensor is not in normal state: self.in_normal_state is False.

ValueError

If parameter rtype is not one of {‘T’, ‘K’}.

write_subtensor(self, inds, mode, overwrite)[source]

Works in the same way as access but permits changing of the tensor data

Parameters
indsint

The index of the axis. e.g [:,:,0] will be at mode=2, inds=0

modeint

The axis to access

overwriteTensor

Overwrite slice with a subtensor