简体   繁体   中英

Octave repeated function minimisation

So I'm trying to perform k-fold cross-validation on a dataset, but am having problems getting octave to accept the input. First I tried using a nested squared loss function upon which to run fminunc, but Octave just says that isn't supported yet. My next attempt has a squaredLoss function file that acts upon a global dataset that I change in my main function each time, but it keeps throwing back that the dataset is undefined. As I can't pass the dataset as a parameter to fminunc, how should I work around this (or am I using globals wrong?)

for i = 1:NUMBER_OF_FOLDS
    global funcdata;
    funcdata = data{i};
    [theta(i), ~] = fminunc(squaredLoss, theta0);
endfor

Where squaredLoss(theta) uses funcdata as if it's a local variable. Any help would be greatly appreciated!

Okay, so it would seem that while Octave does not support nested functions you can use anonymous functions instead see here . So a solution to my given example would be:

function loss = squaredLoss(coef, funcdata)
    ...
endfunction

for the squaredLoss function file and this in my cross-validation loop:

for i = 1:NUMBER_OF_FOLDS
    funcdata = data{i};
    lossFunction = @(coef)squaredLoss(coef, funcdata);
    [theta(i), ~] = fminunc(lossFunction, theta0);
endfor

Good luck wandering souls!

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