Deep Neural Network in R
Deep Neural Networks (DNNs) are among the most powerful machine learning techniques for solving complex prediction and pattern recognition problems. Inspired by the structure of the human brain, neural networks consist of interconnected processing units (neurons) that learn relationships from data through multiple hidden layers.
Deep learning has transformed industries including healthcare, finance, autonomous vehicles, natural language processing, fraud detection, and computer vision by enabling models to learn highly complex patterns automatically.
In this tutorial, you’ll learn how to build a Deep Neural Network in R using Keras, preprocess data, train the model, evaluate performance, and improve prediction accuracy.
What Is a Deep Neural Network?
A Deep Neural Network (DNN) is an artificial neural network with multiple hidden layers between the input and output layers.
Each neuron receives information, applies mathematical transformations using activation functions, and passes the result to the next layer.
Unlike traditional statistical models, deep neural networks automatically learn complex nonlinear relationships without requiring manual feature engineering.
Why Use Deep Neural Networks?
Deep neural networks are capable of:
- Learning nonlinear relationships
- Discovering hidden patterns in data
- Handling high-dimensional datasets
- Processing structured and unstructured data
- Improving prediction accuracy
- Automatically learning feature representations
Applications include:
- Image recognition
- Speech recognition
- Customer churn prediction
- Financial forecasting
- Medical diagnosis
- Recommendation systems
- Natural language processing
- Predictive analytics
Neural Network Architecture
A typical neural network contains:
- Input Layer – receives predictor variables
- Hidden Layers – extract increasingly complex features
- Output Layer – produces predictions
The number of hidden layers determines whether the model is considered “deep.”
Required Packages
Install and load the required libraries.
install.packages(c(
"keras",
"tensorflow",
"mlbench",
"dplyr"
))library(keras)
library(tensorflow)
library(mlbench)
library(dplyr)Load the Dataset
We’ll use the well-known Boston Housing dataset.
data("BostonHousing", package = "mlbench")
data <- BostonHousing
str(data)The dataset contains:
- 506 observations
- 13 predictor variables
- 1 response variable (
medv)
The response variable medv represents the median value of owner-occupied homes.
Convert Factor Variables
Neural networks require numerical inputs.
Convert factor variables to numeric.
data <- data %>%
mutate(across(where(is.factor), ~ as.numeric(as.character(.))))Split the Data
Create training and testing datasets.
set.seed(123)
train_index <- sample(
seq_len(nrow(data)),
size = 0.7 * nrow(data)
)
train <- data[train_index, ]
test <- data[-train_index, ]Separate predictors and target variables.
x_train <- as.matrix(train[, -14])
y_train <- train[, 14]
x_test <- as.matrix(test[, -14])
y_test <- test[, 14]Standardize the Predictors
Neural networks generally perform better when predictor variables are standardized.
train_mean <- apply(x_train, 2, mean)
train_sd <- apply(x_train, 2, sd)
x_train <- scale(
x_train,
center = train_mean,
scale = train_sd
)
x_test <- scale(
x_test,
center = train_mean,
scale = train_sd
)Build the Neural Network
Create a simple deep learning model.
model <- keras_model_sequential() %>%
layer_dense(
units = 32,
activation = "relu",
input_shape = ncol(x_train)
) %>%
layer_dense(
units = 16,
activation = "relu"
) %>%
layer_dense(
units = 1
)Understanding the Layers
The model contains:
- 13 input variables
- Hidden layer with 32 neurons
- Hidden layer with 16 neurons
- One output neuron
The ReLU (Rectified Linear Unit) activation function is commonly used because it improves training efficiency and reduces the vanishing gradient problem.
View the Model Summary
summary(model)The summary reports:
- Layer types
- Output shapes
- Trainable parameters
- Total model parameters
Compile the Model
Before training, specify the optimizer, loss function, and evaluation metric.
model %>% compile(
optimizer = "adam",
loss = "mse",
metrics = c("mae")
)Where:
- Adam is an adaptive optimization algorithm widely used in deep learning.
- MSE (Mean Squared Error) is appropriate for regression problems.
- MAE (Mean Absolute Error) provides an interpretable measure of prediction error.
Train the Model
Fit the model using the training data.
history <- model %>%
fit(
x_train,
y_train,
epochs = 100,
batch_size = 32,
validation_split = 0.2,
verbose = 1
)During training, you’ll observe output similar to:
Epoch 1/100
loss: 730
mae: 25
val_loss: 270
val_mae: 15As training progresses:
- Training loss decreases.
- Validation loss typically decreases before stabilizing.
- MAE becomes smaller.
A decreasing loss generally indicates that the model is learning useful patterns.
Evaluate the Model
Assess performance on the unseen test dataset.
model %>%
evaluate(
x_test,
y_test
)Example output:
Loss: 121.2
MAE: 9.02Lower values indicate better predictive performance.
Generate Predictions
Predict house prices.
predictions <- model %>%
predict(x_test)Compare Predictions with Actual Values
results <- data.frame(
Actual = y_test,
Predicted = predictions
)
head(results)Scatter Plot: Actual vs Predicted
Visualize prediction accuracy.
plot(
y_test,
predictions,
xlab = "Actual",
ylab = "Predicted",
main = "Actual vs Predicted"
)
abline(0, 1, col = "red")A good regression model produces points clustered around the diagonal reference line.
Improving Model Performance
The initial network can often be improved by increasing model capacity and applying regularization.
Example:
model <- keras_model_sequential() %>%
layer_dense(
units = 100,
activation = "relu",
input_shape = ncol(x_train)
) %>%
layer_dropout(
rate = 0.4
) %>%
layer_dense(
units = 50,
activation = "relu"
) %>%
layer_dropout(
rate = 0.2
) %>%
layer_dense(
units = 1
)Additional hidden layers increase learning capacity, while dropout randomly disables neurons during training to reduce overfitting.
After retraining, the evaluation might improve substantially.
Example:
Loss = 12.3
MAE = 2.48This represents a significant reduction in prediction error compared with the initial model.
Tips for Better Neural Networks
Consider the following strategies to improve performance:
- Normalize or standardize input features.
- Increase training data when possible.
- Tune the number of neurons and hidden layers.
- Experiment with different activation functions.
- Use dropout or L2 regularization to reduce overfitting.
- Apply early stopping to halt training when validation performance no longer improves.
- Adjust batch size and learning rate.
- Evaluate models using cross-validation when appropriate.
Applications of Deep Neural Networks
Deep neural networks are widely used in:
- Computer vision
- Image classification
- Speech recognition
- Natural language processing
- Fraud detection
- Financial forecasting
- Healthcare diagnostics
- Predictive maintenance
- Recommendation engines
- Autonomous vehicles
Common Challenges
Despite their power, deep neural networks have some limitations:
- Require more computational resources than traditional machine learning models.
- Often need larger datasets for optimal performance.
- Hyperparameter tuning can be time-consuming.
- Model interpretation is more difficult than with linear models or decision trees.
- Susceptible to overfitting if regularization is not applied.
Conclusion
Deep Neural Networks are powerful machine learning models capable of learning complex nonlinear relationships that traditional statistical techniques often cannot capture. Using the Keras package in R, you can efficiently build, train, and evaluate deep learning models for regression and classification tasks.
Successful deep learning projects rely on careful data preprocessing, feature scaling, appropriate network architecture, and thoughtful hyperparameter tuning. Techniques such as dropout regularization, additional hidden layers, and modern optimizers like Adam can significantly improve model performance. With proper validation and evaluation, deep neural networks can deliver highly accurate predictions across a wide range of real-world applications in data science, artificial intelligence, and predictive analytics.
