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
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:
objectParameters general to all solutions.
- shape
Tensor shape for generated problem.
- Type:
- factor_generator
Method to generate factor matrices.
- Type:
- 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.
- noise
Amount of Gaussian noise to add to solution. If data is sparse noise is only added to nonzero entries.
- Type:
- __new__(**kwargs)
- class pyttb.create_problem.CPProblem[source]
Bases:
BaseProblemParameters specifying CP Solutions.
- shape
Tensor shape for generated problem.
- Type:
- factor_generator
Method to generate factor matrices.
- Type:
- 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.
- noise
Amount of Gaussian noise to add to solution. If data is sparse noise is only added to nonzero entries.
- Type:
- weight_generator
Method to generate weights for ktensor solution.
- Type:
- __new__(**kwargs)
- __init__(shape: int | ~collections.abc.Iterable[int], factor_generator: ~collections.abc.Callable[[tuple[int, ...]], ~numpy.ndarray] = <function randn>, symmetric: list[tuple[int, int]] | None = None, num_factors: int = 2, noise: float = 0.1, weight_generator: ~collections.abc.Callable[[tuple[int, ...]], ~numpy.ndarray] = <bound method RandomState.random of RandomState(MT19937)>, sparse_generation: float | None = None) None
- class pyttb.create_problem.TuckerProblem[source]
Bases:
BaseProblemParameters specifying Tucker Solutions.
- shape
Tensor shape for generated problem.
- Type:
- factor_generator
Method to generate factor matrices.
- Type:
- 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.
- noise
Amount of Gaussian noise to add to solution. If data is sparse noise is only added to nonzero entries.
- Type:
- core_generator
Method to generate weights for ttensor solution.
- Type:
collections.abc.Callable[[tuple[int, …]], pyttb.tensor.tensor | pyttb.sptensor.sptensor | numpy.ndarray]
- __new__(**kwargs)
- __init__(shape: int | ~collections.abc.Iterable[int], factor_generator: ~collections.abc.Callable[[tuple[int, ...]], ~numpy.ndarray] = <function randn>, symmetric: list[tuple[int, int]] | None = None, num_factors: list[int] | None = None, noise: float = 0.1, core_generator: ~collections.abc.Callable[[tuple[int, ...]], ~pyttb.tensor.tensor | ~pyttb.sptensor.sptensor | ~numpy.ndarray] = <function randn>) None
- class pyttb.create_problem.ExistingSolution[source]
Bases:
objectParameters for using an existing tensor solution.
- solution
Pre-existing tensor solution (ktensor or ttensor).
- noise
Amount of Gaussian noise to add to solution. If data is sparse noise is only added to nonzero entries.
- Type:
- __new__(**kwargs)
- class pyttb.create_problem.ExistingCPSolution[source]
Bases:
ExistingSolutionParameters for using an existing CP tensor solution.
- solution
Pre-existing ktensor solution.
- Type:
- noise
Amount of Gaussian noise to add to solution. If data is sparse noise is only added to nonzero entries.
- Type:
- 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)
- class pyttb.create_problem.ExistingTuckerSolution[source]
Bases:
ExistingSolutionParameters for using an existing Tucker tensor solution.
- solution
Pre-existing ttensor solution.
- Type:
- noise
Amount of Gaussian noise to add to solution. If data is sparse noise is only added to nonzero entries.
- Type:
- __new__(**kwargs)
- class pyttb.create_problem.MissingData[source]
Bases:
objectParameters to control missing data.
- missing_pattern
An explicit tensor representing missing data locations.
- Type:
- 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:
- 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).