-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsynthetic_Frobenius_delta_choice.py
182 lines (154 loc) · 5.17 KB
/
synthetic_Frobenius_delta_choice.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import numpy as np
from matplotlib import pyplot as plt
import NMF_Frobenius as nmf_fro
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import sys
import plotly.io as pio
#pio.kaleido.scope.mathjax = None
# Personnal comparison toolbox
# you can get it at
# https://github.com/cohenjer/shootout
from shootout.methods.runners import run_and_track
from shootout.methods.post_processors import df_to_convergence_df
from shootout.methods.post_processors import interpolate_time_and_error, median_convergence_plot
plt.close('all')
# --------------------- Choose parameters for grid tests ------------ #
if len(sys.argv)==1 or sys.argv[1]==0:
seeds = [] #no run
skip=True
else:
seeds = list(np.arange(int(sys.argv[1])))
skip=False
variables = {
'mnr' : [[200,100,5]],
'NbIter_inner' : [100],
'delta' : [0,0.001,0.01,0.05,0.1,0.3,0.6,0.9],
'SNR' : [100],
"seed": seeds,
#"distribution" : "uniform"
}
algs = ["Proposed-testing delta and inner iters"]
name = "Fro_run_delta-choice-10-05-2023"
@run_and_track(algorithm_names=algs, path_store="Results/", name_store=name,
skip=skip,
**variables
)
def one_run(mnr=[100,100,5],SNR=50, NbIter=20000, tol=0, NbIter_inner=10, verbose=False, show_it=1000, delta=0.4, seed=1):
NbIter = int(NbIter*(delta+0.1)) # will be 1500 for 100 iter inner
m, n, r = mnr
# Fixed the signal
rng = np.random.RandomState(seed+20)
Worig = rng.rand(m, r)
Horig = rng.rand(r, n)
Vorig = Worig.dot(Horig)
# Initialization for H0 as a random matrix
Hini = rng.rand(r, n)
Wini = rng.rand(m, r) #sparse.random(rV, cW, density=0.25).toarray()
# adding noise to the observed data
#N = np.random.poisson(1,size=Vorig.shape) # integers
N = rng.rand(m,n) # uniform
sigma = 10**(-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
error, W, H, toc, cnt = nmf_fro.NMF_proposed_Frobenius(V, Wini, Hini, NbIter, NbIter_inner, tol=tol, delta=delta, verbose=verbose)
return {"errors" : error,
"timings" : toc,
"cnt": cnt[::10], # subsample otherwise too big
}
# -------------------- Post-Processing ------------------- #
import pandas as pd
import plotly.io as pio
pio.templates.default= "plotly_white"
# Load results
df = pd.read_pickle("Results/"+name)
# Interpolating time (choose fewer points for better vis), adaptive grid since time varies across plots
ovars_inter = ["algorithm", "delta"]
df = interpolate_time_and_error(df, npoints = 100, adaptive_grid=True, groups=ovars_inter)
# Making a convergence plot dataframe
# We will show convergence plots for various sigma values, with only n=100
ovars = list(variables.keys())
df_conv = df_to_convergence_df(
df,
max_time=np.Inf,
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"})
# Converting to median for iterations and timings
df_conv_median_time = median_convergence_plot(df_conv, type_x="timings")
# Convergence plots with all runs
pxfig = px.line(df_conv_median_time,
x="timings",
y= "errors",
color='delta',
line_dash='delta',
log_y=True,
labels={"delta": r"$\delta$"}
#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/2, # in px
height=450,
xaxis=dict(range=[0,1.5], title_text="Time (s)"),
yaxis=dict(title_text="Fit")
)
pxfig.update_xaxes(
matches = None,
showticklabels = True
)
pxfig.update_yaxes(
matches=None,
showticklabels=True
)
pxfig.write_image("Results/"+name+".pdf")
pxfig.show()
# Figure showing cnt for each algorithm
# 1. make long format for cnt
# TODO: improve shootout to better handle this case
df_conv_cnt = df_to_convergence_df(df, groups=True, groups_names=ovars, err_name="cnt", other_names=ovars, time_name=False, max_time=False)
# 2. median plots
df_conv_median_cnt = median_convergence_plot(df_conv_cnt, type_x=None, err_name="cnt")
pxfig2 = px.line(df_conv_median_cnt,
x="it",
y= "cnt",
color='delta',
line_dash='delta',
log_y=True,
#error_y="q_errors_p",
#error_y_minus="q_errors_m",
labels={"delta": r"$\delta$"}
)
# Final touch
pxfig2.update_traces(
selector=dict(),
line_width=2.5,
#error_y_thickness = 0.3,
)
pxfig2.update_layout(
font_size = 12,
width=450*1.62/2, # in px
height=450,
xaxis=dict(range=[0,2000], title_text="Outer iteration"),
yaxis=dict(title_text="Number of inner loops")
)
pxfig2.update_xaxes(
matches = None,
showticklabels = True
)
pxfig2.update_yaxes(
matches=None,
showticklabels=True
)
pxfig2.write_image("Results/"+name+"_cnt.pdf")
pxfig.write_image("Results/"+name+".pdf")
pxfig2.show()