调试了两天,一边理解神经网络模型,一边在matlab上实现。刚刚终于调试通了minst手写体识别的简单神经网络。识别度不高,破机子也跑不起来太大规模的神经网络。就选了三层神经网络。
昨天晚上调通了之后只要隐含层的节点增大,就会偶尔出现过早拟合的问题。早上起来,把权值的初始化改小了之后,发现不会出现过早拟合的问题啦。当然这个参数初始化也是有讲究的,对于这个问题我也就采用了随意初始化。
当然不管是matlab还是python都有机器学习相关的包,但是如果想要深入理解神经网络模型的执行过程,我们还是需要自己手动实现的。当然关于数学优化的问题我是使用的ng之前提到过的minfunc函数。自己应该比较蛋疼。。。
初始化权值
initialWeight函数
function [stack] = initialWeight(H)
%input:
% H ,储存每层的节点个数
%output:
% stack - 储存各层权值信息
%the number of layers;
n = size(H,1);
stack = cell(1,n-1);
for i =1:n-1
stack{i}.w = rand(H(i+1),H(i))*0.001;
stack{i}.b = zeros(H(i+1),1);
end
列向量转化
param2stack
function [stack] = param2stack(param,H)
%
% Argument:
% param - is the colume vector of all the weights and bias;
% H - is the number of each layer unit except the bias unit
% Output:
% stack.w - is a cell data structure,and store the weights of all unit
% except the bias unit.w{1} means the first layer weights.
% stack.b - is a cell data structure,and store the weight of bias
% unit. b{1} means the first weights of the first bias unit.
% Functional:
% transfer the colume weight vector to the cell data structure.
%calculate the dimenstion of layer numbers.
n = size(H,1);
%initial the data struct;
stack=cell(1,n-1);
j=0;
for i=1:n-1
stack{i}.b=param(j+1