Skip to content

Commit c571df8

Browse files
authored
Merge pull request tensorly#593 from Mithrillion/partial_tucker_to_tensor
Allow tucker_to_tensor() to work with partial_tucker() outputs with the modes parameter
2 parents 7009e92 + 36de3f1 commit c571df8

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

tensorly/tests/test_tucker_tensor.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,45 @@ def test_tucker_to_tensor():
9191
assert_array_equal(true_res, res)
9292

9393

94+
def test_tucker_to_tensor_with_partial_modes():
95+
"""Test for tucker_to_tensor with partial modes"""
96+
X = tl.tensor(
97+
np.array(
98+
[
99+
[[1.0, 13], [4, 16], [7, 19], [10, 22]],
100+
[[2, 14], [5, 17], [8, 20], [11, 23]],
101+
[[3, 15], [6, 18], [9, 21], [12, 24]],
102+
]
103+
)
104+
)
105+
ranks = [3, 4]
106+
U = [
107+
tl.tensor(np.arange(R * s, dtype=float).reshape((R, s)))
108+
for (R, s) in zip(ranks, tl.shape(X)[1:])
109+
]
110+
true_res = np.array(
111+
[
112+
[
113+
[120.0, 456.0, 792.0, 1128.0],
114+
[400.0, 1472.0, 2544.0, 3616.0],
115+
[680.0, 2488.0, 4296.0, 6104.0],
116+
],
117+
[
118+
[126.0, 486.0, 846.0, 1206.0],
119+
[422.0, 1582.0, 2742.0, 3902.0],
120+
[718.0, 2678.0, 4638.0, 6598.0],
121+
],
122+
[
123+
[132.0, 516.0, 900.0, 1284.0],
124+
[444.0, 1692.0, 2940.0, 4188.0],
125+
[756.0, 2868.0, 4980.0, 7092.0],
126+
],
127+
]
128+
)
129+
res = tucker_to_tensor((X, U), modes=[1, 2])
130+
assert_array_equal(true_res, res)
131+
132+
94133
def test_tucker_to_unfolded():
95134
"""Test for tucker_to_unfolded
96135

tensorly/tucker_tensor.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ def _validate_tucker_tensor(tucker_tensor):
4747
return tuple(shape), tuple(rank)
4848

4949

50-
def tucker_to_tensor(tucker_tensor, skip_factor=None, transpose_factors=False):
50+
def tucker_to_tensor(
51+
tucker_tensor, skip_factor=None, transpose_factors=False, modes=None
52+
):
5153
"""Converts the Tucker tensor into a full tensor
5254
5355
Parameters
@@ -59,14 +61,18 @@ def tucker_to_tensor(tucker_tensor, skip_factor=None, transpose_factors=False):
5961
Note that in any case, `modes`, if provided, should have a lengh of ``tensor.ndim``
6062
transpose_factors : bool, optional, default is False
6163
if True, the matrices or vectors in in the list are transposed
64+
modes : None or int list, optional, default is None
65+
list of the modes on which to perform the decomposition
6266
6367
Returns
6468
-------
6569
2D-array
6670
full tensor of shape ``(factors[0].shape[0], ..., factors[-1].shape[0])``
6771
"""
6872
core, factors = tucker_tensor
69-
return multi_mode_dot(core, factors, skip=skip_factor, transpose=transpose_factors)
73+
return multi_mode_dot(
74+
core, factors, skip=skip_factor, transpose=transpose_factors, modes=modes
75+
)
7076

7177

7278
def tucker_normalize(tucker_tensor):

0 commit comments

Comments
 (0)