Commit bf81d2d1 authored by maximilian.seeber's avatar maximilian.seeber
Browse files

changed the visualization

parent 7a1fe738
import SetProxPythonPath
from proxtoolbox.experiments.MC.MC_Experiment import MC_Experiment
MC = MC_Experiment(algorithm='ALS', fraction_known_entrees=0.5, rank=2, anim=True)
MC = MC_Experiment(algorithm='ALS', fraction_known_entrees=0.3, rank=6, anim=True, n_2=200)
MC.run()
MC.animate(MC.algorithm)
MC.show()
\ No newline at end of file
import SetProxPythonPath
from proxtoolbox.experiments.MC.MC_Experiment import MC_Experiment
MC = MC_Experiment(algorithm='AP', fraction_known_entrees=0.4, rank=2, anim=True)
MC = MC_Experiment(algorithm='AP', fraction_known_entrees=0.4, rank=6, anim=True, n_2=200)
MC.run()
MC.animate(MC.algorithm)
MC.show()
\ No newline at end of file
import SetProxPythonPath
from proxtoolbox.experiments.MC.MC_Experiment import MC_Experiment
MC = MC_Experiment(algorithm='DR', fraction_known_entrees=0.5, anim=True)
MC = MC_Experiment(algorithm='DR', fraction_known_entrees=0.3, anim=True, n_2=200)
MC.run()
MC.animate(MC.algorithm)
MC.show()
\ No newline at end of file
......@@ -48,14 +48,30 @@ class MC_Experiment(Experiment):
self.diagnostic = False
self.gamma = gamma
self.lambd = lambd
self.frob_list = []
self.rank_list = []
def loadData(self):
self.mask = np.random.rand(self.n_1, self.n_2) < self.p
# self.u0 = (np.random.randn(self.n_1, self.r))@(np.random.randn(self.r, self.n_2))
self.u0 = np.ones((self.n_1, self.n_2))
self.u0[10:70, 10:20] = 0
self.u0[10:70, 50:60] = 0
self.u0[20:80, 10:20] = 0
self.u0[30:40, 20:30] = 0
self.u0[40:60, 30:40] = 0
self.u0[60:70, 40:50] = 0
self.u0[20:80, 50:60] = 0
self.u0[40:80, 80:90] = 0.5
self.u0[20:40, 90:100] = 0.5
self.u0[60:70, 90:100] = 0.5
self.u0[40:80, 100:110] = 0.5
self.u0[20:80, 130:140] = 0
self.u0[20:30, 140:150] = 0
self.u0[20:60, 150:160] = 0
self.u0[20:30, 160:170] = 0
self.u0[20:80, 170:180] = 0
self.M = np.copy(self.u0)
if self.algorithm_name == 'ALS':
......@@ -81,24 +97,34 @@ class MC_Experiment(Experiment):
# for key in self.output['stats']:
# print(key)
fig, ax = plt.subplots(1, 3, figsize=(16, 5))
fig, ((ax11, ax12), (ax21, ax22)) = plt.subplots(2, 2, figsize=(16, 5))
ax[0].imshow(self.M)
ax[0].set(title = 'original matrix')
ax[1].imshow(u)
ax[1].set(title = 'reconstructed matrix')
ax[2].semilogy(ch)
ax[2].set(xlabel='$k$', ylabel='$|| X_{k-1} - X_k ||$', title='change for each iteration')
ax11.imshow(self.M)
ax11.set(title = 'original matrix')
ax12.imshow(self.M * self.mask)
ax12.set(title = 'algorithm input')
ax21.semilogy(self.frob_list)
ax21.set(xlabel='$k$', ylabel='$|| M - X_k ||$', title='distance to M')
ax22.semilogy(ch)
ax22.set(xlabel='$k$', ylabel='$|| X_{k-1} - X_k ||$', title='change for each iteration')
plt.show()
def animate(self, alg):
# print(alg.u_new)
# print(alg.u_new)
if alg.u_new is not None:
if self.algorithm_name == 'DR':
self.animateFigure(alg.proxOperators[0].eval(alg.u_new), f'$X_{{{alg.iter}}}$')
self.frob_list += [np.linalg.norm(alg.proxOperators[0].eval(alg.u_new) - self.M)]
self.rank_list += [np.linalg.matrix_rank(alg.u_new)]
else:
self.animateFigure(alg.u_new, f'$X_{{{alg.iter}}}$')
self.frob_list += [np.linalg.norm(alg.u_new - self.M)]
self.rank_list += [np.linalg.matrix_rank(alg.u_new)]
if alg.iter < 50:
time.sleep(0.3)
time.sleep(0.1)
\ No newline at end of file
time.sleep(0.1)
else:
if self.algorithm_name == 'ALS':
self.animateFigure(self.u0[0].T@self.u0[1], '$X_0$')
else:
self.animateFigure(self.u0, '$X_0$')
time.sleep(0.7)
\ No newline at end of file
......@@ -11,8 +11,11 @@ class MC_prox_nuc(ProxOperator):
self.gamma = experiment.gamma
def eval(self, u):
n_1, n_2 = u.shape
U, sigma, V = np.linalg.svd(u)
sigma = sigma - self.gamma
sigma[sigma < 0] = 0
E = np.diag(sigma)
n = min(n_1, n_2)
E = np.zeros((n_1, n_2))
E[:n, :n] = np.diag(sigma)
return U @ E @ V
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment