-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshow.py
101 lines (75 loc) · 3.9 KB
/
show.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
"""
Script for generating plots.
"""
from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
import argparse
from stepback.record import Record
from stepback.utils import get_output_filenames
from stepback.plotting import plot_stability, plot_step_sizes
################# Main setup ###############################
parser = argparse.ArgumentParser(description='Generate step-back plots.')
parser.add_argument('-i', '--id', nargs='?', type=str, default='test', help="The id of the config (its file name).")
args = parser.parse_args()
try:
exp_id = args.id
save = True
except:
exp_id = 'cifar100_resnet110'
save = False
output_names = get_output_filenames(exp_id)
############################################################
#%%
#%matplotlib qt5
plt.rcParams["font.family"] = "serif"
plt.rcParams['font.size'] = 13
plt.rcParams['axes.linewidth'] = 1
plt.rc('text', usetex=True)
#%%
R = Record(output_names)
R.filter(drop={'name': ['momo-adam-star', 'momo-star']})
R.filter(drop={'name': ['adabelief', 'adabound', 'lion', 'prox-sps']})
R.filter(keep={'lr_schedule': 'constant'}) # only show constant learning rate results
base_df = R.base_df # base dataframe for all plots
id_df = R.id_df # dataframe with the optimizer setups that were run
# _ = R.plot_metric(s='val_score', log_scale=False, legend=True)
#%% plot training curves for a subset of runs:
# takes 3 best runs per methods
best = base_df[base_df.epoch==base_df.epoch.max()].groupby('name')['val_score'].nlargest(3)
#best = base_df[base_df.epoch==base_df.epoch.max()].groupby('name')['train_loss'].nsmallest(3)
ixx = base_df.id[best.index.levels[1]]
df1 = base_df.loc[base_df.id.isin(ixx),:]
y0 = 0.3 if exp_id=='cifar100_resnet110' else 0.4 if exp_id=='cifar10_vit' else 0.6
fig, ax = R.plot_metric(df=df1, s='val_score', ylim=(y0, 1.05*df1.val_score.max()), log_scale=False, figsize=(4,3.5), legend=False)
fig.subplots_adjust(top=0.975,bottom=0.16,left=0.16,right=0.975)
if save:
fig.savefig('output/plots/' + exp_id + f'/all_val_score.pdf')
fig, ax = R.plot_metric(df=df1, s='train_loss', log_scale=True, figsize=(4,3.5), legend=False)
fig.subplots_adjust(top=0.975,bottom=0.16,left=0.17,right=0.975)
if save:
fig.savefig('output/plots/' + exp_id + f'/all_train_loss.pdf')
#%% stability plots
FIGSIZE = (4.8,3.2)
fig, axs = plot_stability(R, score='val_score', xaxis='lr', sigma=1, legend=None, cutoff=None, figsize=FIGSIZE, save=save)
fig, axs = plot_stability(R, score='train_loss', xaxis='lr', sigma=1, legend=None, cutoff=None, figsize=FIGSIZE, save=save)
fig, axs = plot_stability(R, score=['train_loss', 'val_score'], xaxis='lr', sigma=1, legend=None, cutoff=None, figsize=(4.8,6.4), save=save)
#%% plots the adaptive step size
### THIS PLOT IS ONLY RELEVANT FOR METHODS WITH ADAPTIVE STEP SIZE
###################################
if exp_id == 'cifar10_resnet20':
_ = plot_step_sizes(R, method='momo', grid=(3,3), start=None, stop=None, save=save)
_ = plot_step_sizes(R, method='momo-adam', grid=(3,2), start=1, stop=None, save=save)
elif exp_id == 'cifar10_vgg16':
_ = plot_step_sizes(R, method='momo', grid=(3,3), start=2, stop=11, save=save)
_ = plot_step_sizes(R, method='momo-adam', grid=(3,3), start=2, stop=11, save=save)
elif exp_id == 'mnist_mlp':
_ = plot_step_sizes(R, method='momo', grid=(3,2), start=1, stop=None, save=save)
_ = plot_step_sizes(R, method='momo-adam', grid=(3,2), start=None, stop=None, save=save)
elif exp_id == 'cifar100_resnet110':
_ = plot_step_sizes(R, method='momo', grid=(3,2), start=1, stop=7, save=save)
_ = plot_step_sizes(R, method='momo-adam', grid=(3,2), start=1, stop=7, save=save)
elif exp_id == 'cifar10_vit':
_ = plot_step_sizes(R, method='momo', grid=(2,2), start=1, stop=5, save=save)
_ = plot_step_sizes(R, method='momo-adam', grid=(2,2), start=None, stop=None, save=save)
# %%