-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsynthetic_comparisons_Frobenius_nls.py
168 lines (142 loc) · 5.38 KB
/
synthetic_comparisons_Frobenius_nls.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import numpy as np
from matplotlib import pyplot as plt
import NLS_Frobenius as nls_f
import nn_fac
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import time
import sys
import plotly.io as pio
pio.kaleido.scope.mathjax = None
#ToChange for new shootout version:
# - nbseed -> seeds and skip
# - type_x for median
# Personnal comparison toolbox
# you can get it at
# https://github.com/cohenjer/shootout
from shootout.methods.runners import run_and_track
plt.close('all')
# --------------------- Choose parameters for grid tests ------------ #
if len(sys.argv)==1 or int(sys.argv[1])==0:
seeds = [] #no run
skip=True
else:
seeds = list(np.arange(int(sys.argv[1])))
skip=False
variables = {
"add_track" : {"distribution" : "uniform"},
"mnr" : [[200,100,5], [1000,400,20]],
"NbIter" : [200], # for Lee and Seung also
"SNR" : [100, 30],
"delta" : 0,
"seed" : seeds,
"distribution" : "uniform",
"show_it" : 100,
"epsilon" : 1e-8
}
algs = ["MU_Fro","fastMU_Fro_ex","GD_Fro", "NeNMF_Fro", "HALS", "fastMU_Fro"]
name = "l2_nls_run-10-05-2023"
@run_and_track(algorithm_names=algs, path_store="Results/", name_store=name, skip=skip,
**variables)
def one_run(**cfg):
m, n, r = cfg["mnr"]
# Fixed the signal
rng = np.random.RandomState(cfg["seed"]+20)
Worig = rng.rand(m, r)
Horig = rng.rand(r, n)
Vorig = Worig.dot(Horig)
# prints
verbose = True
# Initialization for H0 as a random matrix
Hini = rng.rand(r, n)
# adding Gaussian noise to the observed data
N = rng.randn(m,n)
sigma = 10**(-cfg["SNR"]/20)*np.linalg.norm(Vorig)/np.linalg.norm(N)
V = Vorig + sigma*N
# One noise, one init; NMF is not unique and nncvx so we will find several results
# Baselines
error0, H0, toc0 = nls_f.NMF_Lee_Seung(V, Worig, Hini, cfg["NbIter"], legacy=False, delta=cfg["delta"], verbose=verbose)
error2, H2, toc2 = nls_f.Grad_descent(V , Worig, Hini, cfg["NbIter"], delta=cfg["delta"], verbose=verbose)
error3, H3, toc3 = nls_f.NeNMF(V, Worig, Hini, itermax=cfg["NbIter"], delta=cfg["delta"], verbose=verbose)
# HALS is unfair because we compute things before. We add the time needed for this back after the algorithm
tic = time.perf_counter()
WtV = Worig.T@V
WtW = Worig.T@Worig
toc4_offset = time.perf_counter() - tic
H4, _, _, _, error4, toc4 = nn_fac.nnls.hals_nnls_acc(WtV, WtW, np.copy(Hini), maxiter=cfg["NbIter"], return_error=True, delta=cfg["delta"], M=V)
toc4 = [toc4[i] + toc4_offset for i in range(len(toc4))] # leave the 0 in place for init
toc4[0]=0
# Proposed methods
error1, H1, toc1 = nls_f.NeNMF_optimMajo(V, Worig, Hini, itermax=cfg["NbIter"], delta=cfg["delta"], verbose=verbose, gamma=1)
error5, H5, toc5 = nls_f.NMF_proposed_Frobenius(V, Worig, Hini, cfg["NbIter"], delta=cfg["delta"], verbose=verbose)
return {"errors" : [error0, error1, error2, error3, error4, error5],
"timings" : [toc0, toc1, toc2, toc3, toc4, toc5],
}
# -------------------- Post-Processing ------------------- #
import pandas as pd
import shootout.methods.post_processors as pp
pio.templates.default= "plotly_white"
df = pd.read_pickle("Results/"+name)
# Remove extrapolation
df = df[df["algorithm"] != "fastMU_Fro_ex"]
# Interpolating time (choose fewer points for better vis), adaptive grid since time varies across plots
ovars_inter = ["mnr", "SNR", "algorithm"]
df = pp.interpolate_time_and_error(df, npoints = 100, adaptive_grid=True, groups=ovars_inter)#, strategy="min_curve")
ovars = ["mnr", "SNR", "seed"]
# Making a convergence plot dataframe
# We will show convergence plots for various sigma values, with only n=100
df_conv = pp.df_to_convergence_df(df, groups=True, groups_names=ovars, other_names=ovars, err_name="errors_interp", time_name="timings_interp")
df_conv = df_conv.rename(columns={"timings_interp": "timings", "errors_interp": "errors"})
# Median plot
df_conv_median_time = pp.median_convergence_plot(df_conv, type_x="timings", mean=False)
# Convergence plots with all runs
pxfig = px.line(#df_conv_median_time,
df_conv_median_time,
#x="timings",
x="timings",
y= "errors",
color='algorithm',
line_dash='algorithm',
facet_col="mnr",
facet_row="SNR",
log_y=True,
facet_col_spacing=0.1,
facet_row_spacing=0.1,
#line_group="groups",
#error_y="q_errors_p",
#error_y_minus="q_errors_m",
)
# Final touch
pxfig.update_traces(
selector=dict(),
line_width=2.5,
#error_y_thickness = 0.3,
)
pxfig.update_layout(
font_size = 12,
width=450*1.62, # in px
height=450,
xaxis1=dict(range=[0,0.05], title_text="Time (s)"),
xaxis3=dict(range=[0,0.05]),
xaxis2=dict(range=[0,0.01], title_text="Time (s)"),
xaxis4=dict(range=[0,0.005]),
yaxis1=dict(title_text="Fit"),
yaxis3=dict(title_text="Fit")
)
pxfig.update_xaxes(
matches = None,
showticklabels = True
)
pxfig.update_yaxes(
matches=None,
showticklabels=True
)
# updating titles
for i,ann in enumerate(pxfig.layout.annotations):
if ann.text[:3]=="mnr":
ann.text="[m,n,r]"+ann.text[3:]
# we save twice because of kaleido+browser bug...
pxfig.write_image("Results/"+name+".pdf")
pxfig.write_image("Results/"+name+".pdf")
pxfig.show()