Code Generator | MATLAB

MATLAB Neural Network Data Compression

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.


Empty image or helper icon

Prompt

Write a MATLAB program for data compression. Data has 9 * 7 pixels so a total of 63 input units and 63 output units are required. Number of hidden units are 30. Check the performance with (i) learning rate = 0.5 and momentum factor = 0.5, (ii) learning rate = 0.5 momentum factor = 0.4. The given input pattern is

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

  1. 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.
  2. Input Validation:

    • Ensure that the input pattern is a 9x7 matrix; otherwise, an error is thrown.
  3. Network Initialization:

    • The neural network is defined using feedforwardnet with 30 hidden units.
    • Learning rate and momentum are set as per the input parameters.
  4. Training:

    • The network is trained using train function to minimize reconstruction error.
  5. 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:

  1. Define a 9x7 input pattern.
  2. Call the compressData function twice with different learning rates and momentum factors.
  3. 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.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

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.