def test_parafac_linesearch(): """ Test that we more rapidly converge to a solution with line search. """ rng = tl.check_random_state(1234) eps = 10e-2 tensor = T.tensor(rng.random_sample((5, 5, 5))) kt = parafac(tensor, rank=5, init='random', random_state=1234, n_iter_max=10, tol=10e-9) rec = tl.cp_to_tensor(kt) kt_ls = parafac(tensor, rank=5, init='random', random_state=1234, n_iter_max=10, tol=10e-9, linesearch=True) rec_ls = tl.cp_to_tensor(kt_ls) rec_error = T.norm(tensor - rec) / T.norm(tensor) rec_error_ls = T.norm(tensor - rec_ls) / T.norm(tensor) assert_( rec_error_ls - rec_error < eps, f'Relative reconstruction error with line-search={rec_error_ls} VS {rec_error} without.' 'CP with line-search seems to have converged more slowly.')
def test_gcp_continuous_loss_functions(): cont_losses = ['normal', 'gaussian'] opts = ['lbfgsb', 'sgd'] rng = tl.check_random_state(1234) shp = (4, 5, 6) rank = 4 #tensor = generate_test_tensor('normal', shp) size = 1 for i in shp: size *= i data1 = rng.random(size) tensor = tl.tensor(data1.reshape(shp, order='F'), dtype=tl.float64) ## CHECK CONTINUOUS DATA-CENTRIC LOSS print("\n***************************************************") print("\t Testing continuous data") for loss in cont_losses: print("***************************************************\n") print("Loss function type: {}".format(loss)) for opt in opts: mTen = gcp(tensor, rank, type=loss, opt=opt, maxiters=1000, epciters=100) assert (mTen is not None), "gcp({}}) returned null".format(opt) mTen = tl.cp_to_tensor(mTen) assert (tensor.size == mTen.size), "Unequal number of tensor elements. \ Tensor: {} CPTensor: {}".format(tensor.size, tl.cp_to_tensor(mTen).size) score = 1 - (tl.norm(tensor - mTen) / tl.norm(tensor)) print("Score: {0:0.4f}\n".format(score))
def test_symmetric_parafac_power_iteration(monkeypatch): """Test for symmetric Parafac optimized with robust tensor power iterations""" rng = tl.check_random_state(1234) tol_norm_2 = 10e-1 tol_max_abs = 10e-1 size = 5 rank = 4 true_factor = tl.tensor(rng.random_sample((size, rank))) true_weights = tl.ones(rank) tensor = tl.cp_to_tensor((true_weights, [true_factor] * 3)) weights, factor = symmetric_parafac_power_iteration(tensor, rank=10, n_repeat=10, n_iteration=10) rec = tl.cp_to_tensor((weights, [factor] * 3)) error = tl.norm(rec - tensor, 2) error /= tl.norm(tensor, 2) assert_(error < tol_norm_2, 'norm 2 of reconstruction higher than tol') # Test the max abs difference between the reconstruction and the tensor assert_( tl.max(tl.abs(rec - tensor)) < tol_max_abs, 'abs norm of reconstruction error higher than tol') assert_class_wrapper_correctly_passes_arguments( monkeypatch, symmetric_parafac_power_iteration, SymmetricCP, ignore_args={}, rank=3)
def create_cp(dims, rank, sparsity=None, method='rand', weights=False, return_tensor=False, noise=None, sparse_noise=True): # TODO: investigate performance impact of setting backend here tl.set_backend('pytorch') if method == 'rand': randfunc = torch.rand elif method == 'randn': randfunc = torch.randn else: raise NotImplementedError(f'Unknown random method: {method}') n_dims = len(dims) factors = [randfunc((dim, rank)) for dim in dims] if sparsity is not None: if isinstance(sparsity, float): sparsity = [sparsity for _ in range(n_dims)] elif not isinstance(sparsity, list) and not isinstance( sparsity, tuple): raise ValueError( 'Sparsity parameter should either be a float or tuple/list.') # Sparsify factors for dim in range(n_dims): n_el = dims[dim] * rank to_del = round(sparsity[dim] * n_el) if to_del == 0: continue idxs = torch.tensor(random.sample(range(n_el), to_del)) factors[dim].view(-1)[idxs] = 0 # torch.randperm(n_el, device=device)[:n_select] ten = None # Add noise if noise is not None: ten = tl.cp_to_tensor((torch.ones(rank), factors)) if (sparsity is None or not sparse_noise): nten = torch.randn(ten.size()) ten += noise * (norm(ten) / norm(nten)) * nten else: flat = ten.view(-1) nzs = torch.nonzero(flat, as_tuple=True)[0] nvec = torch.randn(nzs.size(0)) flat[nzs] += noise * (norm(ten) / norm(nvec)) * nvec if return_tensor: if ten is None: return tl.cp_to_tensor((torch.ones(rank), factors)) return ten if weights: return torch.ones(rank), factors return factors
def test_cprand_random(): """ For a noised I*J*K rank r random tensor, with random initialized factor matrices plot err_fast and exact err for simple / complicated case """ I = 50 J = 50 K = 50 r = 10 # rank n_samples = int(10 * r * np.log(r) + 1) # nb of randomized samples fac_true, noise = init_factors(I, J, K, r, True) t = tl.cp_to_tensor((None, fac_true)) + noise print(tl.norm(t)) factors = random_init_fac(t, r) weights2, factors2, it2, error2, error_es2 = CPRAND( t, r, n_samples, factors=copy.deepcopy(factors), exact_err=True, it_max=500, err_it_max=400) plt.figure(0) plt.plot(range(len(error2)), error2, 'b-', label="exact") plt.plot(range(len(error_es2)), error_es2, 'r--', label="err fast") plt.xlabel('it') plt.yscale('log') plt.title('cprand for complicated case') plt.ylabel('terminaison criterion') plt.legend(loc='best') plt.figure(1) fac_true, noise = init_factors(I, J, K, r, False) t = tl.cp_to_tensor((None, fac_true)) + noise print(tl.norm(t)) factors = random_init_fac(t, r) weights2, factors2, it2, error2, error_es2 = CPRAND( t, r, n_samples, factors=copy.deepcopy(factors), exact_err=True, it_max=500, err_it_max=400) plt.plot(range(len(error2)), error2, 'b-', label="exact") plt.plot(range(len(error_es2)), error_es2, 'r--', label="err fast") plt.xlabel('it') plt.yscale('log') plt.title('cprand for simple case') plt.ylabel('terminaison criterion') plt.legend(loc='best')
def test_herals_score(): """ Test of score for a 3 order simple tensor """ # create a kruskal tensor # factor matrices I = 3 J = 3 K = 3 r = 3 factors, noise = init_factors(I, J, K, r) t_krus = tl.cp_to_tensor((None, factors)) factors_init = random_init_fac(t_krus, r) weights, factors1, it, error1, l, pct = her_Als(t_krus, r, factors=factors_init, it_max=500, list_factors=True) print(score(factors, factors1)) weights1, factors1n = tl.cp_normalize((weights, factors1)) weight, factorsn = tl.cp_normalize((None, factors)) for i in factors1n: print(i) for i in factorsn: print(i) print(it) print(error1[len(error1) - 1])
def test_gcp_1(): """ Test for generalized CP""" ## Test 1 - shapes and dimensions # Create tensor with random elements rng = tl.check_random_state(1234) d = 3 n = 4 shape = (40, 50, 60) tensor = tl.tensor(rng.random(shape), dtype=tl.float32) # tensor = (np.arange(n**d, dtype=float).reshape((n,)*d)) # tensor = tl.tensor(tensor) # a 4 x 4 x 4 tensor tensor_shape = tensor.shape # Find gcp decomposition of the tensor rank = 20 mTen = gcp(tensor, rank, type='normal', state=rng, maxiters=1e5) print(mTen) assert(mTen is not None), "gcp returned null" assert(len(mTen[1]) == d), "Number of factors should be 3, currently has " + str(len(mTen[1])) # Check each factor matrices has the correct number of columns for k in range(d): rows, columns = tl.shape(mTen[1][k]) assert(columns == rank), "Factor matrix {} needs {} columns, but only has {}".format(i+1, rank, columns) # Check CPTensor has same number of elements as tensor mTen = tl.cp_to_tensor(mTen) assert(tensor.size == mTen.size), "Unequal number of tensor elements. Tensor: {} CPTensor: {}".format(tensor.size,tl.cp_to_tensor(mTen).size) score = 1 - (tl.norm(tensor - mTen)/tl.norm(tensor)) print("Score: {}".format(score))
def reconstructed_variance(tFac, tIn=None): """ This function calculates the amount of variance captured (R2X) by the tensor method. """ tMask = np.isfinite(tIn) vTop = np.sum( np.square(tl.cp_to_tensor(tFac) * tMask - np.nan_to_num(tIn))) vBottom = np.sum(np.square(np.nan_to_num(tIn))) return 1.0 - vTop / vBottom
def test_parafac_power_iteration(): """Test for symmetric Parafac optimized with robust tensor power iterations""" rng = check_random_state(1234) tol_norm_2 = 10e-1 tol_max_abs = 10e-1 shape = (5, 3, 4) rank = 4 tensor = random_cp(shape, rank=rank, full=True, random_state=rng) ktensor = parafac_power_iteration(tensor, rank=10, n_repeat=10, n_iteration=10) rec = tl.cp_to_tensor(ktensor) error = tl.norm(rec - tensor, 2) / tl.norm(tensor, 2) assert_( error < tol_norm_2, f'Norm 2 of reconstruction error={error} higher than tol={tol_norm_2}.' ) error = tl.max(tl.abs(rec - tensor)) assert_( error < tol_max_abs, f'Absolute norm of reconstruction error={error} higher than tol={tol_max_abs}.' )
def test_als(): """ Test of als for a kruskal tensor, start with true factors """ # create a kruskal tensor # factor matrices A = np.arange(9).reshape(3, 3) B = np.arange(6).reshape(2, 3) + 9 C = np.arange(6).reshape(2, 3) + 15 factors = [] factors += [A] factors += [B] factors += [C] t_krus = tl.cp_to_tensor((None, factors)) #weights_cp,factors_cp=tl.cp_normalize((None,factors)) weights, factors, it, error1, l = als(t_krus, 3, factors=factors, list_factors=True) for i in factors: print(i) print(it)
def test_gcp_sgd(): """ Test sgd optimization functionality """ # Create a random tensor np.random.seed(1234) d = 3 shp = (100, 20, 30) size = 1 for i in shp: size *= i data = np.random.rand(size) tensor = tl.tensor(data.reshape(shp, order='F'), dtype=tl.float64) rank = 10 mTen = gcp(tensor, rank, type='normal', opt='sgd', maxiters=100, epciters=10) assert (mTen is not None), "gcp returned null" assert (len(mTen[1]) == d), "Number of factors should be 3, currently has " + str(len(mTen[1])) # Check each factor matrices has the correct number of columns for k in range(d): rows, columns = tl.shape(mTen[1][k]) assert (columns == rank), "Factor matrix {} needs {} columns, but only has {}".format(i + 1, rank, columns) # Check CPTensor has same number of elements as tensor mTen = tl.cp_to_tensor(mTen) assert (tensor.size == mTen.size), "Unequal number of tensor elements. Tensor: {} CPTensor: {}".format(tensor.size, tl.cp_to_tensor( mTen).size) score = 1 - (tl.norm(tensor - mTen) / tl.norm(tensor)) print("Score: {0:0.4f}".format(score))
def test_err_rand_fast(): """ Test of err_rand_fast for a kruskal tensor plot the terminaison criterion with exact error of CPRAND """ A = np.arange(9).reshape(3, 3) B = np.arange(6).reshape(2, 3) + 9 C = np.arange(6).reshape(2, 3) + 15 factors = [] factors += [A] factors += [B] factors += [C] t_krus = tl.cp_to_tensor((None, factors)) rank = 3 n_samples = int(10 * rank * np.log(rank) + 1) weights, factors, it, err_ex, error, l = CPRAND(t_krus, rank, n_samples, list_factors=True) plt.plot(range(len(err_ex)), err_ex, 'b-', label="exact") plt.plot(range(len(error)), error, 'r--', label="err fast") plt.xlabel('it') plt.yscale('log') plt.title('cprand for t_krus') plt.ylabel('terminaison criterion') plt.legend(loc='best')
def test_randomized_svd(): """ Imports the tensor of union of all genes among 6 cell lines and performs parafac. """ tensor, _, _ = form_tensor() tInit = initialize_cp(tensor, 7) tfac = parafac(tensor, rank=7, init=tInit, linesearch=True, n_iter_max=2) r2x = 1 - tl.norm( (tl.cp_to_tensor(tfac) - tensor))**2 / (tl.norm(tensor))**2 assert r2x > 0
def init_factors(I, J, K, r, noise_level=0.1, scale=False, nn=False, snr=False): """ Initialize a three way tensor's factor matrices Parameters ---------- I : int dimension of mode 1. J : int dimension of mode 2. K : int dimension of mode 3. r : int rank. noise_level : float, optional noise level. The default is 0.001. scale : boolean, optional whether to scale the singular value or not. The default is False. snr : boolean, optional whether return SNR or not Returns ------- factors : list of matrices factors noise : tensor noise tensor. """ A = np.random.normal(0, 1, size=(I, r)) B = np.random.normal(0, 1, size=(J, r)) C = np.random.normal(0, 1, size=(K, r)) if nn == True: A = np.abs(A) B = np.abs(B) C = np.abs(C) if (scale == True): A = sv_scale_to_100(A) B = sv_scale_to_100(B) C = sv_scale_to_100(C) factors = [A, B, C] x = tl.cp_to_tensor((None, factors)) N = np.random.normal(0, 1, size=(I, J, K)) noise = noise_level * tl.norm(x) / tl.norm(N) * N SNR = 10 * np.log10(tl.norm(x)**2 / tl.norm(noise)**2) if snr == True: return (factors, noise, SNR) else: return (factors, noise)
def sort_factors(tFac): """ Sort the components from the largest variance to the smallest. """ rr = tFac.rank tensor = deepcopy(tFac) vars = np.array([ totalVar(delete_component(tFac, np.delete(np.arange(rr), i))) for i in np.arange(rr) ]) order = np.flip(np.argsort(vars)) tensor.weights = tensor.weights[order] tensor.factors = [fac[:, order] for fac in tensor.factors] np.testing.assert_allclose(tl.cp_to_tensor(tFac), tl.cp_to_tensor(tensor)) if hasattr(tFac, 'mFactor'): tensor.mFactor = tensor.mFactor[:, order] np.testing.assert_allclose(buildGlycan(tFac), buildGlycan(tensor)) return tensor
def factorTensor(tensor, numComps): """ Takes Tensor, and mask and returns tensor factorized form. """ tfac = non_negative_parafac(np.nan_to_num(tensor), rank=numComps, mask=np.isfinite(tensor), n_iter_max=5000, tol=1e-9) tensor = tensor.copy() tensor[np.isnan(tensor)] = tl.cp_to_tensor(tfac)[np.isnan(tensor)] return non_negative_parafac_hals(tensor, numComps, n_iter_max=5000)
def test_sort(): """ Test that sorting does not affect anything. """ tOrig, mOrig = createCube() tFac = random_cp(tOrig.shape, 3) tFac.mFactor = np.random.randn(mOrig.shape[1], 3) R2X = calcR2X(tFac, tOrig, mOrig) tRec = tl.cp_to_tensor(tFac) mRec = buildGlycan(tFac) tFac = sort_factors(tFac) sR2X = calcR2X(tFac, tOrig, mOrig) stRec = tl.cp_to_tensor(tFac) smRec = buildGlycan(tFac) np.testing.assert_allclose(R2X, sR2X) np.testing.assert_allclose(tRec, stRec) np.testing.assert_allclose(mRec, smRec)
def test_gcp_loss_functions(): """ Test for generalized CP loss functions""" losses = ['normal', 'gaussian', 'binary', 'bernoulli-odds', 'bernoulli-logit', \ 'count', 'poisson', 'poisson-log', 'rayleigh'] opts = ['lbfgsb', 'sgd'] np.random.seed(1234) shp =(4, 5, 6) size = 1 for i in shp: size *= i data = np.random.rand(size) tensor = tl.tensor(data.reshape(shp, order='F'), dtype=tl.float64) rank = 6 # Check loss functions - lbfgs optimization print("\t\t\t LBFGSB optimization") for loss in losses: print("***************************************************\n") print("Loss function type: {}".format(loss)) mTen = gcp(tensor, rank, type=loss, opt='lbfgsb', maxiters=1000, epciters=100) assert (mTen is not None), "gcp returned null" # Check CPTensor has same number of elements as tensor mTen = tl.cp_to_tensor(mTen) assert (tensor.size == mTen.size), "Unequal number of tensor elements. \ Tensor: {} CPTensor: {}".format(tensor.size, tl.cp_to_tensor(mTen).size) score = 1 - (tl.norm(tensor - mTen) / tl.norm(tensor)) print("Score: {0:0.4f}".format(score)) # Check loss functions - sgd optimization print("***************************************************") print("\t\t\t SGD optimization") print("***************************************************\n") for loss in losses: mTen = gcp(tensor, rank, type=loss, opt='sgd', maxiters=100, epciters=10) assert (mTen is not None), "gcp returned null" # Check CPTensor has same number of elements as tensor mTen = tl.cp_to_tensor(mTen) assert (tensor.size == mTen.size), "Unequal number of tensor elements. \ Tensor: {} CPTensor: {}".format(tensor.size, tl.cp_to_tensor(mTen).size) score = 1 - (tl.norm(tensor - mTen) / tl.norm(tensor)) print("Score: {0:0.4f}".format(score))
def test_cp_norm(): """Test for cp_norm """ shape = (8, 5, 6, 4) rank = 25 cp_tensor = random_cp(shape=shape, rank=rank, full=False, normalise_factors=True) tol = 10e-5 rec = tl.cp_to_tensor(cp_tensor) true_res = tl.norm(rec, 2) res = cp_norm(cp_tensor) assert_(tl.abs(true_res - res) <= tol)
def DRMF(Y, epsilon, R, maxiters, init): S = np.zeros_like(Y) for it in range(maxiters): X = Y - S _, kruskal_tensor = parafac(X, rank=R, n_iter_max=15, init=init) S = Y - tl.cp_to_tensor((None, kruskal_tensor)) p = topk(S, epsilon, S.shape) S = np.zeros_like(X) for elem in p: s = elem[1] S[s[0], s[1], s[2]] = elem[0] X = Y - S return [X, S, kruskal_tensor[0], kruskal_tensor[1], kruskal_tensor[2]]
def test_masked_parafac(linesearch): """Test for the masked CANDECOMP-PARAFAC decomposition. This checks that a mask of 1's is identical to the unmasked case. """ tensor = random_cp((4, 4, 4), rank=1, full=True) mask = np.ones((4, 4, 4)) mask[1, :, 3] = 0 mask[:, 2, 3] = 0 mask = tl.tensor(mask) tensor_mask = tensor*mask - 10000.0*(1 - mask) fac = parafac(tensor_mask, svd_mask_repeats=0, mask=mask, n_iter_max=0, rank=1, init="svd") fac_resvd = parafac(tensor_mask, svd_mask_repeats=10, mask=mask, n_iter_max=0, rank=1, init="svd") err = tl.norm(tl.cp_to_tensor(fac) - tensor, 2) err_resvd = tl.norm(tl.cp_to_tensor(fac_resvd) - tensor, 2) assert_(err_resvd < err, 'restarting SVD did not help') # Check that we get roughly the same answer with the full tensor and masking mask_fact = parafac(tensor, rank=1, mask=mask, init='random', random_state=1234, linesearch=linesearch) fact = parafac(tensor, rank=1) diff = cp_to_tensor(mask_fact) - cp_to_tensor(fact) assert_(T.norm(diff) < 0.001, 'norm 2 of reconstruction higher than 0.001')
def factorize(num_comp=10): """ Using Parafac as a tensor factorization. """ tensor, genes, cellLines = form_tensor() # perform parafac and CP decomposition r2x = np.zeros(num_comp) tfacs = [] for i in range(num_comp): # parafac tInit = initialize_cp(tensor, rank=i + 1) fac_p = parafac(tensor, rank=i + 1, init=tInit, linesearch=True) r2x[i] = 1 - ((tl.norm(tl.cp_to_tensor(fac_p) - tensor) ** 2) / tl.norm(tensor) ** 2) tfacs.append(fac_p) return tfacs[np.where(r2x > 0.5)[0][0]], r2x, genes, cellLines
def cluster_mema(): """ Plot the clustermap for the ECM data, separately when it is decomposed. """ tensor, ligand, ecm, measurements = import_LINCS_MEMA() fac = parafac(tensor, 6, n_iter_max=2000, linesearch=True, tol=1e-12) fac = cp_normalize(fac) fac = cp_flip_sign(fac, mode=2) facZero = pd.DataFrame( fac.factors[0], columns=[f"{i}" for i in np.arange(1, fac.rank + 1)], index=ligand) decreased_ligand = facZero.loc[((-0.1 >= facZero).any(1) | (facZero >= 0.1).any(1))] facOne = pd.DataFrame(fac.factors[1], columns=[f"{i}" for i in np.arange(1, fac.rank + 1)], index=ecm) decreased_ecm = facOne.loc[((-0.1 >= facOne).any(1) | (facOne >= 0.1).any(1))] facTwo = pd.DataFrame(fac.factors[2], columns=[f"{i}" for i in np.arange(1, fac.rank + 1)], index=measurements) decreased_measurement = facTwo.loc[((-0.1 >= facTwo).any(1) | (facTwo >= 0.1).any(1))] print( "R2X: ", 1.0 - np.linalg.norm(tl.cp_to_tensor(fac) - tensor)**2.0 / np.linalg.norm(tensor)**2.0) g = sns.clustermap(decreased_ligand, cmap="PRGn", method="centroid", center=0, figsize=(8, 12), col_cluster=False) plt.savefig("output/clustergram_ligand.svg") g = sns.clustermap(decreased_ecm, cmap="PRGn", method="centroid", center=0, figsize=(8, 16), col_cluster=False) plt.savefig("output/clustergram_ECM.svg") g = sns.clustermap(decreased_measurement, cmap="PRGn", method="centroid", center=0, figsize=(8, 18), col_cluster=False) plt.savefig("output/clustergram_measurements.svg")
def test_err_rand(): """ Test of err_rand for a kruskal tensor """ # create a kruskal tensor # factor matrices A = np.arange(9).reshape(3, 3) B = np.arange(6).reshape(2, 3) + 9 C = np.arange(6).reshape(2, 3) + 15 factors = [] factors += [A] factors += [B] factors += [C] t_krus = tl.cp_to_tensor((None, factors)) print(err_rand(t_krus, None, factors, 2))
def test_cp_mode_dot(): """Test for cp_mode_dot We will compare cp_mode_dot (which operates directly on decomposed tensors) with mode_dot (which operates on full tensors) and check that the results are the same. """ rng = tl.check_random_state(12345) shape = (5, 4, 6) rank = 3 cp_ten = random_cp(shape, rank=rank, orthogonal=True, full=False) full_tensor = tl.cp_to_tensor(cp_ten) # matrix for mode 1 matrix = tl.tensor(rng.random_sample((7, shape[1]))) # vec for mode 2 vec = tl.tensor(rng.random_sample(shape[2])) # Test cp_mode_dot with matrix res = cp_mode_dot(cp_ten, matrix, mode=1, copy=True) # Note that if copy=True is not respected, factors will be changes # And the next test will fail res = tl.cp_to_tensor(res) true_res = mode_dot(full_tensor, matrix, mode=1) assert_array_almost_equal(true_res, res) # Check that the data was indeed copied rec = tl.cp_to_tensor(cp_ten) assert_array_almost_equal(full_tensor, rec) # Test cp_mode_dot with vec res = cp_mode_dot(cp_ten, vec, mode=2, copy=True) res = tl.cp_to_tensor(res) true_res = mode_dot(full_tensor, vec, mode=2) assert_equal(res.shape, true_res.shape) assert_array_almost_equal(true_res, res)
def test_err(): """ Test of err """ # create a kruskal tensor # factor matrices A = np.arange(9).reshape(3, 3) B = np.arange(6).reshape(2, 3) + 9 C = np.arange(6).reshape(2, 3) + 15 factors = [] factors += [A] factors += [B] factors += [C] t_krus = tl.cp_to_tensor((None, factors)) weights_cp, factors_cp = tl.cp_normalize((None, factors)) print(base.err(t_krus, weights_cp, factors_cp))
def test_gcp_nnContinuous_loss_functions(): cont_nn_losses = ['rayleigh', 'gamma'] opts = ['lbfgsb', 'sgd'] rng = tl.check_random_state(1234) shp = (4, 5, 6) rank = 4 tensor = generate_test_tensor('rayleigh', shp) for loss in cont_nn_losses: print("***************************************************\n") print("Loss function type: {}".format(loss)) for opt in opts: mTen = gcp(tensor, rank, type=loss, opt=opt, maxiters=1000, epciters=100) assert (mTen is not None), "gcp({}}) returned null".format(opt) mTen = tl.cp_to_tensor(mTen) assert (tensor.size == mTen.size), "Unequal number of tensor elements. \ Tensor: {} CPTensor: {}".format(tensor.size, tl.cp_to_tensor(mTen).size) score = 1 - (tl.norm(tensor - mTen) / tl.norm(tensor)) print("Score: {0:0.4f}\n".format(score))
def calcR2X(tFac, tIn=None, mIn=None): """ Calculate R2X. Optionally it can be calculated for only the tensor or matrix. """ assert (tIn is not None) or (mIn is not None) vTop, vBottom = 0.0, 0.0 if tIn is not None: tMask = np.isfinite(tIn) vTop += np.sum( np.square(tl.cp_to_tensor(tFac) * tMask - np.nan_to_num(tIn))) vBottom += np.sum(np.square(np.nan_to_num(tIn))) if mIn is not None: mMask = np.isfinite(mIn) recon = tFac if isinstance(tFac, np.ndarray) else buildGlycan(tFac) vTop += np.sum(np.square(recon * mMask - np.nan_to_num(mIn))) vBottom += np.sum(np.square(np.nan_to_num(mIn))) return 1.0 - vTop / vBottom
def makeFigure(): """ make heatmaps of factors when decomposed individually. """ ax, f = getSetup((20, 10), (3, 1)) tensor, ligand, ecm, measurements = import_LINCS_MEMA() fac = parafac(tensor, 5, n_iter_max=2000, linesearch=True, tol=1e-8) fac = cp_flip_sign(fac, 2) fac.normalize() facZero = pd.DataFrame(fac.factors[0], columns=[f"{i}" for i in np.arange(1, fac.rank + 1)], index=ligand) facOne = pd.DataFrame(fac.factors[1], columns=[f"{i}" for i in np.arange(1, fac.rank + 1)], index=ecm) facTwo = pd.DataFrame(fac.factors[2], columns=[f"{i}" for i in np.arange(1, fac.rank + 1)], index=measurements) print("R2X: ", 1.0 - np.linalg.norm(tl.cp_to_tensor(fac) - tensor)**2.0 / np.linalg.norm(tensor)**2.0) sns.heatmap(facZero.T, ax=ax[0], center=0) sns.heatmap(facOne.T, ax=ax[1], center=0) sns.heatmap(facTwo.T, ax=ax[2], center=0) return f
def test_herals(): """ Test of herals for a kruskal tensor """ # create a kruskal tensor # factor matrices A = np.arange(9).reshape(3, 3) B = np.arange(6).reshape(2, 3) + 9 C = np.arange(6).reshape(2, 3) + 15 factors = [] factors += [A] factors += [B] factors += [C] t_krus = tl.cp_to_tensor((None, factors)) weights, fac, it, error, cpt = her_Als(t_krus, 3) for i in fac: print(i) print(it) print(error[len(error) - 1])