0% found this document useful (0 votes)
20 views3 pages

MATLAB ANN Training and Evaluation Guide

This document provides an example MATLAB code for training an artificial neural network (ANN) using a dataset. It includes steps for loading and preprocessing data, defining and training the ANN, evaluating its performance, and optimizing feature selection. The code demonstrates how to calculate accuracy and display a confusion matrix for both the initial and optimized models.

Uploaded by

Thaviru Stanley
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

MATLAB ANN Training and Evaluation Guide

This document provides an example MATLAB code for training an artificial neural network (ANN) using a dataset. It includes steps for loading and preprocessing data, defining and training the ANN, evaluating its performance, and optimizing feature selection. The code demonstrates how to calculate accuracy and display a confusion matrix for both the initial and optimized models.

Uploaded by

Thaviru Stanley
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Example MATLAB Code: Training an ANN

Load and Preprocess Data

% Load dataset (replace '[Link]' with your dataset file)


data = readtable('[Link]');

% Split features (X) and labels (Y)


X = table2array(data(:, 1:end-1)); % Assuming features are in all columns except the last
Y = table2array(data(:, end)); % Assuming the last column contains labels

% Normalize the features for better ANN performance


X = normalize(X);

% Convert labels to categorical (if necessary)


Y = categorical(Y);

% Split data into training and testing sets (70% train, 30% test)
cv = cvpartition(size(X, 1), 'HoldOut', 0.3);
XTrain = X(training(cv), :);
YTrain = Y(training(cv), :);
XTest = X(test(cv), :);
YTest = Y(test(cv), :);
Design and Train the ANN

% Define ANN architecture


inputLayerSize = size(XTrain, 2); % Number of features
hiddenLayerSize = 10; % Number of neurons in the hidden layer
outputLayerSize = numel(unique(Y)); % Number of unique classes

% Create the ANN using feedforwardnet


net = feedforwardnet(hiddenLayerSize);
% Configure the ANN
[Link]{1}.size = inputLayerSize;
[Link]{1}.transferFcn = 'tansig'; % Hyperbolic tangent sigmoid activation function
[Link]{2}.transferFcn = 'softmax'; % Output layer activation
[Link] = 'trainlm'; % Levenberg-Marquardt optimization
[Link] = 0.7; % Training ratio
[Link] = 0.15; % Validation ratio
[Link] = 0.15; % Testing ratio

% Train the network


[net, tr] = train(net, XTrain', full(ind2vec(double(YTrain'))));

% Display training progress


view(net);
Evaluate Performance

% Test the ANN on the testing dataset


YPred = net(XTest');
YPredClass = vec2ind(YPred);

% Convert predicted classes to match original labels


YPredClass = categorical(YPredClass, 1:outputLayerSize, categories(YTrain));

% Calculate performance metrics


accuracy = sum(YPredClass == YTest) / numel(YTest) * 100;
confMatrix = confusionmat(YTest, YPredClass);

% Display results
fprintf('Accuracy: %.2f%%\n', accuracy);
disp('Confusion Matrix:');
disp(confMatrix);
% Plot confusion matrix
figure;
confusionchart(YTest, YPredClass);
title('Confusion Matrix');
Feature Selection and Optimization

% Use Sequential Feature Selection for optimization


opts = statset('display','iter');
fun = @(trainX, trainY, testX, testY) ...
loss(net, trainX', full(ind2vec(double(trainY'))) - ...
net(testX')', full(ind2vec(double(testY'))));

[selectedFeatures, history] = sequentialfs(fun, X, double(Y), 'options', opts);

% Train ANN with selected features


XOptimizedTrain = XTrain(:, selectedFeatures);
XOptimizedTest = XTest(:, selectedFeatures);

netOptimized = train(net, XOptimizedTrain', full(ind2vec(double(YTrain'))));

% Evaluate the optimized ANN


YPredOptimized = netOptimized(XOptimizedTest');
YPredClassOptimized = vec2ind(YPredOptimized);
accuracyOptimized = sum(YPredClassOptimized == double(YTest)) / numel(YTest) * 100;

fprintf('Optimized Accuracy: %.2f%%\n', accuracyOptimized);

Common questions

Powered by AI

Converting predicted numeric class outputs to categorical labels is necessary to ensure consistency with the original label format, facilitating comparison and evaluation of model performance. In the code, this is achieved using 'vec2ind' to convert the predicted numeric indices from the ANN (YPred) to class indices. Subsequently, these indices are converted to categorical labels using 'categorical' and mapped to the original categories using 'categories(YTrain)'. This process allows the evaluation metrics, like accuracy and confusion matrix, to correctly reflect the performance of the ANN by comparing predicted and actual categorical classes.

The primary data preprocessing steps include loading the dataset, splitting features and labels, normalizing the features, converting labels to categorical, and splitting the data into training and testing sets. Loading the dataset allows the program to access the data it needs to learn from. Splitting features (X) and labels (Y) helps in separating the input data from the desired output. Normalization of features is important to scale the data to a common range, which improves ANN performance by preventing features with larger magnitudes from dominating the learning process. Converting labels to categorical ensures that the output matches the required format for ANN processing. Finally, splitting the data into training and testing sets allows the evaluation of the ANN's performance on unseen data, which is crucial for assessing its generalization capability.

Changing the number of neurons in the hidden layer impacts the capacity of the ANN to learn and represent complex patterns. Increasing the neurons may lead to a higher model capacity, allowing it to capture more complex relationships in the data, which could improve accuracy if these patterns exist. However, too many neurons can also lead to overfitting, where the network performs well on training data but poorly on unseen data due to memorization rather than learning. Conversely, too few neurons may result in an underfitting model that is unable to represent the data adequately. Adjusting this parameter requires balancing the model complexity with the risk of overfitting, depending on the dataset size, complexity, and computational resources available.

Normalization of features contributes to improved ANN performance by bringing input data into a common scale, typically between 0 and 1 or -1 and 1. This process ensures that no single feature disproportionately influences the learning process due to its magnitude, facilitating more effective weight updates during training. It also improves the convergence rate of optimization algorithms, leading to faster and potentially more stable learning. Omitting normalization might lead to prolonged training times and poor convergence behavior, where features with larger scales dominate, causing ineffective learning and potential model degradation, especially if different features have widely varying scales.

The choice of activation functions affects the learning process by determining how the output of a neuron is calculated from the weighted sum of inputs. For instance, using 'tansig' (tanh activation) in the hidden layer allows the network to model complex non-linear patterns due to its non-linear nature and output between -1 and 1. This helps with gradient back-propagation due to its derivative. The 'softmax' function in the output layer normalizes outputs to a probability distribution, essential for multi-class classification tasks, as it allows the model to output the probability of each class. The effectiveness of learning and performance is sensitive to these choices, as inappropriate functions may lead to vanishing or exploding gradients or poor classification calibration, impacting the overall network's performance.

The code enhances model generalization by splitting the dataset into training, validation, and testing sets, with specified ratios of 70% for training and 30% for testing (along with internal validation during training). The use of validation data helps monitor performance and prevent overfitting by assessing how the model performs on unseen data during training. Regular evaluation metrics, like accuracy and confusion matrix on testing data, provide insights into the model's ability to generalize. Furthermore, normalization of features and feature selection processes contribute to a balanced model that is less likely to fit noise and more likely to capture underlying data patterns, ultimately improving generalization.

Sequential feature selection plays a critical role in optimizing the ANN by systematically evaluating the performance impact of individual features and identifying the most relevant subset that improves model efficiency and accuracy. This method iteratively selects or removes features based on their contribution to reducing a predefined loss function, allowing the ANN to focus only on the most impactful information. In the code, 'sequentialfs' is used to perform feature selection with a custom loss function linked to the network's performance ('loss') to determine the optimal subset. The benefits include reduced computational complexity, prevention of overfitting by eliminating irrelevant features, and improved generalization to new data, which ultimately can lead to a more robust and accurate model.

Evaluating the ANN using a confusion matrix provides detailed insights into the classification performance by showing the number of true positives, false positives, true negatives, and false negatives for each class. While accuracy gives an overall sense of performance, a confusion matrix breaks down performance per class, revealing potential model biases or weaknesses. It helps identify specific areas where the ANN is performing well or poorly, allowing for a more nuanced understanding of model behavior. This information is crucial for deciding on further improvements, especially in cases of class imbalance or when misclassification of certain classes has more serious consequences.

The Levenberg-Marquardt optimization method is used to train the ANN, as indicated by the 'trainlm' argument. This algorithm is designed to minimize the error in regression and improve convergence speed by combining the concepts of gradient descent and Gauss-Newton. An advantage of using Levenberg-Marquardt over other methods is its increased efficiency and ability to handle complex and nonlinear mappings by rapidly reducing error and finding the optimum weights effectively. This is particularly useful for datasets where the relationships between inputs and outputs are not straightforward, as it helps the network to reach optimal solutions faster and with greater reliability compared to simple gradient-based methods.

The ANN architecture is defined by specifying the input layer size, hidden layer size, and output layer size. The input layer size is set to the number of features (inputLayerSize = size(XTrain, 2)), which determines how many input nodes are required. The hidden layer size (hiddenLayerSize = 10) is set to 10 neurons, which serves as a parameter to adjust the capacity of the network to learn complex patterns. The output layer size is set to the number of unique classes, as it determines the number of output nodes needed for classification tasks. Additionally, transfer functions are specified (tansig for the hidden layer and softmax for the output layer), determining how the neurons in each layer process inputs. The specified training function (trainlm for Levenberg-Marquardt optimization) influences how the network learns.

You might also like