matlab - 将函数 fminunc 与逻辑回归的 BFGS 方法进行比较

标签 matlab machine-learning octave cross-validation

我正在构建一个算法,该算法使用 BFGS 方法来查找 Octave 中二进制数据集的逻辑回归中的参数。

现在,我正在努力解决一些我认为是过度拟合问题的问题。我对多个数据集运行该算法,它实际上收敛到与 Octave 的 fminunc 函数相同的结果。然而,对于特定的“数据集类型”,该算法收敛到非常高的参数值,这与给出这些参数的合理值的 fminunc 相反。我添加了一个正则化项,并且实际上实现了我的算法收敛到 fminunc 的相同值。

这种特定类型的数据集具有可以用直线完全分隔的数据。我的问题是:为什么这对于 BFGS 方法来说是一个问题,但对于 fminunc 来说不是问题?这个函数如何在不进行正则化的情况下避免这个问题?我可以在我的算法中实现这个吗?

我的算法代码如下:

function [beta] = Log_BFGS(data, L_0)
clc 
close
%************************************************************************
%************************************************************************
%Loading the data:
[n, e] = size(data);
d = e - 1;
n; %Number of observations.
d; %Number of features.
Y = data(:, e); %Labels´ values
X_o = data(:, 1:d); 
X = [ones(n, 1) X_o]; %Features values            

%Initials conditions:
beta_0 = zeros(e, 1);

beta = [];
beta(:, 1) = beta_0;
N = 600; %Max iterations
Tol = 1e-10; %Tolerance
error = .1;
L = L_0; %Regularization parameter
B = eye(e);

options = optimset('GradObj', 'on', 'MaxIter', 600);
[beta_s] = fminunc(@(t)(costFunction(t, X, Y, L)), beta_0, options);
disp('Beta obtained with the fminunc function');
disp("--------------");
disp(beta_s)

k = 1;
a_0 = 1;
% Define the sigmoid function
h = inline('1.0 ./ (1.0 + exp(-z))'); 

while (error > Tol && k < N)
  beta_k = beta(:, k);
  x_0 = X*beta_k;
  h_0 = h(x_0);
  beta_r = [0 ; beta(:, k)(2:e, :)];
  g_k = ((X)'*(h_0 - Y) + L*beta_r)/n;
  d_k = -pinv(B)*g_k;
  a = 0.1; %I´ll implement an Armijo line search here (soon) 
  beta(:, k+1) = beta(:, k) + a*d_k;
  beta_k_1 = beta(:, k+1);
  x_1 = X*beta_k_1;
  h_1 = h(x_1);
  beta_s = [0 ; beta(:, k+1)(2:e, :)];
  g_k_1 = (transpose(X)*(h_1 - Y) + L*beta_s)/n;
  s_k = beta(:, k+1) - beta(:, k);
  y_k = g_k_1 - g_k;
  B = B - B*s_k*s_k'*B/(s_k'*B*s_k) + y_k*y_k'/(s_k'*y_k);
  k = k + 1;
  error = norm(d_k);
endwhile

%Accuracy of the logistic model:

p = zeros(n, 1);
for j = 1:n
  if (1./(1. + exp(-1.*(X(j, :)*beta(:, k)))) >= 0.5)
    p(j) = 1;
  else
    p(j) = 0;
  endif
endfor
R = mean(double(p == Y));
beta = beta(:, k);

%Showing the results:

disp("Estimation of logistic regression model Y = 1/(1 + e^(beta*X)),")
disp("using the algorithm BFGS =")
disp("--------------")
disp(beta)
disp("--------------")
disp("with a convergence error in the last iteration of:")
disp(error)
disp("--------------")
disp("and a total number of") 
disp(k-1) 
disp("iterations")
disp("--------------")
if k == N
  disp("The maximum number of iterations was reached before obtaining the desired error")
else
  disp("The desired error was reached before reaching the maximum of iterations")
endif
disp("--------------")
disp("The precision of the logistic regression model is given by (max 1.0):")
disp("--------------")
disp(R)
disp("--------------")

endfunction

我得到的数据集结果如下图所示。如果您需要这种情况下使用的数据,请告诉我。

Results of the algorithm

最佳答案

检查目标!

解向量的值很好,但整个优化是由目标驱动的。您说fminunc 给出了这些参数的合理值,但是该模型中未定义reasonable。 您的低值(value)解决方案和高值(value)解决方案都可以实现几乎相同的目标,这并非不可能。这就是这些求解器唯一关心的(当不使用调节项时)。

所以重要的问题是:是否有一个独特的解决方案(应该不允许这些结果)?仅当您的数据集具有满排名时!因此,也许您的数据存在秩不足,并且您获得了两个同样好的解决方案。当然,由于数值问题可能会存在细微的差异,这始终是错误的来源,尤其是在更复杂的优化算法中。

关于matlab - 将函数 fminunc 与逻辑回归的 BFGS 方法进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44222246/

相关文章:

Matlab:实负奇根

matlab - 使用 matlab 中交叉验证的 Knn 分类器模型预测新数据集(测试数据)的标签

optimization - Paperboat 格式在 ML 性能优化方面有什么优势?

r - R 中的偏最小二乘分类

python - Python 中 Matlab 的 'fread' 是什么?

matlab - 在 MATLAB 中使用标签向量合并矩阵

image-processing - 验证码图像字符分割

octave - Xubuntu 14.04 上的 QtOctave - 预兆未显示 - 错误 : 'create_set' undefined

floating-point - 为什么 Octave 四舍五入到 1 'earlier' 而不是 0?

matlab - 将数组集成到另一个数组上的规范方法是什么