TensorLearn
A Python Package for Advanced Tensor Learning
Install / Use
/learn @rmsolgi/TensorLearnREADME
TensorLearn
This is a library for some tensor decomposition and tensor-based methods. This is a project under development and more methods will be added. The current methods are functional.
Installation
Use the package manager pip to install tensorlearn in Python.
pip install tensorlearn
methods
Tensor Train Decomposition
CANDECOMP/PARAFAC (CP) Decomposition
Tucker Decomposition
Tensor Completion using CP and ALS
Tensor Operations
Matrix Operations
<a name="cpcompletionals-id"></a>cp_completion_als
tensorlearn.cp_completion_als(tensor, samples, rank, iteration, cp_iteration=100)
This implementation for tensor completion is based on CP decomposition given a fixed rank.
Arguments
- tensor < array >: The given tensor to be decomposed.
- samples < array >: An array of
0s and1s where1s represent observed samples, and0s indicate missing entries. This array's size must match the dimensions of the tensor. - rank < int >: The rank for CP decomposition.
- iteration < int >: The iteration for the ALS algorithm.
- cp_iteration < int >: The iteration for initialization.
Return
-
weights < array >: the vector of normalization weights (lambda) in CP decomposition
-
factors < list of arrays >: factor matrices of CP decomposition
<a name="autoranktt-id"></a>auto_rank_tt
tensorlearn.auto_rank_tt(tensor, epsilon)
This implementation of tensor-train decomposition determines the ranks automatically based on a given error bound according to Oseledets (2011). Therefore the user does not need to specify the ranks. Instead the user specifies an upper error bound (epsilon) which bounds the error of the decomposition. For more information and details please see the page tensor-train decomposition.
Arguments
-
tensor < array >: The given tensor to be decomposed.
-
epsilon < float >: The error bound of decomposition in the range [0,1].
Return
- TT factors < list of arrays >: The list includes numpy arrays of factors (or TT cores) according to TT decomposition. Length of the list equals the dimension of the given tensor to be decomposed.
<a name="cpalsrandinit-id"></a>cp_als_rand_init
tensorlearn.cp_als_rand_init(tensor, rank, iteration, random_seed=None)
This is an implementation of CANDECOMP/PARAFAC (CP) decomposition using alternating least squares (ALS) algorithm with random initialization of factors.
Arguments
-
tensor < array >: the given tensor to be decomposed
-
rank < int >: number of ranks
-
iterations < int >: the number of iterations of the ALS algorithm
-
random_seed < int >: the seed of random number generator for random initialization of the factor matrices
Return
-
weights < array >: the vector of normalization weights (lambda) in CP decomposition
-
factors < list of arrays >: factor matrices of CP decomposition
<a name="tuckerhosvd-id"></a>tucker_hosvd
tensorlearn.tucker_hosvd(tensor, epsilon)
Arguments
- tensor < array >: the given tensor to be decomposed
- epsilon < float >: The error bound of decomposition in the range [0,1].
Return
- core_factor < array >: Core tensor factor of Tucker
- factor_matrices < list >: A list of factor matrices of Tucker
<a name="tttotensor-id"></a>tt_to_tensor
tensorlearn.tt_to_tensor(factors)
Returns the full tensor given the TT factors
Arguments
- factors < list of numpy arrays >: TT factors
Return
- full tensor < numpy array >
<a name="ttcr-id"></a>tt_compression_ratio
tensorlearn.tt_compression_ratio(factors)
Returns data compression ratio for tensor-train decompostion
Arguments
- factors < list of numpy arrays >: TT factors
Return
- Compression ratio < float >
<a name="cptotensor-id"></a>cp_to_tensor
Returns the full tensor given the CP factor matrices and weights
tensorlearn.cp_to_tensor(weights, factors)
Arguments
-
weights < array >: the vector of normalization weights (lambda) in CP decomposition
-
factors < list of arrays >: factor matrices of the CP decomposition
Return
- full tensor < array >
<a name="tuckertotensor-id"></a>tucker_to_tensor
Returns the full tensor given the Tucker core factor and factor matrices
tensorlearn.tucker_to_tensor(core_factor,factor_matrices)
Arguments
-
core_factor < array >: Core factor of Tucker decomposition
-
factor_matrices < list of arrays >: factor matrices of Tucker decomposition
Return
- full tensor < array >
<a name="cpcr-id"></a>cp_compression_ratio
Returns data compression ratio for CP- decompostion
tensorlearn.cp_compression_ratio(weights, factors)
Arguments
-
weights < array >: the vector of normalization weights (lambda) in CP decomposition
-
factors < list of arrays >: factor matrices of the CP decomposition
Return
- Compression ratio < float >
<a name="tuckercr-id"></a>Tucker_compression_ratio
Returns data compression ratio for Tucker decomposition.
tensorlearn.tucker_compression_ratio(core_factor,factor_matrices)
Arguments
-
core_factor < array >: Core factor of Tucker decomposition
-
factor_matrices < list of arrays >: factor matrices of Tucker decomposition
Return
- Compression ratio < float >
<a name="tensorresize-id"></a>tensor_resize
tensorlearn.tensor_resize(tensor, new_shape)
This method reshapes the given tensor to a new shape. The new size must be bigger than or equal to the original shape. If the new shape results in a tensor of greater size (number of elements) the tensor fills with zeros. This works similar to numpy.ndarray.resize()
Arguments
-
tensor < array >: the given tensor
-
new_shape < tuple >: new shape
Return
- tensor < array >: tensor with new given shape
<a name="unfold-id"></a>unfold
tensorlearn.unfold(tensor, n)
Unfold the tensor with respect to dimension n.
Arguments
-
tensor < array >: tensor to be unfolded
-
n < int >: dimension based on which the tensor is unfolded
Return
- matrix < array >: unfolded tensor with respect to dimension n
<a name="tfronorm-id"></a>tensor_frobenius_norm
tensorlearn.tensor_frobenius_norm(tensor)
Calculates the frobenius norm of the given tensor.
Arguments
- tensor < array >: the given tensor
Return
- frobenius norm < float >
<a name="etsvd-id"></a>error_truncated_svd
tensorlearn.error_truncated_svd(x, error)
This method conducts a compact svd and return sigma (error)-truncated SVD of a given matrix. This is an implementation using numpy.linalg.svd with full_matrices=False. This method is used in TT-SVD algorithm in auto_rank_tt.
Arguments
-
x < 2D array >: the given matrix to be decomposed
-
error < float >: the given error in the range [0,1]
Return
- r, u, s, vh < int, numpy array, numpy array, numpy array >
<a name="colwisekron-id"></a>column_wise_kronecker
tensorlearn.column_wise_kronecker(a, b)
Returns the column wise Kronecker product (Sometimes known as Khatri Rao) of two given matrices.
Arguments
- a,b < 2D array >: the given matrices
Return
- column wise Kronecker product < array >
Related Skills
YC-Killer
2.7kA library of enterprise-grade AI agents designed to democratize artificial intelligence and provide free, open-source alternatives to overvalued Y Combinator startups. If you are excited about democratizing AI access & AI agents, please star ⭐️ this repository and use the link in the readme to join our open source AI research team.
best-practices-researcher
The most comprehensive Claude Code skills registry | Web Search: https://skills-registry-web.vercel.app
groundhog
398Groundhog's primary purpose is to teach people how Cursor and all these other coding agents work under the hood. If you understand how these coding assistants work from first principles, then you can drive these tools harder (or perhaps make your own!).
isf-agent
a repo for an agent that helps researchers apply for isf funding
