-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpirements.py
309 lines (263 loc) · 10.7 KB
/
expirements.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
"""
Benchmark experiments
~~~~~~~~~~~~~~~~~~~~~
Experiments used to benchmark the different setting of both MP and CL
"""
import time
import numpy as np
from peernet.PeerNet import PeerNet
import peernet.constants as sharedVars
from peernet.helpers import save
from peernet.constants import DATASETS_FOLDER
import plots
# ------------------------- Model Propagation / Iterations x Cost -------------
def experiment_mp_iter(model, config, network, dataset, confidence, analysis):
"""Model Propagation / Iterations x Cost"""
# init PeerNet with a configuration option
p2p = PeerNet(config)
# Setup the P2P network and the communication between neighbors
p2p.network(network).init()
# Load and randomly distribute training samples between nodes
p2p.load_dataset(f"./datasets/{dataset[0]}", df=dataset[1], min_samples=dataset[2], sep=dataset[3])
start_time = time.time()
iterations = analysis['iterations']
sharedVars.STOP_CONDITION = iterations[1]
p2p.train(
model=model,
pre=dataset[4],
algorithm="MP",
params={'confidence': confidence, 'debug': False, 'show_results': False},
analysis=analysis['type']
)
print(f"\nSWITCH {sharedVars.STOP_CONDITION} done in {time.time() - start_time} seconds")
# Plotting
x = range(iterations[0], iterations[1] + 1, iterations[2])
y = p2p.results
# print(y)
save(f"OLD_results/mp_iterations_{confidence}", (x, y))
labels = {'x': "Iterations", 'y': "Cost", 'title': "Iterations X Cost"}
# plots.iterations(x, y, labels)
# ddd
start_time = time.time()
iterations = analysis['iterations']
sharedVars.STOP_CONDITION = iterations[1]
p2p.train(
model=model,
pre=dataset[4],
algorithm="MP",
params={'confidence': not confidence, 'debug': False, 'show_results': False},
analysis=analysis['type']
)
print(f"\nSWITCH {sharedVars.STOP_CONDITION} done in {time.time() - start_time} seconds")
# Plotting
x = range(iterations[0], iterations[1] + 1, iterations[2])
y = p2p.results
# print(y)
save(f"OLD_results/mp_iterations_{not confidence}", (x, y))
labels = {'x': "Iterations", 'y': "Cost", 'title': "Iterations X Cost"}
# plots.iterations(x, y, labels)
fileA = "./results/mp_iterations_True"
fileB = "./results/mp_iterations_False"
info = {
'xlabel': "Iterations",
# 'ylabel': "Test accuracy",
'ylabel': "Test cost",
'title': "MP with and without confidence w.r.t.the number of iterations."
}
plots.file_mp_iter(fileA, fileB, info)
# ------------------------- Model Propagation / Data unbalancedness x Cost ----
def experiment_mp_unbalancedness(model, config, network, dataset, confidence, analysis):
"""Model Propagation / Data unbalancedness x Cost"""
p2p = PeerNet(config)
# Setup the P2P network and the communication between neighbors
p2p.network(network).init()
# Train the model using one of the approaches: "MP", "CL" or "LL"
sharedVars.STOP_CONDITION = analysis['SC']
a = p2p.epsilon
while p2p.epsilon <= 1:
print(f"SWITCH: {p2p.epsilon}")
start_time = time.time()
p2p.load_dataset(f"./datasets/{dataset[0]}", df=dataset[1], min_samples=dataset[2], sep=dataset[3],
data_distribution="uniform")
show_results = p2p.epsilon == 1
p2p.train(
model=model,
pre=dataset[4],
algorithm="MP",
params={'confidence': confidence, 'debug': False, 'show_results': False},
analysis=analysis['type']
)
print(f"\nSWITCH done in {time.time() - start_time} seconds")
b = p2p.epsilon
# Plotting
x = np.arange(0, 1 + sharedVars.EPSILON_STEP, sharedVars.EPSILON_STEP)
y = p2p.results
save(f"OLD_results/mp_epsilon_{analysis['SC']}_{confidence}", (x, y))
# Train the model using one of the approaches: "MP", "CL" or "LL"
sharedVars.STOP_CONDITION = analysis['SC']
# new
p2p.epsilon = 0.0
p2p.results = []
a = p2p.epsilon
while p2p.epsilon <= 1:
print(f"SWITCH: {p2p.epsilon}")
start_time = time.time()
p2p.load_dataset(f"./datasets/{dataset[0]}", df=dataset[1], min_samples=dataset[2], sep=dataset[3],
data_distribution="uniform")
show_results = p2p.epsilon == 1
p2p.train(
model=model,
pre=dataset[4],
algorithm="MP",
params={'confidence': not confidence, 'debug': False, 'show_results': False},
analysis=analysis['type']
)
print(f"\nSWITCH done in {time.time() - start_time} seconds")
b = p2p.epsilon
# Plotting
x = np.arange(0, 1 + sharedVars.EPSILON_STEP, sharedVars.EPSILON_STEP)
y = p2p.results
save(f"OLD_results/mp_epsilon_{analysis['SC']}_{not confidence}", (x, y))
# plots
fileA = f"./results/mp_epsilon_{analysis['SC']}_{confidence}"
fileB = f"./results/mp_epsilon_{analysis['SC']}_{not confidence}"
info = {
'xlabel': "Width ε",
'ylabel': "Cost",
'title': "MP with and without confidence w.r.t. data unbalancednesss."
}
plots.file_mp_iter(fileA, fileB, info)
def experiment_mp_data(model, config, network, dataset, confidence, analysis):
"""Model Propagation / Data unbalancedness x Cost"""
p2p = PeerNet(config)
p2p.network(network).init()
output = []
for i in [0.0, 1.0]: # np.arange(0.9, 1.01, 0.1)
results = []
for j in range(10):
p2p.epsilon = i
p2p.results = []
print(f"RUN: {i}:{j}")
start_time = time.time()
sharedVars.STOP_CONDITION = 100
data = f"{DATASETS_FOLDER}/{dataset[0]}"
p2p.load_dataset(data, df=dataset[1], min_samples=dataset[2], sep=dataset[3], data_distribution="uniform")
p2p.train(
model=model,
pre=dataset[4],
algorithm="MP",
params={'confidence': confidence, 'debug': False, 'show_results': False},
analysis='data'
)
results.append(p2p.results)
print(f"\nRUN done in {time.time() - start_time} seconds")
rr = np.mean(results)
print(f"SWITCH {i} :: +++++++++++++++++++++ DONE R= {rr}")
output.append(rr)
print("Experiment done ++")
print(output)
# ------------------------- Model Propagation / Data unbalancedness x Cost ----
def experiment_mp_graph_sparsity(model, config, network, dataset, confidence, analysis):
# Train the model using one of the approaches: "MP", "CL" or "LL"
p = 0.0
a = p
results = []
while p <= 1:
print(f"SWITCH: {p}")
sharedVars.STOP_CONDITION = analysis['SC']
start_time = time.time()
p2p = PeerNet(config)
p2p.network(network, p).init()
p2p.load_dataset(f"./datasets/{dataset[0]}", df=dataset[1], min_samples=dataset[2], sep=dataset[3])
show_results = p == 0.9 # and False
print(f"PEERS: {sum([len(node.peers) for node in p2p.nodes])}")
p2p.train(
model=model,
pre=dataset[4],
algorithm="MP",
params={'confidence': confidence, 'debug': False, 'show_results': show_results, 'epochs': 200},
analysis=analysis['type']
)
print(f"\nSWITCH {p} done in {time.time() - start_time} seconds")
results.append(p2p.results)
print(f"COST={p2p.results}")
p2p.stop()
p = round(p + 0.1, 1)
b = p
# Plotting
x = np.arange(a, b, 0.1)
y1 = results
print(y1)
save(f"OLD_results/mp_sparsity_{analysis['SC']}_{confidence}", (x, y1))
# labels = {'x': "Graph Sparsity", 'y': "Cost", 'title': "Graph Sparsity X Cost"}
# plots.iterations(x, y1, labels)
# ------------------------- Model Propagation / Measure x Communication ----
def experiment_mp_communication(model, config, network, dataset, confidence, analysis):
start_time = time.time()
sharedVars.STOP_CONDITION = analysis['SC']
p2p = PeerNet(config)
p = 0.94
p2p.network(network, p).init()
p2p.load_dataset(f"./datasets/{dataset[0]}", df=dataset[1], min_samples=dataset[2], sep=dataset[3])
p2p.train(
model=model,
pre=dataset[4],
algorithm="MP",
params={'confidence': confidence, 'debug': False, 'show_results': False},
analysis=analysis['type']
)
print(f"\nDone in {time.time() - start_time} seconds")
print(f"Number of communication with {p} nodes for {analysis['SC']} == {p2p.results}")
# p2p.stop()
# ------------------------- Collaborative Learning / Iterations x Cost -------------
def experiment_cl_iter(model, config, network, dataset, analysis):
"""Collaborative Learning / Iterations x Cost."""
p2p = PeerNet(config)
p2p.network(network).init()
data = f"{DATASETS_FOLDER}/{dataset[0]}"
p2p.load_dataset(data, df=dataset[1], min_samples=dataset[2], sep=dataset[3])
start_time = time.time()
iterations = analysis['iterations']
sharedVars.STOP_CONDITION = iterations[1]
p2p.train(
model=model,
pre=dataset[4],
algorithm="CL",
params={'debug': False, 'show_results': True, 'epochs': 100},
analysis=analysis['type']
)
print(f"\nSWITCH {sharedVars.STOP_CONDITION} done in {time.time() - start_time} seconds")
# Plotting
x = range(iterations[0], iterations[1] + 1, iterations[2])
y = p2p.results
print("X:")
print(x)
print("Y:")
print(y)
save(f"OLD_results/cl_iterations_iter_{iterations[1]}", (x, y))
labels = {'x': "Iterations", 'y': "Cost", 'title': "Iterations X Cost"}
plots.iterations(x, y, labels)
# ------------------------- Local Learning / Accuracy -------------------------
def experiment_ll(model, config, network, dataset, confidence, analysis):
"""
@param model: Used machine learning model
@param config: configuration file or a tuple in the form (Number of nodes, INIT_PORT)
@param network: random or static
@param dataset: tuple (dataset_file, df?, min_samples, separator, pre_processing)
@param confidence: use confidence or not
@param analysis:
"""
# init PeerNet with a configuration option
p2p = PeerNet(config)
# Setup the P2P network and the communication between neighbors
p2p.network(network).init()
# Load and randomly distribute training samples between nodes
p2p.load_dataset(f"./datasets/{dataset[0]}", df=dataset[1], min_samples=dataset[2], sep=dataset[3])
# Train the model using one of the approaches: "MP", "CL" or "LL"
p2p.train(
model=model,
pre=dataset[4],
algorithm="LL",
params={'confidence': confidence},
analysis=analysis['type']
)