Skip to content

Commit 5336518

Browse files
committed
Merge branch 'main' into complex-test
2 parents cc6ba77 + 37e5bf8 commit 5336518

5 files changed

Lines changed: 43 additions & 13 deletions

File tree

‎.github/workflows/test.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
elif [[ "${{matrix.BACKEND}}" == "mxnet" ]]; then
3737
echo "Installing MXNet";
3838
sudo apt install gfortran;
39-
pip install mxnet<3 --pre -f https://dist.mxnet.io/python;
39+
pip install mxnet;
4040
elif [[ "${{matrix.BACKEND}}" == "jax" ]]; then
4141
echo "Installing JAX";
4242
pip install jax jaxlib;

‎tensorly/decomposition/_cmtf_als.py‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import warnings
12
import tensorly as tl
23
from ..tenalg import khatri_rao
34
from ..cp_tensor import CPTensor, validate_cp_rank, cp_to_tensor, cp_normalize
@@ -10,7 +11,13 @@
1011

1112

1213
def coupled_matrix_tensor_3d_factorization(
13-
tensor_3d, matrix, rank, init="svd", n_iter_max=100, normalize_factors=False
14+
tensor_3d,
15+
matrix,
16+
rank,
17+
init="svd",
18+
n_iter_max=100,
19+
tol=1e-6,
20+
normalize_factors=False,
1421
):
1522
"""
1623
Calculates a coupled matrix and tensor factorization of 3rd order tensor and matrix which are
@@ -43,6 +50,10 @@ def coupled_matrix_tensor_3d_factorization(
4350
matrix that is coupled with tensor in first mode: Y = [[A, V]]
4451
rank : int
4552
rank for CP decomposition of X
53+
tol : float, optional
54+
(Default: 1e-6) Relative reconstruction error tolerance. The
55+
algorithm is considered to have found the global minimum when the
56+
reconstruction error is less than `tol`.
4657
4758
Returns
4859
-------
@@ -72,6 +83,11 @@ def coupled_matrix_tensor_3d_factorization(
7283

7384
# initialize values
7485
tensor_cp = initialize_cp(tensor_3d, rank, init=init)
86+
# the coupled factor should be initialized with the concatenated dataset
87+
coupled_unfold = tl.concatenate((tl.unfold(tensor_3d, 0), matrix), axis=1)
88+
coupled_init = initialize_cp(coupled_unfold, rank, init=init)
89+
tensor_cp.factors[0] = coupled_init.factors[0]
90+
7591
rec_errors = []
7692

7793
# alternating least squares
@@ -81,7 +97,8 @@ def coupled_matrix_tensor_3d_factorization(
8197
V = tl.transpose(tl.lstsq(tensor_cp.factors[0], matrix)[0])
8298

8399
# Loop over modes of the tensor
84-
for ii in range(tl.ndim(tensor_3d)):
100+
# We want to solve for mode 0 last, since the coupled factor matrix is most influential and SVD gave us a good approximation
101+
for ii in reversed(range(tl.ndim(tensor_3d))):
85102
kr = khatri_rao(tensor_cp.factors, skip_matrix=ii)
86103
unfolded = tl.unfold(tensor_3d, ii)
87104

@@ -100,12 +117,16 @@ def coupled_matrix_tensor_3d_factorization(
100117
)
101118

102119
if iteration > 0 and (
103-
tl.abs(error_new - error_old) / error_old <= 1e-8 or error_new < 1e-5
120+
tl.abs(error_new - error_old) / error_old <= tol or error_new < tol
104121
):
105122
break
123+
106124
error_old = error_new
107125
rec_errors.append(error_new)
108126

127+
if iteration == n_iter_max - 1:
128+
warnings.warn("Reached maximum iteration number without convergance.")
129+
109130
matrix_pred = CPTensor((None, [tensor_cp.factors[0], V]))
110131

111132
if normalize_factors:

‎tensorly/decomposition/robust_decomposition.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def robust_pca(
131131

132132
# Convergence check
133133
if iteration > 1:
134-
if max(rec_X[-1], rec_D[-1]) <= tol:
134+
if rec_X[-1] <= tol and rec_D[-1] <= tol:
135135
if verbose:
136136
print("\nConverged in {} iterations".format(iteration))
137137
break

‎tensorly/decomposition/tests/test_cmtf_als.py‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import tensorly as tl
33

44
from .._cmtf_als import coupled_matrix_tensor_3d_factorization
5-
from ...cp_tensor import cp_to_tensor, CPTensor
5+
from ...cp_tensor import cp_to_tensor
66
from ...random import random_cp
77
from ...testing import assert_
88

@@ -37,11 +37,13 @@ def test_coupled_matrix_tensor_3d_factorization():
3737
tl.norm(tensor_true - tensor_pred) ** 2
3838
+ tl.norm(matrix_true - matrix_pred) ** 2
3939
)
40-
# TODO: These error checks do not always pass, possibly due to poor SVD initialization.
41-
# assert_(error < tol_norm_2,
42-
# 'norm 2 of reconstruction higher than tol')
40+
41+
assert_(error < tol_norm_2, "norm 2 of reconstruction higher than tol")
42+
4343
# Test the max abs difference between the reconstruction and the tensor
44-
# assert_(
45-
# tl.max(tl.abs(tensor_true - tensor_pred)) + tl.max(
46-
# tl.abs(matrix_true - matrix_pred)) < tol_max_abs,
47-
# 'abs norm of reconstruction error higher than tol')
44+
assert_(
45+
tl.max(tl.abs(tensor_true - tensor_pred))
46+
+ tl.max(tl.abs(matrix_true - matrix_pred))
47+
< tol_max_abs,
48+
"abs norm of reconstruction error higher than tol",
49+
)

‎tensorly/tests/test_tt_tensor.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
import pytest
1010

1111

12+
skip_mxnet = pytest.mark.skipif(
13+
tl.get_backend() == "mxnet",
14+
reason="MXNet currently does not support transpose for tensors of order > 6.",
15+
)
16+
17+
1218
def test_validate_tt_tensor():
1319
rng = tl.check_random_state(12345)
1420
true_shape = (3, 4, 5)
@@ -150,6 +156,7 @@ def test_validate_tt_rank():
150156
assert_(n_param >= n_param_tensor * coef)
151157

152158

159+
@skip_mxnet
153160
@pytest.mark.parametrize("n_pad", [1, 2])
154161
def test_pad_tt_rank(n_pad):
155162
"""Test for pad_tt_rank"""

0 commit comments

Comments
 (0)