Ton*_*oni 13 matlab r gradient-descent logistic-regression
短格式:
如何通过R中的梯度下降实现多类Logistic回归分类算法?可optim()当有两个以上的标签可以用吗?
MatLab代码是:
function [J, grad] = cost(theta, X, y, lambda)
m = length(y);
J = 0;
grad = zeros(size(theta));
h_theta = sigmoid(X * theta);
J = (-1/m)*sum(y.*log(h_theta) + (1-y).*log(1-h_theta)) +...
(lambda/(2*m))*sum(theta(2:length(theta)).^2);
trans = X';
grad(1) = (1/m)*(trans(1,:))*(h_theta - y);
grad(2:size(theta, 1)) = 1/m * (trans(2:size(trans,1),:)*(h_theta - y) +...
lambda * theta(2:size(theta,1),:));
grad = grad(:);
end
Run Code Online (Sandbox Code Playgroud)
和...
function [all_theta] = oneVsAll(X, y, num_labels, lambda)
m = size(X, 1);
n = size(X, 2);
all_theta = zeros(num_labels, n + 1);
initial_theta = zeros(n+1, 1);
X = [ones(m, 1) X];
options = optimset('GradObj', 'on', 'MaxIter', 50);
for c = 1:num_labels,
[theta] = ...
fmincg (@(t)(cost(t, X, (y == c), lambda)), ...
initial_theta, options);
all_theta(c,:) = theta';
end
Run Code Online (Sandbox Code Playgroud)
长格式:
虽然可能不需要遵循这个问题,但数据集可以在这里下载,一旦下载并放在R目录中,加载为:
library(R.matlab)
data <- readMat('data.mat')
str(data)
List of 2
$ X: num [1:5000, 1:400] 0 0 0 0 0 0 0 0 0 0 ...
$ y: num [1:5000, 1] 10 10 10 10 10 10 10 10 10 10 ...
Run Code Online (Sandbox Code Playgroud)
X具有5,000个示例的矩阵也是如此,每个示例包含400个特征,恰好是1到10的手写数字的20 x 20图像的400像素,例如这9:
应用逻辑回归算法来基于这400个像素中的值的"计算机视觉"来预测手写数量,这提出了不是二元决策的额外挑战.如本R-bloggers示例所示,使用ad hoc梯度下降循环优化系数不太可能有效.
基于两个解释变量(特征)和二分结果,在R-bloggers中也有一个很好的例子.该示例使用optim()R函数,这似乎是要走的路.
即使我已阅读文档,但我在设置这个更复杂的示例时遇到了问题,我们必须在10个可能的结果中做出决定:
library(R.matlab)
data <- readMat('data.mat')
X = data$X # These are the values for the pixels in all 5000 examples.
y = data$y # These are the actual correct labels for each example.
y = replace(y, y == 10, 0) # Replacing 10 with 0 for simplicity.
# Defining the sigmoid function for logistic regression.
sigmoid = function(z){
1 / (1 + exp(-z))
}
X = cbind(rep(1, nrow(X)), X) # Adding an intercept or bias term (column of 1's).
# Defining the regularized cost function parametrized by the coefficients.
cost = function(theta){
hypothesis = sigmoid(X%*%theta)
# In "J" below we will need to have 10 columns of y:
y = as.matrix(model.matrix(lm(y ~ as.factor(y))))
m = nrow(y)
lambda = 0.1
# The regularized cost function is:
J = (1/m) * sum(-y * log(hypothesis) - (1 - y) * log(1 - hypothesis)) +
(lambda/(2 * m)) * sum(theta[2:nrow(theta), 1]^2)
J
}
no.pixels_plus1 = ncol(X) # These are the columns of X plus the intercept.
no.digits = length(unique(y)) # These are the number of labels (10).
# coef matrix rows = no. of labels; cols = no. pixels plus intercept:
theta_matrix = t(matrix(rep(0, no.digits*no.pixels_plus1), nrow = no.digits))
cost(theta_matrix) # The initial cost:
# [1] 0.6931472
theta_optim = optim(par = theta_matrix, fn = cost) # This is the PROBLEM step!
Run Code Online (Sandbox Code Playgroud)
显然这似乎不完整,并给我错误信息:
Error in X %*% theta : non-conformable arguments
Run Code Online (Sandbox Code Playgroud)
请注意,X%*%theta_matrix没有任何问题.所以问题必须在于我有10个分类器(0到9),并且我不得不创建一个包含10个y列向量的矩阵,以使该操作在该函数中可行cost.有可能解决方案通过虚拟代码传递y带有某些行的向量:y = as.matrix(model.matrix(lm(y ~ as.factor(y))))就像我上面的非工作代码一样,但又一次,我不知道这封装了"一对一"的想法 - 好的,可能不是,也许这就是问题所在.
否则,它似乎适用于具有二进制分类器的R-bloggers帖子,并且与相同的代码非常平行.
那么这个问题的正确语法是什么?
请注意,我试图将其与其他所有数字进行比较,但我认为这在复杂性方面没有意义.