Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Add utils for draw landscape #338

Merged
merged 2 commits into from
Aug 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 70 additions & 1 deletion benchmark/FedHPOB/fedhpob/utils/draw.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import json
import datetime
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
Expand All @@ -23,7 +24,6 @@ def logloader(file):


def ecdf(model, data_list, algo, sample_client=None, key='test_acc'):
import datetime
from fedhpob.benchmarks import TabularBenchmark

# Draw ECDF from target data_list
Expand Down Expand Up @@ -288,5 +288,74 @@ def rank_over_time(root,
suffix, Y_label)


def landscape(model='cnn',
dname='femnist',
algo='avg',
sample_client=None,
key='test_acc'):
import plotly.graph_objects as go
from fedhpob.config import fhb_cfg
from fedhpob.benchmarks import TabularBenchmark

z = []
benchmark = TabularBenchmark(model, dname, algo, device=-1)

def get_best_config(benchmark):
results, config = [], []
for idx in tqdm(range(len(benchmark.table))):
row = benchmark.table.iloc[idx]
if sample_client is not None and row[
'sample_client'] != sample_client:
continue
result = eval(row['result'])
val_loss = result['val_avg_loss']
try:
best_round = np.argmin(val_loss)
except:
continue
results.append(result[key][best_round])
config.append(row)
best_index = np.argmax(results)
return config[best_index], results[best_index]

# config, _ = get_best_config(benchmark)
config = {'wd': 0.0, 'dropout': 0.5, 'step': 1.0}
config_space = benchmark.get_configuration_space()
X, Y = sorted(list(config_space['batch'])), sorted(list(
config_space['lr']))
print(X, Y)
for lr in Y:
y = []
for batch in X:
xy = {'lr': lr, 'batch': batch}
print({**config, **xy})
res = benchmark({
**config,
**xy
}, {
'sample_client': 1.0,
'round': 249
},
fhb_cfg=fhb_cfg,
seed=12345)
y.append(res['function_value'])
z.append(y)
Z = np.array(z)
fig = go.Figure(data=[go.Surface(z=Z, x=X, y=Y)])
fig.update_layout(title='FEMNIST (FedAvg)',
autosize=False,
width=900,
height=900,
margin=dict(l=65, r=50, b=65, t=90),
scene=dict(
xaxis_title='BS',
yaxis_title='LR',
zaxis_title='ACC',
))
fig.write_image(os.path.join('figures', 'femnist_fedavg_landscape.pdf'))

return


if __name__ == '__main__':
ecdf('gcn', ['cora', 'citeseer', 'pubmed'], sample_client=1.0)