BP神经网络

function [W,err]=BPTrain(data,label,hiddenlayers,nodes,type)
%Train the bp artial nueral net work
%input data,label,layers,nodes,type
%data:dim*n
%label:1*n
%layers:m:number of hidden layers
%nodes:num_1;num_2...num_m
%type==1:create and train
%type==0:train
%tanh / 双曲正切: tanh(x) = sinh(x) / cosh(x)=[e^x - e^(-x)] / [e^x + e^(-x)]
%(tanh(x))'=sech^2(x)
%sech / 双曲正割: sech(x) = 1 / cosh(x) = 2 / [e^x + e^(-x)]
if type==1
%create the nureal network and train
nodes=[size(data,1);nodes];
nodes=[nodes+1;size(label,1)];
%W{1}=random(,nodes(1));
layers=hiddenlayers+2;
for i=1:layers-2
W{i}=rand(nodes(i),nodes(i+1)-1);
end
W{layers-1}=rand(nodes(layers-1),nodes(layers));
else
%do nothing
end
%train the bp network
%the termination condition
%iteration.error
iter=0;
error=inf;
maxiter=2000;
lr=0.1;
epision=0.1;
tic
while iter<maxiter&&error>epision
iter=iter+1;
error=0;
for k=1:size(data,2)
%forward process
y{1}=[data(:,k)];
v{1}=y{1};
for i=1:layers-1
y{i}=[1;y{i}];
v{i+1}=W{i}'*y{i};
y{i+1}=tanh(v{i+1});
end
%back process
error=error+abs(label(k)-y{layers});
delta=(label(k)-y{layers}).*((sech(v{layers}).^2));
W{layers-1}=W{layers-1}+lr.*(y{layers-1}*delta);
for i=layers-1:-1:2
delta=sech(v{i}).^2.*(W{i}(1:size(W{i},1)-1,:)*delta);
W{i-1}=W{i-1}+lr.*(y{i-1}*delta');
end
end
err(iter)=error;
error
end
toc

測试代码

function res=BPTest(W,data)
for k=1:size(data,2)
y=data(:,k);
for i=1:length(W)-1
y=[1;y];
y=tanh((W{i}'*y));
end
res(k)=tanh(W{i+1}'*[1;y]);
end
global rbf_sigma;
global rbf_center;
global rbf_weight;
if strcmp(traintype,'data')
traindist=pdist2(traindata,traindata);
rbf_sigma=max(max(traindist))/(scale.^2);%/(2*sqrt(sqrt(length(traindata))));
rbf_center=traindata;
Phi=exp(-traindist./rbf_sigma);
rbf_weight=inv(Phi)*trainlabel; else if strcmp(traintype,'cluster')
[Idx,C,sumD,D]=kmeans(traindata,K,'emptyaction','singleton');
traindist=pdist2(traindata,C);
Cdist=pdist2(C,C);
rbf_sigma=max(max(Cdist))/(scale.^2);%/(2*sqrt(sqrt(length(traindata))));
rbf_center=C;
Phi=exp(-traindist./rbf_sigma);
rbf_weight=inv(Phi'*Phi)*Phi'*trainlabel;
else if strcmp(traintype,'descend') end end
end

測试 代码

function predcict=RBFTest(data)

global rbf_sigma;
global rbf_center;
global rbf_weight; testdist=pdist2(data,rbf_center); predcict=exp(-testdist./(2*rbf_sigma))*rbf_weight;

简洁的BP及RBF神经网络代码的更多相关文章

  1. RBF神经网络

    RBF神经网络 RBF神经网络通常只有三层,即输入层.中间层和输出层.其中中间层主要计算输入x和样本矢量c(记忆样本)之间的欧式距离的Radial Basis Function (RBF)的值,输出层 ...

  2. RBF神经网络和BP神经网络的关系

    作者:李瞬生链接:https://www.zhihu.com/question/44328472/answer/128973724来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  3. RBF神经网络学习算法及与多层感知器的比较

    对于RBF神经网络的原理已经在我的博文<机器学习之径向基神经网络(RBF NN)>中介绍过,这里不再重复.今天要介绍的是常用的RBF神经网络学习算法及RBF神经网络与多层感知器网络的对比. ...

  4. RBF神经网络的matlab简单实现

    径向基神经网络 1.径向基函数 (Radial Basis Function,RBF) 神经网络是一种性能良好的前向网络,具有最佳逼近.训练简洁.学习收敛速度快以及克服局部最小值问题的性能,目前已经证 ...

  5. stanford coursera 机器学习编程作业 exercise4--使用BP算法训练神经网络以识别阿拉伯数字(0-9)

    在这篇文章中,会实现一个BP(backpropagation)算法,并将之应用到手写的阿拉伯数字(0-9)的自动识别上. 训练数据集(training set)如下:一共有5000个训练实例(trai ...

  6. RBF神经网络——直接看公式,本质上就是非线性变换后的线性变化(RBF神经网络的思想是将低维空间非线性不可分问题转换成高维空间线性可分问题)

    Deeplearning Algorithms tutorial 谷歌的人工智能位于全球前列,在图像识别.语音识别.无人驾驶等技术上都已经落地.而百度实质意义上扛起了国内的人工智能的大旗,覆盖无人驾驶 ...

  7. 基于HHT和RBF神经网络的故障检测——第二篇论文读后感

    故障诊断主要包括三部分: 1.故障信号检测方法(定子电流信号检测 [ 定子电流幅值和电流频谱 ] ,振动信号检测,温度信号检测,磁通检测法,绝缘检测法,噪声检测法) 2.故障信号的处理方法,即故障特征 ...

  8. RBF神经网络通用函数 newrb, newrbe

      RBF神经网络通用函数 newrb, newrbe 1.newrb 其中P为输入向量,T为输出向量,GOAL为均方误差的目标,SPREED为径向基的扩展速度.返回值是一个构建好的网络,用newrb ...

  9. BP神经网络代码

    close allclear allclcload x.txt; load y.txt; inputs = x';targets = y; % 创建一个模式识别网络(两层BP网络),同时给出中间层神经 ...

随机推荐

  1. bzoj 1879: [Sdoi2009]Bill的挑战

    题目链接 bzoj 1879: [Sdoi2009]Bill的挑战 题解 n<=15,装压吧 对所有字符串进行装压 可以预处理一个数组can[i][j]表示所有的字符串中,有哪些可以在第i位匹配 ...

  2. 【bzoj1076】【SCOI2008】【奖励关】期望最优值dp

    [pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=60582219 Description 你正在玩你 ...

  3. 解决android客户端使用soap与服务器通讯错误415

    在编写一个android client与服务器使用soap通讯,虽然能连上但不是正常的200代码,而是415,经查询是"HTTP 415 错误 – 不 支持的媒体类型(Unsupported ...

  4. 阿里云ECS在CentOS 6.9中使用Nginx提示:nginx: [emerg] socket() [::]:80 failed (97: Address family not supported by protocol)的解决方法

    说明: 1.[::]:80这个是IPv6的地址. 2.阿里云截至到今天还不支持IPv6. 解决方式: 1.普通解决方式:开启IPv6的支持,不过这个方法在阿里云行不通. vim /etc/nginx/ ...

  5. Intellij IDEA 14.x 菜单项中Compile、Make和Build的区别

    Compile.Make和Build的区别 针对Java的开发工具,一般都有Compile.Make和Build三个菜单项,完成的功能的都差不多,但是又有区别. 编译,是将源代码转换为可执行代码的过程 ...

  6. [转] IplImage, CvMat, Mat 的关系

    拼装小火车 的原文 IplImage, CvMat, Mat 的关系 opencv中常见的 与图像操作有关的数据容器有Mat,cvMat和IplImage,这三种类型都可以代表和显示图像,但是,Mat ...

  7. 制作ubuntu16.04的docker镜像

    来自http://www.jianshu.com/p/029a1ed4fd64 背景 因为笔者是在vagrant转移到docker的玩家,所以对系统镜像情有独钟.如果你是windows.mac用户,那 ...

  8. Hive删除分区

    Hive删除分区语句: alter table table_name drop if exists partition(dt=30301111)

  9. ElasticSearch的按日期排序问题

    ES中有一个sort域,类型为date,格式是: yyyy-MM-dd HH:mm:ss 但是,在实际应用中,想仅仅按yyyy-MM-dd排序.我的处理过程是,用es的script,提取出日期,然后按 ...

  10. 编译安装Apache httpd和php搭建KodExplorer网盘

    编译安装Apache httpd和php搭建KodExplorer网盘 环境说明: 系统版本    CentOS 6.9 x86_64 软件版本    httpd-2.2.31        php- ...