Create Test Problems (pyttb.create_problem)

pyttb.create_problem.create_problem(problem_params: CPProblem, missing_params: MissingData | None = None) tuple[ttb.ktensor, ttb.tensor | ttb.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[ttb.ktensor | ttb.ttensor, ttb.tensor | ttb.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

Generate sparse count data from CP solution If we assume each model parameter is the input to a Poisson process, then we can generate a sparse test problems. This requires that all the factor matrices and lambda be nonnegative. The default factor generator (‘randn’) won’t work since it produces both positive and negative values.

>>> shape = (20, 15, 10)
>>> num_factors = 4
>>> A = []
>>> for n in range(len(shape)):
...     A.append(np.random.rand(shape[n], num_factors))
...     for r in range(num_factors):
...         p = np.random.permutation(np.arange(shape[n]))
...         idx = p[1 : round(0.2 * shape[n])]
...         A[n][idx, r] *= 10
>>> S = ttb.ktensor(A)
>>> _ = S.normalize(sort=True)
>>> existing_params = ExistingCPSolution(S, noise=0.0, sparse_generation=500)
>>> solution, data = create_problem(existing_params)
class pyttb.create_problem.BaseProblem[source]

Bases: object

Parameters general to all solutions.

shape

Tensor shape for generated problem.

Type:

Shape

factor_generator

Method to generate factor matrices.

Type:

solution_generator

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: Shape, factor_generator: solution_generator = <function randn>, symmetric: list[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.

factor_generator

Method to generate factor matrices.

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.

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.

weight_generator

Method to generate weights for ktensor solution.

Type:

solution_generator

__new__(**kwargs)
__init__(shape: Shape, factor_generator: solution_generator = <function randn>, symmetric: list[tuple[int, int]] | None = None, num_factors: int = 2, noise: float = 0.1, weight_generator: solution_generator = <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.

factor_generator

Method to generate factor matrices.

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.

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.

core_generator

Method to generate weights for ttensor solution.

Type:

core_generator_t

__new__(**kwargs)
__init__(shape: Shape, factor_generator: solution_generator = <function randn>, symmetric: list[tuple[int, int]] | None = None, num_factors: list[int] | None = None, noise: float = 0.1, core_generator: core_generator_t = <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:

ttb.ktensor | ttb.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: ttb.ktensor | ttb.ttensor, noise: float = 0.1) None
class pyttb.create_problem.ExistingCPSolution[source]

Bases: ExistingSolution

Parameters for using an existing CP tensor solution.

solution

Pre-existing ktensor solution.

Type:

ttb.ktensor

noise

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

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: ttb.ktensor, noise: float = 0.1, sparse_generation: float | None = None) None
class pyttb.create_problem.ExistingTuckerSolution[source]

Bases: ExistingSolution

Parameters for using an existing Tucker tensor solution.

solution

Pre-existing ttensor solution.

Type:

ttb.ttensor

noise

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

__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:

ttb.sptensor | ttb.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: Shape) None | ttb.tensor | ttb.sptensor[source]

Generate a tensor pattern of missing data.

__eq__(other)

Return self==value.

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

Return repr(self).