简洁的BP及RBF神经网络代码
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神经网络代码的更多相关文章
- RBF神经网络
RBF神经网络 RBF神经网络通常只有三层,即输入层.中间层和输出层.其中中间层主要计算输入x和样本矢量c(记忆样本)之间的欧式距离的Radial Basis Function (RBF)的值,输出层 ...
- RBF神经网络和BP神经网络的关系
作者:李瞬生链接:https://www.zhihu.com/question/44328472/answer/128973724来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...
- RBF神经网络学习算法及与多层感知器的比较
对于RBF神经网络的原理已经在我的博文<机器学习之径向基神经网络(RBF NN)>中介绍过,这里不再重复.今天要介绍的是常用的RBF神经网络学习算法及RBF神经网络与多层感知器网络的对比. ...
- RBF神经网络的matlab简单实现
径向基神经网络 1.径向基函数 (Radial Basis Function,RBF) 神经网络是一种性能良好的前向网络,具有最佳逼近.训练简洁.学习收敛速度快以及克服局部最小值问题的性能,目前已经证 ...
- stanford coursera 机器学习编程作业 exercise4--使用BP算法训练神经网络以识别阿拉伯数字(0-9)
在这篇文章中,会实现一个BP(backpropagation)算法,并将之应用到手写的阿拉伯数字(0-9)的自动识别上. 训练数据集(training set)如下:一共有5000个训练实例(trai ...
- RBF神经网络——直接看公式,本质上就是非线性变换后的线性变化(RBF神经网络的思想是将低维空间非线性不可分问题转换成高维空间线性可分问题)
Deeplearning Algorithms tutorial 谷歌的人工智能位于全球前列,在图像识别.语音识别.无人驾驶等技术上都已经落地.而百度实质意义上扛起了国内的人工智能的大旗,覆盖无人驾驶 ...
- 基于HHT和RBF神经网络的故障检测——第二篇论文读后感
故障诊断主要包括三部分: 1.故障信号检测方法(定子电流信号检测 [ 定子电流幅值和电流频谱 ] ,振动信号检测,温度信号检测,磁通检测法,绝缘检测法,噪声检测法) 2.故障信号的处理方法,即故障特征 ...
- RBF神经网络通用函数 newrb, newrbe
RBF神经网络通用函数 newrb, newrbe 1.newrb 其中P为输入向量,T为输出向量,GOAL为均方误差的目标,SPREED为径向基的扩展速度.返回值是一个构建好的网络,用newrb ...
- BP神经网络代码
close allclear allclcload x.txt; load y.txt; inputs = x';targets = y; % 创建一个模式识别网络(两层BP网络),同时给出中间层神经 ...
随机推荐
- 计蒜客 28437.Big brother said the calculation-线段树+二分-当前第k个位置的数 ( ACM训练联盟周赛 M)
M. Big brother said the calculation 通过线段树维护. 这个题和杭电的一道题几乎就是一样的题目.HDU5649.DZY Loves Sorting 题意就是一个n的排 ...
- 2018 JUST Programming Contest 1.0 题解
题目链接 gym101778 Problem A 转化成绝对值之后算一下概率.这个题有点像 2018 ZOJ Monthly March Problem D ? 不过那个题要难一些~ #includ ...
- 用fiddler测试移动端翻页
大家在移动端是怎么测试翻页的,肯定都是下拉或上滑吧,我也是这样测试的 但如果你要验证数据是否与pc端数据一致时,可能是第一页,第二页看看,或最后几页数据看看,在pc端看简单,直接点击最后一页就行,在移 ...
- 程设刷题 | 编译C++文件出现to_string is not a member of std 或者 to_string was not declared in this scope的解决方法
写在前面 原文链接:Enabling string conversion functions in MinGW C++在将整型.浮点型.长整型等数据类型转换为字符串时,可使用<string> ...
- C语言基础之注释与常见错误
总结起来,注释有三种: 1.单行注释 1: //哈哈 单行注释 2.多行注释 1: /* 2: asdfasdfasdfasdfasdf 3: */ 其中多行注释如果这样写 1: /* 2: * 函数 ...
- EF for oracle中无法读取配置 显示无法open问题解决方式
1.更新以上设置为 非注销部分 弄了很久很久哈.
- kubernetes监控--Prometheus
本文基于kubernetes 1.5.2版本编写 kube-state-metrics kubectl create ns monitoring kubectl create sa -n monito ...
- appium_v1.4.16版本自动化适配android7.0系统
https://www.cnblogs.com/imlvbu/p/7127940.html 要解决的问题:appium在androidV7.0系统上运行时报错 Failure [INSTALL_FAI ...
- 工具分享:GitHub的克隆工具Cl0neMast3r,轻松搞定各种测试
GitHub,相信大家并不陌生,咱搞技术的应该都会用到它,GitHub主要是进行代码工具的存储.下载等工作.今天介绍一款让我们操作GitHub相关工作变的更简单的工具, GitHub的克隆工具. Cl ...
- ASP.NET MVC学习---(一)ORM框架,EF实体数据模型简介
现如今 对象关系映射(ORM)框架 被大量的使用于企业级应用的开发 为什么要使用ORM? ADO.NET操作数据库不好吗? 我们可以仔细想想 当我们使用ADO.NET操作数据库的时候 我们需要先获取连 ...