2013-05-20

Machine Learning 第三波编程作业 – Multi-class Classification and Neural Networks

仅列出核心代码:

1.lrCostFunction.m

h = sigmoid(X * theta);   %   h_theta(X) : m*1
%   Cost func
J = (-log(h.')*y - log(ones(1, m) - h.')*(ones(m, 1) - y)) / m ...
    +(lambda/(2*m)) * sum(theta(2:end).^2);

%   Gradient
grad(1) = (X(:, 1).' * (h - y)) /m;

grad(2:end) = (X(:, 2:end).' * (h - y)) /m ...
    + (lambda/m) * theta(2:end);

2.oneVsAll.m

options = optimset('GradObj', 'on', 'MaxIter', 50);
initial_theta = zeros(size(X, 2), 1);
for c = 1:num_labels
    [all_theta(c, :)] = fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)),...
        initial_theta, options);   
end

3.predictOneVsAll.m

[~, p] = max(X * all_theta.', [], 2);

4.predict.m

X = [ones(size(X), 1), X]; % Add ones to the X data matrix

X1 = sigmoid(X * Theta1.');
X1 = [ones(size(X1), 1), X1]; % Add ones to the X1 data matrix

[~, p] = max(X1 * Theta2.', [], 2);

课程地址:https://www.coursera.org/course/ml

没有评论:

发表评论