How to Build a Deep Neural Network Model Using Keras in Python?
Share
Condition for Building a Deep Neural Network Model Using Keras in Python
Description: This ANN regression model predicts the weight of students based on input features such as height and other relevant variables. It consists of a sequential architecture with four hidden layers, employing ReLU activation for non-linearity and a linear activation in the output layer for continuous value prediction. The model is optimized using the Adam optimizer and evaluates performance with Mean Squared Error (MSE).
Step-by-Step Process
Step1: Load essential libraries like pandas, scikit-learn, TensorFlow, and Matplotlib for data processing, model building, and visualization.
Step2: Import the BMI dataset containing features like height, weight, and BMI class.
Step3: Identify missing or NaN values in the dataset using .isnull() and .isna() methods.
Step4: Convert the categorical column BmiClass into numeric values using LabelEncoder.
Step5: Separate the dataset into independent features (e.g., height) and the target variable (BmiClass).
Step6: Normalize the features using StandardScaler to ensure uniform scaling for model training.
Step7: Split the dataset into training and testing sets using an 80-20 ratio.
Step8: Define a regression ANN with four hidden layers using ReLU activation and compile it with MSE as the loss function.
Step9: Train the model on the training set for 10 epochs with a batch size of 2, validating on the test set.
Step10: Generate predictions on the test set, compute metrics like MSE, RMSE, MAE, and R², and analyze residuals via a histogram.
Sample Code
#Import Necessary Libraries
import pandas as pd
from sklearn.preprocessing import LabelEncoder, StandardScaler
from sklearn.model_selection import train_test_split
from tensorflow.keras.layers import Input, Dense, Dropout
from tensorflow.keras.models import Model
import matplotlib.pyplot as plt
df = pd.read_csv("/home/soft12/Downloads/sample_dataset/Website/Dataset/NHANES Weight and Height.csv")
#Checking Missing Values
print("Checking Missing Values\n")
print(df.isnull().sum())
#Checking Nan Values
print('\n')
print("Checking Nan Values\n")
print(df.isna().sum())
#Split Dependent and Independent Variables
x = df.drop('Weight (kg)', axis=1)
y = df['Weight (kg)']
#Scaling the data
scaler = StandardScaler()
x = scaler.fit_transform(x)
#Split the data for training and testing
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=.2, random_state=42)
def ANN_model_regression(input_shape):
# Input layer
inputs = Input(shape=(input_shape,))
# Hidden layers
layer1 = Dense(64, activation='relu')(inputs)
layer2 = Dense(32, activation='relu')(layer1)
# Output layer
output_layer = Dense(1, activation='linear')(layer2)
# Build the model
ann_model = Model(inputs=inputs, outputs=output_layer)
# Compile the model with Adam optimizer and regression loss function
ann_model.compile(optimizer='adam', loss='mse', metrics=['mean_squared_error'])
return ann_model
model = ANN_model_regression(X_train.shape[1])
#Summary of Model
model.summary()
model.fit(X_train, y_train, batch_size=2, epochs=10, validation_data=(X_test, y_test))
y_pred = model.predict(X_test)
y_pred = y_pred.ravel()
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
# Assuming y_test and y_pred are the actual and predicted values
print("___Performance_Metrics___\n")
print('Mean Squared Error (MSE): ', mean_squared_error(y_test, y_pred))
print('Root Mean Squared Error (RMSE):', mean_squared_error(y_test, y_pred, squared=False)) # RMSE
print('Mean Absolute Error (MAE): ', mean_absolute_error(y_test, y_pred))
print('R-squared (R2 Score): ', r2_score(y_test, y_pred))
#Inspect Residuals (difference between predicted and actual values)
y_pred = y_pred.ravel()
residuals = y_test - y_pred
plt.hist(residuals, bins=30)
plt.title('Residuals Histogram')
plt.show()