API reference
CP and CMTF
Coupled Matrix Tensor Factorization
- tensorpack.cmtf.calcR2X(tFac, tIn=None, mIn=None)[source]
Calculate R2X. Optionally it can be calculated for only the tensor or matrix.
- tensorpack.cmtf.tensor_degFreedom(tFac) int[source]
Calculate the degrees of freedom within a tensor factorization.
- tensorpack.cmtf.reorient_factors(tFac)[source]
This function ensures that factors are negative on at most one direction.
- tensorpack.cmtf.sort_factors(tFac)[source]
Sort the components from the largest variance to the smallest.
- tensorpack.cmtf.initialize_cmtf(tensor: ndarray, matrix: ndarray, rank: int)[source]
Initialize factors used in parafac. :param tensor: :type tensor: ndarray :param rank: :type rank: int
- Returns:
factors – An initial cp tensor.
- Return type:
CPTensor
- tensorpack.cmtf.initialize_cp(tensor: ndarray, rank: int)[source]
Initialize factors used in parafac. :param tensor: :type tensor: ndarray :param rank: :type rank: int
- Returns:
factors – An initial cp tensor.
- Return type:
CPTensor
Coupled tensor factorization
Decomposition scans
Tucker
Tucker decomposition
- tensorpack.tucker.tucker_decomp(tensor, num_comps: int)[source]
Performs Tucker decomposition, greedily growing the rank of one mode at a time.
Starting from rank 1 along every mode, each step increases the rank of whichever single mode lowers the reconstruction error the most. A mode is no longer considered once its rank reaches
min(num_comps, size of that mode), so no mode is ever given a rank abovenum_compsand the search ends once every mode has reached its limit.- Parameters:
tensor (xarray or ndarray) – multi-dimensional data input
num_comps (int) – the maximum rank to test along each mode.
- Returns:
factors (list of lists) – containing tucker factorization object of each rank.
min_err (list) – list of minimum errors of tensor reconstruction for each rank combination.
min_err_rank (list of lists) – list of the corresponding rank combinations (one rank per mode) for the minimum error. The first entry is all ones; each later entry differs from the previous one by +1 in exactly one mode.
Imputation
- tensorpack.impute.create_missingness(tensor, drop)[source]
Creates missingness for a full tensor. Slgihtly faster than entry_drop()
- tensorpack.impute.entry_drop(tensor, drop, seed=None)[source]
Drops random values within a tensor. Finds a bare minimum cube before dropping values to ensure PCA remains viable.
- Parameters:
tensor (ndarray) – Takes a tensor of any shape. Preference for at least two values present per chord.
drop (int) – To set a percentage, multiply np.sum(np.isfinite(tensor)) by the percentage to find the relevant drop value, rounding to nearest int.
- Returns:
None
- Return type:
tensor is modified with missing values.
- tensorpack.impute.chord_drop(tensor, drop, seed=None)[source]
Removes chords along axis = 0 of a tensor.
- Parameters:
tensor (ndarray) – Takes a tensor of any shape.
drop (int) – To set a percentage, multiply tensor.shape[0] by the percentage to find the relevant drop value, rounding to nearest int.
- Returns:
None
- Return type:
tensor is modified with missing chords.
Linear algebra helpers
- tensorpack.linalg.calcR2X_TnB(tIn, tRecon)[source]
Calculate the top and bottom part of R2X formula separately
- tensorpack.linalg.lstsq_(A: ndarray, B: ndarray, nonneg=False) ndarray[source]
Solve min[Ax - b]_2 with least square, either non-negative or regular :param A (ndarray): :type A (ndarray): m x r matrix :param B (ndarray): :type B (ndarray): m x n matrix
- Returns:
X (ndarray)
- Return type:
r x n (nonegative) matrix that minimizes norm(M*(AX - B))
- tensorpack.linalg.mlstsq(A: ndarray, B: ndarray, uniqueInfo=None, nonneg=False) ndarray[source]
Solve min[Ax - b]_2 while checking missing values :param A (ndarray): :type A (ndarray): m x r matrix, no missing values :param B (ndarray): :type B (ndarray): m x n matrix, may contain missing values
- Returns:
X (ndarray)
- Return type:
r x n matrix that minimizes norm(M*(AX - B))
Plotting
This file makes all standard plots for tensor analysis. Requires a Decomposition object after running relevant values.
- tensorpack.plot.tfacr2x(ax, decomp: Decomposition)[source]
Plots R2X for tensor factorizations for all components up to decomp.max_rr.
- Parameters:
ax (axis object) – Plot information for a subplot of figure f.
decomp (Decomposition) – Takes a Decomposition object that has successfully run decomp.perform_tfac().
- tensorpack.plot.reduction(ax, decomp)[source]
Plots size reduction for tensor factorization versus PCA for all components up to decomp.max_rr.
- Parameters:
ax (axis object) – Plot information for a subplot of figure f.
decomp (Decomposition) – Takes a Decomposition object that has successfully run decomp.perform_tfac() and decomp.perform_PCA().
- tensorpack.plot.q2xchord(ax, decomp)[source]
Plots Q2X for tensor factorization when removing chords from a single mode for all components up to decomp.max_rr. Requires multiple runs to generate error bars.
- Parameters:
ax (axis object) – Plot information for a subplot of figure f.
decomp (Decomposition) – Takes a Decomposition object that has successfully run decomp.Q2X_chord().
- tensorpack.plot.q2xentry(ax, decomp, methodname='CP')[source]
Plots Q2X for tensor factorization versus PCA when removing entries for all components up to decomp.max_rr. Requires multiple runs to generate error bars.
- Parameters:
ax (axis object) – Plot information for a subplot of figure f.
decomp (Decomposition) – Takes a Decomposition object that has successfully run decomp.entry().
methodname (str) – Allows for proper tensor method when naming graph axes.
- tensorpack.plot.tucker_reduced_Dsize(tensor, ranks: list)[source]
Output the error (1 - r2x) for each size of the data at each component # for tucker decomposition. This forms the x-axis of the error vs. data size plot.
- Parameters:
tensor (xarray or numpy.ndarray) – the multi-dimensional input data
ranks (list) – the list of minimum-error Tucker fits for each component-combinations.
- Returns:
sizes – the size of reduced data by Tucker for each error.
- Return type:
- tensorpack.plot.tucker_reduction(ax, decomp: Decomposition, cp_decomp: Decomposition)[source]
Error versus data size for minimum error combination of rank from Tucker decomposition versus CP decomposition. The error for those combinations that are the same dimensions, ie., for a 3-D tensor, [1, 1, 1], [2, 2, 2], etc are shown by a different marker shape and color.
- Parameters:
ax (axis object) – Plot information for a subplot of figure f.
decomp (Decomposition) – Takes a Decomposition object to run perform_tucker().
cp_decomp (Decomposition) – Takes a Decomposition object to run perform_CP().
Example
from tensorpack.tucker import tucker_decomp from tensorpack.plot import tucker_reduced_Dsize, tucker_reduction from tensordata.zohar import data3D as zohar from tensorpack.decomposition import Decomposition b = Decomposition(zohar().tensor, method=tucker_decomp) c = Decomposition(zohar().tensor) import matplotlib.pyplot as plt f = plt.figure() ax = f.add_subplot() fig = tucker_reduction(ax, b, c) plt.savefig(“tucker_cp.svg”)
- tensorpack.plot.plot_weight_mode(ax, factor, labels=False, title='')[source]
Plots heatmaps for a single mode factors.
- Parameters:
ax (axis object) – Plot information for a subplot of figure f.
factor (numpy array) – Factorized mode
labels (list of string or False) – Labels for each of the elements
String (title") – Figure title
- tensorpack.xplots.xplot_R2X(data: DataArray, top_rank=12, ax=None, method=<function perform_CP>)[source]
Plot increasing rank R2X for CP