matlab实现神经网络算法

机器学习 —— 神经网络(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:j+H(i+1),1);

     j=j+H(i+1);

     stack{i}.w=reshape(param(j+1:j+H(i)*H(i+1)),[],H(i));

     j=j+H(i)*H(i+1);
 end

参数矩阵转化为列向量

stack2param

function [param] = stack2param(stack,H)
 %
 % Argument:
 %  H     - is the number of each layer unit except the bias unit
 %  param.w     - is a cell data structure,and store the weights of all unit
 %  except the bias unit.w{1} means the first layer weights.
 %  param.b     - is a cell data structure,and store the weight of bias
 %  unit. b{1} means the first weights of the first bias unit. 
 % Output:
 %  a colume vector.
 % Functional:
 %  transfer the colume weight vector to the cell data structure.

 %initial the cVector
 param = [];

 %calculate the dimenstion of layer numbers.
 n = size(H,1);
 for i=1:n-1
     param = [param;stack{i}.b(:);stack{i}.w(:)];
     % This can be optimized. But since our stacks are relatively short, it
     % is okay
 end

前向传播

forwardpropagation

function [a,z] = forwardpropagation(data,stack)
 %input:
 %  data - is the set of samples;input value;
 %  stack - is the weights of each layer.
 %      stack{i}.w means the ith layer weight
 %      stack{i}.b means the ith layer bais.
 %output:
 %  a    -is the the output of each layer all samples;
 %      a{i} means the ith layer output.


 %the number of layers;
 n = size(stack,2)+1;
 %the number of samples;
 m = size(data,2);
 a = cell(1,n);
 z = cell(1,n);
 a{1} = data;
 for i=2:n
     z{i} = stack{i-1}.w*a{i-1}+repmat(stack{i-1}.b,[1,m]);
     a{i} = sigmoid(z{i});
 end

损失函数

costFunction

function [f,g] = nero_regression(param, data,labels,ei)
  %
  % Arguments:
  %   ei - the models information
  %   
  %   param - 
  %
  %   data - The examples stored in a matrix.  
  %       data(i,j) is the i'th coordinate of the j'th example.
  %   labels - The label for each example.  labels(j) is the j'th example's label.
  %
  %number of layers
  n = ei.layer_num;
  %number of samples;
  m = size(data,2);
  stack = param2stack(param,ei.layer_size);
  %forward propagation
  [a,z] = forwardpropagation(data,stack);
  %backward propagation
  delta = cell(1,n);
  y = zeros(size(a{n}));
  I = sub2ind(size(y), labels, 1:size(y,2));
  y(I)=1;
  delta{n} = a{n} - y;
  for i =1:n-2
      delta{n-i} = stack{n-i}.w'*delta{n-i+1}.*a{n-i}.*(1-a{n-i});
  end


  %calculate the cost
  A = log(exp(z{n})./repmat(sum(exp(z{n})),[ei.layer_size(n),1]));
  f = -1/m*sum(A(I));
  %regulazation 
  %f = f + lamda * sum(param);

  %calculate the gradient
  gstack = cell(1,n-1);
  for i=1:n-1
      %the dimension is H(i+1)*H(i);
      gstack{n-i}.w = (delta{n-i+1}*a{n-i}')/m + ei.lamda*stack{n-i}.w;
      gstack{n-i}.b = sum(delta{n-i+1},2)/m + ei.lamda*stack{n-i}.b; 
  end
  g = stack2param(gstack,ei.layer_size);

主函数

addpath ../common
addpath ../common/minFunc_2012/minFunc
addpath ../common/minFunc_2012/minFunc/compiled
addpath common

% Load the MNIST data for this exercise.
% train.X and test.X will contain the training and testing images.
%   Each matrix has size [n,m] where:
%      m is the number of examples.
%      n is the number of pixels in each image.
% train.y and test.y will contain the corresponding labels (0 to 9).
binary_digits = false;
num_classes = 10;
[train,test] = ex1_load_mnist(binary_digits);



% Add row of 1s to the dataset to act as an intercept term.
train.X = [ones(1,size(train.X,2)); train.X]; 
test.X = [ones(1,size(test.X,2)); test.X];
train.y = train.y+1; % make labels 1-based.%横向量
test.y = test.y+1; % make labels 1-based.

% Training set info
m=size(train.X,2);
n=size(train.X,1);

% Train softmax classifier using minFunc
options=[];
options.display = 'iter';
options.maxFunEvals = 1e6;
options.MaxIter = 200;
%options.Method = 'lbfgs';

%the unit number of output layer
output_num = 10;
ei=[];

%define the 
ei.lamda = 0;
%the dimension of input layer
%the layer number of model
ei.layer_num = 3;
%the number unit of each layer
ei.layer_size=[n;256;output_num];


stack = initialWeight(ei.layer_size);
param = stack2param(stack,ei.layer_size);

tic;
% param=grad_check(@nero_regression, param, 5, train.X, train.y, ei);
 param=minFunc(@nero_regression, param, options, train.X, train.y, ei);
 fprintf('Optimization took %f seconds.\n', toc);
% %theta=[theta, zeros(n,1)]; % expand theta to include the last class.
% 
% % Print out training accuracy.
 tic;
 stack = param2stack(param,ei.layer_size);
 [a,~] = forwardpropagation(train.X,stack);
 [~,pred] = max(a{ei.layer_num});
 acc_train = mean(pred==train.y);
 fprintf('train accuracy:%f\n',acc_train);
% 
% % Print out test accuracy.
 [a,~]=forwardpropagation(test.X,stack);
 [~,pred] = max(a{ei.layer_num});
 acc_test = mean(pred==test.y);
 fprintf('test accuracy:%f\n',acc_test);

最后的运行结果:

这里写图片描述

有点过拟合。。。调整一下lamda应该就可以了。

——2017.3.24

MATLAB实现神经网络算法(附代码) 在MATLAB中实现神经网络算法,通常涉及到使用MATLAB内置的神经网络工具箱,或者自己编写代码来构建和训练神经网络。下面是一个简单的示例,展示如何使用MATLAB实现一个基本的前馈神经网络,并附上相关的代码。 阅读详情

相关推荐

matlab数学建模-神经网络算法(通用神经网络工具箱函数)

3.一些常用工具箱函数(后面一些函数,我挨个试了,有的都可以实现相同效果)网络输入的积函数:将某一层加权输入与偏值相乘结果,作为改层的输入。输入层、输出层和隐含层,其中输出层和隐含层作为计算节点。和函数:通过某一层的加权输入和偏值相加作为该层的输入。权值点积函数:输入向量与权值的点积可以得到加权输入。Hopfield网络、玻尔兹曼机均属于这种类型。网络减神经元有反馈,信息处理是状态的变换。初始化函数:修正权值、偏置等参数。单层权值、偏置初始化函数。单层神经网络初始化函数。

qq_54508596的博客 1265

神经网络编程入门

<br /><br /> 本文主要内容包括: (1) 介绍神经网络基本原理,(2) AForge.NET实现前向神经网络的方法,(3) Matlab实现前向神经网络的方法 。<br /><br /><br />第0节、引例 <br />       本文以Fisher的Iris数据集作为神经网络程序的测试数据集。Iris数据集可以在http://en.wikipedia.org/wiki/Iris_flower_data_set  找到。这里我简要介绍一下Iris数据集:<br />有一批Iris花,已知这

hit_kongquan的专栏 1万+

matlab实现神经网络算法,人工神经网络matlab代码

function [presim ss net] = simnonlin( y,d,n )% y-- 时间序列数据,列向量% d-- 时间延迟参数,正整数% n--用于训练的点的个数,正整数trainset = gettrain(y,d);inputs = trainset(:,1:end-1)';targets = trainset(:,end)';net = feedforwardnet(20,'trainscg');% net = newff(inputs,targets,40);% net = tr

aifamao6的博客 2773

BP神经网络算法及Matlab实现

简单快速入手BP神经网络算法

冰封之夜的博客 1万+

bp神经网络算法matlab程序,bp神经网络的matlab实现

trainlm训练函数最有效%net=newff(P,T,31,{'tansig','purelin'},'trainlm');%创建一个新的前向神经网络net_1=newff(minmax(P),[10,1],{'tansig','purelin'},'traingdm')%当前输入层权值和阈值{1,1}inputbias=net_1.b{1}%当前网络层权值和阈值{2,1}layerbias=net_1.b{2}%设置训练参数=50;这就是p到t的映射关系。...

shirley67269的博客 5065

【老生谈算法】matlab实现BP神经网络算法——BP神经网络算法

BP神经网络是一种典型的多层前馈神经网络,通过反向传播算法进行训练,具有良好的分类和预测性能。BP算法是一种监督学习算法,通过计算输出层与期望输出之间的误差,反向传播误差来调整网络权重,从而使网络输出逐渐逼近期望输出。举例2、利用三层BP神经网络来完成非线性函数的逼近任务,其中隐层神经元个数为五个。举例3、利用三层BP神经网络来完成非线性函数的逼近任务,其中隐层神经元个数为五个。看到期望输出的范围超出,所以输出层神经元利用线性函数作为转移函数。输入X 输出D 输入X 输出D 输入X 输出D。

m0_53407570的博客 470

神经网络算法二matlab实现,模糊神经网络算法之MATLAB实现.doc

模糊神经网络算法之MATLAB实现.doc用户名 密码 提 交登录 注册 矿业人生的空间主页博客相册|个人档案 |好友 |i 贴吧 查看文章 【转】 模糊神经网络算法之 MATLAB 实现2011-05-07 1259转载自 分享最终编辑 whutzxlfunction retstr FnnTraindt,ld,tt,sp retstr-1; 输入参数赋值开始 方便调试程序用,程序调试时去掉...

weixin_34363835的博客 1002

BP神经网络算法的Matlab实现

通过本次实验,我们可以更加深入地理解BP神经网络算法的原理和实现方式,为后续的机器学习研究提供一定的帮助。BP神经网络是一种基于反向传播算法的人工神经网络,其算法流程简单易懂,具有较好的应用效果。在本次实验中,我们使用Matlab实现了BP神经网络算法,并通过示例程序演示其使用方法。在这个示例中,我们创建了一个具有10个隐藏神经元的BP神经网络,并使用4组训练数据进行训练。其中,w(j,i,l)代表第l-1层第j个神经元到第l层第i个神经元的连接权重参数,b(i,l)为第l层第i个神经元的偏置项。

BUG?不存在的! 2045

基于蝙蝠算法优化BP神经网络的数据分类算法及其MATLAB实现-附代码

基于蝙蝠算法优化BP神经网络的数据分类算法及其MATLAB实现-附代码 文章目录基于蝙蝠算法优化BP神经网络的数据分类算法及其MATLAB实现-附代码1 蝙蝠算法与BP神经网络分类模型1.1 蝙蝠算法BA1.2 one-hot编码2 基于蝙蝠算法BA优化的BP神经网络分类算法2.1 优化变量与目标函数的选取2.2 基于BA-BP分类的建模步骤3 算法参数设置4 运行结果MATLAB代码 1 蝙蝠算法与BP神经网络分类模型 BP神经网络通过自身的训练,在训练过程中调整参数,来学习输入数据和输出数据之间的映射规

CJ-leaf的博客 1万+

基于Matlab实现神经网络算法(附上100多个案例源码)

基于Matlab实现神经网络算法(附上100多个案例源码)

m0_62143653的博客 3万+

基于BP神经网络的盲均衡算法实现与Matlab应用

BP神经网络,即反向传播(Backpropagation)神经网络,是最为广泛应用的多层前馈神经网络模型之一。其通过前馈传播过程中的信号输出与期望值进行比较,并利用反向传播误差的方式来调整网络中的权重和偏置值,从而实现网络的学习和训练。盲均衡算法是一种在接收端对传输信号进行均衡处理的技术,用于改善通信信号的性能,特别是在高速数据传输过程中。它不依赖于已知的训练序列或者已知的信道特性,仅利用接收信号的统计特性来实现均衡,因此被称为“盲”算法。

weixin_35006125的博客 885

BP神经网络的Matlab实现——人工智能算法

这几天在各大媒体上接触到了人工智能机器学习,觉得很有意思,于是开始入门最简单的机器算法——神经网络训练算法(Neural Network Training);以前一直觉得机器学习很高深,到处是超高等数学、线性代数、数理统计。入坑发现确实是这样!但是呢由项目实例驱动的学习比起为考试不挂科为目的的学习更为高效、实用!在遗传算法、神经网络算法的学习入门之后觉得数学只要用心看没问题的(即使是蒙特卡洛和马尔

善水的博客 19万+

MATLAB中实现神经网络遗传算法极值寻优

神经网络是一种模仿人脑神经元工作方式的计算模型,它由大量简单的、相互连接的处理单元(神经元)组成。每个神经元接受输入信号,并基于这些信号产生输出。神经网络被广泛应用于模式识别、分类、预测等领域。神经网络工具箱提供了一系列函数和应用,用于简化神经网络的设计、仿真以及训练过程。这些包括:网络创建与初始化: 提供了多种创建网络的函数,例如patternnet等。网络训练: 使用train函数来训练网络,支持多种训练算法,如反向传播、Levenberg-Marquardt等。

weixin_35751412的博客 872

基于遗传算法的BP神经网络优化算法——Matlab实现

基于遗传算法的BP神经网络优化算法——Matlab实现 去发现同类优质开源项目:https://gitcode.com/ 项目介绍 在机器学习和数据分析领域,BP神经网络是一种广泛应用的模型,但其性能往往依赖于参数的优化。为了进一步提升BP神经网络的性能,我们开发了一个基于遗传算法的BP神经网络优化算法,并提供了完整的Matlab实现。该程序不仅适用于学术研究,还可以直接应用于实际项目中,帮助用户...

gitblog_09814的博客 958

C#与MATLAB内核集成:实现神经网络算法

MATLAB神经网络工具箱是一套强大的算法集合,广泛应用于模式识别、数据分析、预测建模以及控制系统的开发。它提供了设计、训练、可视化和仿真神经网络的函数和应用,简化了神经网络的开发流程。工具箱可以应用于多个领域,如:生物医学工程:用于疾病预测、生物信号分析等。金融工程:用于股票价格预测、风险评估等。图像处理:用于图像分类、边缘检测等。语音识别:用于语音信号的处理和识别。

weixin_30820933的博客 642

智能优化 | MATLAB实现CS-ANN布谷鸟优化人工神经网络算法

智能优化 | MATLAB实现CS-ANN布谷鸟优化人工神经网络算法 布谷鸟优化 人工神经网络算法 MATLAB实现CS-ANN布谷鸟优化人工神经网络算法

遗传算法优化神经网络—MATLAB实现

遗传算法优化神经网络MATLAB实现

爱听雨的犬猫 6232

Matlab中各种神经网络算法的使用实例

自己做实验的时候用到的一个资源,感觉还不错,分享出来,希望对你有所帮助,顺表捞点资源,无用不会发,放心下载。

上一篇: 异常向量表和中断向量表的区别
下一篇: 进程
selous
博客等级 码龄12年 89粉丝 82原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值