File size: 10,752 Bytes
782d348 |
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 |
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"id": "6db37aa6",
"metadata": {},
"source": [
"# MoL-MoE Foundation Models - Multi Output (K=6)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f13dd822",
"metadata": {},
"outputs": [],
"source": [
"# System\n",
"import warnings\n",
"import sys\n",
"sys.path.insert(1, '../')\n",
"sys.path.insert(2, '../experts')\n",
"sys.path.insert(3, '../moe')\n",
"warnings.filterwarnings(\"ignore\")\n",
"\n",
"# Deep learning\n",
"import torch.nn.functional as F\n",
"import torch\n",
"from torch import nn\n",
"from moe import MoE, train\n",
"from models import Net\n",
"\n",
"# Machine learning\n",
"from xgboost import XGBClassifier\n",
"from sklearn.metrics import roc_auc_score\n",
"\n",
"# Data\n",
"import pandas as pd\n",
"import numpy as np\n",
"\n",
"# Chemistry\n",
"from rdkit import Chem\n",
"from rdkit.Chem import PandasTools\n",
"from rdkit.Chem import Descriptors\n",
"PandasTools.RenderImagesInAllDataFrames(True)\n",
"\n",
"def normalize_smiles(smi, canonical=True, isomeric=False):\n",
" try:\n",
" normalized = Chem.MolToSmiles(\n",
" Chem.MolFromSmiles(smi), canonical=canonical, isomericSmiles=isomeric\n",
" )\n",
" except:\n",
" normalized = None\n",
" return normalized\n",
"\n",
"torch.manual_seed(0)\n",
"DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')"
]
},
{
"cell_type": "markdown",
"id": "5afbebeb",
"metadata": {},
"source": [
"## Load Foundation Models"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e531965e",
"metadata": {},
"outputs": [],
"source": [
"from experts.selfies_ted.load import SELFIES\n",
"\n",
"model_selfies = SELFIES()\n",
"model_selfies.load()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6cee782c",
"metadata": {},
"outputs": [],
"source": [
"from experts.mhg_model.load import load\n",
"\n",
"mhg_gnn = load()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "93e1dbfa",
"metadata": {},
"outputs": [],
"source": [
"from experts.smi_ted_light.load import load_smi_ted, MolTranBertTokenizer\n",
"\n",
"smi_ted = load_smi_ted()"
]
},
{
"cell_type": "markdown",
"id": "91c14dd5",
"metadata": {},
"source": [
"## Load datasets"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d3945b2d",
"metadata": {},
"outputs": [],
"source": [
"train_df = pd.read_csv(\"../data/moleculenet/bbbp/train.csv\")\n",
"valid_df = pd.read_csv(\"../data/moleculenet/bbbp/valid.csv\")\n",
"test_df = pd.read_csv(\"../data/moleculenet/bbbp/test.csv\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "60dbdc78",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"train_df['canon_smiles'] = train_df['smiles'].apply(normalize_smiles)\n",
"train_df = train_df.dropna(subset='canon_smiles')\n",
"print(train_df.shape)\n",
"train_df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a5e69c82",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"valid_df['canon_smiles'] = valid_df['smiles'].apply(normalize_smiles)\n",
"valid_df = valid_df.dropna(subset='canon_smiles')\n",
"print(valid_df.shape)\n",
"valid_df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bbb1cdd5",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"test_df['canon_smiles'] = test_df['smiles'].apply(normalize_smiles)\n",
"test_df = test_df.dropna(subset='canon_smiles')\n",
"print(test_df.shape)\n",
"test_df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b4f1f377",
"metadata": {},
"outputs": [],
"source": [
"smiles_col = 'canon_smiles'\n",
"target = 'p_np'\n",
"\n",
"# training\n",
"X_train = train_df[smiles_col].to_list()\n",
"y_train = train_df[target]\n",
"\n",
"# validation\n",
"X_valid = valid_df[smiles_col].to_list()\n",
"y_valid = valid_df[target]\n",
"\n",
"# test\n",
"X_test = test_df[smiles_col].to_list()\n",
"y_test = test_df[target]"
]
},
{
"cell_type": "markdown",
"id": "09c0c01c",
"metadata": {},
"source": [
"## Training MoE"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cf69fc9b",
"metadata": {},
"outputs": [],
"source": [
"# arguments\n",
"input_size = 768\n",
"output_size = 2048\n",
"num_experts = 12\n",
"k = 6\n",
"batch_size = 16\n",
"learning_rate = 3e-5\n",
"epochs = 100\n",
"\n",
"# experts\n",
"models = [\n",
" smi_ted, smi_ted, smi_ted, smi_ted, # SMI-TED\n",
" model_selfies, model_selfies, model_selfies, model_selfies, # SELFIES-BART\n",
" mhg_gnn, mhg_gnn, mhg_gnn, mhg_gnn # MHG-GNN\n",
"]\n",
"\n",
"# instantiate the MoE layer\n",
"net = Net(smiles_embed_dim=2048, dropout=0.2, output_dim=2)\n",
"tokenizer = MolTranBertTokenizer('../experts/smi_ted_light/bert_vocab_curated.txt')\n",
"moe_model = MoE(input_size, \n",
" output_size, \n",
" num_experts, \n",
" models=models, \n",
" tokenizer=tokenizer, \n",
" tok_emb=smi_ted.encoder.tok_emb, \n",
" k=k, \n",
" noisy_gating=False, \n",
" verbose=False).to(DEVICE)\n",
"\n",
"net.apply(smi_ted._init_weights)\n",
"\n",
"loss_fn = nn.CrossEntropyLoss()\n",
"params = list(moe_model.parameters()) + list(net.parameters())\n",
"optim = torch.optim.AdamW(params, lr=learning_rate)\n",
"\n",
"train_loader = torch.utils.data.DataLoader(list(zip(X_train, y_train)), batch_size=batch_size,\n",
" shuffle=True, num_workers=1)\n",
"\n",
"# train\n",
"moe_model, net = train(train_loader, moe_model, net, loss_fn, optim, epochs)"
]
},
{
"cell_type": "markdown",
"id": "57aa1cc0",
"metadata": {},
"source": [
"## Evaluate (using auxiliary Net)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b9e18da2",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"moe_model.eval()\n",
"net.eval()\n",
"\n",
"with torch.no_grad():\n",
" out, _ = moe_model(X_test, verbose=False)\n",
" preds = net(out)\n",
" preds_cpu = F.softmax(preds, dim=1)[:, 1]\n",
" print('Prediction probabilities:', preds_cpu[:30])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f204d19d",
"metadata": {},
"outputs": [],
"source": [
"roc_auc = roc_auc_score(y_test, preds_cpu.detach().numpy())\n",
"print(f\"ROC-AUC Score: {roc_auc:.4f}\")"
]
},
{
"cell_type": "markdown",
"id": "3af9e51e",
"metadata": {},
"source": [
"# Training XGBoost from MoE"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cb0d9f5a",
"metadata": {},
"outputs": [],
"source": [
"# extract embeddings\n",
"moe_model.eval()\n",
"net.eval()\n",
"\n",
"with torch.no_grad():\n",
" xgb_train, _ = moe_model(X_train, verbose=True)\n",
" xgb_valid, _ = moe_model(X_valid, verbose=True)\n",
" xgb_test, _ = moe_model(X_test, verbose=True)\n",
" \n",
"xgb_train = xgb_train.detach().numpy()\n",
"xgb_valid = xgb_valid.detach().numpy()\n",
"xgb_test = xgb_test.detach().numpy()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a099f2f1",
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"from sklearn.model_selection import train_test_split, RandomizedSearchCV\n",
"from sklearn.metrics import roc_auc_score\n",
"from xgboost import XGBClassifier\n",
"import numpy as np\n",
"\n",
"# Define lists to store ROC-AUC scores and model instances\n",
"roc_auc_scores = []\n",
"\n",
"# Loop over seeds from 0 to 90 in steps of 10\n",
"for seed in range(0, 91, 10):\n",
" # Define XGBoost parameters with different values for each seed\n",
" xgb_params = {\n",
" 'learning_rate': [0.01, 0.4, 0.6, 0.8],\n",
" 'max_depth': [6, 8, 10, 12],\n",
" 'n_estimators': [1500, 2000, 2200]\n",
" }\n",
"\n",
" # Initialize XGBoost classifier\n",
" xgb_classifier = XGBClassifier()\n",
"\n",
" # Perform RandomizedSearchCV to find optimal hyperparameters\n",
" random_search = RandomizedSearchCV(estimator=xgb_classifier, param_distributions=xgb_params, n_iter=10, scoring='roc_auc', cv=3, random_state=seed)\n",
" random_search.fit(xgb_train, y_train)\n",
"\n",
" # Get best estimator and predict probabilities\n",
" best_estimator = random_search.best_estimator_\n",
" y_prob = best_estimator.predict_proba(xgb_test)[:, 1]\n",
"\n",
" # Evaluate ROC-AUC score\n",
" roc_auc = roc_auc_score(y_test, y_prob)\n",
" roc_auc_scores.append(roc_auc)\n",
"\n",
" print(f\"Seed {seed}: ROC-AUC Score: {roc_auc:.4f}\")\n",
"\n",
"# Calculate standard deviation and average ROC-AUC score\n",
"std_dev = np.std(roc_auc_scores)\n",
"avg_roc_auc = np.mean(roc_auc_scores)\n",
"\n",
"# Plot ROC-AUC scores\n",
"plt.figure(figsize=(8, 6))\n",
"plt.errorbar(range(0, 91, 10), roc_auc_scores, yerr=std_dev, fmt='o', color='b')\n",
"plt.hlines(avg_roc_auc, xmin=-1, xmax=91, colors='r', linestyles='dashed', label=f'Average ROC-AUC: {avg_roc_auc:.4f}')\n",
"plt.xlabel('Seed')\n",
"plt.ylabel('ROC-AUC Score')\n",
"plt.title('ROC-AUC Scores with Standard Deviation')\n",
"plt.legend()\n",
"plt.grid(True)\n",
"plt.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.14"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|