.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/bayes.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_bayes.py: ========================================== 4.1 Bayesian NN (Disinfection Efficiency) ========================================== This file shows how to record epistemic uncertainty in a neural network for modeling Cell Count data. .. GENERATED FROM PYTHON SOURCE LINES 8-24 .. code-block:: Python import numpy as np from easy_mpl import plot import matplotlib.pyplot as plt from SeqMetrics import RegressionMetrics from ai4water.utils import TrainTestSplit from ai4water.postprocessing import ProcessPredictions from utils import SAVE from utils import read_data, BayesModel, version_info from utils import set_rcParams, residual_plot, regression_plot .. GENERATED FROM PYTHON SOURCE LINES 25-28 .. code-block:: Python for lib, ver in version_info().items(): print(lib, ver) .. rst-class:: sphx-glr-script-out .. code-block:: none python 3.9.20 (main, Nov 5 2024, 16:07:55) [GCC 11.4.0] os posix ai4water 1.07 easy_mpl 0.21.4 SeqMetrics 2.0.0 tensorflow 2.10.1 keras.api._v2.keras 2.10.0 numpy 1.21.6 pandas 1.5.3 matplotlib 3.7.1 h5py 3.13.0 sklearn 1.3.1 seaborn 0.13.2 ngboost 0.4.1 shap 0.41.0 .. GENERATED FROM PYTHON SOURCE LINES 29-31 .. code-block:: Python set_rcParams() .. GENERATED FROM PYTHON SOURCE LINES 32-45 .. code-block:: Python data = read_data() input_features = data.columns.tolist()[0:-1] output_features = data.columns.tolist()[-1:] TrainX, TestX, TrainY, TestY = TrainTestSplit(seed=313).split_by_random( data[input_features], data[output_features] ) print(TrainX.shape, TestX.shape, TrainY.shape, TestY.shape) .. rst-class:: sphx-glr-script-out .. code-block:: none (219, 6) (95, 6) (219, 1) (95, 1) .. GENERATED FROM PYTHON SOURCE LINES 46-48 hyperparameters ---------------- .. GENERATED FROM PYTHON SOURCE LINES 48-57 .. code-block:: Python hidden_units = [5, 5] learning_rate = 0.00472268229046 activation = "elu" train_size = len(TrainX) num_epochs = 5000 batch_size = 32 .. GENERATED FROM PYTHON SOURCE LINES 58-60 Build model ------------- .. GENERATED FROM PYTHON SOURCE LINES 60-85 .. code-block:: Python model = BayesModel( model = {"layers": dict(hidden_units=hidden_units, train_size=train_size, activation=activation )}, batch_size=batch_size, epochs=num_epochs, lr=learning_rate, input_features=input_features, output_features=output_features, category= "DL", y_transformation="robust", optimizer="RMSprop", x_transformation=[ {"method": "log2", "features": ["Time (min)"], "replace_zeros": True}, {"method": "quantile", "features": ["Ini. CC"]}, #{"method": "log2", "features": ["sonic_pd"]}, {"method": "quantile", "features": ["h20 Conc."]}, {"method": "quantile", "features": ["Volume (mL)"]}, {"method": "log10", "features": ["Solution pH"]}, ] #wandb_config=dict(project="flowcam", entity="atherabbas", monitor="val_loss") ) .. rst-class:: sphx-glr-script-out .. code-block:: none building DL model for regression problem using layers Model: "model" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= input_1 (InputLayer) [(None, 6)] 0 batch_normalization (BatchN (None, 6) 24 ormalization) dense_variational (DenseVar (None, 5) 665 iational) dense_variational_1 (DenseV (None, 5) 495 ariational) dense (Dense) (None, 1) 6 ================================================================= Total params: 1,190 Trainable params: 1,178 Non-trainable params: 12 _________________________________________________________________ dot plot of model could not be plotted due to You must install pydot (`pip install pydot`) and install graphviz (see instructions at https://graphviz.gitlab.io/download/) for plot_model to work. .. GENERATED FROM PYTHON SOURCE LINES 86-87 model training .. GENERATED FROM PYTHON SOURCE LINES 87-90 .. code-block:: Python model.fit(TrainX, TrainY, validation_data=(TestX, TestY), verbose=0) .. image-sg:: /auto_examples/images/sphx_glr_bayes_001.png :alt: bayes :srcset: /auto_examples/images/sphx_glr_bayes_001.png, /auto_examples/images/sphx_glr_bayes_001_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none ********** Successfully loaded weights from weights_305_0.49733.hdf5 file ********** .. GENERATED FROM PYTHON SOURCE LINES 91-93 training results ------------- .. GENERATED FROM PYTHON SOURCE LINES 93-107 .. code-block:: Python train_predictions = [] for i in range(100): train_predictions.append(model.predict(TrainX, verbose=0)) train_predictions = np.concatenate(train_predictions, axis=1) train_std = np.std(train_predictions, axis=1) train_mean = np.mean(train_predictions, axis=1) metrics = RegressionMetrics(TrainY, train_mean) print(f"R2: {metrics.r2()}") print(f"R2 Score: {metrics.r2_score()}") print(f"RMSE Score: {metrics.rmse()}") print(f"MAE: {metrics.mae()}") .. rst-class:: sphx-glr-script-out .. code-block:: none R2: 0.7626992844691637 R2 Score: 0.7598204574089047 RMSE Score: 10.599818849806407 MAE: 7.110052656580932 .. GENERATED FROM PYTHON SOURCE LINES 108-113 .. code-block:: Python processor = ProcessPredictions( mode="regression", forecast_len=1, path=model.path ) .. GENERATED FROM PYTHON SOURCE LINES 114-116 .. code-block:: Python processor.edf_plot(TrainY, train_mean) .. image-sg:: /auto_examples/images/sphx_glr_bayes_002.png :alt: bayes :srcset: /auto_examples/images/sphx_glr_bayes_002.png, /auto_examples/images/sphx_glr_bayes_002_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [, ] .. GENERATED FROM PYTHON SOURCE LINES 117-122 .. code-block:: Python plot(train_mean, '.', label="Prediction Mean", show=False) plot(TrainY.values, '.', label="True", ax_kws=dict(logy=True)) .. image-sg:: /auto_examples/images/sphx_glr_bayes_003.png :alt: bayes :srcset: /auto_examples/images/sphx_glr_bayes_003.png, /auto_examples/images/sphx_glr_bayes_003_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 123-125 test results ------------- .. GENERATED FROM PYTHON SOURCE LINES 125-134 .. code-block:: Python test_predictions = [] for i in range(100): test_predictions.append(model.predict(TestX, verbose=0)) test_predictions = np.concatenate(test_predictions, axis=1) print(test_predictions.shape) .. rst-class:: sphx-glr-script-out .. code-block:: none (95, 100) .. GENERATED FROM PYTHON SOURCE LINES 135-145 .. code-block:: Python test_std = np.std(test_predictions, axis=1) test_mean = np.mean(test_predictions, axis=1) metrics = RegressionMetrics(TestY, test_mean) print(f"R2: {metrics.r2()}") print(f"R2 Score: {metrics.r2_score()}") print(f"RMSE Score: {metrics.rmse()}") print(f"MAE: {metrics.mae()}") .. rst-class:: sphx-glr-script-out .. code-block:: none R2: 0.5253671948580712 R2 Score: 0.5168349412606081 RMSE Score: 12.59022948084163 MAE: 7.764020284664509 .. GENERATED FROM PYTHON SOURCE LINES 146-148 .. code-block:: Python processor.edf_plot(TestY, test_mean) .. image-sg:: /auto_examples/images/sphx_glr_bayes_004.png :alt: bayes :srcset: /auto_examples/images/sphx_glr_bayes_004.png, /auto_examples/images/sphx_glr_bayes_004_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [, ] .. GENERATED FROM PYTHON SOURCE LINES 149-153 .. code-block:: Python if model.use_wb: model.wb_finish() .. GENERATED FROM PYTHON SOURCE LINES 154-166 .. code-block:: Python residual_plot( TrainY.values, train_mean, TestY.values, test_mean, #label="Cell Count" ) if SAVE: plt.savefig("results/figures/residue_bayes_eff", dpi=600, bbox_inches="tight") plt.show() .. image-sg:: /auto_examples/images/sphx_glr_bayes_005.png :alt: bayes :srcset: /auto_examples/images/sphx_glr_bayes_005.png, /auto_examples/images/sphx_glr_bayes_005_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 167-179 .. code-block:: Python ax = regression_plot( TrainY.values, train_mean, TestY.values, test_mean, max_ticks=None, label="Efficiency (%)" ) ax.set_xlim([-2, 100]) ax.set_ylim([-2, 100]) if SAVE: plt.savefig("results/figures/reg_bayes_eff", dpi=600, bbox_inches="tight") plt.show() .. image-sg:: /auto_examples/images/sphx_glr_bayes_006.png :alt: bayes :srcset: /auto_examples/images/sphx_glr_bayes_006.png, /auto_examples/images/sphx_glr_bayes_006_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none *c* argument looks like a single numeric RGB or RGBA sequence, which should be avoided as value-mapping will have precedence in case its length matches with *x* & *y*. Please use the *color* keyword-argument or provide a 2D array with a single row if you intend to specify the same RGB or RGBA value for all points. .. GENERATED FROM PYTHON SOURCE LINES 180-188 .. code-block:: Python lower = np.min(test_predictions, axis=1) upper = np.max(test_predictions, axis=1) _, ax = plt.subplots(figsize=(6, 3)) ax.fill_between(np.arange(len(lower)), upper, lower, alpha=0.5, color='C1') p1 = ax.plot(test_mean, color="C1", label="Prediction") p2 = ax.fill(np.NaN, np.NaN, color="C1", alpha=0.5) plt.show() .. image-sg:: /auto_examples/images/sphx_glr_bayes_007.png :alt: bayes :srcset: /auto_examples/images/sphx_glr_bayes_007.png, /auto_examples/images/sphx_glr_bayes_007_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 27.051 seconds) .. _sphx_glr_download_auto_examples_bayes.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: bayes.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: bayes.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: bayes.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_