SeriesAWS ML Engineer (Associate)5 / 9

Model Training, Tuning, and Evaluation

Section 6 of AWS Certified ML Engineer course from Udemy.

Introduction#

In the previous section, we reviewed various machine learning (ML) algorithms built into the AWS SageMaker platform. However, most modern approaches require custom-built approaches built on neural networks. This section provides an overview of deep learning, neural network training & evaluation, and the integration of neural techniques with SageMaker.

Deep Learning#

Deep Learning is a subfield of machine learning concerned with deep neural networks, where depth refers to the number of hidden layers within the network.

History of Neural Networks#

An (artificial) Neural Network is a mathematical model organized as a series of layers / matrix transformations connected via non-linear activation functions. Neural networks have extreme representational power, extending far beyond the limits of most classical machine learning algorithms.

The first artificial neuron was introduced in 1943 via the McCulloch-Pitts Neuron, which computes basic boolean logic functions by multiplying binary inputs with weights.

McCulloch Neuron

Image Source

This idea was extended by Frank Rosenblatt in 1957 via the Perceptron, which was the first machine learning algorithm designed for artificial neurons. The perceptron defines a system in which continuous inputs are multiplied by learned weights (parameters) to produce a weighted sum, which is passed through an activation function to produce the final binary output.

Rosenblatt Perceptron

Image Source

Chaining multiple perceptrons together produces a Multi-Layer Perceptron (MLP), which is a basic type of modern neural network.

Activation Functions#

An activation function processes the intermediate output (e.g., weighted sum) corresponding to a single layer within a neural network. There are many types of activation functions:

In general, we tend to start with ReLU then try Leaky ReLU / PReLU for more advanced problems. Multi-class classification problems require Softmax, and RNNs tend to perform well with Tanh.

CNNs#

A Convolutional Neural Network (CNN) is a specific type of neural architecture which builds on the mathematical convolution operation (blends two functions by sliding, reversing, and multiplying them together). (fg)(t)=f(τ)g(tτ)dτ(f * g)(t) = \int_{-\infty}^{\infty} f(\tau) g(t - \tau) d\tau By sliding one function along another, we define a feature-location invariant model. This is particularly efficient in the case of image data, where features used to process input pixels are shared across the entire image!

In the context of image data, CNNs slide a learned matrix of weights (referred to as a kernel) across an image, taking the weighted sum of pixels within the current receptive field of the image. Each position in the output matrix corresponds to a weighted sum of kernel ×\times receptive field. This enables CNNs to scan an entire image and produce location-aware output.

Within a CNN, convolutional layers are responsible for performing the actual convolution operation. Lower-level math is abstracted away in modern deep learning libraries (e.g., Conv2D in TensorFlow). Other common CNN layer types include:

Data dimensionality is particularly tricky when dealing with CNNs. Ensure that your source data is of the appropriate dimensionality (e.g., width×length×color channels\text{width} \times \text{length} \times \text{color channels}), and define weight matrices for layers by considering the dimensionality of previous-layer output.

The amount of possible CNN architectures (layers, layer types, neurons within each layer, and activation functions) is unfathomable! Fortunately, researchers have defined specialized CNN architectures well-suited for particular applications.

RNNs#

A Recurrent Neural Network (RNN) uses the concept of recurrence to process sequence data. It carries information forward through the sequence by maintaining a hidden state. RNNs are used for analyzing time-series data, or data consisting of sequences of arbitrary length (e.g., text).

RNN

There are four major RNN topologies corresponding to different applications, mostly dependent on the types of input and output.

RNN training must account for hidden state from previous tokens - backpropagation through time unrolls recurrence by chaining backpropagation across time steps. Truncated backpropagation through time avoids issues with infeasible computational overhead.

Furthermore, RNNs have different structural motifs:

LSTM

Training Neural Networks#

Neural network parameters are learned via Gradient Descent, which iteratively updates values using the gradient of the loss function w.r.t. parameters.

wk+1=wkα×wLw_{k+1} = w_k - \alpha \times \nabla_w L

There are many different Hyperparameters for gradient descent, which serve as tunable configuration settings for a machine learning model other than directly learned parameters.

Note that batch size and learning rate have great impact on learning. Smaller batch sizes tend to avoid local minima, whereas large batch sizes may converge on the wrong solution at random. Larger learning rates may overshoot the correct solution, while smaller learning rates increase training time. Ultimately, the best values for these hyperparameters depend on the specific problem / dataset at hand, emphasizing the importance of Hyperparameter Tuning.

Regularization Techniques#

In the context of machine learning, Regularization refers to any technique intended to mitigate overfitting. Recall that overfitting refers to a model learning noisy patterns in the training data, which do not properly generalize to the pattern we are trying to represent. Overfitting is characterized by a large gap between training and validation (holdout) performance.

Overfitting

Common regularization techniques include:

Certain regularization methods are based on penalty applied to the loss function during optimization.

Regularization

Lambda λ\lambda is used as a scaling factor to control the impact of the penalty term on loss.

Common Training Issues#

Certain issues are common when training via gradient descent:

These issues may slow and/or completely prevent effective learning, as well as introduce numerical precision errors into the process. They are prevalent in the case of deep neural networks, since gradients are passed to earlier layers via the chain rule (multiplication of local gradients).

Certain activation functions (e.g., ReLU) and neural architectures (residual networks) are better suited to avoid these problems. Overall, gradient checking is a good diagnostic tool to ensure neural network training is successful.

Model Evaluation#

Classification Metrics\underline{\text{Classification Metrics}}

A Confusion Matrix summarizes true positives / negatives and false positives / negatives. For example, a binary confusion matrix might be structured as follows.

Binary Confusion Matrix

Multi-class confusion matrices generalize this concept to a k×kk \times k matrix, with kk representing the number of class labels.

Multi-Class Confusion Matrix

Certain metrics are relevant in the context of binary classification:

Many of these metrics generalize to the multi-class setting by either 1) averaging over classes, or 2) considering a single class in isolation.

Regression Metrics\underline{\text{Regression Metrics}}

R-Squared - otherwise known as the coefficient of determination - measures how well an independent variable explains the variance in a dependent variable. It represents the proportion of variance in the dependent variable that is predicted by the independent variable. We might apply R-squared in the context of regression by computing between observed and predicted values.

Other evaluation metrics for regression measure error between observed and predicted values:

SageMaker Tools#

AMT and Hyperparameter Tuning#

Automatic Model Tuning (AMT) is a SageMaker service for performing hyperparameter tuning over specified ranges, relative to a specified optimization metric. It uses Bayesian optimization to intelligently explore hyperparameter combinations.

There are a few recommended practices for using AMT:

AMT supports early stopping and warm start functionalities to improve tuning results. Additionally, the user may opt to use an alternative approach to Bayesian optimization, such as grid search or random search.

AutoML#

SageMaker Autopilot is a wrapper for AutoML. It enables automatic algorithm selection, preprocessing, hyperparameter tuning, and infrastructure provisioning.

Autopilot has a few built-in training modes:

Autopilot also offers explainability by integrating with SageMaker Clarify. Feature attribution methods (ex: Shapley values) indicate which features are most important for a given prediction.

Model Registry#

The SageMaker Model Registry is a catalog for models, enabling users to manage their model versions and store associated metadata. Centralizing a library of models in one shared location enables easy and regulated access across an organization.

Integrations#

MLFlow is an open-source platform for machine learning and generative AI workflows. It offers useful features for all aspects of the ML process, including observability, evaluations, tracking, model management, and model deployment through the use of a tracking server. SageMaker offers a managed MLFlow tracking server to assist with experimentation.

SageMaker also integrates with Tensorboard to offer visualized model performance over the course of training. This is particularly useful for debugging issues with training, such as vanishing gradients, unstable learning, or overfitting.


(all information obtained from AWS Certified Machine Learning Engineer Associate: Hands On! course on Udemy)

License

CC BY-NC-SA 4.0 This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Related Posts