【Stanford Machine Learning Open Course】12. week4编程题解

这里是斯坦福大学机器学习网络课程的学习笔记。课程地址是:https://class.coursera.org/ml-2012-002/lecture/index

  
本周编程题有:
1. 矩阵形式的正则化logistic回归方法的迭代式 (成本函数&梯度下降公式)
2. 多分类问题机器学习训练(基于正则化logistic回归方法)
3. 多分类问题预估 (基于正则化logistic方法)
4. 多分类问题预估 (基于神经网络,以及已知权重theta)
 
解答:
1. 矩阵形式的正则化logistic回归方法的迭代式 (成本函数&梯度下降公式)
function [J, grad] = lrCostFunction(theta, X, y, lambda)

m = length(y); % number of training examples
J = 0;
grad = zeros(size(theta));

%my code
J = 1/m*(-y'*log(sigmoid(X*theta))-(1-y)'*log(1-sigmoid(X*theta))) + lambda/(2*m)*(theta'*theta-theta(1)^2); grad = 1/m*((sigmoid(X*theta)-y)'*X)' + lambda/m*theta; grad(1) = grad(1)-lambda/m*theta(1); grad = grad(:); end

2. 多分类问题机器学习训练(基于正则化logistic回归方法)

function [all_theta] = oneVsAll(X, y, num_labels, lambda)

m = size(X, 1);
n = size(X, 2);
all_theta = zeros(num_labels, n + 1);
X = [ones(m, 1) X];

%my code
for i = 1:num_labels theta = zeros(size(y,1),1); initial_theta = zeros(n+1,1); options = optimset('GradObj', 'on', 'MaxIter', 50); theta = ... fmincg(@(t)(lrCostFunction(t, X, (y==i),lambda)), initial_theta, options); all_theta(i,:)=theta'; end end

3. 多分类问题预估 (基于正则化logistic方法)

function p = predictOneVsAll(all_theta, X)

m = size(X, 1);
num_labels = size(all_theta, 1);
p = zeros(size(X, 1), 1);
X = [ones(m, 1) X];

%my code
all_predict = sigmoid(X*all_theta');
[predict_values,p]=max(all_predict,[],2);

end

4. 多分类问题预估 (基于神经网络,以及已知权重theta)

function p = predict(Theta1, Theta2, X)

m = size(X, 1);
num_labels = size(Theta2, 1);

p = zeros(size(X, 1), 1);

%my code
a1 = [ones(m,1) X]; %5000*401
z2 = a1*Theta1'; %5000*25
a2 = [ones(size(z2,1),1) sigmoid(z2)]; %5000*(25+1)
z3 = a2*Theta2'; %5000*10
a3 = sigmoid(z3);

[predict_values,p] = max(a3,[],2);

end

 

posted @ 2012-10-09 13:02  liyuxia713  阅读(446)  评论(0编辑  收藏  举报