-
Notifications
You must be signed in to change notification settings - Fork 0
/
tensor_completion.py
70 lines (53 loc) · 2.35 KB
/
tensor_completion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.decomposition import FactorAnalysis, PCA
import tensortools as tt
from tensortools.operations import unfold as tt_unfold, khatri_rao
import tensorly as tl
from tensorly import unfold as tl_unfold
from tensorly.decomposition import parafac
# import some useful functions (they are available in utils.py)
from utils import *
time_factor = np.load("data/time_factor.npy")
neuron_factor = np.load("data/neuron_factor.npy")
trial_factor = np.load("data/trial_factor.npy")
latent = np.load("data/latent.npy")
observed = np.load("data/observed.npy")
# Specify the tensor, and the rank (np. of factors)
X, rank = observed, 3
# Perform CP decompositon using TensorLy
factors_tl = parafac(X, rank=rank)
# Perform CP decomposition using tensortools
U = tt.cp_als(X, rank=rank, verbose=False)
factors_tt = U.factors.factors
# Reconstruct M, with the result of each library
M_tl = reconstruct(factors_tl)
M_tt = reconstruct(factors_tt)
# plot the decomposed factors
plot_factors(factors_tl)
plot_factors(factors_tt)
def decompose_three_way(tensor, rank, max_iter=501, verbose=False):
# a = np.random.random((rank, tensor.shape[0]))
b = np.random.random((rank, tensor.shape[1]))
c = np.random.random((rank, tensor.shape[2]))
for epoch in range(max_iter):
# optimize a
input_a = khatri_rao([b.T, c.T])
target_a = tl.unfold(tensor, mode=0).T
a = np.linalg.solve(input_a.T.dot(input_a), input_a.T.dot(target_a))
# optimize b
input_b = khatri_rao([a.T, c.T])
target_b = tl.unfold(tensor, mode=1).T
b = np.linalg.solve(input_b.T.dot(input_b), input_b.T.dot(target_b))
# optimize c
input_c = khatri_rao([a.T, b.T])
target_c = tl.unfold(tensor, mode=2).T
c = np.linalg.solve(input_c.T.dot(input_c), input_c.T.dot(target_c))
if verbose and epoch % int(max_iter * .2) == 0:
res_a = np.square(input_a.dot(a) - target_a)
res_b = np.square(input_b.dot(b) - target_b)
res_c = np.square(input_c.dot(c) - target_c)
print("Epoch:", epoch, "| Loss (C):", res_a.mean(), "| Loss (B):", res_b.mean(), "| Loss (C):", res_c.mean())
return a.T, b.T, c.T
factors_np = decompose_three_way(X, rank)