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

7 ビュー (過去 30 日間)
DEEPANSHU KAUSHAL
DEEPANSHU KAUSHAL 2021 年 9 月 3 日
回答済み: Ayush Aniket 2025 年 1 月 24 日 5:12
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??

回答 (1 件)

Ayush Aniket
Ayush Aniket 2025 年 1 月 24 日 5:12
You can write a custom objective function and use it in the fmincon MATLAB function to link a trained ANN with an optimization algorithm in MATLAB. Refer to the steps below:
1. Assuming you have a trained neural network model and saved it, you can load it using the load function.
% Load your trained neural network (replace 'yourModel.mat' with your actual file)
load('yourModel.mat', 'trainedANN');
2. The custom objective function will use the ANN to predict outputs and calculate the fitness values. This will allow us to keep the optimization in the ANN space.
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
3. Finally, 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);

カテゴリ

Help Center および File ExchangeSurrogate Optimization についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by