简体   繁体   English

MATLAB中针对成本函数的约束优化,其中传递了额外的参数

[英]Constrain optimization in MATLAB for cost function which have extra parameter passing to it

I'm an electrical engineer who not familiar with MATLAB. 我是一位不熟悉MATLAB的电气工程师。 My question is how can I pass other variable that use for calculate cost function when I call "fmincon"command. 我的问题是,当我调用“ fmincon”命令时,如何传递用于计算成本函数的其他变量。 First I start with making a cost function name "Sum_Square_error.m" for calculate sum square error from estimated output (output that estimate from using Neural network) and real output.In this function I will use weight from welled train network multiply with shrinkage coefficient matrix(c) and call it "modify_input_weight". 首先,我首先创建一个成本函数名称“ Sum_Square_error.m”,以根据估计的输出(使用神经网络进行估计的输出)和实际输出计算平方和误差。在此函数中,我将使用训练好的火车网络的权重乘以收缩系数矩阵(c),并将其称为“ modify_input_weight”。 Then I evaluate neural network with modify_input_weight.So I get the estimate output. 然后我用Modify_input_weight评估神经网络,从而得到估计输出。 After that I get sum square error. 之后,我得到平方和错误。 My objective is to minimize Sum_square_error by adjust weight of neural network by using shrinkage coefficient matrix(c). 我的目标是通过使用收缩系数矩阵(c)来调整神经网络的权重,以将Sum_square_error最小化。

I have already read "fmincon" function reference. 我已经阅读了“ fmincon”功能参考。 I can passing extra parameter for by three method 1. Anonymous Functions 2. Nested Functions 3. Global Variables For this kind of problem which method is best fit. 我可以通过三种方法传递额外的参数:1.匿名函数2.嵌套函数3.全局变量对于这种问题,哪种方法最合适。 I tried to use Anonymous Functions like this 我试图像这样使用匿名函数

------------------------------------------- Sum_Square_error.m ------------------------------------------------------- f = @(c) Sum_Square_error(c,input_weight,X_test,Y_test); ------------------------------------------- Sum_Square_error.m ---- -------------------------------------------------- -f = @(c)Sum_Square_error(c,input_weight,X_test,Y_test);

for i=1:10

    modify_input_weight(:,i) = c(i,1)*input_weight(:,i);

end

net.IW{1,1}= modify_input_weight;   
y = net(X_test);
e = gsubtract(Y_test,y);
f = sum(e)^2;     

end 结束

-------------------------------------------------- Main program ---------------------------------------------------------- --------------------------------------------------主程序------------------------------------------------ ----------

A  = ones(1,10);
b  = s;
lb = zeros(1,10); 
[c,fval] = fmincon(@Sum_Square_error,c0,A,b,[],[],lb,[]); 

but after I tried to run this program, it show many error message. 但是在我尝试运行此程序后,它显示了许多错误消息。 Could someone please help me to passing "c,input_weight,X_test,Y_test" to optimize this cost function. 有人可以帮我传递“ c,input_weight,X_test,Y_test”来优化此成本函数。

in Sum_Square_error.m you dont declare an anonymous function make it a regular function so change Sum_Square_error.mSum_Square_error.m声明匿名函数使其成为常规函数,因此请更改

f = @(c) Sum_Square_error(c,input_weight,X_test,Y_test);

to

function f = Sum_Square_error(c,input_weight,X_test,Y_test)

now in your main, when you say @fun you actually need to pass in the paremeters explicitly 现在在您的主体中,当您说@fun时,您实际上需要显式地传递参数

fmincon(@Sum_Square_error(c,input_Weight,X_test,Y_test),c0,A,b,[],[],lb,[]); 

where you replace c,input_Weight,X_test,Y_test with actual data 在这里用实际数据替换c,input_Weight,X_test,Y_test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM