File size: 12,139 Bytes
87da3f2 | 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 | from typing import Any, List, Dict
from torchmetrics import MetricCollection
import wandb
import pytorch_lightning as pl
import torch
from models.metrics import get_cls_pred_metrics, get_cls_prob_metrics, get_reg_metrics
import numpy as np
import shap
import pandas as pd
def get_model_framework_dict():
model_framework = {
"elastic_net": "stand_alone",
"logistic_regression": "stand_alone",
"svm": "stand_alone",
"xgboost": "stand_alone",
"catboost": "stand_alone",
"lightgbm": "stand_alone",
"widedeep_tab_mlp": "pytorch",
"widedeep_tab_resnet": "pytorch",
"widedeep_tab_net": "pytorch",
"widedeep_tab_transformer": "pytorch",
"widedeep_ft_transformer": "pytorch",
"widedeep_saint": "pytorch",
"widedeep_tab_fastformer": "pytorch",
"widedeep_tab_perceiver": "pytorch",
"pytorch_tabular_autoint": "pytorch",
"pytorch_tabular_tabnet": "pytorch",
"pytorch_tabular_node": "pytorch",
"pytorch_tabular_category_embedding": "pytorch",
"pytorch_tabular_ft_transformer": "pytorch",
"pytorch_tabular_tab_transformer": "pytorch",
"nbm_spam_spam": "pytorch",
"nbm_spam_nam": "pytorch",
"nbm_spam_nbm": "pytorch",
"arm_net_models": "pytorch",
"danet": "pytorch",
"nam": "pytorch",
"stg": "pytorch",
"coxnet": "stand_alone"
}
return model_framework
class BaseModel(pl.LightningModule):
def __init__(self, **kwargs):
super().__init__()
self.save_hyperparameters(logger=False)
self.produce_probabilities = False
self.produce_importance = False
if self.hparams.task == "classification":
self.loss_fn = torch.nn.CrossEntropyLoss(reduction='mean')
if self.hparams.output_dim < 2:
raise ValueError(f"Classification with {self.hparams.output_dim} classes")
self.metrics = get_cls_pred_metrics(self.hparams.output_dim)
self.metrics = {f'{k}_pl': v for k, v in self.metrics.items()}
self.metrics_dict = {k:v[0] for k,v in self.metrics.items()}
self.metrics_prob = get_cls_prob_metrics(self.hparams.output_dim)
self.metrics_prob = {f'{k}_pl': v for k, v in self.metrics_prob.items()}
self.metrics_prob_dict = {k:v[0] for k,v in self.metrics_prob.items()}
elif self.hparams.task == "regression":
if self.hparams.loss_type == "MSE":
self.loss_fn = torch.nn.MSELoss(reduction='mean')
elif self.hparams.loss_type == "L1Loss":
self.loss_fn = torch.nn.L1Loss(reduction='mean')
else:
raise ValueError("Unsupported loss_type")
self.metrics = get_reg_metrics()
self.metrics = {f'{k}_pl': v for k, v in self.metrics.items()}
self.metrics_dict = {k: v[0] for k, v in self.metrics.items()}
self.metrics_prob_dict = {}
self.metrics_trn = MetricCollection(self.metrics_dict)
self.metrics_trn_prob = MetricCollection(self.metrics_prob_dict)
self.metrics_val = self.metrics_trn.clone()
self.metrics_val_prob = self.metrics_trn_prob.clone()
self.metrics_tst = self.metrics_trn.clone()
self.metrics_tst_prob = self.metrics_trn_prob.clone()
def on_train_start(self):
# by default lightning executes validation step sanity checks before training starts,
# so we need to make sure all MaxMetric doesn't store accuracy from these checks
# self.max_metric.reset()
pass
def on_fit_start(self) -> None:
if wandb.run is not None:
for stage_type in ['trn', 'val', 'tst']:
for m in self.metrics:
wandb.define_metric(f"{stage_type}/{m}", summary=self.metrics[m][1])
if self.hparams.task == "classification":
for m in self.metrics_prob:
wandb.define_metric(f"{stage_type}/{m}", summary=self.metrics_prob[m][1])
wandb.define_metric(f"{stage_type}/loss", summary='min')
def calc_out_and_loss(self, out, y, stage):
loss = self.loss_fn(out, y)
return out, loss
def forward(self, batch):
pass
def forward_train(self, batch):
return self.forward(batch)
def forward_eval(self, batch):
return self.forward(batch)
def step(self, batch: Dict, stage:str):
y = batch["target"]
batch_size = y.size(0)
if self.hparams.task == "regression":
y = y.view(batch_size, -1)
if stage == "trn":
out = self.forward_train(batch)
else:
out = self.forward_eval(batch)
out, loss = self.calc_out_and_loss(out, y, stage)
logs = {"loss": loss}
non_logs = {}
if self.hparams.task == "classification":
probs = torch.softmax(out, dim=1)
preds = torch.argmax(out, dim=1)
non_logs["preds"] = preds
non_logs["targets"] = y
if stage == "trn":
logs.update(self.metrics_trn(preds, y))
try:
logs.update(self.metrics_trn_prob(probs, y))
except ValueError:
pass
elif stage == "val":
logs.update(self.metrics_val(preds, y))
try:
logs.update(self.metrics_val_prob(probs, y))
except ValueError:
pass
elif stage == "tst":
logs.update(self.metrics_tst(preds, y))
try:
logs.update(self.metrics_tst_prob(probs, y))
except ValueError:
pass
elif self.hparams.task == "regression":
if stage == "trn":
logs.update(self.metrics_trn(out, y))
elif stage == "val":
logs.update(self.metrics_val(out, y))
elif stage == "tst":
logs.update(self.metrics_tst(out, y))
return loss, logs, non_logs
def training_step(self, batch: Dict, batch_idx: int):
loss, logs, non_logs = self.step(batch, "trn")
d = {f"trn/{k}": v for k, v in logs.items()}
self.log_dict(d, on_step=False, on_epoch=True, logger=True)
logs.update(non_logs)
return logs
def training_epoch_end(self, outputs: List[Any]):
pass
def validation_step(self, batch: Dict, batch_idx: int):
loss, logs, non_logs = self.step(batch, "val")
d = {f"val/{k}": v for k, v in logs.items()}
self.log_dict(d, on_step=False, on_epoch=True, logger=True)
logs.update(non_logs)
return logs
def validation_epoch_end(self, outputs: List[Any]):
pass
def test_step(self, batch: Dict, batch_idx: int):
loss, logs, non_logs = self.step(batch, "tst")
d = {f"tst/{k}": v for k, v in logs.items()}
self.log_dict(d, on_step=False, on_epoch=True, logger=True)
logs.update(non_logs)
return logs
def test_epoch_end(self, outputs: List[Any]):
pass
def predict_step(self, batch: Dict, batch_idx):
out = self.forward(batch)
return out
def on_epoch_end(self):
for m in self.metrics_dict:
self.metrics_trn[m].reset()
self.metrics_val[m].reset()
self.metrics_tst[m].reset()
for m in self.metrics_prob_dict:
self.metrics_trn_prob[m].reset()
self.metrics_val_prob[m].reset()
self.metrics_tst_prob[m].reset()
def get_feature_importance(self, data, features, method="shap_kernel"):
if method.startswith("shap"):
if self.hparams.task == "regression":
def predict_func(X):
batch = {
'all': torch.from_numpy(np.float32(X[:, features['all_ids']])),
'continuous': torch.from_numpy(np.float32(X[:, features['con_ids']])),
'categorical': torch.from_numpy(np.int32(X[:, features['cat_ids']])),
}
tmp = self.forward(batch)
return tmp.cpu().detach().numpy()
if method == "shap_kernel":
explainer = shap.KernelExplainer(predict_func, data)
shap_values = explainer.shap_values(data)
if isinstance(shap_values, list):
shap_values = shap_values[0]
if method == "shap_sampling":
explainer = shap.SamplingExplainer(predict_func, data)
shap_values = explainer.shap_values(data)
if isinstance(shap_values, list):
shap_values = shap_values[0]
elif method == "shap_deep":
explainer = shap.DeepExplainer(self, torch.from_numpy(data))
shap_values = explainer.shap_values(torch.from_numpy(data))
else:
raise ValueError(f"Unsupported feature importance method: {method}")
importance_values = np.mean(np.abs(shap_values), axis=0)
elif self.hparams.task == "classification":
def predict_func(X):
self.produce_probabilities = True
batch = {
'all': torch.from_numpy(np.float32(X[:, features['all_ids']])),
'continuous': torch.from_numpy(np.float32(X[:, features['con_ids']])),
'categorical': torch.from_numpy(np.int32(X[:, features['cat_ids']])),
}
tmp = self.forward(batch)
return tmp.cpu().detach().numpy()
if method == "shap_kernel":
explainer = shap.KernelExplainer(predict_func, data)
shap_values = explainer.shap_values(data)
if method == "shap_sampling":
explainer = shap.SamplingExplainer(predict_func, data)
shap_values = explainer.shap_values(data)
elif method == "shap_deep":
explainer = shap.DeepExplainer(self, torch.from_numpy(data))
shap_values = explainer.shap_values(torch.from_numpy(data))
else:
raise ValueError(f"Unsupported feature importance method: {method}")
importance_values = np.zeros(len(features['all']))
for cl_id in range(len(shap_values)):
importance_values += np.mean(np.abs(shap_values[cl_id]), axis=0)
else:
raise ValueError("Unsupported task")
elif method == "none":
importance_values = np.zeros(len(features['all']))
else:
raise ValueError(f"Unsupported feature importance method: {method}")
feature_importances = pd.DataFrame.from_dict(
{
'feature': features['all'],
'importance': importance_values
}
)
return feature_importances
def configure_optimizers(self):
"""Choose what optimizers and learning-rate schedulers to use in your optimization.
Normally you'd need one. But in the case of GANs or similar you might have multiple.
See examples here:
https://pytorch-lightning.readthedocs.io/en/latest/common/lightning_module.html#configure-optimizers
"""
optimizer = torch.optim.Adam(
params=self.parameters(),
lr=self.hparams.optimizer_lr,
weight_decay=self.hparams.optimizer_weight_decay
)
scheduler = torch.optim.lr_scheduler.StepLR(
optimizer=optimizer,
step_size=self.hparams.scheduler_step_size,
gamma=self.hparams.scheduler_gamma
)
return (
{
"optimizer": optimizer,
"lr_scheduler": scheduler
}
)
|