Deep Learning with MATLAB
Deep Learning is a subset of Machine Learning that involves neural networks with three or more layers. MATLAB provides an extensive set of tools and functions to design, train, and analyze deep neural networks. This section covers the basics, examples, and advanced functionalities of deep learning in MATLAB.
Key Concepts in Deep Learning
Deep learning involves training models using large datasets. The key components include:
- Neural Networks: Composed of input, hidden, and output layers.
- Activation Functions: Functions like ReLU, sigmoid, and softmax used in neurons.
- Optimization: Techniques to minimize error, such as stochastic gradient descent.
Example 1: Creating and Training Neural Networks
Here is an example of creating and training a simple feedforward neural network:
% Define layers
layers = [
imageInputLayer([28 28 1])
fullyConnectedLayer(10)
softmaxLayer
classificationLayer];
% Load sample dataset
load('digitsData.mat');
% Define training options
options = trainingOptions('sgdm', ...
'MaxEpochs', 5, ...
'MiniBatchSize', 64, ...
'Plots', 'training-progress');
% Train the network
net = trainNetwork(trainImages, trainLabels, layers, options);
The output of training progress will be shown in a separate MATLAB window, illustrating loss and accuracy over epochs.
Working with Pretrained Models
MATLAB provides several pretrained models such as ResNet, AlexNet, and GoogLeNet. You can use these models for transfer learning:
% Load a pretrained model
net = resnet50();
% Modify for new classification
lgraph = layerGraph(net);
newLayers = [
fullyConnectedLayer(5), ... % 5 classes
softmaxLayer, ...
classificationLayer];
lgraph = replaceLayer(lgraph, 'fc1000', newLayers);
% Train on new data
trainData = augmentedImageDatastore([224 224], trainingImages, ...
'ColorPreprocessing', 'gray2rgb');
options = trainingOptions('adam', 'InitialLearnRate', 0.001);
trainedNet = trainNetwork(trainData, layers, options);
Example 2: Solving an Equation Using a Neural Network
Suppose we want to approximate the equation y = x2 using a neural network:
% Define dataset
x = linspace(-10, 10, 1000)';
y = x.^2;
% Define layers
layers = [
featureInputLayer(1)
fullyConnectedLayer(10)
reluLayer
fullyConnectedLayer(1)];
% Train network
net = trainNetwork(x, y, layers, trainingOptions('adam', 'Plots', 'training-progress'));
Example 3: Convolutional Neural Networks (CNNs) in MATLAB
Convolutional Neural Networks (CNNs) are widely used for image recognition and classification tasks. They are composed of convolutional layers, pooling layers, fully connected layers, and activation functions.
Building and Training a CNN for Image Classification
In this example, we will train a CNN to classify images from the CIFAR-10 dataset, which contains 60,000 images across 10 classes.
Step 1: Load and Prepare Data
% Load CIFAR-10 dataset
[trainImages, trainLabels, testImages, testLabels] = helperCIFAR10Data.load();
% Normalize image data to [0, 1]
trainImages = trainImages / 255;
testImages = testImages / 255;
% Display sample images
figure;
for i = 1:25
subplot(5, 5, i);
imshow(trainImages(:,:,:,i));
title(string(trainLabels(i)));
end
Step 2: Define CNN Architecture
We define the CNN layers, including convolutional layers, pooling layers, and fully connected layers.
layers = [
imageInputLayer([32 32 3])
convolution2dLayer(3, 32, 'Padding', 'same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2, 'Stride', 2)
convolution2dLayer(3, 64, 'Padding', 'same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2, 'Stride', 2)
fullyConnectedLayer(512)
reluLayer
dropoutLayer(0.5)
fullyConnectedLayer(10)
softmaxLayer
classificationLayer];
Step 3: Specify Training Options
Specify training parameters such as learning rate, epochs, and mini-batch size.
options = trainingOptions('adam', ...
'InitialLearnRate', 0.001, ...
'MaxEpochs', 10, ...
'MiniBatchSize', 64, ...
'Shuffle', 'every-epoch', ...
'ValidationData', {testImages, testLabels}, ...
'ValidationFrequency', 30, ...
'Verbose', false, ...
'Plots', 'training-progress');
Step 4: Train the CNN
Train the CNN using the specified layers and training options.
% Train the network
net = trainNetwork(trainImages, trainLabels, layers, options);
Step 5: Evaluate the CNN
Evaluate the trained model on test data and compute accuracy.
% Test the trained network
YPred = classify(net, testImages);
accuracy = mean(YPred == testLabels);
disp("Test Accuracy: " + accuracy);
Output Example
Training completed successfully. Test Accuracy: 0.85
Example 4: Recurrent Neural Networks (RNNs) in MATLAB
Recurrent Neural Networks (RNNs) are a class of neural networks designed for sequential data such as time series, text, or any data where the current input depends on previous inputs.
Building and Training an RNN for Sequence Prediction
In this example, we will use an RNN to predict the next value in a sine wave sequence.
Step 1: Generate and Visualize the Data
% Generate a sine wave sequence
t = 0:0.01:10; % Time steps
data = sin(2*pi*0.2*t); % Sine wave
% Split data into training and testing sets
trainData = data(1:800);
testData = data(801:end);
% Visualize the data
figure;
plot(t, data);
title('Sine Wave');
xlabel('Time');
ylabel('Amplitude');
Step 2: Define the RNN Architecture
The RNN consists of sequence input layers, LSTM (Long Short-Term Memory) layers, and fully connected layers for prediction.
layers = [
sequenceInputLayer(1)
lstmLayer(50, 'OutputMode', 'sequence')
fullyConnectedLayer(1)
regressionLayer];
Step 3: Prepare the Data for Training
We reshape the data to match the input requirements of the RNN.
% Reshape the training and testing data
XTrain = trainData(1:end-1)'; % Inputs
YTrain = trainData(2:end)'; % Targets
XTest = testData(1:end-1)';
YTest = testData(2:end)';
Step 4: Specify Training Options
Set training parameters such as epochs, mini-batch size, and learning rate.
options = trainingOptions('adam', ...
'MaxEpochs', 250, ...
'GradientThreshold', 1, ...
'InitialLearnRate', 0.005, ...
'MiniBatchSize', 32, ...
'Shuffle', 'every-epoch', ...
'Plots', 'training-progress', ...
'Verbose', 0);
Step 5: Train the RNN
Train the RNN using the specified layers and training options.
% Train the RNN
net = trainNetwork(XTrain, YTrain, layers, options);
Step 6: Evaluate the RNN
Use the trained model to predict the next values in the sine wave sequence and compare with the actual values.
% Make predictions on the test set
YPred = predict(net, XTest, 'MiniBatchSize', 1);
% Calculate the root mean square error (RMSE)
rmse = sqrt(mean((YPred - YTest).^2));
disp("Test RMSE: " + rmse);
% Plot the results
figure;
plot(YTest, 'b', 'DisplayName', 'Actual'); hold on;
plot(YPred, 'r', 'DisplayName', 'Predicted');
legend;
title('RNN Prediction vs Actual');
xlabel('Time Step');
ylabel('Amplitude');
Output Example
Training completed successfully. Test RMSE: 0.015
Example 5: Transfer Learning in Deep Learning Using MATLAB
Transfer learning is a technique in deep learning where a pre-trained model is used as the starting point for a new task. It allows leveraging the knowledge learned from large datasets to solve problems with smaller, domain-specific datasets.
Example: Classifying Images Using a Pre-Trained Network
In this example, we use a pre-trained network (ResNet-50) to classify images of flowers into different categories.
Step 1: Load a Pre-Trained Network
We use ResNet-50, a popular deep convolutional neural network pre-trained on ImageNet.
% Load the pre-trained ResNet-50 network
net = resnet50;
% Display the architecture of the network
analyzeNetwork(net);
Step 2: Load and Prepare the Dataset
Download or load your image dataset. The dataset is split into training and validation sets.
% Load image dataset
dataDir = fullfile('path_to_your_data');
imds = imageDatastore(dataDir, ...
'IncludeSubfolders', true, ...
'LabelSource', 'foldernames');
% Split into training and validation sets
[imdsTrain, imdsValidation] = splitEachLabel(imds, 0.8, 'randomized');
% Resize images to match the input size of ResNet-50
inputSize = net.Layers(1).InputSize;
augmentedTrain = augmentedImageDatastore(inputSize(1:2), imdsTrain);
augmentedValidation = augmentedImageDatastore(inputSize(1:2), imdsValidation);
Step 3: Modify the Network for Transfer Learning
Replace the final layers of the pre-trained network to match the number of classes in your dataset.
% Replace the final layers
numClasses = numel(categories(imdsTrain.Labels));
layers = net.Layers;
layers(end-2) = fullyConnectedLayer(numClasses, 'Name', 'fc', ...
'WeightLearnRateFactor', 10, 'BiasLearnRateFactor', 10);
layers(end) = classificationLayer('Name', 'output');
% Create a new network
lgraph = layerGraph(layers);
Step 4: Set Training Options
Configure training options such as the optimizer, learning rate, and mini-batch size.
options = trainingOptions('sgdm', ...
'MiniBatchSize', 32, ...
'MaxEpochs', 10, ...
'InitialLearnRate', 0.001, ...
'Shuffle', 'every-epoch', ...
'ValidationData', augmentedValidation, ...
'ValidationFrequency', 10, ...
'Verbose', false, ...
'Plots', 'training-progress');
Step 5: Train the Network
Train the modified network on the new dataset.
% Train the network
trainedNet = trainNetwork(augmentedTrain, lgraph, options);
Step 6: Evaluate the Model
Evaluate the performance of the trained model on the validation set.
% Classify validation images
[YPred, scores] = classify(trainedNet, augmentedValidation);
% Compute accuracy
accuracy = mean(YPred == imdsValidation.Labels);
disp("Validation Accuracy: " + accuracy);
Output Example
Validation Accuracy: 0.92
Step 7: Visualize the Results
Plot sample images along with their predicted labels and probabilities.
% Display some predictions
idx = randperm(numel(imdsValidation.Files), 9);
figure;
for i = 1:9
subplot(3,3,i);
img = readimage(imdsValidation, idx(i));
imshow(img);
title(string(YPred(idx(i))) + ", " + num2str(max(scores(idx(i),:)), 2));
end
Example 6: Visualizing Feature Maps and Training Progress in Deep Learning Using MATLAB
Understanding how a neural network interprets input data is crucial for debugging and improving its performance. MATLAB provides tools to visualize feature maps and monitor training progress for deep learning models.
Visualizing Feature Maps and Monitoring Training
In this example, we train a convolutional neural network (CNN) on the CIFAR-10 dataset and visualize the feature maps of the first convolutional layer. We also show how to monitor training progress using MATLAB’s training plot.
Step 1: Load and Prepare the Dataset
We use the CIFAR-10 dataset, which consists of 60,000 32×32 color images in 10 classes.
% Load CIFAR-10 dataset
dataDir = tempdir;
unzip('https://www.cs.toronto.edu/~kriz/cifar-10-matlab.tar.gz', dataDir);
[trainingImages, trainingLabels, testImages, testLabels] = ...
loadCIFAR10(dataDir);
% Preprocess the images to normalize pixel values between 0 and 1
trainingImages = double(trainingImages) / 255;
testImages = double(testImages) / 255;
Step 2: Define the CNN Architecture
We define a simple convolutional neural network with one convolutional layer, a ReLU activation, a pooling layer, and fully connected layers.
layers = [
imageInputLayer([32 32 3], 'Name', 'input')
convolution2dLayer(3, 16, 'Padding', 'same', 'Name', 'conv1')
reluLayer('Name', 'relu1')
maxPooling2dLayer(2, 'Stride', 2, 'Name', 'pool1')
fullyConnectedLayer(10, 'Name', 'fc')
softmaxLayer('Name', 'softmax')
classificationLayer('Name', 'output')
];
Step 3: Specify Training Options
Set up the training options, including monitoring the progress using a training plot.
options = trainingOptions('sgdm', ...
'InitialLearnRate', 0.01, ...
'MaxEpochs', 10, ...
'MiniBatchSize', 64, ...
'Plots', 'training-progress', ...
'Verbose', false);
Step 4: Train the Network
Train the CNN on the CIFAR-10 dataset while monitoring the training progress.
% Train the network
net = trainNetwork(trainingImages, trainingLabels, layers, options);
Step 5: Visualize Feature Maps
Once the network is trained, visualize the feature maps of the first convolutional layer for a sample input image.
% Select a sample image from the test set
sampleImage = testImages(:,:,:,1);
imshow(sampleImage);
title('Sample Input Image');
% Get the feature maps from the first convolutional layer
featureMaps = activations(net, sampleImage, 'conv1');
% Plot the feature maps
numFeatureMaps = size(featureMaps, 3);
figure;
for i = 1:numFeatureMaps
subplot(4, 4, i);
imagesc(featureMaps(:,:,i));
axis off;
title(['Feature Map ', num2str(i)]);
end
Step 6: Interpret Training Progress
Review the training progress plot to analyze the network’s learning over epochs.
% Training progress plot is displayed automatically if 'Plots' option is set
% Training accuracy and loss curves indicate model performance over time
Output Example
The feature maps show the output of the filters applied in the first convolutional layer. Below is an example visualization for 16 filters:
Feature Map 1 Feature Map 2 Feature Map 3 ... [Image] [Image] [Image] ... ...
The training progress plot shows accuracy and loss curves, helping identify overfitting or underfitting:
[Training Progress Plot]
Useful MATLAB Functions for Deep Learning
Practice Problems
Test Yourself
1. Train a network to classify CIFAR-10 images using AlexNet.
2. Modify ResNet for transfer learning and train it on a custom dataset with 10 classes.
3. Create a neural network to approximate the function f(x) = sin(x)
.
4. Modify the example 3 for CNN architecture to include additional convolutional layers and test its performance.
5. For example 4, increase the number of LSTM units in the hidden layer and evaluate the impact on accuracy.
6. In example 5, replace ResNet-50 with another pre-trained network like Inception-v3. Train and evaluate it.