Coursera机器学习-吴恩达
背景:判别手写体数字,分别实现逻辑回归和神经网络,并对比正确率
% 1.读取训练集,并随机打印一下
clear ; close all; clc
input_layer_size = 400; % 20x20 Input Images of Digits
num_labels = 10; % 10 labels, from 1 to 10
%读取训练集,是手写体的图像矩阵,自动将特征放入X,预测结果放入y
load('ex3data1.mat');
m = size(X, 1);
% Randomly select 100 data points to display
rand_indices = randperm(m);
%图像的矩阵值
sel = X(rand_indices(1:100), :);
displayData(sel);
% 2.逻辑回归向量化(Vectorize Logistic Regression)
%用一些测试数据来测试代价函数
theta_t = [-2; -1; 1; 2];
% 将 5*1 向量 和 5*3 的 0.1 至 1.5 矩阵合在一起
X_t = [ones(5,1) reshape(1:15,5,3)/10];
y_t = ([1;0;1;0;1] >= 0.5);
lambda_t = 3;
[J grad] = lrCostFunction(theta_t, X_t, y_t, lambda_t);
% 函数 lrCostFunction:
function [J, grad] = lrCostFunction(theta, X, y, lambda)
m = length(y);
% 初始化参数
J = 0;
grad = zeros(size(theta));
% 假设函数:
hx = sigmoid(X * theta);
% 计算代价函数和梯度
J = 1 / m * sum((-y) .* log(hx) - (1 - y) .* log(1 - hx)) + lambda / (2 * m) * (sum(theta .^ 2) - (theta(1) ^ 2));
grad = 1 / m * (X' * (hx - y) + lambda * theta);
%这里注意theta值,theta(1)是不参与的,需要把theta(1)减去
grad(1) = grad(1) - lambda / m * theta(1);
grad = grad(:);
end
fprintf('\nCost: %f\n', J);
fprintf(' %f \n', grad);
% 3.正式开始训练模型
% 设置 惩罚系数
lambda = 0.1;
% 计算最优theta
[all_theta] = oneVsAll(X, y, num_labels, lambda);
% 函数 oneVsAll:
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];
initial_theta = zeros(n + 1, 1);
options = optimset('GradObj', 'on', 'MaxIter', 50);
for i = 1 : num_labels
% 设置 c 为手写体结果,判断y,c是否相等
c = i * ones(size(y));
[theta] = fmincg(@(t)(lrCostFunction(t, X, (y == c), lambda)), initial_theta, options);
all_theta(i, :) = theta;
end
end