-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.py
90 lines (67 loc) · 2.65 KB
/
main.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
import pandas as pd
import numpy as np
import os
import hydra
from omegaconf import DictConfig, OmegaConf
from models import KAN_CLASSIFIER, LP_CLASSIFIER
from utils.utils import load_data, create_directory
from sklearn.utils import check_random_state
@hydra.main(config_name="config_hydra.yaml", config_path="config")
def main(args: DictConfig):
with open("config.yaml", "w") as f:
OmegaConf.save(args, f)
xtrain, ytrain, xtest, ytest = load_data(file_name=args.dataset)
output_dir = args.output_dir
create_directory(output_dir)
rng = check_random_state(args.random_state)
if args.task_to_solve == "classification":
output_dir_task = output_dir + args.task_to_solve + "/"
create_directory(output_dir_task)
output_dir_dataset = output_dir_task + args.dataset + "/"
create_directory(output_dir_dataset)
_accs_kan = []
_accs_lp = []
for _ in range(args.runs):
kan_classifier = KAN_CLASSIFIER(
width=args.width,
output_dir=output_dir_dataset,
steps=args.steps,
k=args.k,
grid=args.grid,
random_state=rng.randint(0, np.iinfo(np.int32).max),
)
_, accuracy_kan_test = kan_classifier.fit_and_validate(
xtrain=xtrain, ytrain=ytrain, xval=xtest, yval=ytest
)
_accs_kan.append(accuracy_kan_test)
lp_classifier = LP_CLASSIFIER(
random_state=rng.randint(0, np.iinfo(np.int32).max)
)
_, accuracy_lp_test = lp_classifier.fit_and_validate(
xtrain=xtrain, ytrain=ytrain, xval=xtest, yval=ytest
)
_accs_lp.append(accuracy_lp_test)
df = pd.DataFrame(
columns=[
"accuracy-kan-mean",
"accuracy-kan-std",
"accuracy-lp-mean",
"accuracy-lp-std",
]
)
df.loc[len(df)] = {
"accuracy-kan-mean": np.mean(_accs_kan),
"accuracy-kan-std": np.std(_accs_kan),
"accuracy-lp-mean": np.mean(_accs_lp),
"accuracy-lp-std": np.std(_accs_lp),
}
df.to_csv(output_dir_dataset + "/results.csv", index=False)
if args.get_formulas:
formulas = kan_classifier.get_symbolic_function()
for i, string in enumerate(formulas):
filename = os.path.join(output_dir_dataset, f"formula_class_{i+1}.txt")
string = str(string)
with open(filename, "w") as file:
file.write(string)
if __name__ == "__main__":
main()