matlab实现神经网络算法

调试了两天,一边理解神经网络模型,一边在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
  • 2
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值