-
Notifications
You must be signed in to change notification settings - Fork 0
/
full_experience.py
470 lines (370 loc) · 12.6 KB
/
full_experience.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
from clearml import Task
from clearml.automation.controller import PipelineDecorator
@PipelineDecorator.component(
return_values=["users_df"],
cache=False,
task_type=Task.TaskTypes.data_processing,
)
def load_data_users():
import pandas as pd
from clearml import Dataset
ds = Dataset.get(
dataset_name="data_users",
dataset_project=Task.current_task().get_project_name(),
auto_create=True,
)
if not ds.is_final():
print("Made Dataset from scratch")
users_df = pd.DataFrame(
{
"id": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"type": [
"free",
"free",
"free",
"paid",
"paid",
"free",
"paid",
"free",
"paid",
"free",
],
"gender": ["f", "m", "m", "m", "f", "m", "f", "m", "f", "m"],
"age": [21, 22, 28, 23, 30, 22, 31, 33, 26, 20],
}
)
users_df.to_csv("./users.csv")
ds.add_files("./users.csv")
ds.finalize(auto_upload=True)
else:
print("Reused Dataset")
users_df = pd.read_csv(ds.get_local_copy() + "/users.csv").drop(
"Unnamed: 0", axis=1
)
return users_df
@PipelineDecorator.component(
return_values=["events_df"],
cache=True,
task_type=Task.TaskTypes.data_processing,
)
def load_data_events():
import pandas as pd
events_df = pd.DataFrame(
{
"id": [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
],
"type": ["view"] * 6 + ["click"] * 4 + ["view"] * 8 + ["click"] * 2,
"user_id": [0, 1, 2, 3, 4, 2, 1, 2, 3, 4, 5, 6, 5, 6, 2, 8, 7, 7, 8, 9],
"session_duration_m": [
3,
2,
2,
1,
4,
1,
3,
2,
2,
1,
4,
1,
3,
5,
8,
1,
3,
5,
8,
2,
],
}
)
return events_df
@PipelineDecorator.component(
return_values=["full_df"],
cache=False,
task_type=Task.TaskTypes.data_processing,
repo="[email protected]:alex-burlacu-clear-ml/pipelines-with-modules.git",
packages=["pandas", "clearml"],
)
def join_data(users_df, events_df):
# TODO: make or reuse dataset, keep lineage info
from dummy_module import function_whatever
import pandas as pd
from clearml import Dataset
ds = Dataset.get(
dataset_name="data_full",
dataset_project=Task.current_task().get_project_name(),
auto_create=True,
)
if not ds.is_final():
full_df = pd.merge(
events_df,
users_df,
left_on="user_id",
right_on="id",
suffixes=["_of_event", "_of_user"],
)
full_df.set_index("id_of_event").drop("id_of_user", axis=1)
full_df.to_csv("./full.csv")
ds.add_files("./full.csv")
ds.finalize(auto_upload=True)
else:
full_df = pd.read_csv(ds.get_local_copy() + "/full.csv").drop(
"Unnamed: 0", axis=1
)
function_whatever(full_df)
return full_df
@PipelineDecorator.component(
return_values=["clean_df"],
cache=True,
task_type=Task.TaskTypes.data_processing,
# repo="[email protected]:alex-burlacu-clear-ml/test.git",
# packages=["-r requirements.txt"],
)
def clean_data(full_df):
# TODO: make or reuse dataset, keep lineage info
import pandas as pd
type_of_event_fact, _ = pd.factorize(full_df["type_of_event"])
full_df["type_of_event"] = type_of_event_fact
gender_fact, _ = pd.factorize(full_df["gender"])
full_df["gender"] = gender_fact
type_of_user_ohe = pd.get_dummies(full_df["type_of_user"], prefix="type_of_user")
full_df = full_df.join(type_of_user_ohe).drop("type_of_user", axis=1)
return full_df.drop("user_id", axis=1)
@PipelineDecorator.component(
return_values=["X_train", "X_test", "y_train", "y_test"],
task_type=Task.TaskTypes.data_processing,
# repo="[email protected]:alex-burlacu-clear-ml/test.git",
# packages=["-r requirements.txt"],
)
def make_training_testing_data(clean_df):
import pandas as pd
from sklearn.model_selection import train_test_split
X, y = clean_df.drop("type_of_event", axis=1), clean_df.type_of_event
X, y = X.values.astype(float), y.values.astype(int)
return train_test_split(X, y.reshape(-1, 1), test_size=0.3, shuffle=False)
@PipelineDecorator.component(
return_values=["model", "criterion"],
cache=True,
task_type=Task.TaskTypes.training,
# repo="[email protected]:alex-burlacu-clear-ml/test.git",
# packages=["-r requirements.txt"],
)
def make_model(input_size, hparams):
import torch
model = torch.nn.Sequential(
torch.nn.Linear(input_size, hparams["hidden_size"]),
torch.nn.ReLU() if hparams["activation_type"] == "relu" else torch.nn.Tanh(),
torch.nn.Linear(hparams["hidden_size"], 1),
torch.nn.Sigmoid(),
)
criterion = torch.nn.BCELoss()
return model, criterion
@PipelineDecorator.component(
return_values=["model"],
cache=False,
task_type=Task.TaskTypes.training,
# repo="[email protected]:alex-burlacu-clear-ml/test.git",
# packages=["-r requirements.txt"],
)
def train_model(model, criterion, X_train, y_train):
import torch
from tensorboardX import SummaryWriter
import time
import numpy as np
from tqdm import tqdm
num_epochs = 20
batch_size = 4
writer = SummaryWriter()
optimizer = torch.optim.Adam(model.parameters())
since = time.time()
pbar = tqdm(range(num_epochs))
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
dataset = torch.utils.data.TensorDataset(
torch.from_numpy(np.array(X_train)).float(), torch.from_numpy(np.array(y_train)).float()
)
dataloader = torch.utils.data.DataLoader(
dataset, batch_size=batch_size, shuffle=True
)
model.to(device)
model.train()
writer.add_graph(model, dataset[0][0])
for epoch in pbar:
running_loss = 0.0
epoch_loss = 0
with tqdm(dataloader, unit="batch") as tepoch:
tepoch.set_description("Epoch {}/{}".format(epoch + 1, num_epochs))
for inputs, labels in tepoch:
batch_size = inputs.size(0)
inputs = inputs.to(device)
labels = labels.to(device)
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item() * inputs.size(0)
epoch_loss = running_loss / len(dataset)
pbar.set_postfix({"loss": f"{epoch_loss:.4f}"})
print()
writer.add_scalar("Loss/train", epoch_loss, epoch)
for name, param in model.named_parameters():
writer.add_histogram(f"{name}/weights/train", param, epoch)
writer.add_histogram(f"{name}/grads/train", param.grad, epoch)
time_elapsed = time.time() - since
print(
"Training complete in {:.0f}m {:.0f}s".format(
time_elapsed // 60, time_elapsed % 60
)
)
writer.close()
return model
@PipelineDecorator.component(
cache=False,
task_type=Task.TaskTypes.qc,
# repo="[email protected]:alex-burlacu-clear-ml/test.git",
# packages=["-r requirements.txt"],
)
def evaluate_data(full_df):
from allegroai import Task
import pandas as pd
import matplotlib.pyplot as plt
import io
task = Task.current_task()
print(task.id)
full_df.plot.scatter(x="age", y="session_duration_m")
plt.show()
full_df[["user_id", "session_duration_m"]].groupby(
"user_id"
).mean().reset_index().plot.bar(x="user_id", y="session_duration_m")
plt.savefig("mean_session_duration_by_user.png")
buf = io.BytesIO()
full_df[["gender", "session_duration_m"]].groupby(
"gender"
).sum().reset_index().plot.pie(x="gender", y="session_duration_m")
plt.savefig(buf, format="png")
buf.seek(0)
task.get_logger().report_media(
title="pie chart", series="Stats", stream=buf, file_extension="png"
)
with open("/tmp/file.txt", "w") as fptr:
fptr.write("Hello from the other side")
task.get_logger().report_media(
title="some text", series="Stats", local_path="/tmp/file.txt"
)
@PipelineDecorator.component(
return_values=["validation_accuracy"],
cache=False,
task_type=Task.TaskTypes.qc,
# repo="[email protected]:alex-burlacu-clear-ml/test.git",
# packages=["-r requirements.txt"],
monitor_metrics=[("validation", "validation_accuracy")],
)
def evaluate_model(model, criterion, X_test, y_test):
from sklearn import metrics
import numpy as np
import torch
import time
from tqdm import tqdm
since = time.time()
running_loss = 0.0
running_corrects = 0
epoch_loss = 0
epoch_accuracy = 0
predicted = []
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
dataset = torch.utils.data.TensorDataset(
torch.from_numpy(np.array(X_test)).float(), torch.from_numpy(np.array(y_test)).float()
)
dataloader = torch.utils.data.DataLoader(dataset, batch_size=1, shuffle=True)
model.to(device)
model.eval()
with tqdm(dataloader, unit="batch") as tepoch:
for inputs, labels in tepoch:
inputs = inputs.to(device)
labels = labels.to(device)
with torch.set_grad_enabled(False):
outputs = model(inputs)
loss = criterion(outputs, labels)
_, preds = torch.max(outputs, 1)
predicted.append(preds.item())
running_loss += loss.item() * inputs.size(0)
running_corrects += torch.sum(preds == labels.data)
epoch_loss = running_loss / len(dataset)
epoch_accuracy = running_corrects.double() / len(dataset)
time_elapsed = time.time() - since
print(
"Evaluation complete in {:.0f}m {:.0f}s".format(
time_elapsed // 60, time_elapsed % 60
)
)
print("Val Acc: {:4f}".format(epoch_accuracy))
print("Val Loss: {:4f}".format(epoch_loss))
print(
f"Classification report for classifier {model}:\n"
f"{metrics.classification_report(y_test, predicted)}\n"
)
disp = metrics.ConfusionMatrixDisplay.from_predictions(y_test, predicted)
disp.figure_.suptitle("Confusion Matrix")
print(f"Confusion matrix:\n{disp.confusion_matrix}")
return epoch_accuracy
@PipelineDecorator.component(
cache=False,
task_type=Task.TaskTypes.custom,
# repo="[email protected]:alex-burlacu-clear-ml/test.git",
)
def publish_results_or_smth():
print("A valid one")
@PipelineDecorator.pipeline(
name="test pipeline logic",
project="a-new-project",
version="0.0.2",
pipeline_execution_queue="queue-7",
# packages=["-r requirements.txt"],
# repo="[email protected]:alex-burlacu-clear-ml/test.git",
)
def main(min_acceptable_val_accuracy=0.65, activation_type="relu", hidden_size=128):
users_df = load_data_users()
events_df = load_data_events()
full_df = join_data(users_df, events_df)
evaluate_data(full_df)
clean_df = clean_data(full_df)
X_train, X_test, y_train, y_test = make_training_testing_data(clean_df)
hparams = {
"activation_type": activation_type,
"hidden_size": hidden_size,
}
for _ in range(3):
model, criterion = make_model(len(clean_df.columns) - 1, hparams)
trained_model = train_model(model, criterion, X_train, y_train)
validation_accuracy = evaluate_model(trained_model, criterion, X_test, y_test)
if validation_accuracy > min_acceptable_val_accuracy:
publish_results_or_smth()
return validation_accuracy
if __name__ == "__main__":
# PipelineDecorator.run_locally()
# PipelineDecorator.debug_pipeline()
PipelineDecorator.set_default_execution_queue("queue-7")
main()