吴恩达机器学习练习3:Logistic regression(Feedforward propagation neural networks)

本小节使用前馈神经网络对上小节的5000个手写体进行识别。
1、神经网络模型
使用的前馈型神经网络有3层:一个输入层、一个隐藏层和一个输出层。
每个手写体的大小为20×20,因此设计输入层单元数为400(除去额外基本单元1个),输出层为10(对应10个数字1-10)。设计隐藏层的单元数为25。
其神经网络的模型如下:
吴恩达机器学习练习3:Logistic regression(Feedforward propagation neural networks)
程序已经给定了训练得到的权重参数theta,存储在ex3data1.mat中。
其中theta1表示从第1层到第2层的权重矩阵,其维度为25×401。theta2表示从第2层到第3层的权重矩阵,其维度为10×26。
2、前馈传播和预测
设置神经网络每一层的数量

%% Setup the parameters you will use for this exercise
input_layer_size  = 400;  % 20x20 Input Images of Digits
hidden_layer_size = 25;   % 25 hidden units
num_labels = 10;          % 10 labels, from 1 to 10   
                          % (note that we have mapped "0" to label 10)
load('ex3data1.mat');%导入手写样本数据
m = size(X, 1);%m为样本数量

吴恩达机器学习练习3:Logistic regression(Feedforward propagation neural networks)
其中样本数据中X表示为5000个样本,每个样本有400维特征值,y值为每个样本对应的标签值。
导入已经训练好的神经网络权重参数:

% Load the weights into variables Theta1 and Theta2
load('ex3weights.mat');

吴恩达机器学习练习3:Logistic regression(Feedforward propagation neural networks)
根据神经网络模型完成预测函数p = predict(Theta1, Theta2, X):

function p = predict(Theta1, Theta2, X)

% 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);

a1 = [ones(m,1) X];
z2 = a1*Theta1';
a2 = [ones(m,1) sigmoid(z2)];
z3 = a2*Theta2';
a3 = sigmoid(z3);

A = a3;
[max_A,p] = max(A, [], 2);

end

运行程序,得到其训练的准确度为97.52%

Training Set Accuracy: 97.520000
Program paused. Press enter to continue.

继续运行程序,随机选取手写图片进行预测。

Displaying Example Image

Neural Network Prediction: 8 (digit 8)
Paused - press enter to continue, q to exit:

Displaying Example Image

Neural Network Prediction: 8 (digit 8)
Paused - press enter to continue, q to exit:

Displaying Example Image

Neural Network Prediction: 2 (digit 2)
Paused - press enter to continue, q to exit:

Displaying Example Image

Neural Network Prediction: 6 (digit 6)
Paused - press enter to continue, q to exit:

Displaying Example Image

Neural Network Prediction: 10 (digit 0)
Paused - press enter to continue, q to exit:

Displaying Example Image

Neural Network Prediction: 6 (digit 6)
Paused - press enter to continue, q to exit:

Displaying Example Image

Neural Network Prediction: 2 (digit 2)
Paused - press enter to continue, q to exit:

Displaying Example Image

Neural Network Prediction: 10 (digit 0)
Paused - press enter to continue, q to exit:

Displaying Example Image

Neural Network Prediction: 5 (digit 5)
Paused - press enter to continue, q to exit:

Displaying Example Image

Neural Network Prediction: 5 (digit 5)
Paused - press enter to continue, q to exit:

Displaying Example Image

Neural Network Prediction: 9 (digit 9)
Paused - press enter to continue, q to exit:

Displaying Example Image

Neural Network Prediction: 6 (digit 6)
Paused - press enter to continue, q to exit:

Displaying Example Image

Neural Network Prediction: 5 (digit 5)
Paused - press enter to continue, q to exit:

Displaying Example Image

Neural Network Prediction: 6 (digit 6)
Paused - press enter to continue, q to exit:

Displaying Example Image

Neural Network Prediction: 8 (digit 8)
Paused - press enter to continue, q to exit:

Displaying Example Image

Neural Network Prediction: 1 (digit 1)
Paused - press enter to continue, q to exit:

Displaying Example Image

Neural Network Prediction: 2 (digit 2)
Paused - press enter to continue, q to exit:

Displaying Example Image

Neural Network Prediction: 1 (digit 1)
Paused - press enter to continue, q to exit:

Displaying Example Image

Neural Network Prediction: 5 (digit 5)
Paused - press enter to continue, q to exit:

Displaying Example Image

Neural Network Prediction: 10 (digit 0)
Paused - press enter to continue, q to exit: