Skip to content

Commit 92a3d6e

Browse files
Changed cp_als() param 'tensor' to 'input_tensor' to avoid ambiguity (#118)
* Changed cp_als() param 'tensor' to 'input_tensor' to avoid ambiguity * Formatted changes with isort and black.
1 parent 4284885 commit 92a3d6e

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

pyttb/cp_als.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010

1111
def cp_als(
12-
tensor,
12+
input_tensor,
1313
rank,
1414
stoptol=1e-4,
1515
maxiters=1000,
@@ -114,8 +114,8 @@ def cp_als(
114114
"""
115115

116116
# Extract number of dimensions and norm of tensor
117-
N = tensor.ndims
118-
normX = tensor.norm()
117+
N = input_tensor.ndims
118+
normX = input_tensor.norm()
119119

120120
# Set up dimorder if not specified
121121
if not dimorder:
@@ -139,17 +139,19 @@ def cp_als(
139139
init.ncomponents == rank
140140
), "Initial guess does not have {} components".format(rank)
141141
for n in dimorder:
142-
if init.factor_matrices[n].shape != (tensor.shape[n], rank):
142+
if init.factor_matrices[n].shape != (input_tensor.shape[n], rank):
143143
assert False, "Mode {} of the initial guess is the wrong size".format(n)
144144
elif isinstance(init, str) and init.lower() == "random":
145145
factor_matrices = []
146146
for n in range(N):
147-
factor_matrices.append(np.random.uniform(0, 1, (tensor.shape[n], rank)))
147+
factor_matrices.append(
148+
np.random.uniform(0, 1, (input_tensor.shape[n], rank))
149+
)
148150
init = ttb.ktensor.from_factor_matrices(factor_matrices)
149151
elif isinstance(init, str) and init.lower() == "nvecs":
150152
factor_matrices = []
151153
for n in range(N):
152-
factor_matrices.append(tensor.nvecs(n, rank))
154+
factor_matrices.append(input_tensor.nvecs(n, rank))
153155
init = ttb.ktensor.from_factor_matrices(factor_matrices)
154156
else:
155157
assert False, "The selected initialization method is not supported"
@@ -159,7 +161,7 @@ def cp_als(
159161
fit = 0
160162

161163
# Store the last MTTKRP result to accelerate fitness computation
162-
U_mttkrp = np.zeros((tensor.shape[dimorder[-1]], rank))
164+
U_mttkrp = np.zeros((input_tensor.shape[dimorder[-1]], rank))
163165

164166
if printitn > 0:
165167
print("CP_ALS:")
@@ -176,7 +178,7 @@ def cp_als(
176178
# Iterate over all N modes of the tensor
177179
for n in dimorder:
178180
# Calculate Unew = X_(n) * khatrirao(all U except n, 'r').
179-
Unew = tensor.mttkrp(U, n)
181+
Unew = input_tensor.mttkrp(U, n)
180182

181183
# Save the last MTTKRP result for fitness check.
182184
if n == dimorder[-1]:
@@ -245,11 +247,11 @@ def cp_als(
245247

246248
if printitn > 0:
247249
if normX == 0:
248-
normresidual = M.norm() ** 2 - 2 * tensor.innerprod(M)
250+
normresidual = M.norm() ** 2 - 2 * input_tensor.innerprod(M)
249251
fit = normresidual
250252
else:
251253
normresidual = np.sqrt(
252-
np.abs(normX**2 + M.norm() ** 2 - 2 * tensor.innerprod(M))
254+
np.abs(normX**2 + M.norm() ** 2 - 2 * input_tensor.innerprod(M))
253255
)
254256
fit = 1 - (normresidual / normX) # fraction explained by model
255257
print(f" Final f = {fit:e}")

0 commit comments

Comments
 (0)