Octave: fmincg and Anonymous Functions
One-vs-all Classfication 的作业里用到了 fmincg,但是语法有点奇怪:
[theta] = fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)), initial_theta, options);
我们 lrCostFunction 的定义是:
function [J, grad] = lrCostFunction(theta, X, y, lambda)
forum 里 The meaning of @(t) in fmincg ? 解释得非常好:
The
@-symbol means that what we are sending to thefmincg-function,lrCostFunction, is not a matrix, vector or number, but a function. And the(t)symbol means that it is a function oft.lrCostFunctionis a function of four varibles, but with the command@(t)lrCostFunction(t,X,(y==c),lambda)it becomas a function of one varible,t, and the rest of the varablas are set toX,(y==c), andlambda.
We use this notation becausefmincgminimize a function with respect to one varible.
进一步得知 @(t) 其实是 Anonymous Function,参 11.11.2 Anonymous Functions。
这里有点像 Adapter Pattern,在 Java 里就这么写了:
public ReturnType anony(Theta t) {
return lrCostFunction(t, X, (y == c), lambda);
}
留下评论