Skip to content

Commit 36e2ec0

Browse files
Pavel PopovPavel Popov
authored andcommitted
added wandb logger
1 parent 4c794b5 commit 36e2ec0

File tree

4 files changed

+87
-33
lines changed

4 files changed

+87
-33
lines changed

src/scripts/tune_ts_baseline.py

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,33 @@
1212
from src.settings import LOGS_ROOT, UTCNOW
1313
from src.ts import load_ABIDE1, TSQuantileTransformer
1414

15+
import wandb
16+
import time
17+
from collections import defaultdict
18+
1519

1620
class Experiment(IExperiment):
1721
def __init__(self, quantile: bool) -> None:
1822
super().__init__()
1923
self._quantile: bool = quantile
2024
self._trial: optuna.Trial = None
2125

26+
# init wandb logger
27+
self.wandbLogger: wandb.run = wandb.init(project="tune_ts", name="baseline")
28+
# for timer
29+
self.start: float = 0.0
30+
# set classifiers
31+
self.classifiers = [
32+
"LogisticRegression",
33+
"SGDClassifier",
34+
"AdaBoostClassifier",
35+
"RandomForestClassifier",
36+
]
37+
# initialize tables for different classifiers
38+
self.wandb_tables: dict = {}
39+
for classifier in self.classifiers:
40+
self.wandb_tables[classifier] = wandb.Table(columns=["score", "time"])
41+
2242
def on_tune_start(self):
2343
features, labels = load_ABIDE1()
2444
X_train, X_test, y_train, y_test = train_test_split(
@@ -43,20 +63,14 @@ def on_experiment_start(self, exp: "IExperiment"):
4363
# setup model
4464
clf_type = self._trial.suggest_categorical(
4565
"classifier",
46-
choices=[
47-
"LogisticRegression",
48-
"SGDClassifier",
49-
"AdaBoostClassifier",
50-
"RandomForestClassifier",
51-
],
66+
choices=self.classifiers,
5267
)
68+
5369
if clf_type == "LogisticRegression":
5470
solver = self._trial.suggest_categorical(
5571
"classifier.logistic.solver", ["liblinear", "lbfgs"]
5672
)
57-
decay = self._trial.suggest_loguniform(
58-
"classifier.logistic.C", low=1e-3, high=1e3
59-
)
73+
decay = self._trial.suggest_loguniform("classifier.logistic.C", low=1e-3, high=1e3)
6074
if solver == "liblinear":
6175
penalty = self._trial.suggest_categorical(
6276
"classifier.logistic.penalty", ["l1", "l2"]
@@ -71,9 +85,7 @@ def on_experiment_start(self, exp: "IExperiment"):
7185
penalty = self._trial.suggest_categorical(
7286
"classifier.sgd.penalty", ["l1", "l2", "elasticnet"]
7387
)
74-
alpha = self._trial.suggest_loguniform(
75-
"classifier.sgd.alpha", low=1e-4, high=1e-2
76-
)
88+
alpha = self._trial.suggest_loguniform("classifier.sgd.alpha", low=1e-4, high=1e-2)
7789
self.classifier = SGDClassifier(
7890
loss="modified_huber",
7991
penalty=penalty,
@@ -103,9 +115,7 @@ def run_dataset(self) -> None:
103115
self.classifier.fit(X_train, y_train)
104116
y_pred = self.classifier.predict(X_test)
105117
y_score = self.classifier.predict_proba(X_test)
106-
report = get_classification_report(
107-
y_true=y_test, y_pred=y_pred, y_score=y_score, beta=0.5
108-
)
118+
report = get_classification_report(y_true=y_test, y_pred=y_pred, y_score=y_score, beta=0.5)
109119
for stats_type in [0, 1, "macro", "weighted"]:
110120
stats = report.loc[stats_type]
111121
for key, value in stats.items():
@@ -119,14 +129,43 @@ def on_experiment_end(self, exp: "IExperiment") -> None:
119129
self._score = self.experiment_metrics[1]["ABIDE1"]["score"]
120130

121131
def _objective(self, trial) -> float:
132+
# start timer
133+
self.start = time.process_time()
134+
122135
self._trial = trial
123136
self.run()
137+
138+
# log overall score
139+
self.wandbLogger.log({"overall score": self._score})
140+
141+
self.wandb_tables[type(self.classifier).__name__].add_data(
142+
self._score, time.process_time() - self.start
143+
)
144+
124145
return self._score
125146

126147
def tune(self, n_trials: int):
127148
self.on_tune_start()
128149
self.study = optuna.create_study(direction="maximize")
129150
self.study.optimize(self._objective, n_trials=n_trials, n_jobs=1)
151+
152+
# log score and experiment time
153+
for classifier in self.classifiers:
154+
tableLength = len(self.wandb_tables[classifier].get_column("score"))
155+
self.wandb_tables[classifier].add_column(name="step", data=list(range(tableLength)))
156+
157+
line_series = wandb.plot.line_series(
158+
xs=self.wandb_tables[classifier].get_column("step"),
159+
ys=[
160+
self.wandb_tables[classifier].get_column("score"),
161+
self.wandb_tables[classifier].get_column("time"),
162+
],
163+
keys=["score", "time"],
164+
title=classifier,
165+
xname="step",
166+
)
167+
wandb.log({classifier: line_series})
168+
130169
logfile = f"{LOGS_ROOT}/{UTCNOW}-ts-baseline-q{self._quantile}.optuna.csv"
131170
df = self.study.trials_dataframe()
132171
df.to_csv(logfile, index=False)

src/scripts/tune_ts_lstm.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
from src.settings import LOGS_ROOT, UTCNOW
1717
from src.ts import load_ABIDE1, TSQuantileTransformer
1818

19+
import wandb
20+
1921

2022
class LSTM(nn.Module):
2123
def __init__(
@@ -28,9 +30,7 @@ def __init__(
2830
super(LSTM, self).__init__()
2931
self.hidden_size = hidden_size
3032
self.bidirectional = bidirectional
31-
self.lstm = nn.LSTM(
32-
hidden_size=hidden_size, bidirectional=bidirectional, **kwargs
33-
)
33+
self.lstm = nn.LSTM(hidden_size=hidden_size, bidirectional=bidirectional, **kwargs)
3434
self.fc = nn.Sequential(
3535
nn.Dropout(p=fc_dropout),
3636
nn.Linear(2 * hidden_size if bidirectional else hidden_size, 1),
@@ -60,6 +60,9 @@ def __init__(self, quantile: bool, max_epochs: int, logdir: str) -> None:
6060
self.max_epochs = max_epochs
6161
self.logdir = logdir
6262

63+
# init wandb logger
64+
self.wandbLogger: wandb.run = wandb.init(project="tune_ts", name="lstm")
65+
6366
def on_tune_start(self):
6467
features, labels = load_ABIDE1()
6568
X_train, X_test, y_train, y_test = train_test_split(
@@ -105,9 +108,7 @@ def on_experiment_start(self, exp: "IExperiment"):
105108
hidden_size=self._trial.suggest_int("lstm.hidden_size", 32, 256, log=True),
106109
num_layers=self._trial.suggest_int("lstm.num_layers", 1, 4),
107110
batch_first=True,
108-
bidirectional=self._trial.suggest_categorical(
109-
"lstm.bidirectional", [True, False]
110-
),
111+
bidirectional=self._trial.suggest_categorical("lstm.bidirectional", [True, False]),
111112
fc_dropout=self._trial.suggest_uniform("lstm.fc_dropout", 0.1, 0.9),
112113
)
113114
self.criterion = nn.BCEWithLogitsLoss()
@@ -160,9 +161,7 @@ def run_dataset(self) -> None:
160161
y_test = np.hstack(all_targets)
161162
y_score = np.hstack(all_scores)
162163
y_pred = (y_score > 0.5).astype(np.int32)
163-
report = get_classification_report(
164-
y_true=y_test, y_pred=y_pred, y_score=y_score, beta=0.5
165-
)
164+
report = get_classification_report(y_true=y_test, y_pred=y_pred, y_score=y_score, beta=0.5)
166165
for stats_type in [0, 1, "macro", "weighted"]:
167166
stats = report.loc[stats_type]
168167
for key, value in stats.items():
@@ -182,6 +181,10 @@ def on_experiment_end(self, exp: "IExperiment") -> None:
182181
def _objective(self, trial) -> float:
183182
self._trial = trial
184183
self.run()
184+
185+
# log score
186+
self.wandbLogger.log({"score": self._score})
187+
185188
return self._score
186189

187190
def tune(self, n_trials: int):

src/scripts/tune_ts_mlp.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
from src.settings import LOGS_ROOT, UTCNOW
1717
from src.ts import load_ABIDE1, TSQuantileTransformer
1818

19+
import wandb
20+
1921

2022
class ResidualBlock(nn.Module):
2123
def __init__(self, block):
@@ -77,6 +79,9 @@ def __init__(self, quantile: bool, max_epochs: int, logdir: str) -> None:
7779
self.max_epochs = max_epochs
7880
self.logdir = logdir
7981

82+
# init wandb logger
83+
self.wandbLogger: wandb.run = wandb.init(project="tune_ts", name="mlp")
84+
8085
def on_tune_start(self):
8186
features, labels = load_ABIDE1()
8287
X_train, X_test, y_train, y_test = train_test_split(
@@ -173,9 +178,7 @@ def run_dataset(self) -> None:
173178
y_test = np.hstack(all_targets)
174179
y_score = np.hstack(all_scores)
175180
y_pred = (y_score > 0.5).astype(np.int32)
176-
report = get_classification_report(
177-
y_true=y_test, y_pred=y_pred, y_score=y_score, beta=0.5
178-
)
181+
report = get_classification_report(y_true=y_test, y_pred=y_pred, y_score=y_score, beta=0.5)
179182
for stats_type in [0, 1, "macro", "weighted"]:
180183
stats = report.loc[stats_type]
181184
for key, value in stats.items():
@@ -195,6 +198,10 @@ def on_experiment_end(self, exp: "IExperiment") -> None:
195198
def _objective(self, trial) -> float:
196199
self._trial = trial
197200
self.run()
201+
202+
# log score
203+
self.wandbLogger.log({"score": self._score})
204+
198205
return self._score
199206

200207
def tune(self, n_trials: int):

src/scripts/tune_ts_transformer.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
from src.settings import LOGS_ROOT, UTCNOW
1717
from src.ts import load_ABIDE1, TSQuantileTransformer
1818

19+
import wandb
20+
1921

2022
class Transformer(nn.Module):
2123
def __init__(
@@ -56,6 +58,9 @@ def __init__(self, quantile: bool, max_epochs: int, logdir: str) -> None:
5658
self.max_epochs = max_epochs
5759
self.logdir = logdir
5860

61+
# init wandb logger
62+
self.wandbLogger: wandb.run = wandb.init(project="tune_ts", name="transformer")
63+
5964
def on_tune_start(self):
6065
features, labels = load_ABIDE1()
6166
X_train, X_test, y_train, y_test = train_test_split(
@@ -96,9 +101,7 @@ def on_experiment_start(self, exp: "IExperiment"):
96101
),
97102
}
98103
# setup model
99-
hidden_size = self._trial.suggest_int(
100-
"transformer.hidden_size", 4, 128, log=True
101-
)
104+
hidden_size = self._trial.suggest_int("transformer.hidden_size", 4, 128, log=True)
102105
num_heads = self._trial.suggest_int("transformer.num_heads", 1, 4)
103106
self.model = Transformer(
104107
input_size=53, # PRIOR
@@ -157,9 +160,7 @@ def run_dataset(self) -> None:
157160
y_test = np.hstack(all_targets)
158161
y_score = np.hstack(all_scores)
159162
y_pred = (y_score > 0.5).astype(np.int32)
160-
report = get_classification_report(
161-
y_true=y_test, y_pred=y_pred, y_score=y_score, beta=0.5
162-
)
163+
report = get_classification_report(y_true=y_test, y_pred=y_pred, y_score=y_score, beta=0.5)
163164
for stats_type in [0, 1, "macro", "weighted"]:
164165
stats = report.loc[stats_type]
165166
for key, value in stats.items():
@@ -179,6 +180,10 @@ def on_experiment_end(self, exp: "IExperiment") -> None:
179180
def _objective(self, trial) -> float:
180181
self._trial = trial
181182
self.run()
183+
184+
# log score
185+
self.wandbLogger.log({"score": self._score})
186+
182187
return self._score
183188

184189
def tune(self, n_trials: int):

0 commit comments

Comments
 (0)