{ "cells": [ { "cell_type": "markdown", "id": "71a03ed6", "metadata": {}, "source": [ "# Uncertainty with Bayesian DJINN\n", "\n", "With dropout left on at prediction time, each pass through a network gives a\n", "slightly different answer. Running many passes through every network in the\n", "ensemble gives a spread of predictions for each input, which can be used as an\n", "estimate of the model's uncertainty." ] }, { "cell_type": "code", "execution_count": 1, "id": "ba11302b", "metadata": { "execution": { "iopub.execute_input": "2026-10-25T03:49:12.201523Z", "iopub.status.busy": "2026-10-25T03:49:12.201409Z", "iopub.status.idle": "2026-10-25T03:49:14.439501Z", "shell.execute_reply": "2026-10-25T03:49:14.438762Z" } }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "from sklearn.datasets import fetch_california_housing\n", "from sklearn.model_selection import train_test_split\n", "\n", "from djinn import DJINN_Regressor" ] }, { "cell_type": "code", "execution_count": 2, "id": "dee9dfc2", "metadata": { "execution": { "iopub.execute_input": "2026-10-25T03:49:14.442013Z", "iopub.status.busy": "2026-10-25T03:49:14.441797Z", "iopub.status.idle": "2026-10-25T03:49:14.455175Z", "shell.execute_reply": "2026-10-25T03:49:14.454658Z" } }, "outputs": [ { "data": { "text/plain": [ "((1800, 8), (600, 8), (600, 8))" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X, y = fetch_california_housing(return_X_y=True)\n", "rng = np.random.default_rng(0)\n", "idx = rng.choice(len(y), 3000, replace=False)\n", "X_train, X_test, y_train, y_test = train_test_split(\n", " X[idx], y[idx], test_size=0.2, random_state=0\n", ")\n", "X_fit, X_cal, y_fit, y_cal = train_test_split(\n", " X_train, y_train, test_size=0.25, random_state=0\n", ")\n", "X_fit.shape, X_cal.shape, X_test.shape" ] }, { "cell_type": "markdown", "id": "84e9a0ee", "metadata": {}, "source": [ "A quarter of the training data is held back as a calibration set. The model\n", "never sees it during training. It's only used at the end, to build intervals\n", "with a known coverage." ] }, { "cell_type": "markdown", "id": "337ec1dc", "metadata": {}, "source": [ "## Fit with dropout\n", "\n", "`dropout_keep_prob` is the chance that a hidden neuron is kept in each pass.\n", "Values around 0.95 with 5 to 10 trees work well. Dropout is applied during\n", "training too, so the networks learn to cope with it." ] }, { "cell_type": "code", "execution_count": 3, "id": "e8e6b5d0", "metadata": { "execution": { "iopub.execute_input": "2026-10-25T03:49:14.456744Z", "iopub.status.busy": "2026-10-25T03:49:14.456621Z", "iopub.status.idle": "2026-10-25T03:50:07.881328Z", "shell.execute_reply": "2026-10-25T03:50:07.880739Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 10min 29s, sys: 546 ms, total: 10min 29s\n", "Wall time: 53.3 s\n" ] }, { "data": { "text/html": [ "
DJINN_Regressor(dropout_keep_prob=0.95, epochs=300, learning_rate=0.005,\n",
" max_tree_depth=6, n_trees=5, random_state=0)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. | \n", " | n_trees | \n", "5 | \n", "
| \n", " | max_tree_depth | \n", "6 | \n", "
| \n", " | dropout_keep_prob | \n", "0.95 | \n", "
| \n", " | epochs | \n", "300 | \n", "
| \n", " | learning_rate | \n", "0.005 | \n", "
| \n", " | batch_size | \n", "None | \n", "
| \n", " | weight_decay | \n", "1e-08 | \n", "
| \n", " | random_state | \n", "0 | \n", "
| \n", " | device | \n", "'cpu' | \n", "
| \n", " | verbose | \n", "0 | \n", "