这学期一直在跟进 Coursera上的 Machina Learning 公开课, 老师Andrew Ng是coursera的创始人之一,Machine Learning方面的大牛。这门课程对想要了解和初步掌握机器学习的人来说是不二的选择。这门课程涵盖了机器学习的一些基本概念和方法,同时这门课程的编程作业对于掌握这些概念和方法起到了巨大的作用。
课程地址 https://www.coursera.org/learn/machine-learning
笔记主要是简要记录下课程内容,以及MATLAB编程作业....
Neural Networks: Representation
lrCostFunction.m
1 h_theta = sigmoid( X * theta);
2
3 eqsum = sum( -y .* log( h_theta) - (1-y) .* log( 1 - h_theta));
4
5 extra_fun = lambda / (2*m) * (theta' * theta - theta(1) * theta(1));
6 J = 1/m * eqsum + extra_fun;
7
8 E = h_theta - y;
9 grad = 1/m * X' * E + lambda / m .* theta;
10 grad(1) = grad(1) - lambda / m * theta(1);
oneVsAll.m
1 % Set Initial theta
2 initial_theta = zeros(n + 1, 1);
3
4 % Set options for fminunc
5 options = optimset('GradObj', 'on', 'MaxIter', 50);
6
7 % Run fmincg to obtain the optimal theta
8 % This function will return theta and the cost
9 for c = 1:num_labels
10 [all_theta(c,:)] = fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)),initial_theta, options);
11 end
predictOneVsAll.m
1 h = sigmoid( X * all_theta' ); % 5000 x 401 x 401 x 10 = 5000 x 10
2 [max_value ,p] = max(h, [], 2);
predict.m
1 X = [ones(m, 1) X];
2 % Theta1 has size 25 x 401
3 % Theta2 has size 10 x 26
4 z2 = X * Theta1'; % 5000 x 25
5 a2 = sigmoid(z2); % 5000 x 25
6 a2 = [ones(m,1) a2]; % 5000 x 26
7 z3 = a2 * Theta2'; % 5000 x 10
8 a3 = sigmoid(z3);
9
10 [max_value ,p] = max(a3, [], 2);