Building and testing the model without Monte Carlo Dropout Method
Step 1: Import the necessary libraries
Python
import numpy as np import pandas as pd import tensorflow as tf from tensorflow import keras from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split |
Step 2: Load and split the Dataset
Python
data = load_iris() df = pd.DataFrame(data.data, columns = data.feature_names) df[ 'target' ] = data.target X = df.iloc[:, : - 1 ] y = df[ 'target' ] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2 , random_state = 42 ) |
Step 3: Create a neural network with dropout layers
Python
model = keras.Sequential([ keras.layers. Input (shape = ( 4 ,)), keras.layers.Dense( 64 , activation = 'relu' ), keras.layers.Dropout( 0.5 ), keras.layers.Dense( 32 , activation = 'relu' ), keras.layers.Dropout( 0.5 ), keras.layers.Dense( 3 , activation = 'softmax' ) ]) # Compile the model model. compile (optimizer = 'adam' , loss = 'sparse_categorical_crossentropy' , metrics = [ 'accuracy' ]) model.fit(X_train, y_train, epochs = 50 , verbose = 0 ) # training the model |
Step 4:Evaluate the model
Python
standard_accuracy = model.evaluate(X_test, y_test, verbose = 0 )[ 1 ] print (f "Standard Model Accuracy: {standard_accuracy}" ) |
Output:
Standard Model Accuracy: 0.7666666507720947
What is Monte Carlo (MC) dropout?
Monte Carlo Dropout was introduced in a 2016 research paper by Yarin Gal and Zoubin Ghahramani, is a technique that combines two powerful concepts in machine learning: Monte Carlo methods and dropout regularization. This innovation can be thought of as an upgrade to traditional dropout, offering the potential for significantly more accurate predictions. It is done is at time of testing . In this article, we’ll delve into the concepts and workings of Monte Carlo Dropout.
Contact Us