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.
- 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.
- 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:
BaseProblem
Parameters specifying CP Solutions.
- 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.
- 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:
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.
- 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.
- 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:
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).
- 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:
ExistingSolution
Parameters for using an existing tucket 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:
ExistingSolution
Parameters for using an existing tucket 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:
object
Parameters 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).