From f7fba93d517f4a10948208c9290c8856b9c2615b Mon Sep 17 00:00:00 2001 From: Carl Schmitz Date: Tue, 25 Feb 2025 08:51:10 +1000 Subject: [PATCH] Added info about loading/importing model --- CRISPR_DeepEnsemble.ipynb | 486 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 472 insertions(+), 14 deletions(-) diff --git a/CRISPR_DeepEnsemble.ipynb b/CRISPR_DeepEnsemble.ipynb index c481f83..ab222b2 100644 --- a/CRISPR_DeepEnsemble.ipynb +++ b/CRISPR_DeepEnsemble.ipynb @@ -9,15 +9,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Import and set defaults\n", "import CRISPR_DeepEnsemble\n", "import torch as t\n", - "import numpy as np\n", - "import pandas as pd\n", "from scipy.stats import spearmanr, pearsonr \n", "\n", "t.manual_seed(123)\n", @@ -39,7 +37,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -55,7 +53,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -88,9 +86,409 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/home/carl/.local/lib/python3.12/site-packages/torch/nn/modules/lazy.py:181: UserWarning: Lazy modules are a new feature under heavy development so changes to the API or functionality can happen at any moment.\n", + " warnings.warn('Lazy modules are a new feature under heavy development '\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Training Model 1/25 for 50 epochs...\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + " 0%| | 0/50 [00:00" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], "source": [ "# Example uncertainty plot\n", "L,U,IQR = myEnsemble.uncertainty_bounds(inputs =(S_test, F_test), n_samples=1000,\n", @@ -124,21 +533,70 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Spearman: 0.843, Pearson: 0.839\n" + ] + } + ], "source": [ "# Compute Spearman correlation between predictions and true answers in test set\n", "vals = (pred.numpy(), y_test)\n", "print(f\"Spearman: {round(spearmanr(*vals)[0],3)}, Pearson: {round(pearsonr(*vals)[0],3)}\")" ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "# Save model\n", + "myEnsemble.save('CRISPR_DeepEnsemble')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Spearman: 0.843, Pearson: 0.839\n" + ] + } + ], + "source": [ + "# Load model\n", + "# NOTE: If you are getting import errors when loading the model uncomment the two lines below\n", + "# import sys\n", + "# sys.modules['CRISPR_DeepEnsemble'] = CRISPR_DeepEnsemble\n", + "\n", + "newEnsemble = CRISPR_DeepEnsemble.RegressionDeepEnsemble(load_from='CRISPR_DeepEnsemble.zip')\n", + "\n", + "# Predict values\n", + "L,U,IQR = newEnsemble.uncertainty_bounds(inputs =(S_test, F_test), n_samples=1000,\n", + " lower=0.01, upper=0.99) \n", + "loaded_pred = newEnsemble.predict(inputs = (S_test, F_test))\n", + "\n", + "# Compute Spearman correlation between predictions and true answers in test set\n", + "vals = (loaded_pred.numpy(), y_test)\n", + "print(f\"Spearman: {round(spearmanr(*vals)[0],3)}, Pearson: {round(pearsonr(*vals)[0],3)}\")" + ] } ], "metadata": { "kernelspec": { - "display_name": "jupyterHub", + "display_name": "Python 3", "language": "python", - "name": "jupyterhub" + "name": "python3" }, "language_info": { "codemirror_mode": { @@ -150,7 +608,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.10" + "version": "3.12.1" } }, "nbformat": 4,