Skip to content
Merged
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ htmlcov
__pycache__
.vs*
TestResults
.DS_Store
12 changes: 6 additions & 6 deletions pyttb/cp_als.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ def cp_als(
>>> weights = np.array([1., 2.])
>>> fm0 = np.array([[1., 2.], [3., 4.]])
>>> fm1 = np.array([[5., 6.], [7., 8.]])
>>> K = ttb.ktensor.from_data(weights, [fm0, fm1])
>>> K = ttb.ktensor([fm0, fm1], weights)
>>> np.random.seed(1)
>>> M, Minit, output = ttb.cp_als(K.full(), 2) # doctest: +ELLIPSIS
CP_ALS:
Iter 0: f = ... f-delta = ...
Iter 1: f = ... f-delta = ...
Final f = ...
>>> print(M) # doctest: +ELLIPSIS
ktensor of shape 2 x 2
ktensor of shape (2, 2)
weights=[108.4715... 8.6114...]
factor_matrices[0] =
[[0.4187... 0.3989...]
Expand All @@ -85,7 +85,7 @@ def cp_als(
[[0.6188... 0.2581...]
[0.7854... 0.9661...]]
>>> print(Minit) # doctest: +ELLIPSIS
ktensor of shape 2 x 2
ktensor of shape (2, 2)
weights=[1. 1.]
factor_matrices[0] =
[[4.1702...e-01 7.2032...e-01]
Expand Down Expand Up @@ -147,12 +147,12 @@ def cp_als(
factor_matrices.append(
np.random.uniform(0, 1, (input_tensor.shape[n], rank))
)
init = ttb.ktensor.from_factor_matrices(factor_matrices)
init = ttb.ktensor(factor_matrices)
elif isinstance(init, str) and init.lower() == "nvecs":
factor_matrices = []
for n in range(N):
factor_matrices.append(input_tensor.nvecs(n, rank))
init = ttb.ktensor.from_factor_matrices(factor_matrices)
init = ttb.ktensor(factor_matrices)
else:
assert False, "The selected initialization method is not supported"

Expand Down Expand Up @@ -208,7 +208,7 @@ def cp_als(
U[n] = Unew
UtU[:, :, n] = U[n].T @ U[n]

M = ttb.ktensor.from_data(weights, U)
M = ttb.ktensor(U, weights)

# This is equivalent to innerprod(X,P).
iprod = np.sum(
Expand Down
11 changes: 4 additions & 7 deletions pyttb/cp_apr.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def cp_apr(
factor_matrices.append(
np.random.uniform(0, 1, (input_tensor.shape[n], rank))
)
init = ttb.ktensor.from_factor_matrices(factor_matrices)
init = ttb.ktensor(factor_matrices)

# Call solver based on the couce of algorithm parameter, passing all the other input parameters
if algorithm.lower() == "mu":
Expand Down Expand Up @@ -243,8 +243,7 @@ def tt_cp_apr_mu(
nTimes = np.zeros((maxiters,))

# Set up for iteration - initializing M and Phi.
# TODO replace with copy
M = ttb.ktensor.from_tensor_type(init)
M = init.copy()
M.normalize(normtype=1)
Phi = [] # np.zeros((N,))#cell(N,1)
for n in range(N):
Expand Down Expand Up @@ -453,8 +452,7 @@ def tt_cp_apr_pdnr(
init[n][tmpIdx, 0] = 1e-8

# Start with the initial guess, normalized using the vector L1 norm
# TODO replace with copy
M = ttb.ktensor.from_tensor_type(init)
M = init.copy()
M.normalize(normtype=1)

# Sparse tensor flag affects how Pi and Phi are computed.
Expand Down Expand Up @@ -824,8 +822,7 @@ def tt_cp_apr_pqnr(
init[n][tmpIdx, 0] = 1e-8

# Start with the initial guess, normalized using the vector L1 norm
# TODO replace with copy
M = ttb.ktensor.from_tensor_type(init)
M = init.copy()
M.normalize(normtype=1)

# Sparse tensor flag affects how Pi and Phi are computed.
Expand Down
6 changes: 3 additions & 3 deletions pyttb/import_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ def import_data(filename, index_base=1):
shape = import_shape(fp)
data = import_array(fp, np.prod(shape))
fp.close()
return ttb.tensor().from_data(data, shape)
return ttb.tensor.from_data(data, shape)

elif data_type == "sptensor":
shape = import_shape(fp)
nz = import_nnz(fp)
subs, vals = import_sparse_array(fp, len(shape), nz, index_base)
fp.close()
return ttb.sptensor().from_data(subs, vals, shape)
return ttb.sptensor.from_data(subs, vals, shape)

elif data_type == "matrix":
shape = import_shape(fp)
Expand All @@ -59,7 +59,7 @@ def import_data(filename, index_base=1):
fac = np.reshape(fac, np.array(fac_shape))
factor_matrices.append(fac)
fp.close()
return ttb.ktensor().from_data(weights, factor_matrices)
return ttb.ktensor(factor_matrices, weights, copy=False)


def import_type(fp):
Expand Down
Loading