Real World Neural Network Application

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
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