如何更正网格搜索?

时间:2019-12-14 21:35:27

标签: matlab svm hyperparameters

尝试使用网格搜索为我的svm模型找到最佳超参数,但它只会为超参数返回1。

Failed to load resource: the server responded with a status of 404 (Not Found)

内部折叠交叉验证功能 并在出现某些问题的网格函数下方

function evaluations = inner_kfold_trainer(C,q,k,features_xy,labels)

features_xy_flds = kdivide(features_xy, k);
labels_flds = kdivide(labels, k);

evaluations = zeros(k,3);

for i = 1:k

    fprintf('Fold %i of %i\n',i,k);

    train_data =  cell2mat(features_xy_flds(1:end ~= i));
    train_labels = cell2mat(labels_flds(1:end ~= i));
    test_data = cell2mat(features_xy_flds(i));
    test_labels = cell2mat(labels_flds(i));

    %AU1 
    train_labels = train_labels(:,1);
    test_labels = test_labels(:,1);


    [k,~] = size(test_labels);

    %train
    sv = fitcsvm(train_data,train_labels, 'KernelFunction','polynomial', 'PolynomialOrder',q,'BoxConstraint',C);
    sv.predict(test_data);

    %Calculate evaluative measures
    %svm_outputs = zeros(k,1);
    sv_predictions = sv.predict(test_data);


    [precision,recall,F1] = evaluation(sv_predictions,test_labels);
    evaluations(i,1) = precision;
    evaluations(i,2) = recall;
    evaluations(i,3) = F1;


end

save('eval.mat', 'evaluations');

end

例如,当我运行function [q,C] = grid_search(features_xy,labels,k) % n x n grid n = 3; q_grid = linspace(1,19,n); C_grid = linspace(1,59,n); tic evals = zeros(n,n,3); for i = 1:n for j = 1:n fprintf('## i=%i, j=%i ##\n', i, j); svm_results = inner_kfold_trainer(C_grid(i), q_grid(j),k,features_xy,labels); evals(i,j,:) = mean(svm_results(:,:)); % precision only %evals(i,j,:) = max(svm_results(:,1)); toc end end f = evals; % retrieving the best value of the hyper parameters, to use in the outer % fold [M1,I1] = max(f); [~,I2] = max(M1(1,1,:)); index = I1(:,:,I2); C = C_grid(index(1)) q = q_grid(index(2)) end 时,对于任何k(折数)值,我都会得到C = 1和q = 1。另外features_xy是500 * 98矩阵。

0 个答案:

没有答案