用于手写识别的神经网络?

时间:2015-01-25 15:18:51

标签: matlab machine-learning neural-network

我一直在关注Andrew Ng关于机器学习的课程,我目前对手写识别工具的实现有一些疑问。

- 首先他说他使用了MNIST数据集的一个子集,其中包含5000个训练样例,每个训练样例都是20x20灰度格式的图像。有了这个,他说我们有一个长度为400个元素的向量,即#34;展开"之前描述的数据。这是否意味着火车组具有以下格式?

Training example 1 v[1,2,...,400]
Training example 2 v[1,2,...,400]
...
Training example 5000 v[1,2,...,400]

对于编码部分,作者在Matlab中提供了以下完整代码:

%% Machine Learning Online Class - Exercise 3 | Part 2: Neural Networks

%  Instructions
%  ------------
% 
%  This file contains code that helps you get started on the
%  linear exercise. You will need to complete the following functions 
%  in this exericse:
%
%     lrCostFunction.m (logistic regression cost function)
%     oneVsAll.m
%     predictOneVsAll.m
%     predict.m
%
%  For this exercise, you will not need to change any code in this file,
%  or any other files other than those mentioned above.
%

%% Initialization
clear ; close all; clc

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

%% =========== Part 1: Loading and Visualizing Data =============
%  We start the exercise by first loading and visualizing the dataset. 
%  You will be working with a dataset that contains handwritten digits.
%

% Load Training Data
fprintf('Loading and Visualizing Data ...\n')

load('ex3data1.mat');
m = size(X, 1);

% Randomly select 100 data points to display
sel = randperm(size(X, 1));
sel = sel(1:100);

displayData(X(sel, :));

fprintf('Program paused. Press enter to continue.\n');
pause;

%% ================ Part 2: Loading Pameters ================
% In this part of the exercise, we load some pre-initialized 
% neural network parameters.

fprintf('\nLoading Saved Neural Network Parameters ...\n')

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

%% ================= Part 3: Implement Predict =================
%  After training the neural network, we would like to use it to predict
%  the labels. You will now implement the "predict" function to use the
%  neural network to predict the labels of the training set. This lets
%  you compute the training set accuracy.

pred = predict(Theta1, Theta2, X);

fprintf('\nTraining Set Accuracy: %f\n', mean(double(pred == y)) * 100);

fprintf('Program paused. Press enter to continue.\n');
pause;

%  To give you an idea of the network's output, you can also run
%  through the examples one at the a time to see what it is predicting.

%  Randomly permute examples
rp = randperm(m);

for i = 1:m
    % Display 
    fprintf('\nDisplaying Example Image\n');
    displayData(X(rp(i), :));

    pred = predict(Theta1, Theta2, X(rp(i),:));
    fprintf('\nNeural Network Prediction: %d (digit %d)\n', pred, mod(pred, 10));

    % Pause
    fprintf('Program paused. Press enter to continue.\n');
    pause;
end

并且学生应该完成预测功能,我做了以下几点:

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];
% ====================== YOUR CODE HERE ======================
% Instructions: Complete the following code to make predictions using
%               your learned neural network. You should set p to a 
%               vector containing labels between 1 to num_labels.
%
% Hint: The max function might come in useful. In particular, the max
%       function can also return the index of the max element, for more
%       information see 'help max'. If your examples are in rows, then, you
%       can use max(A, [], 2) to obtain the max for each row.
%

a1 = X;
a2 = sigmoid(a1*Theta1');
a2 = [ones(m , 1) a2];
a3 = sigmoid(a2*Theta2');
[M , p] = max(a3 , [] , 2);

即使认为它运行我也不完全了解它是如何工作的(我刚刚按照作者网站上的分步说明)。我对以下内容表示怀疑:

  • 作者认为X(输入)是一个5000 x 400元素的数组,或者它有400个神经元作为输入,有10个神经元作为输出和一个隐藏层。这是否意味着5000 x 400值是训练集?

  • 作者给出了θ1和θ2的值,我认为它们是内层计算的权重,但是如何获得值?为什么他使用隐藏层的25个神经元而不是24或30?

任何帮助都会被贬低。 感谢

2 个答案:

答案 0 :(得分:2)

我前段时间做了同样的课程。

X是输入数据。因此,X是由每个400个元素的5000个向量组成的矩阵。没有训练集,因为网络是经过预先训练的。

通常训练theta 1和2的值。如何做到这一点是接下来几个讲座的主题。 (反向传播算法)

我不完全确定,为什么他使用25个神经元作为隐藏层。然而我的猜测是,这个神经元的数量只是有效,而不需要让训练步骤永远。

答案 1 :(得分:2)

让我们分解你的问题:

  

首先他说他使用的是MNIST数据集的一个子集   包含5000个训练样例,每个训练样例都是一个图像   采用20x20灰度格式。有了它,他说我们有一个向量   400个长度元素,即数据的“展开”   先前描述过。这是否意味着火车组有东西   喜欢以下格式? (...)

你走在正确的轨道上。每个训练示例都是20x20图像。在课程中介绍的最简单的神经网络模型将每个图像视为一个简单的1x400矢量(“展开”意味着这个转换)。数据集存储在矩阵中,因为这样您就可以更快地利用Octave / Matlab使用的高效线性代数库来执行计算。您不一定要将所有训练示例存储为5000x400矩阵,但这样您的代码运行速度会更快。

  

作者认为X(输入)是一个5000 x 400元素的数组,   或者它有400个神经元作为输入,10个神经元作为输出和隐藏   层。这是否意味着5000 x 400值是训练集?

“输入图层”只是输入图像。您可以将其视为已经计算出输出值的神经元或者来自网络外部的值(想想您的视网膜。它就像您视觉系统的输入层)。因此,该网络具有400个输入单元(“展开的”20x20图像)。但是,当然,您的训练集不包含单个图像,因此您将所有5000个图像放在一个5000x400矩阵中以形成训练集。

  

作者给出了我们认为的theta 1和theta 2的值   作为内层计算的权重,但是如何   获得了价值?

使用称为反向传播的算法找到这些theta值。如果您还没有在课程中实施它,请耐心等待。它很快就会出现在练习中!顺便说一句,是的,他们是重量。

  

为什么他使用隐藏层的25个神经元而不是24或30?

他可能选择了一个不太慢的任意值,也没有太差的表现。您可能会为此超参数找到更好的值。但如果你增加太多,训练程序可能会花费更长的时间。此外,由于您只使用了一小部分空洞训练集(原始MNIST有60000个训练样例和28x28图像),您需要使用“小”数量的隐藏单位来防止过度拟合。如果您使用太多单位,您的神经元将“通过心脏学习”训练示例,并且无法推广到新的看不见的数据。找到超级参数,例如隐藏单元的数量,是一种你将掌握经验的艺术(可能有贝叶斯优化和更先进的方法,但这是另一个故事xD)。