-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_eda.py
131 lines (104 loc) · 4.77 KB
/
output_eda.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
from collections import defaultdict
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import sklearn
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
from sklearn.feature_selection import SelectKBest, f_regression, mutual_info_regression, r_regression
def get_stat_info(df: pd.DataFrame):
df = df.copy()
print(df.describe())
y = df["final_loss"]
df.drop(columns=["final_loss", "Size", "Generated Text", "total_training_time", "Heads"]
+ [col for col in df.columns if "Step " in col], inplace=True)
f_features, p_values = f_regression(df, y)
mi_features = mutual_info_regression(df, y)
r_features = r_regression(df, y)
print(df.columns)
print(f"F Statistics:\n{f_features}")
print(f"Mutual Info:\n{mi_features}")
print(f"Pearson:\n{r_features}")
def display_feature_importance(df: pd.DataFrame, include_training_time: bool):
df = df.copy()
clf = RandomForestRegressor()
if include_training_time:
y = df[["final_loss", "total_training_time"]]
else:
y = df[["final_loss"]]
df.drop(columns=["final_loss", "total_training_time", "Size", "Generated Text"] +
[col for col in df.columns if "Step " in col], inplace=True)
clf.fit(df, y)
plt.figure(figsize=(12,12))
plt.bar(df.columns, clf.feature_importances_)
plt.xticks(rotation=45)
if include_training_time:
plt.title("Feature Importance for Loss and Training Time")
else:
plt.title("Feature Importance for Loss")
plt.show()
def plot_hyper_parameter_by(hyper_parameter: str, data: defaultdict, field_prefix: str, label: str):
for hyper_param_value, values in data.items():
xs = [row['step'] for row in values]
ys = [row[f'{field_prefix}_mean'] for row in values]
y_range_max = [row[f'{field_prefix}_max'] for row in values]
y_range_min = [row[f'{field_prefix}_min'] for row in values]
plt.plot(xs, ys, label=f"{hyper_parameter}({hyper_param_value})")
plt.fill_between(xs, y_range_min, y_range_max, alpha=0.15)
plt.xscale('symlog')
# plt.yscale('symlog')
plt.xlabel("Step")
plt.ylabel(label)
plt.legend()
plt.title(f"{label} by {hyper_parameter} - {max(xs) + 100} Steps")
# fig.set_ylim(ymin=0)
plt.show()
def display_hyper_parameter_info(df: pd.DataFrame):
hyper_parameters = ['LR', 'Heads', 'Embeddings', 'Block Size', 'Batch Size', 'Layers']
for i, hyper_parameter in enumerate(hyper_parameters):
print("****")
fig = plt.figure(i)
to_drop = [s for s in hyper_parameters if s != hyper_parameter]
df_copy = df.copy(deep=True)
df_copy.drop(columns=to_drop, inplace=True)
print(df_copy.columns)
df_grouped = df_copy.groupby(by=[hyper_parameter, 'Step']).agg(['mean', 'min', 'max'])
data = defaultdict(list) # key is series hyper param value
for index_name in df_grouped.index:
hyper_param_value, step = index_name
# print(index_name)
train_loss_mean, train_loss_min, train_loss_max, \
val_loss_mean, val_loss_min, val_loss_max, \
accum_time_sec_mean, accum_time_sec_min, accum_time_sec_max = df_grouped.loc[index_name].to_list()
row = {'step': step,
'training_loss_mean': train_loss_mean, 'training_loss_min': train_loss_min,
'training_loss_max': train_loss_max,
'val_loss_mean': val_loss_mean, 'val_loss_min': val_loss_min, 'val_loss_max': val_loss_max,
'accum_time_sec_mean': accum_time_sec_mean, 'accum_time_sec_min': accum_time_sec_min,
'accum_time_sec_max': accum_time_sec_max
}
data[hyper_param_value].append(row)
# print(df_grouped.loc[index_name])
plot_hyper_parameter_by(hyper_parameter, data, "training_loss", "Training Loss")
plot_hyper_parameter_by(hyper_parameter, data, "val_loss", "Validation Loss")
plot_hyper_parameter_by(hyper_parameter, data, "accum_time_sec", "Training Time")
if __name__ == '__main__':
from pathlib import Path
curdir = Path(".")
dfs =[]
#for file_name in curdir.glob("**/output.csv"):
for file_name in curdir.glob("output.csv"):
df = pd.read_csv(file_name)
dfs.append(df)
df = pd.concat(dfs)
get_stat_info(df)
print("-----"*5)
display_feature_importance(df, include_training_time=True)
display_feature_importance(df, include_training_time=False)
print("-----"*5)
dfs = []
#for file_name in curdir.glob("**/output_steps.csv"):
for file_name in curdir.glob("output_steps.csv"):
df = pd.read_csv(file_name)
dfs.append(df)
df = pd.concat(dfs)
display_hyper_parameter_info(df)