-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_zero_baseline.py
More file actions
120 lines (106 loc) · 3.67 KB
/
train_zero_baseline.py
File metadata and controls
120 lines (106 loc) · 3.67 KB
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
print("--------------------------------")
print("DEPRESSION CLASSIFICATION // V.0")
print("--------------------------------")
print("PROCESS: CONV NN TRAINING")
print("Importing libraries...")
import os
from tqdm import tqdm
import random
import pandas as pd
import torch
import torch.nn as nn
import numpy as np
from core.data import create_dataloaders, process_data_folds
from core.training_loops import run_zeroR_baseline
from core.eval import create_metrics_table
device = 'cuda' if torch.cuda.is_available() else 'cpu'
DATA_DIR = "data/processed_dataframes/"
RAW_PTH = DATA_DIR + "/data_raw.csv"
SMOTE_PTH = DATA_DIR + "/data_resampled.csv"
KFOLD_DIR = DATA_DIR + "/kfolds"
KFOLD_SMOTE_DIR = DATA_DIR + "/kfolds_smote"
NUM_FOLDS = len(os.listdir(KFOLD_DIR))//2
RESULTS_DIR = "results"
LONG_FEATURE = True
preprocessing_settings = {
'resample' : True,
'log_base' : None,
'scale_range' : (0,1),
'use_standard' : False,
'use_gaussian' : 30,
'batch_size' : 32
}
feature_settings = {
'use_feature' : False,
'long_feature' : True,
'window_size' : 30,
'quarter_diff' : False,
'simple' : True
}
hyperparameter_settings = {
'learning_rate' : 0.000005,
'weight_decay' : 1e-4,
'epochs' : 10,
'in_shape' : 1,
'out_shape' : 1,
'hidden_shape' : 32,
'flatten_factor' : 720
}
print("Loading data...")
# load data and cross validation folds
data_directory = RAW_PTH
kfolds_directory = KFOLD_DIR
data = pd.read_csv(data_directory, index_col=0)
kfolds = []
for i in range(NUM_FOLDS):
train_index = []
test_index = []
with open(kfolds_directory + f"/fold{i}t.txt", "r") as tfile:
for sample_name in tfile:
train_index.append(sample_name.strip())
with open(kfolds_directory + f"/fold{i}e.txt", "r") as efile:
for sample_name in efile:
test_index.append(sample_name.strip())
kfolds.append((train_index, test_index))
dataframes = process_data_folds(data, kfolds, preprocessing_settings, feature_settings)
# augment data for cnn and wrap into dataloaders
dataloaders = []
for (X_train, X_test, y_train, y_test) in dataframes:
(train_dataloader, test_dataloader) = create_dataloaders(X_train, X_test, y_train, y_test, shuffle=True, batch_size=32)
dataloaders.append((train_dataloader, test_dataloader))
# setup output directory, class weights, and loss function,
# aka criterion, for model training and evaluation
class_weights = torch.tensor([1]).to(device)
class_weights_dict = {
0 : 1,
1 : class_weights.item()
}
criterion = nn.BCEWithLogitsLoss(pos_weight=class_weights).to(device)
# if GRID_SEARCH:
# print("Performing gridsearch...")
# # select a random training dataframe to optimize
# index = random.randint(0, len(dataframes) - 1)
# (X_train, _, y_train, _) = dataframes[index]
# # gridsearch for optimal hyperparameters
# gridsearch = RandomizedSearchCV(estimator=XGBClassifier(),
# param_distributions=hyperparameter_grid,
# n_iter=135,
# cv=2,
# verbose=True)
# gridsearch.fit(X_train, y_train)
# print(f"absolute cinema of params: {gridsearch.best_params_}")
# # set settings to the optimal parameters
# for param in gridsearch.best_params_:
# hyperparameter_settings[param] = gridsearch.best_params_[param]
print("Evaluating model...")
zeroR_results = run_zeroR_baseline(
data=dataloaders,
criterion=criterion,
device=device,
)
print(zeroR_results)
print(hyperparameter_settings)
zeroR_results.to_csv(os.path.join(RESULTS_DIR, "zeroR.csv"))
metrics = create_metrics_table([zeroR_results])
print(metrics)
print("Done.")