rbf核神经网络程序matlab程序_第1页
rbf核神经网络程序matlab程序_第2页
rbf核神经网络程序matlab程序_第3页
rbf核神经网络程序matlab程序_第4页
rbf核神经网络程序matlab程序_第5页
已阅读5页,还剩29页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实用标准文案文档#include RadialBasisNetwork.h#include Exception.h#include Matrix.h#include File.husing namespace std;namespace annie/* Creates a Radial basis function network. All the outputs will have a bias.* param inputs Number of inputs taken in by the network* param centers Number of centers the networ

2、k has. Each center will be* an inputs-dimensional point* param outputs The number of outputs given by the neuron. All of themwill have* a biasreal*/RadialBasisNetwork:RadialBasisNetwork(int inputs, int centers, int outputs,(*CenterArray)1024) : Network(inputs,outputs)int i,j;/extern real CenterArray

3、WORDNUMinputs;centroid = new realinputs;/ Layer of input. Each member is an InputNeuron/InputLayer *_inputLayer;_inputLayer = new InputLayer(0,inputs);/* Number of centers in the network.* If you plan to extend this class, then the onus of keeping this value* consistent lies on you*/_nCenters = cent

4、ers;/ Layer of centers, each member is a CenterNEuron_centerLayer = new Layer(1);for (i=0;i_nCenters;i+)for( j=0;jinputs;j+)centroidj=CenterArrayij;new/CenterNeuron *c = CenterNeuron(Layer:MAX_LAYER_SIZE*1+i,inputs);CenterNeuron *c = new CenterNeuron(Layer:MAX_LAYER_SIZE*1+i,inputs, centroid);for (j

5、=0;jconnect(&_inputLayer-getNeuron(j);_centerLayer-addNeuron(c);/ Layer of output, each member if a SimpleNeuron_outputLayer = new Layer(2);for (i=0;isetActivationFunction(identity,didentity);for (j=0;jconnect(&_centerLayer-getNeuron(j);_outputLayer-addNeuron(n);/ Copy constructor, NOT YET IMPLEMENT

6、EDRadialBasisNetwork:RadialBasisNetwork(RadialBasisNetwork &src) : Network(src) int i,j,lbl;int inputs = src._inputLayer-getSize();int centers = src._centerLayer-getSize();int outputs = src._outputLayer-getSize();new_inputLayer =InputLayer(src._inputLayer-getLabel(),src._inputLayer-getSize();_nCente

7、rs = src._nCenters;lbl = src._centerLayer-getLabel();_centerLayer = new Layer(lbl);for (i=0;igetNeuron(i);实用标准文案c-setCenter(cSrc.getCenter();for (j=0;jconnect(&_inputLayer-getNeuron(j);_centerLayer-addNeuron(c);lbl = src._outputLayer-getLabel();_outputLayer = new Layer(lbl);for (i=0;igetNeuron(i);n-

8、setBias(nSrc.getBias();n-setActivationFunction(identity,didentity);for (j=0;jaddNeuron(n);文档实用标准文案/* Loads a network from a text file* see save* param filename Name of the file from which to load network structure* throws Exception On any error*/RadialBasisNetwork:RadialBasisNetwork(const char *file

9、name) : Network(0,0)File file;int i,j;tryfile.open(filename);catch (Exception &e)string error(getClassName();error = error + : + getClassName() + () - + e.what();throw Exception(error);string s;s=file.readWord();if (pare(getClassName()!=0)string error(getClassName();error = error + : + getClass

10、Name() + () - File supplied is not about this type of network.;throw Exception(error);int maxLayerSize = Layer:MAX_LAYER_SIZE;while (!file.eof()s=file.readWord();if (!pare(INPUTS)_nInputs=file.readInt();_inputLayer = new InputLayer(0,_nInputs);else if (!pare(OUTPUTS)_nOutputs=file.readInt(

11、);_outputLayer = new Layer(2);for (i=0;isetActivationFunction(identity,didentity);_outputLayer-addNeuron(n);else if (!pare(CENTERS)_nCenters = file.readInt();_centerLayer = new Layer(1);for (i=0;iaddNeuron(n);else if (!pare(CENTER_POINTS)for (i=0;igetNeuron(i);VECTOR center;for (j=0;jgetIn

12、putCount();j+)center.push_back(file.readDouble(); n.setCenter(center);else if (!pare(MAX_LAYER_SIZE)maxLayerSize=file.readInt();else if (!pare(Biases)for (i=0;igetNeuron(i);if (file.readChar()=t)o.setBias(file.readDouble();elseo.removeBias();else if (!pare(BEGIN_META_DATA)static const

13、 basic_string :size_type npos = (basic_string :size_type)-1;string end(END_META_DATA);string metaData;s = file.readLine();while (s.find(end,0)=npos)metaData = metaData + s + n;s = file.readLine();if (metaData.length()0)metaData.erase(metaData.length()-1);setMetaData(metaData);else if (!pare(Con

14、nections)/Connect inputs to centersfor (i=0;igetNeuron(i);for (j=0;jgetNeuron(j);/Connect centers to outputsfor (i=0;igetNeuron(i);for (j=0;jgetNeuron(j),file.readDouble();elsecerrgetClassName():getClassName()() - Unrecognizedtoken (sgetNeuron(i).getCenter();catch (Exception &e)string error(getClass

15、Name();error = error + :getCenter() - + e.what();throw Exception(error);return answer;/CenterNeuron&/RadialBasisNetwork:getCenterNeuron(int i)/ try/ / return (CenterNeuron&)(_centerLayer-getNeuron(i);/ / catch (Exception &e)/ /string error(getClassName();/error = error + :getCenterNeuron() - + e.wha

16、t();/throw Exception(error);/ /* Returns the output of the network for the given input.* param input A vector of getDimension() reals* return The corresponding output of the network*/VECTORRadialBasisNetwork:getOutput(VECTOR &input)try_inputLayer-setInput(input);return _outputLayer-getOutput();catch

17、(Exception e)string error(getClassName();error = error + :getOutput() - +e.what();throw Exception(error);/* Sets the ith center point to the given point.* param i The center that is to be changed* param center The getInputCount() dimensional point*/voidRadialBasisNetwork:setCenter(int i, VECTOR &cen

18、ter)tryCenterNeuron &c = (CenterNeuron&)_centerLayer-getNeuron(i);c.setCenter(center);catch (Exception &e)string error(getClassName();error = error + :setCenter() - + e.what();throw Exception(e);/* Sets the ith center point to the given point.* param i The center that is to be changed* param center

19、The getInputCount() dimensional point*/voidRadialBasisNetwork:setCenter(int i, real *center)try文档实用标准文案CenterNeuron &c = (CenterNeuron&)_centerLayer-getNeuron(i); c.setCenter(center);catch (Exception &e)string error(getClassName();error = error + :setCenter() - + e.what();throw Exception(e);/* Sets

20、the weight between the given center and output* param center Index of the center (0=centergetCenterCount().* param output Index of the output (0=outputgetNeuron(center);SimpleNeuron &o = (SimpleNeuron&)_outputLayer-getNeuron(output);o.connect(&c,weight);catch (Exception &e)string error(getClassName(

21、);error = error + :setWeight() - + e.what();throw Exception(error);/* Returns the weight of the link between the given center and output* param center Index of the center (0=centergetCenterCount().* param output Index of the output (0=outputgetNeuron(output);CenterNeuron &c = (CenterNeuron&)_centerL

22、ayer-getNeuron(center); return o.getWeight(&c);catch (Exception &e)string error(getClassName();error = error + :getWeight() - + e.what();throw Exception(error);/* Sets the bias of the ith output.* param i The index of the output (0=igetNeuron(i);n.setBias(bias);catch (Exception &e)string error(getCl

23、assName();error = error + :setBias() - + e.what();throw Exception(e);/* Returns the bias of the ith output* param i The index of the output (0=igetNeuron(i);文档实用标准文案return n.getBias();catch (Exception &e)string error(getClassName();error = error + :setBias() - + e.what();throw Exception(e);/* Wrappe

24、r function to allow getOutput() to work for an array* of real as input as well.* Does exactly the same thing as Network:getOutput(real*). */VECTORRadialBasisNetwork:getOutput(real *input) return Network:getOutput(input); const char * RadialBasisNetwork:getClassName() return RadialBasisNetwork; / The

25、 number of centers in the networkintRadialBasisNetwork:getCenterCount() return _nCenters;/* Sets the activation function of the center neurons.* (The activation function is gaussian by default)* param f The activation function to be used.* param df The derivation of the activation function, used in

26、gradient descent training*/voidRadialBasisNetwork:setCenterActivationFunction(ActivationFunction1 f) /RadialBasisNetwork:setCenterActivationFunction(ActivationFunction f,ActivationFunction df)int i;for (i=0;igetNeuron(i);/c.setActivationFunction(f,df);c.setActivationFunction(f);/* Prevents the ith o

27、utput from having any bias.* param i The index of the output (0=igetNeuron(i);o.removeBias();catch (Exception &e)string error(getClassName();error = error + :removeBias() - + e.what();throw Exception(error);/* Trains the weights of the network, centers are kept fixed.* param T The TrainingSet from w

28、hich input/desired-output pairs will beobtained*/voidRadialBasisNetwork:trainWeights(TrainingSet &T)if (T.getInputSize() != getInputCount()/getInputCount()继承于 Networkstring error(getClassName();error = error + :trainWeights() - Invalid TrainingSet provided.;throw Exception(error);int output;int i,j;

29、int p = T.getSize();/number of training pattersint h = getCenterCount();/number of centersVECTOR in,y;继承于/do for each outputfor (output=0; outputgetNeuron(output);/ 为何要强制类型转换?if (hasBias = outNrn.hasBias()effectiveH+;/setup matricesMatrix *Y = new Matrix(p, 1);/ 存放 p 个模式下输出层各个节点的输出值Matrix *W = NULL;

30、/ 存放权值Matrix *V = new Matrix(p, effectiveH);/ 存放 p 个模式各自隐节点输出值 Matrix *VT = NULL;if (p!=effectiveH)VT = new Matrix(effectiveH ,p);extern int Hidden_num;for (i=0;ip;i+)T.getNextPair(in,y);getOutput(in);for (j=0;jelementAt(i,j) = _centerLayer-getNeuron(j).getOutput();/ 为 第 i,j 个元素赋值if (VT)VT-elementAt

31、(j,i) = V-elementAt(i,j);if (hasBias)V-elementAt(i,j) = 1.0;if (VT)VT-elementAt(j,i) = 1.0;Y-elementAt(i,0) = youtput; / for i=0.p)if (VT)Matrix *VTVinv, *VTY;tryMatrix *VTV;VTV = VT-multiply(V);VTVinv = VTV-inverse();delete VTV;catch (Exception &e)string error(getClassName();error = error + :trainW

32、eights() - + e.what();throw Exception(error);VTY = VT-multiply(Y);W = VTVinv-multiply(VTY);delete VTVinv;delete VTY; / if VTelseMatrix *Vinv;tryVinv = V-inverse();catch (Exception &e)string error(getClassName();error = error + :trainWeights() - + e.what();throw Exception(error);W = Vinv-multiply(Y);

33、delete Vinv;/set the weightsfor (j=0;jelementAt(j,0);if (hasBias)outNrn.setBias(W-elementAt(j,0);/cleanupdelete Y;delete W;delete V;if (VT)delete VT; / for (output = 1.total outputs)/Save the network to a text filevoidRadialBasisNetwork:save(const char *filename)ofstream s;int i,j;s.open(filename,io

34、s:out);if (!s)string error(getClassName();error = error + :save() - Could not open file for writing.;throw Exception(error);sANNIE_FILE ANNIE_VERSIONendl;type offollowings# Network information, the next line identifies the networkendl;s# Constructing a network from a file which doesnt have the ident

35、ifierendl;s# should result in an error endl;s# DO NOT MAKE FORMAT CHANGES TO THIS FILEendl; sRadialBasisNetworkendl;sMAX_LAYER_SIZE Layer:MAX_LAYER_SIZEendl;sINPUTS getInputCount()endl;sCENTERS getCenterCount()endl;sOUTPUTS getOutputCount()endl; snBEGIN_META_DATAendl;if (!getMetaData().empty()sgetMe

36、taData()endl;sEND_META_DATAnendl;sendl;s# Below follows description of the centers. One center per line.endl;s# Each center is a point in d-dimensional space where d = the number of inputsendl;s#文档实用标准文案endl; sCENTER_POINTSendl;for (i=0; igetNeuron(i);VECTOR center = c.getCenter();VECTOR:iterator it

37、;for (it = center.begin(); it!=center.end(); it+)s(*it) ;sendl;sendl;s# Now the biases for each of the outputs. One line per output.endl;has as# Line starts with a t followed by the bias if the neuron biasendl;s# otherwise line has an f indicating no biasendl;s#endl;sBiasesendl;for (i=0;igetNeuron(i);文档实用标准文案if (o.hasBias()st o.getBias();elsesf

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论