-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_nn_lit_trainer.py
197 lines (154 loc) · 4.81 KB
/
simple_nn_lit_trainer.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
import os
import sys
import string
import random
# from sklearn.metrics import confusion_matrix, recall_score, precision_score
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import StratifiedKFold
from sklearn.utils.class_weight import compute_class_weight
import optuna
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.metrics import ConfusionMatrixDisplay
import csv
import ast
from datetime import date, datetime, time, timedelta
from datetime import datetime
import pickle as pkl
import time
from sklearn import preprocessing
from sklearn.metrics import top_k_accuracy_score
import numpy as np
import pandas as pd
import sklearn
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import MinMaxScaler
from joblib import dump, load
import matplotlib.pyplot as plt
import torch
from torch.utils.data import TensorDataset
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils
from torch.optim.lr_scheduler import *
from models.simple_nn import *
import wandb
import torchmetrics
import logging
import pandas as pd
import numpy as np
import torch
import os
import torch
from torch import nn
from torch.utils.data import DataLoader
from torchvision import datasets, transforms
import pytorch_lightning as pl
from pytorch_lightning.loggers import WandbLogger # newline 1
from pytorch_lightning import Trainer
from pytorch_lightning.callbacks.early_stopping import EarlyStopping
from models.simple_nn_lightning import *
from utils.simple_nn_utils import *
from utils.data_loader import *
################
### Settings ###
################
data_fp = f"/gpfs/milgram/project/rtaylor/shared/ABDPain_EarlyDiags/unq_pt_enc_single_label_full_clean_label.csv"
logger_fp = f"/gpfs/milgram/project/rtaylor/shared/ABDPain_EarlyDiags/model_outputs/simple_nn_logging.txt"
# Create W&B study
wandb.init(project="test-project", entity="decile")
WANDB_RUN_NAME = wandb.run.name
logging.basicConfig(
filename=logger_fp,
level=logging.INFO,
format=f"{WANDB_RUN_NAME} | %(asctime)s | %(levelname)s | %(message)s",
)
logging.info(f"Data File loc: {data_fp}")
device = "cuda" if torch.cuda.is_available() else "cpu"
wandb.config = {
"learning_rate": 0.00001,
"lr_weight_decay": 0.0,
"lr_scheduler": False,
"epochs": 500,
"batch_size": 256,
# drop columns that don't have at least this many non-NA values
"drop_sparse_cols": 10,
# "multilabel":True,
"dropout": 0.2,
# can be "inverse", None, "effective_sample", or "balanced"
"class_weight_type": "inverse",
# The amount to normalize by in inverse class weighting
"class_weight_inv_lambda": 10000.0,
# only used if class_weight_type is "effective sample"
"weight_beta": 0.999,
}
print(wandb.config)
# load data
(
X_train_input,
X_test_input,
y_train,
y_test,
idxs_train,
idxs_test,
class_labels,
) = load_data(data_fp)
# we need to store this value for the NN model definition
INPUT_DIM = X_train_input.shape[1]
train_dataset = TensorDataset(X_train_input, torch.tensor(le.transform(y_train)))
test_dataset = TensorDataset(X_test_input, torch.tensor(le.transform(y_test)))
train_loader = DataLoader(
train_dataset,
batch_size=wandb.config["batch_size"],
# collate_fn=collate_wrapper,
pin_memory=True,
)
test_loader = DataLoader(
test_dataset,
batch_size=wandb.config["batch_size"],
# collate_fn=collate_wrapper,
pin_memory=True,
)
logging.info(f"Created train/test loaders")
##########################
### Model Train Funcs. ###
##########################
try:
model = AbdPainPredictionMLP(INPUT_DIM, N_CLASSES, wandb.config["dropout"]).to(
device
)
print(model, flush=True)
wandb.watch(model)
wandb_logger = WandbLogger() # newline 2
trainer = Trainer(logger=wandb_logger)
if wandb.config["lr_scheduler"]:
scheduler = MultiStepLR(
opt, milestones=list(range(0, wandb.config["epochs"], 100))[1:], gamma=0.5
)
else:
scheduler = None
# opt = torch.optim.RMSprop(model.parameters(), lr=0.001)
if wandb.config["class_weight_type"]:
loss = torch.nn.CrossEntropyLoss(weight=class_weights)
print(class_weights.shape)
else:
loss = torch.nn.CrossEntropyLoss()
train(
model,
loss,
opt,
train_loader,
test_loader,
n_epochs=wandb.config["epochs"],
scheduler=scheduler,
)
torch.save(
model,
f"/gpfs/milgram/project/rtaylor/shared/ABDPain_EarlyDiags/models/{WANDB_RUN_NAME}.model",
)
# y_pred = model(test_features)
# top_k_accuracy_score(y_test, y_pred, labels=le.classes_, k=10)
except Exception as e:
pass
finally:
wandb.finish()