How will an Optimization Algorithm search inside an already trained neural network??

2 views (last 30 days)
Can someone please help me on how to link an already trained ANN with an optimization algorithm??
I am finding a lot of stuff online on training ANN using the optimization algorithm. But I don't want that. I am working on a different strategy.
First, a neural network has been trained to predict multiple outputs (regression for (i) antenna multiband frequencies, (ii) return loss at these frequencies, (iii) bandwidth at these frequencies, (iv) gain at these frequencies and (v) directivity at these frequencies.
The weights and bias for the trained ANN have been obtained.
Next, I have framed a set of objective functions for my optimization code. For example:
FF1: (2.4- f1)^2 + (5.2- f2)^2 + (8.5- f3)^2 + (32-f4)^4
FF2: (-20 + RC1)^2 + (-20 + RC2)^2 + (-20 + RC3)^2 + (-20 + RC4)^2
FF3: (1.5 - B1)^2 + (2 - B2)^2 + (0.5 - B3)^2 + (1.3 - B4)^2
FF4: (2 - G1)^2 + (3 - G2)^2 + (1 - G3)^2 + (3 - G4)^2
FF5: (2 - D1)^2 + (1.1 - D2)^2 + (0.5 - D3)^2 + (1.3 - D4)^2
Here FF1 is the fitness function for the four frequencies (f1 to f4) of the antenna, FF2 is the fitness function for the return loss (RC1 to RC4) at the four frequencies (f1 to f4) of the antenna, FF3 is the fitness function for the bandwidths (B1 to B4) at the four frequencies (f1 to f4) of the antenna, FF4 is the fitness function for the gain (G1 to G4) at the four frequencies (f1 to f4) of the antenna, and FF5 is the fitness function for the directivity (D1 to D4) at the four frequencies (f1 to f4) of the antenna, respectively.
In order to minimize these 5 objective functions, I want the optimization code to search within the trained ANN and then provide solution. Does someone have a code of this sort and can you explain me its functioning??

Answers (1)

Prateekshya
Prateekshya on 9 Oct 2024
Hello Deepanshu,
To link a trained ANN with an optimization algorithm in MATLAB, you can use MATLAB's optimization toolbox. Let us take an example of minimizing the objective functions based on the predictions from your trained ANN.
  • Load or Define Your Trained ANN: Assuming you have a trained neural network model, you should have access to it in MATLAB. If your model is saved, you can load it using the load function.
% Load your trained neural network (replace 'yourModel.mat' with your actual file)
load('yourModel.mat', 'trainedANN');
  • Define the Objective Function: The objective function will use the ANN to predict outputs and calculate the fitness values.
function totalFF = objectiveFunction(inputs, trainedANN)
% Predict using the trained ANN
predictions = trainedANN(inputs');
% Extract predicted outputs
f = predictions(1:4);
RC = predictions(5:8);
B = predictions(9:12);
G = predictions(13:16);
D = predictions(17:20);
% Calculate the fitness functions
FF1 = (2.4 - f(1))^2 + (5.2 - f(2))^2 + (8.5 - f(3))^2 + (32 - f(4))^4;
FF2 = (-20 + RC(1))^2 + (-20 + RC(2))^2 + (-20 + RC(3))^2 + (-20 + RC(4))^2;
FF3 = (1.5 - B(1))^2 + (2 - B(2))^2 + (0.5 - B(3))^2 + (1.3 - B(4))^2;
FF4 = (2 - G(1))^2 + (3 - G(2))^2 + (1 - G(3))^2 + (3 - G(4))^2;
FF5 = (2 - D(1))^2 + (1.1 - D(2))^2 + (0.5 - D(3))^2 + (1.3 - D(4))^2;
% Total objective function to minimize
totalFF = FF1 + FF2 + FF3 + FF4 + FF5;
end
  • Optimize Using fmincon: You can use MATLAB's fmincon function to minimize the objective function. This function is part of the Optimization Toolbox and is suitable for constrained optimization.
% Initial guess for the inputs (adjust size based on your input requirements)
initialInputs = rand(10, 1); % Example initial inputs
% Set optimization options
options = optimoptions('fmincon', 'Display', 'iter', 'Algorithm', 'sqp');
% Run optimization
[optimizedInputs, fval] = fmincon(@(inputs) objectiveFunction(inputs, trainedANN), ...
initialInputs, [], [], [], [], [], [], [], options);
% Display the optimized inputs
disp('Optimized Inputs:');
disp(optimizedInputs);
  • Test and Validate: Ensure that your ANN model is correctly loaded and that the predictions are accurate. Validate the optimized inputs by checking the predicted outputs against expected results.
I hope this helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!