Create Test Problems (pyttb.create_problem)

pyttb.create_problem.create_problem(problem_params: CPProblem, missing_params: MissingData | None = None) Tuple[ktensor, tensor | sptensor][source]
pyttb.create_problem.create_problem(problem_params: TuckerProblem, missing_params: MissingData | None = None) Tuple[ttensor, tensor]
pyttb.create_problem.create_problem(problem_params: ExistingSolution, missing_params: MissingData | None = None) Tuple[ktensor | ttensor, tensor | sptensor]

Generate a problem and solution.

Parameters:
  • problem_params – Parameters related to the problem to generate, or an existing solution.

  • missing_params – Parameters to control missing data in the generated data/solution.

Examples

Base example params

>>> shape = (5, 4, 3)

Generate a CP problem

>>> cp_specific_params = CPProblem(shape=shape, num_factors=3, noise=0.1)
>>> no_missing_data = MissingData()
>>> solution, data = create_problem(cp_specific_params, no_missing_data)
>>> diff = (solution.full() - data).norm() / solution.full().norm()
>>> bool(np.isclose(diff, 0.1))
True

Generate Tucker Problem

>>> tucker_specific_params = TuckerProblem(shape, num_factors=[3, 3, 2], noise=0.1)
>>> solution, data = create_problem(tucker_specific_params, no_missing_data)
>>> diff = (solution.full() - data).norm() / solution.full().norm()
>>> bool(np.isclose(diff, 0.1))
True

Use existing solution

>>> factor_matrices = [np.random.random((dim, 3)) for dim in shape]
>>> weights = np.random.random(3)
>>> existing_ktensor = ttb.ktensor(factor_matrices, weights)
>>> existing_params = ExistingSolution(existing_ktensor, noise=0.1)
>>> solution, data = create_problem(existing_params, no_missing_data)
>>> assert solution is existing_ktensor
class pyttb.create_problem.BaseProblem[source]

Bases: object

Parameters general to all solutions.

shape

Tensor shape for generated problem.

Type:

int | Iterable[int]

factor_generator

Method to generate factor matrices.

Type:

Callable[[Tuple[int, …]], numpy.ndarray]

symmetric

List of modes that should be symmetric. For instance, [(1,2), (3,4)] specifies that modes 1 and 2 have identical factor matrices, and modes 3 and 4 also have identical factor matrices.

Type:

list[Tuple[int, int]] | None

num_factors

Number of factors.

Type:

int | list[int] | None

noise

Amount of Gaussian noise to add to solution. If data is sparse noise is only added to nonzero entries.

Type:

float

__new__(**kwargs)
__init__(shape: int | ~typing.Iterable[int], factor_generator: ~typing.Callable[[~typing.Tuple[int, ...]], ~numpy.ndarray] = <function randn>, symmetric: list[~typing.Tuple[int, int]] | None = None, num_factors: int | list[int] | None = None, noise: float = 0.1) None
class pyttb.create_problem.CPProblem[source]

Bases: BaseProblem

Parameters specifying CP Solutions.

shape

Tensor shape for generated problem.

Type:

int | Iterable[int]

factor_generator

Method to generate factor matrices.

Type:

Callable[[Tuple[int, …]], numpy.ndarray]

symmetric

List of modes that should be symmetric. For instance, [(1,2), (3,4)] specifies that modes 1 and 2 have identical factor matrices, and modes 3 and 4 also have identical factor matrices.

Type:

list[Tuple[int, int]] | None

num_factors

Number of factors.

Type:

int

noise

Amount of Gaussian noise to add to solution. If data is sparse noise is only added to nonzero entries.

Type:

float

weight_generator

Method to generate weights for ktensor solution.

Type:

Callable[[Tuple[int, …]], numpy.ndarray]

__new__(**kwargs)
__init__(shape: int | ~typing.Iterable[int], factor_generator: ~typing.Callable[[~typing.Tuple[int, ...]], ~numpy.ndarray] = <function randn>, symmetric: list[~typing.Tuple[int, int]] | None = None, num_factors: int = 2, noise: float = 0.1, weight_generator: ~typing.Callable[[~typing.Tuple[int, ...]], ~numpy.ndarray] = <bound method RandomState.random of RandomState(MT19937)>, sparse_generation: float | None = None) None
class pyttb.create_problem.TuckerProblem[source]

Bases: BaseProblem

Parameters specifying Tucker Solutions.

shape

Tensor shape for generated problem.

Type:

int | Iterable[int]

factor_generator

Method to generate factor matrices.

Type:

Callable[[Tuple[int, …]], numpy.ndarray]

symmetric

List of modes that should be symmetric. For instance, [(1,2), (3,4)] specifies that modes 1 and 2 have identical factor matrices, and modes 3 and 4 also have identical factor matrices.

Type:

list[Tuple[int, int]] | None

num_factors

Number of factors.

Type:

list[int] | None

noise

Amount of Gaussian noise to add to solution. If data is sparse noise is only added to nonzero entries.

Type:

float

core_generator

Method to generate weights for ttensor solution.

Type:

Callable[[Tuple[int, …]], pyttb.tensor.tensor | pyttb.sptensor.sptensor | numpy.ndarray]

__new__(**kwargs)
__init__(shape: int | ~typing.Iterable[int], factor_generator: ~typing.Callable[[~typing.Tuple[int, ...]], ~numpy.ndarray] = <function randn>, symmetric: list[~typing.Tuple[int, int]] | None = None, num_factors: list[int] | None = None, noise: float = 0.1, core_generator: ~typing.Callable[[~typing.Tuple[int, ...]], ~pyttb.tensor.tensor | ~pyttb.sptensor.sptensor | ~numpy.ndarray] = <function randn>) None
class pyttb.create_problem.ExistingSolution[source]

Bases: object

Parameters for using an existing tensor solution.

solution

Pre-existing tensor solution (ktensor or ttensor).

Type:

pyttb.ktensor.ktensor | pyttb.ttensor.ttensor

noise

Amount of Gaussian noise to add to solution. If data is sparse noise is only added to nonzero entries.

Type:

float

__new__(**kwargs)
__init__(solution: ktensor | ttensor, noise: float = 0.1) None
class pyttb.create_problem.ExistingCPSolution[source]

Bases: ExistingSolution

Parameters for using an existing tucket tensor solution.

solution

Pre-existing ktensor solution.

Type:

pyttb.ktensor.ktensor

noise

Amount of Gaussian noise to add to solution. If data is sparse noise is only added to nonzero entries.

Type:

float

sparse_generation

Generate a sparse tensor that can be scaled so that the column factors and weights are stochastic. Provide a number of nonzeros to be inserted. A value in range [0,1) will be interpreted as a ratio.

Type:

float | None

__new__(**kwargs)
__init__(solution: ktensor, noise: float = 0.1, sparse_generation: float | None = None) None
class pyttb.create_problem.ExistingTuckerSolution[source]

Bases: ExistingSolution

Parameters for using an existing tucket tensor solution.

solution

Pre-existing ttensor solution.

Type:

pyttb.ttensor.ttensor

noise

Amount of Gaussian noise to add to solution. If data is sparse noise is only added to nonzero entries.

Type:

float

__new__(**kwargs)
__init__(solution: ttensor, noise: float = 0.1) None
class pyttb.create_problem.MissingData[source]

Bases: object

Parameters to control missing data.

missing_ratio

Proportion of missing data.

Type:

float

missing_pattern

An explicit tensor representing missing data locations.

Type:

pyttb.sptensor.sptensor | pyttb.tensor.tensor | None

sparse_model

Whether to generate sparse rather than dense missing data pattern. Only useful for large tensors that don’t easily fit in memory and when missing ratio > 0.8.

Type:

bool

has_missing() bool[source]

Check if any form of missing data is requested.

raise_symmetric()[source]

Raise for unsupported symmetry request.

get_pattern(shape: int | Iterable[int]) None | tensor | sptensor[source]

Generate a tensor pattern of missing data.

__eq__(other)

Return self==value.

__hash__ = None
__init__(missing_ratio: float = 0.0, missing_pattern: sptensor | tensor | None = None, sparse_model: bool = False) None
__repr__()

Return repr(self).