简体   繁体   中英

Multiple outputs from anonymous function in fminsearch

I am trying to return results from an fminsearch optimization. I am using fminsearch to find the optimum hyperparameters (variable z) for an SVM. The anonymous function is minimizing classification error ('Crit') but I also wish to return another variable (the selected feature for given hyperparameters) obtained at the same iteration ('Features'):

fun = @(z)SVM_min_fn(Data,Labels,exp(z(1)),exp(z(2)),num_folds);
[z_opt,Crit] = fminsearch(fun,z0,opts);

function [Crit Features] = SVM_min_fn(Data,Labels,rbf_sigma,boxconstraint,num_folds)
direction = 'forward';
opts = statset('display','iter');
kernel = 'rbf';

disp(sprintf('RBF sigma: %1.4f. Boxconstraint: %1.4f',rbf_sigma,boxconstraint))
c = cvpartition(Labels,'k',num_folds);
opts = statset('display','iter','TolFun',1e-3);
fun = @(x_train,y_train,x_test,y_test)SVM_class_fun(x_train,y_train,x_test,y_test,kernel,rbf_sigma,boxconstraint);
[fs,history] = sequentialfs(fun,Data,Labels,'cv',c,'direction',direction,'options',opts);

Features = find(fs==1);        % Features selected for given sigma and C
[Crit,h] = min(history.Crit);  % Mean classification error

Is there a way to make 'fminsearch' return both 'Crit' and Features ? Saving to workspace does not work as the Features are not the correct ones for the hyperparameters returned by 'fminsearch'

It's simplest if you do one more function evaluation after fminsearch completes:

fun = @(z)SVM_min_fn(Data,Labels,exp(z(1)),exp(z(2)),num_folds);
[z_opt,Crit] = fminsearch(fun,z0,opts);

[~, Features_opt] = fun(z_opt);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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