Hyperparameter optimization for ECOC: how can I choose the variables to be minimized?

조회 수: 2 (최근 30일)
1) When training an ECOC classifier for multiclass classification, with knn as base learner, I would like to optimize only the NumNeighbors parameter.
How can it be done?
I'm now using this code:
% variable labels contains the labels of training data
tknn = templateKNN('Distance', @distKNN); % I WOULD LIKE TO USE THIS DISTANCE
N = size(XKnn,1);
c = cvpartition(N,'LeaveOut');
% Use leave one out
mdlknnCecoc = fitcecoc(XKnn,labelsRed, ...
'OptimizeHyperparameters','auto', ...
'HyperparameterOptimizationOptions',struct( 'UseParallel',...
true,'CVPartition',c), 'Learners',tknn);
resPreds = predict(mdlknnCecoc, XKnn); % I don't know why kfoldPredict function does not work
myLoss = double(sum(abs(resPreds - labels)))/double(N); % this is the loss function I wish to minimize
  댓글 수: 1
Elena Casiraghi
Elena Casiraghi 2019년 9월 22일
편집: Elena Casiraghi 2019년 9월 22일
I'm now trying to use the following code to choose the parameter to optimize (and to use my own ditance function for the knns and my own loss function). It's working, at least it seems so. Could anyone confirm me that I'm doing it right? How can I use optimizableVariable function to optimize the value argument Coding?
% nTrain = number of elements in my training set
% distKnn is my distance function
% myLoss is myLoss function
% maxK is the maximum value for the number of neighbors
num = optimizableVariable('n',[1,maxK],'Type','integer');
c = cvpartition(nTrain,'LeaveOut');
fun = @(x)kfoldLoss(fitcecoc(XKnn,labelsRed,'CVPartition',c,...
'Learners',templateKNN('Distance', @distKNN,'NumNeighbors',x.n)),'LossFun',@myLoss);
results = bayesopt(fun,num,'Verbose',0,'UseParallel',true);
% train a model with the best parameter
bestMdl = fitcecoc(XKnn,labelsRed,'CVPartition',c,...
'Learners',templateKNN('Distance', @distKNN,'NumNeighbors',results.n));

댓글을 달려면 로그인하십시오.

답변 (0개)

제품


릴리스

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by