Non Linear Regression for a surface
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have the following matrices, and their dimensions:
X (1*249 double)
Y (1*20 double)
Z (20*249 double)
I know that the relation between X and Z is a Sigmoid function ( Z = 1/exp(b(1).*X + b(2))).
I would like to find a function Z = function(X,Y), with the variables X, and Y.
2 commentaires
Réponse acceptée
S0852306
le 24 Juil 2023
Modifié(e) : S0852306
le 24 Juil 2023
@Hidd_1 "I found a function that fitt the surface with R2 = 0.92, do you have any tips how can I improve it?"
R-squared: 0.999089
RMSE: 0.1797
MAE: 0.1202
The fitted surface match your data quite well.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1440958/image.png)
The model I used is a neural net, and its mathematical model is just lots of "weighted sigmoid function".
Notation:
is point-wise sigmoid function, and
are weight matrices and bias vectors
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1440963/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1440968/image.png)
The neural net perform the following operation;
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1441628/image.png)
Neural nets are universal approximator, so if you want further improvement, use a larger net, but be careful of overfitting.
clear; clc; close all;
load('Z.mat');
Z=Inter_cubic;
% to run this script, download the tool
% at file exchange: https://tinyurl.com/wre9r5uk
%% Reshape the data to required format
X= 1:1:249;
Y = 1:0.2:4.8;
count=0;
for i=1:numel(X)
for j=1:numel(Y)
count=count+1;
input(1,count)=X(i);
input(2,count)=Y(j);
output(count)=Inter_cubic(count);
end
end
%% model set up
NN.InputAutoScaling='on'; NN.LabelAutoScaling='on'; % perform normalization x'=(x-mean(x))/std(x)
NN.ActivationFunction='Sigmoid'; % 'Gaussian' actually performs better
InSize=2; OutSize=1; % input : x,y ,output : z=f(x,y)
LayerStruct=[InSize,5,5,5,OutSize]; % change the size of network here, e.x. [2,10,10,1];
NN=Initialization(LayerStruct,NN);
%% least square solver
option.MaxIteration=500;
NN=OptimizationSolver(input,output,NN,option);
%% stats
R=FittingReport(input,output,NN);
SST=sum((output-mean(output)).^2);
SSE=sum(R.ErrorVector.^2);
Rsquared=1-SSE/SST;
%% visualization
p=NN.Evaluate(input);
[Xmesh,Ymesh]=meshgrid(X,Y);
pMatrix=reshape(p,size(Z,1),size(Z,2));
figure
scatter3(input(1,:),input(2,:),output,'.')
hold on
s=surf(Xmesh,Ymesh,pMatrix);
s.EdgeColor='none';
legend('data point','fitted surface')
3 commentaires
S0852306
le 26 Juil 2023
Modifié(e) : S0852306
le 27 Juil 2023
Type "NN.weight" and "NN.bias" in command window to get
,
, or just click the "NN" struct in your
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1443077/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1443082/image.png)
workspace, like this
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1442807/image.jpeg)
you can see all the parameters. here's the code of neural net:
(I think this code explain everything)
function FunctionOutput=ANN(data,NN)
if strcmp(NN.InputAutoScaling,'on')==1
data=NN.InputScaleVector.*data-NN.InputCenterVector; % linear transform
end
v=data;
for i=1:NN.depth-1
v=NN.weight{i}*v+NN.bias{i};
v=NN.active(v);
end
v=NN.OutActive(NN.weight{NN.depth}*v+NN.bias{NN.depth});
FunctionOutput=v;
"data" is your input vector, i.e. [x;y] (column vector)
For the output normalization, here's the code and formula:
z=NN.LabelScaleVector.*FunctionOutput+NN.LabelCenterVector;
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1443087/image.png)
Notations : ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1443092/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1443092/image.png)
Since I had performed input normalization, you need perform simple linear transform before put data into the net. you can get the coefficients (
) by just typing NN.InputScaleVector,
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1443097/image.png)
NN.InputCenterVector...
Summury
the complete computation of this NN model is:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1443102/image.png)
z is the true output of this model.
Hope this answers your question!
If not, I'll upload a live script explaining the math in detail over this weekend :)
btw, could you also leave this question in the discussion section of
question. Thanks! (I'll post a code and provide a step-by-step numerical example, it
will make these mathematical expressions much easier to understand.)
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!