Introduction

Welcome to the final lesson of "Building and Applying Your Neural Network Library"! Congratulations on making it this far — you've accomplished something truly remarkable. Over the course of this path, you've built a complete, modular neural network library from scratch, learning the inner workings of layers, activations, optimizers, loss functions, and the orchestration that brings them all together. You've also mastered the essential data preparation techniques needed for real-world machine learning applications.

Today, we're going to experience the incredible satisfaction of seeing all your hard work come together. We'll use our custom-built neural network library to tackle a real regression problem: predicting California housing prices. You'll see how the modular architecture you've carefully constructed makes it surprisingly straightforward to define complex neural networks, train them efficiently, and evaluate their performance on real data.

This lesson represents the culmination of your journey — the moment when theory meets practice, and your carefully crafted code proves its worth on a meaningful problem. Let's put your neural network library to the ultimate test!

Setting Up the Data

Let's start by loading our components and setting up the data preprocessing pipeline. Since you mastered data preparation in the previous lesson, we'll handle this efficiently and effortlessly:

library(reticulate)
library(caret)

# Ensure sklearn is available
py_require("scikit-learn")

# Import sklearn datasets module
sklearn_datasets <- import("sklearn.datasets")

source("activations/functions.R")
source("layers/dense.R")
source("losses/mse.R")
source("optimizers/sgd.R")
source("models/model.R")
source("models/sequential.R")

# Load the California housing dataset
housing <- sklearn_datasets$fetch_california_housing()
X <- housing$data
y <- matrix(housing$target, ncol = 1)

# Create train/test split (80/20)
set.seed(42)
train_indices <- sample(1:nrow(X), size = floor(0.8 * nrow(X)))
X_train <- X[train_indices, ]
X_test <- X[-train_indices, ]
y_train <- y[train_indices, , drop = FALSE]
y_test <- y[-train_indices, , drop = FALSE]

# Standardize features
X_train_mean <- colMeans(X_train)
X_train_sd <- apply(X_train, 2, sd)
X_train_scaled <- scale(X_train, center = X_train_mean, scale = X_train_sd)
X_test_scaled <- scale(X_test, center = X_train_mean, scale = X_train_sd)

# Standardize target variable
y_train_mean <- mean(y_train)
y_train_sd <- sd(y_train)
y_train_scaled <- (y_train - y_train_mean) / y_train_sd
y_test_scaled <- (y_test - y_train_mean) / y_train_sd

num_features <- ncol(X_train_scaled)  # Will be 8 for this dataset
Defining the Neural Network Architecture
Compiling the Model

Now we'll compile our model with appropriate training configurations so that it's ready to begin the learning process:

# Compile the model
model$compile(optimizer_name = 'sgd', learning_rate = 0.005, loss_name = 'mse')  # Smaller LR for this dataset

The compilation step configures our training setup. We're using SGD (stochastic gradient descent) with a learning rate of 0.005, which is slightly smaller than in our previous examples. Real-world datasets often benefit from more conservative learning rates that allow for stable, consistent learning across the diverse feature landscape.

When you run this code, you'll see:

Model compiled with optimizer: sgd (lr: 0.005), loss: mse
Training the Network

Now for the moment of truth — training our neural network on real data:

# Train the model
cat("\nTraining the model...\n")
model$fit(X_train_scaled, y_train_scaled, epochs = 200, batch_size = 32, verbose = 1)

This single method call triggers a sophisticated training process. Your model will process 200 epochs of training, where each epoch involves multiple mini-batches of 32 samples each. The verbose output shows the learning progress:

Training the model...
Epoch    1/200, Loss: 1.002847
Epoch   20/200, Loss: 0.518293
Epoch   40/200, Loss: 0.385762
Epoch   60/200, Loss: 0.325441
Epoch   80/200, Loss: 0.289536
Epoch  100/200, Loss: 0.267184
Epoch  120/200, Loss: 0.251289
Epoch  140/200, Loss: 0.239374
Epoch  160/200, Loss: 0.230102
Epoch  180/200, Loss: 0.222841
Epoch  200/200, Loss: 0.216924
Training finished.

The decreasing loss values demonstrate successful learning! Your network started with a loss of 1.003 and steadily improved to 0.217 — a clear sign that it's learning meaningful patterns in the housing data. The gradual, consistent decrease indicates stable training without the erratic behavior that can plague poorly configured networks.

Making Predictions and Evaluating Performance

With our model trained, we can now make predictions on the test set and evaluate how well it generalizes to unseen data:

# Make predictions on the test set
y_pred_scaled <- model$predict(X_test_scaled)

# Calculate MSE on scaled predictions and scaled true values
test_loss_scaled <- mse_loss(y_test_scaled, y_pred_scaled)
cat(sprintf("\nTest MSE (scaled): %.4f\n", test_loss_scaled))

This produces our first evaluation metric:

Test MSE (scaled): 0.2304

The test MSE of 0.2304 is quite close to our final training loss of 0.217, which is excellent news! This similarity indicates that our model is generalizing well rather than overfitting to the training data. When test performance closely matches training performance, it suggests we've found genuine patterns rather than memorizing specific training examples.

However, this scaled MSE, while useful for training, doesn't give us an intuitive sense of prediction accuracy. Housing prices measured in standardized units don't mean much to us humans, who think in hundreds of thousands of dollars!

Interpreting Results in Real-World Terms
Looking at Sample Predictions
Conclusion

Congratulations! You've successfully completed the entire journey of building and applying your own neural network library. What you've accomplished in this final lesson represents the true power of the modular, well-designed system you've constructed over the past five lessons. Your achievement is remarkable — you've taken raw California housing data and successfully trained a multi-layer neural network to predict house prices with meaningful accuracy.

The elegance of your solution demonstrates the value of good software design, with clean, readable code that seamlessly integrates data preprocessing, model definition, training, and evaluation. In the upcoming practice exercises, you'll have the opportunity to apply these skills hands-on, building confidence in your ability to tackle real-world machine learning problems with your custom-built neural network library.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal