File size: 7,902 Bytes
3957f36 |
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 |
from lightning.pytorch import seed_everything
from lightning.pytorch.callbacks import ModelCheckpoint
from lightning.pytorch.callbacks.early_stopping import EarlyStopping
import lightning.pytorch as pl
from pytorch_lightning.loggers import TensorBoardLogger
import pandas as pd
from sklearn.model_selection import train_test_split
from transformers import AutoTokenizer
from ast import literal_eval
# imports from our own modules
import config
from model import (
BERTContrastiveLearning_simcse,
BERTContrastiveLearning_simcse_w,
BERTContrastiveLearning_samp,
BERTContrastiveLearning_samp_w,
)
from dataset import (
ContrastiveLearningDataModule_simcse,
ContrastiveLearningDataModule_simcse_w,
ContrastiveLearningDataModule_samp,
ContrastiveLearningDataModule_samp_w,
)
if __name__ == "__main__":
seed_everything(0, workers=True)
# Initialize tensorboard logger
logger = TensorBoardLogger("logs", name="MIMIC-tr")
query_df = pd.read_csv(
"/home/sunx/data/aiiih/projects/sunx/ccf_fuzzy_diag/data_proc/mimic_data/processed_train/processed.csv"
)
# query_df = query_df.head(1000)
query_df["concepts"] = query_df["concepts"].apply(literal_eval)
query_df["codes"] = query_df["codes"].apply(literal_eval)
query_df["codes"] = query_df["codes"].apply(
lambda x: [val for val in x if val is not None]
) # remove None in lists
query_df = query_df.drop(columns=["one_hot"])
train_df, val_df = train_test_split(query_df, test_size=config.split_ratio)
tokenizer = AutoTokenizer.from_pretrained("emilyalsentzer/Bio_ClinicalBERT")
sim_df = pd.read_csv(
"/home/sunx/data/aiiih/projects/sunx/ccf_fuzzy_diag/data_proc/pairwise_scores.csv"
)
all_d = pd.read_csv(
"/home/sunx/data/aiiih/projects/sunx/ccf_fuzzy_diag/data_proc/all_d_full.csv"
)
all_d["synonyms"] = all_d["synonyms"].apply(literal_eval)
all_d["ancestors"] = all_d["ancestors"].apply(literal_eval)
dictionary = dict(zip(all_d["concept"], all_d["synonyms"]))
# SimCSE
data_module1 = ContrastiveLearningDataModule_simcse(
train_df,
val_df,
tokenizer,
)
data_module1.setup()
print("Number of training data:", len(data_module1.train_dataset))
print("Number of validation data:", len(data_module1.val_dataset))
model1 = BERTContrastiveLearning_simcse(
n_batches=len(data_module1.train_dataset) / config.batch_size,
n_epochs=config.max_epochs,
lr=config.learning_rate,
unfreeze=config.unfreeze_ratio,
)
checkpoint1 = ModelCheckpoint(
dirpath="/home/sunx/data/aiiih/projects/sunx/ccf_fuzzy_diag/train/ckpt/simcse/v1",
filename="{epoch}-{step}",
# save_weights_only=True,
save_last=True,
every_n_train_steps=config.log_every_n_steps,
monitor=None,
save_top_k=-1,
)
trainer1 = pl.Trainer(
accelerator=config.accelerator,
devices=config.devices,
strategy="ddp",
logger=logger,
max_epochs=config.max_epochs,
min_epochs=config.min_epochs,
precision=config.precision,
callbacks=[
EarlyStopping(
monitor="validation_loss", min_delta=1e-3, patience=3, mode="min"
),
checkpoint1,
],
profiler="simple",
log_every_n_steps=config.log_every_n_steps,
)
trainer1.fit(model1, data_module1)
# SimCSE_w
data_module2 = ContrastiveLearningDataModule_simcse_w(
train_df,
val_df,
query_df,
tokenizer,
sim_df,
all_d,
)
data_module2.setup()
print("Number of training data:", len(data_module2.train_dataset))
print("Number of validation data:", len(data_module2.val_dataset))
model2 = BERTContrastiveLearning_simcse_w(
n_batches=len(data_module2.train_dataset) / config.batch_size,
n_epochs=config.max_epochs,
lr=config.learning_rate,
unfreeze=config.unfreeze_ratio,
)
checkpoint2 = ModelCheckpoint(
dirpath="/home/sunx/data/aiiih/projects/sunx/ccf_fuzzy_diag/train/ckpt/simcse_w/v1",
filename="{epoch}-{step}",
# save_weights_only=True,
save_last=True,
every_n_train_steps=config.log_every_n_steps,
monitor=None,
save_top_k=-1,
)
trainer2 = pl.Trainer(
accelerator=config.accelerator,
devices=config.devices,
strategy="ddp",
logger=logger,
max_epochs=config.max_epochs,
min_epochs=config.min_epochs,
precision=config.precision,
callbacks=[
EarlyStopping(
monitor="validation_loss", min_delta=1e-3, patience=3, mode="min"
),
checkpoint2,
],
profiler="simple",
log_every_n_steps=config.log_every_n_steps,
)
trainer2.fit(model2, data_module2)
# Samp
data_module3 = ContrastiveLearningDataModule_samp(
train_df,
val_df,
query_df,
tokenizer,
dictionary,
sim_df,
)
data_module3.setup()
print("Number of training data:", len(data_module3.train_dataset))
print("Number of validation data:", len(data_module3.val_dataset))
model3 = BERTContrastiveLearning_samp(
n_batches=len(data_module3.train_dataset) / config.batch_size,
n_epochs=config.max_epochs,
lr=config.learning_rate,
unfreeze=config.unfreeze_ratio,
)
checkpoint3 = ModelCheckpoint(
dirpath="/home/sunx/data/aiiih/projects/sunx/ccf_fuzzy_diag/train/ckpt/samp/v1",
filename="{epoch}-{step}",
# save_weights_only=True,
save_last=True,
every_n_train_steps=config.log_every_n_steps,
monitor=None,
save_top_k=-1,
)
trainer3 = pl.Trainer(
accelerator=config.accelerator,
devices=config.devices,
strategy="ddp",
logger=logger,
max_epochs=config.max_epochs,
min_epochs=config.min_epochs,
precision=config.precision,
callbacks=[
EarlyStopping(
monitor="validation_loss", min_delta=1e-3, patience=3, mode="min"
),
checkpoint3,
],
profiler="simple",
log_every_n_steps=config.log_every_n_steps,
)
trainer3.fit(model3, data_module3)
# Samp_w
data_module4 = ContrastiveLearningDataModule_samp_w(
train_df,
val_df,
query_df,
tokenizer,
dictionary,
sim_df,
all_d,
)
data_module4.setup()
print("Number of training data:", len(data_module4.train_dataset))
print("Number of validation data:", len(data_module4.val_dataset))
model4 = BERTContrastiveLearning_samp_w(
n_batches=len(data_module4.train_dataset) / config.batch_size,
n_epochs=config.max_epochs,
lr=config.learning_rate,
unfreeze=config.unfreeze_ratio,
)
checkpoint4 = ModelCheckpoint(
dirpath="/home/sunx/data/aiiih/projects/sunx/ccf_fuzzy_diag/train/ckpt/samp_w/v1",
filename="{epoch}-{step}",
# save_weights_only=True,
save_last=True,
every_n_train_steps=config.log_every_n_steps,
monitor=None,
save_top_k=-1,
)
trainer4 = pl.Trainer(
accelerator=config.accelerator,
devices=config.devices,
strategy="ddp",
logger=logger,
max_epochs=config.max_epochs,
min_epochs=config.min_epochs,
precision=config.precision,
callbacks=[
EarlyStopping(
monitor="validation_loss", min_delta=1e-3, patience=3, mode="min"
),
checkpoint4,
],
profiler="simple",
log_every_n_steps=config.log_every_n_steps,
)
trainer4.fit(model4, data_module4)
|