建立一个正确的hypothesis function其实关键就在于找到合适的theta,使用优化后的高级算法, fmincg(更适用于参数非常多的情况)和 fminunc均可返回一组theta和该组theta对应的cost。
oneVsAll.m(处理multiclass问题,需要对每一个class独立地训练出一个classifier)
function [all_theta] = oneVsAll(X, y, num_labels, lambda)
%ONEVSALL trains multiple logistic regression classifiers and returns all
%the classifiers in a matrix all_theta, where the i-th row of all_theta
%corresponds to the classifier for label i
% [all_theta] = ONEVSALL(X, y, num_labels, lambda) trains num_labels
% logistic regression classifiers and returns each of these classifiers
% in a matrix all_theta, where the i-th row of all_theta corresponds
% to the classifier for label i
% Some useful variables
m = size(X, 1);
n = size(X, 2);
% You need to return the following variables correctly
all_theta = zeros(num_labels, n + 1);
% Add ones to the X data matrix
X = [ones(m, 1) X];
initial_theta = zeros(n + 1, 1);%每一行代表一个classifier
options = optimset('GradObj', 'on', 'MaxIter', 50);
alltheta=[];%先创建一个空矩阵,之后每得到一个Classifier便将其theta作为一个行向量添加到这个矩阵里
for c=1:num_labels
[theta]=fmincg(@(t)(lrCostFunction(t, X, (y == c), lambda)),initial_theta, options);
%这个函数会返回一个theta列向量和该组theta所对应的的cost,若不加中括号将得到cost的值
%每一次都是利用X矩阵里所有的训练样本训练出一个类的classifier,相应的,y不是矩阵而是列向量,每个元素就是label的值
%每一个c代表一个class,y==c将返回一个向量,将向量y中等于c的置一,否则置零。对于每一组样本而言,其结果要么与c同为一,要么不同为零
%如此便达到了oneVsAll的目的
alltheta=[alltheta;theta'];
end;
all_theta=alltheta;A
end
lrCostFunction接受theta、X、y,计算cost值和用于各theta迭代的偏导数,fmincg利用这些东西迭代指定次数(迭代的是theta)得到一组theta。
lrCostFunction.m
function [J, grad] = lrCostFunction(theta, X, y, lambda)
%LRCOSTFUNCTION Compute cost and gradient for logistic regression with
%regularization
% J = LRCOSTFUNCTION(theta, X, y, lambda) computes the cost of using
% theta as the parameter for regularized logistic regression and the
% gradient of the cost w.r.t. to the parameters.
% Initialize some useful values
m = length(y); % number of training examples
% You need to return the following variables correctly
J = 0;
grad = zeros(size(theta));
h=sigmoid(X*theta);
A=-1*y'*log(h);
B=(1.-y)'*log(1.-h);
thetao=theta;
thetao(1,1)=0;
C=thetao.^2;
J=1/m*sum(A-B)+lambda/(2*m)*sum(C);%cost
grad=1/m*(X'*(h-y));
d=grad(1);
grad=grad+lambda/m*theta;
grad(1)=d;
grad = grad(:);
end
接下来输入测试数据,做出假设
predictOneVsAll.m
function p = predictOneVsAll(all_theta, X)
%PREDICT Predict the label for a trained one-vs-all classifier. The labels
%are in the range 1..K, where K = size(all_theta, 1).
% p = PREDICTONEVSALL(all_theta, X) will return a vector of predictions
% for each example in the matrix X. Note that X contains the examples in
% rows. all_theta is a matrix where the i-th row is a trained logistic
% regression theta vector for the i-th class. You should set p to a vector
% of values from 1..K (e.g., p = [1; 3; 1; 2] predicts classes 1, 3, 1, 2
% for 4 examples)
m = size(X, 1);
num_labels = size(all_theta, 1);
% You need to return the following variables correctly
p = zeros(size(X, 1), 1);
% Add ones to the X data matrix
X = [ones(m, 1) X];
[a,p]=max(X*all_theta',[],2);
%X*all_theta'得到的即为预测值,每一行代表X中每一组数据的预测结果,元素并不一定是0/1,而是一个代表了概率的介于0-1的值,最大的那一个视为预测结果
%第三个参数代表返回列向量,向量a是每行的最大值组成的,向量p则由每行最大值对应的列号组成
end
NeuralNetwork的Classification predict,已经给好theta1和theta2了
predict.m
function p = predict(Theta1, Theta2, X)
%PREDICT Predict the label of an input given a trained neural network
% p = PREDICT(Theta1, Theta2, X) outputs the predicted label of X given the
% trained weights of a neural network (Theta1, Theta2)
% Useful values
m = size(X, 1);
num_labels = size(Theta2, 1);
% You need to return the following variables correctly
p = zeros(size(X, 1), 1);
X = [ones(m, 1) X];%向待测试的数据中加入X0,每一行都是一组input,对应得到一个output的行向量
z2=X*Theta1';
a2=sigmoid(z2);
a2=[ones(m, 1) a2];
z3=a2*Theta2';
a3=sigmoid(z3);
[a,p]=max(a3,[],2);
%矩阵运算时矩阵的维数/结构经常容易搞懵,一定要想清楚了
end