Prompt
Answer
MATLAB Data Compression using Neural Networks
This document demonstrates a MATLAB program for data compression using a neural network with the specified parameters. The input data consists of 9x7 pixels, resulting in a total of 63 input and output units with 30 hidden units. The performance is evaluated for two sets of learning rates and momentum factors.
Function Overview
compressData
function compressed_output = compressData(input_pattern, learning_rate, momentum)
% compressData performs data compression using a neural network
%
% Parameters:
% input_pattern: A matrix representing the 9x7 pixel data [double matrix]
% learning_rate: Learning rate for the neural network [double]
% momentum: Momentum factor for the neural network [double]
%
% Returns:
% compressed_output: The compressed output after training the network [double matrix]
%
% Throws:
% Error if input_pattern is not 9x7 matrix
% Validate input sizes
if size(input_pattern, 1) ~= 9 || size(input_pattern, 2) ~= 7
error("Input pattern must be a 9x7 matrix.");
end
% Reshape input data into a vector
input_vec = input_pattern(:)';
% Initialize neural network
net = feedforwardnet(30); % 30 hidden units
% Set the learning rate and momentum
net.trainParam.lr = learning_rate;
net.trainParam.mc = momentum;
% Training parameters
net.trainParam.epochs = 1000; % Maximum number of epochs
net.trainParam.goal = 1e-3; % Training goal
% Train the network
[net, tr] = train(net, input_vec, input_vec);
% Simulate the network with the same input
compressed_output = net(input_vec);
% Reshape back to 9x7 matrix
compressed_output = reshape(compressed_output, 9, 7);
end
Main Script
main
% Main script to evaluate data compression performance
% Define the 9x7 input pattern
input_pattern = rand(9, 7); % Example input pattern with random data for demonstration
% Parameters set (i): Learning rate = 0.5, Momentum = 0.5
learning_rate_1 = 0.5;
momentum_1 = 0.5;
compressed_output_1 = compressData(input_pattern, learning_rate_1, momentum_1);
disp("Compressed output with learning_rate = 0.5, momentum = 0.5:");
disp(compressed_output_1);
% Parameters set (ii): Learning rate = 0.5, Momentum = 0.4
learning_rate_2 = 0.5;
momentum_2 = 0.4;
compressed_output_2 = compressData(input_pattern, learning_rate_2, momentum_2);
disp("Compressed output with learning_rate = 0.5, momentum = 0.4:");
disp(compressed_output_2);
Explanation
Function Definition:
- The
compressData
function performs data compression using a simple feedforward neural network with 30 hidden units. - Inputs: A 9x7 pixel matrix, learning rate, and momentum factor.
- Output: Compressed data reshaped back to a 9x7 matrix.
- The
Input Validation:
- Ensure that the input pattern is a 9x7 matrix; otherwise, an error is thrown.
Network Initialization:
- The neural network is defined using
feedforwardnet
with 30 hidden units. - Learning rate and momentum are set as per the input parameters.
- The neural network is defined using
Training:
- The network is trained using
train
function to minimize reconstruction error.
- The network is trained using
Output Compilation:
- The function simulates the network with the given input to obtain compressed data.
- The compressed data is reshaped to its original 9x7 dimensions.
Code Usage Example
To run the above code, follow these steps:
- Define a 9x7 input pattern.
- Call the
compressData
function twice with different learning rates and momentum factors. - Display the compressed outputs for both scenarios.
Conclusion
This MATLAB code effectively compresses a 9x7 pixel input using a neural network, demonstrating performance evaluation with different hyperparameters. For further learning and expertise in MATLAB, consider exploring training resources from the Enterprise DNA Platform.
Description
This document showcases a MATLAB program for compressing a 9x7 pixel matrix using a neural network with adjustable learning rates and momentum factors, while demonstrating performance evaluation through training.