-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmp_expirements.py
143 lines (122 loc) · 4.82 KB
/
mp_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
import gc
import time
from pydoc import locate
import numpy as np
import peernet.constants as c
from peernet.PeerNet import PeerNet
from peernet.helpers import save, bold
def communication_rounds(nodes, topology, data, algorithm, protocol, args=None):
"""Experiment: communication rounds X Accuracy"""
print(f"Nodes: {nodes} | Protocol: {protocol} | confidence: {args['protocol']['confidence']}")
analysis = "communication_rounds"
# analysis = "byzantine_metrics"
# analysis = "byzantine"
# analysis = "ban_epsilon"
y = abs_exp(nodes, topology, data, algorithm, protocol, args, analysis)
x = range(1, len(y) + 1, 1)
file_name = generate_filename(nodes, protocol, args, analysis)
save(file_name, (x, y))
return file_name
def byzantine(nodes, topology, data, algorithm, protocol, args=None):
"""Experiment: communication rounds X Accuracy"""
print(f"Nodes: {nodes} | Protocol: {protocol} | confidence: {args['protocol']['confidence']}")
analysis = "byzantine"
y = abs_exp(nodes, topology, data, algorithm, protocol, args, analysis)
x = range(1, len(y) + 1, 1)
file_name = generate_filename(nodes, protocol, args, analysis)
save(file_name, (x, y))
return file_name
def byzantine_metrics(nodes, topology, data, algorithm, protocol, args=None):
"""Experiment: communication rounds X metrics"""
print(f"Protocol: {protocol} | confidence: {args['protocol']['confidence']}")
analysis = "byzantine_metrics"
info = abs_exp(nodes, topology, data, algorithm, protocol, args, analysis)
accuracy, precision, recall, f_score = info
x = range(1, len(accuracy) + 1, 1)
file_name = generate_filename(nodes, protocol, args, analysis)
save(file_name, (x, info))
return file_name
def contribution_factor(nodes, topology, data, algorithm, protocol, args=None):
"""Model Propagation Experiment: communication rounds X ignored nodes"""
print(f"Protocol: {protocol} | confidence: {args['protocol']['confidence']}")
analysis = "contribution_factor"
info = abs_exp(nodes, topology, data, algorithm, protocol, args, analysis)
y = info[0]
z = info[1]
x = range(1, len(y) + 1, 1)
file_name = f"results/contribution_factor_{nodes}_BI"
save(file_name, (x, y, z))
return file_name
def data_unbalancedness(nodes: int = 10, topology='random', data=None, algorithm=None, protocol=None, args=None):
"""Model Propagation Experiment: width X Loss"""
c.ACCURACY_METRIC = "loss"
analysis = "data_unbalancedness"
x = np.arange(0.0, 1.1, 0.1)
y = []
print(f"Protocol: {protocol} | confidence: {args['protocol']['confidence']}")
for i in x:
i = round(i, 2)
print(bold(f"DATA UNBALANCEDNESS >> balancedness={i}"))
data['balancedness'] = i
r = abs_exp(nodes, topology, data, algorithm, protocol, args, analysis)
print(f"RESULT OF b={i} >> sum(loss) = {r}")
y.append(r)
time.sleep(1)
file_name = generate_filename(nodes, protocol, args, analysis)
save(file_name, (x, y))
return file_name
def graph_sparsity(nodes, topology, data, algorithm, protocol, args=None):
"""Model Propagation Experiment: width X Loss"""
c.ACCURACY_METRIC = "loss"
analysis = "graph_sparsity"
x = np.arange(1, 1.1, 0.1)
y = []
for i in x:
i = round(i, 2)
print(bold(f"GRAPH SPARSITY >> p={i}"))
r = abs_exp(nodes, topology, data, algorithm, protocol, args, analysis, p=i)
print(f"RESULT OF p={i} >> COST = {r}")
y.append(r)
time.sleep(2)
file_name = generate_filename(nodes, protocol, args, analysis)
save(file_name, (x, y))
return file_name
# ------------------------------ Internal functions ---------------------------
def abs_exp(nodes, topology, data, algorithm, protocol, args, analysis) -> list:
# instantiate PeerNet
p2p = PeerNet((nodes, c.START_PORT))
# load topology class
topology_class = locate(c.TOPOLOGY[topology])
# setup topology
p2p.network(topology_class)
# init the P2P system
p2p.init()
# Load and randomly distribute training samples among nodes
p2p.load_data(**data)
p2p.byzantine(args['behavior'])
# p2p.info()
# start training
p2p.train(
algorithm=algorithm,
protocol=protocol,
args=args,
analysis=analysis
)
p2p.stop()
r = p2p.results
del p2p
p2p = None
gc.collect()
return r
def generate_filename(n, protocol, args, analysis, balancedness=None):
confidence = args.get('protocol', {}).get('confidence')
if protocol == "CDPL" and confidence:
# ctype = f"F__{c.EPSILON_FAIRNESS}"
ctype = "F"
elif protocol == "MP" and confidence:
ctype = "C"
else:
ctype = "N"
if balancedness:
ctype = f"{ctype}_{balancedness}"
return f"results/{analysis}_{n}_{ctype}"