收藏 分销(赏)

rbf核神经网络程序matlab程序.doc

上传人:仙人****88 文档编号:12021642 上传时间:2025-08-28 格式:DOC 页数:17 大小:96KB 下载积分:10 金币
下载 相关 举报
rbf核神经网络程序matlab程序.doc_第1页
第1页 / 共17页
rbf核神经网络程序matlab程序.doc_第2页
第2页 / 共17页


点击查看更多>>
资源描述
#include "RadialBasisNetwork.h" #include "Exception.h" #include "Matrix.h" #include "File.h" using 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 network has. Each center will be * an inputs-dimensional point * @param outputs The number of outputs given by the neuron. All of them will have * a bias */ RadialBasisNetwork::RadialBasisNetwork(int inputs, int centers, int outputs, real (*CenterArray)[1024]) : Network(inputs,outputs) { int i,j; //extern real CenterArray[WORDNUM][inputs]; centroid = new real[inputs]; /// 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 = centers; /// Layer of centers, each member is a CenterNEuron _centerLayer = new Layer(1); for (i=0;i<_nCenters;i++) { for(j=0;j<inputs;j++) { centroid[j]=CenterArray[i][j]; } //CenterNeuron *c = new CenterNeuron(Layer::MAX_LAYER_SIZE*1+i,inputs); CenterNeuron *c = new CenterNeuron(Layer::MAX_LAYER_SIZE*1+i,inputs, centroid); for (j=0;j<inputs;j++) { c->connect(&_inputLayer->getNeuron(j)); } _centerLayer->addNeuron(c); } /// Layer of output, each member if a SimpleNeuron _outputLayer = new Layer(2); for (i=0;i<outputs;i++) { SimpleNeuron *n = new SimpleNeuron(Layer::MAX_LAYER_SIZE*2+i,true); n->setActivationFunction(identity,didentity); for (j=0;j<centers;j++) n->connect(&_centerLayer->getNeuron(j)); _outputLayer->addNeuron(n); } } /// Copy constructor, NOT YET IMPLEMENTED RadialBasisNetwork::RadialBasisNetwork(RadialBasisNetwork &src) : Network(src) { int i,j,lbl; int inputs = src._inputLayer->getSize(); int centers = src._centerLayer->getSize(); int outputs = src._outputLayer->getSize(); _inputLayer = new InputLayer(src._inputLayer->getLabel(),src._inputLayer->getSize()); _nCenters = src._nCenters; lbl = src._centerLayer->getLabel(); _centerLayer = new Layer(lbl); for (i=0;i<centers;i++) { CenterNeuron *c = new CenterNeuron(Layer::MAX_LAYER_SIZE*lbl+i,inputs); CenterNeuron &cSrc = (CenterNeuron&)src._centerLayer->getNeuron(i); c->setCenter(cSrc.getCenter()); for (j=0;j<inputs;j++) c->connect(&_inputLayer->getNeuron(j)); _centerLayer->addNeuron(c); } lbl = src._outputLayer->getLabel(); _outputLayer = new Layer(lbl); for (i=0;i<outputs;i++) { SimpleNeuron *n = new SimpleNeuron(Layer::MAX_LAYER_SIZE*lbl+i); SimpleNeuron &nSrc = (SimpleNeuron&)src._outputLayer->getNeuron(i); n->setBias(nSrc.getBias()); n->setActivationFunction(identity,didentity); for (j=0;j<centers;j++) { throw Exception("RadialBasisNetwork::RadialBasisNetwork() - Copy constructor not fully implemented"); } _outputLayer->addNeuron(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 *filename) : Network(0,0) { File file; int i,j; try { file.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 + "::" + getClassName() + "() - 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(); _outputLayer = new Layer(2); for (i=0;i<getOutputCount();i++) { SimpleNeuron *n = new SimpleNeuron(maxLayerSize*2+i,true); n->setActivationFunction(identity,didentity); _outputLayer->addNeuron(n); } } else if (!pare("CENTERS")) { _nCenters = file.readInt(); _centerLayer = new Layer(1); for (i=0;i<getCenterCount();i++) { CenterNeuron *n = new CenterNeuron(maxLayerSize*1+i,getInputCount()); _centerLayer->addNeuron(n); } } else if (!pare("CENTER_POINTS")) { for (i=0;i<getCenterCount();i++) { CenterNeuron &n = (CenterNeuron&)_centerLayer->getNeuron(i); VECTOR center; for (j=0;j<getInputCount();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;i<getOutputCount();i++) { if (file.eof()) break; SimpleNeuron &o = (SimpleNeuron&)_outputLayer->getNeuron(i); if (file.readChar()=='t') o.setBias(file.readDouble()); else o.removeBias(); } } else if (!pare("BEGIN_META_DATA")) { static const basic_string <char>::size_type npos = (basic_string <char>::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("Connections")) { //Connect inputs to centers for (i=0;i<getCenterCount();i++) { CenterNeuron &c = (CenterNeuron&)_centerLayer->getNeuron(i); for (j=0;j<getInputCount();j++) c.connect(&_inputLayer->getNeuron(j)); } //Connect centers to outputs for (i=0;i<getOutputCount();i++) { SimpleNeuron &o = (SimpleNeuron&)_outputLayer->getNeuron(i); for (j=0;j<getCenterCount();j++) o.connect(&_centerLayer->getNeuron(j),file.readDouble()); } } else cerr<<getClassName()<<"::"<<getClassName()<<"() - Unrecognized token ("<<s<<"). Ignoring.\n"; } // while (!file.eof()) file.close(); } RadialBasisNetwork::~RadialBasisNetwork() { delete _inputLayer; delete _centerLayer; delete _outputLayer; delete []centroid; } /** Returns the point corresponding to the ith center. * @param i The center whose point is wanted * @return The getInputCount() dimensional point corresponding to the * ith center */ VECTOR RadialBasisNetwork::getCenter(int i) { VECTOR answer; try { answer = ((CenterNeuron&)_centerLayer->getNeuron(i)).getCenter(); } catch (Exception &e) { string error(getClassName()); 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.what(); // 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 */ VECTOR RadialBasisNetwork::getOutput(VECTOR &input) { try { _inputLayer->setInput(input); return _outputLayer->getOutput(); } catch(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 */ void RadialBasisNetwork::setCenter(int i, VECTOR &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 the ith center point to the given point. * @param i The center that is to be changed * @param center The getInputCount() dimensional point */ void RadialBasisNetwork::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 the weight between the given center and output * @param center Index of the center (0<=center<getCenterCount()). * @param output Index of the output (0<=output<getOutputCount()). * @param weight The weight to give to the link between the center and output. * @throws Exception if any of the parameters given is invalid */ void RadialBasisNetwork::setWeight(int center, int output, real weight) { try { CenterNeuron &c = (CenterNeuron&)_centerLayer->getNeuron(center); SimpleNeuron &o = (SimpleNeuron&)_outputLayer->getNeuron(output); o.connect(&c,weight); } catch (Exception &e) { string error(getClassName()); 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<=center<getCenterCount()). * @param output Index of the output (0<=output<getOutputCount()). * @throws Exception if any of the parameters given is invalid */ real RadialBasisNetwork::getWeight(int center, int output) { try { SimpleNeuron &o = (SimpleNeuron&)_outputLayer->getNeuron(output); CenterNeuron &c = (CenterNeuron&)_centerLayer->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<=i<getOutputCount()). * @param bias The bias to be given to that output. * @throws Exception if that neuron isn't allowed to be biased. */ void RadialBasisNetwork::setBias(int i, real bias) { try { SimpleNeuron &n = (SimpleNeuron&)(_outputLayer->getNeuron(i)); n.setBias(bias); } catch (Exception &e) { string error(getClassName()); error = error + "::setBias() - " + e.what(); throw Exception(e); } } /** Returns the bias of the ith output * @param i The index of the output (0<=i<getOutputCount()). * @return The bias of the output. If there is no bias, it returns 0.0 */ real RadialBasisNetwork::getBias(int i) { try { SimpleNeuron &n = (SimpleNeuron&)(_outputLayer->getNeuron(i)); return n.getBias(); } catch (Exception &e) { string error(getClassName()); error = error + "::setBias() - " + e.what(); throw Exception(e); } } /** Wrapper function to allow getOutput() to work for an array * of real as input as well. * Does exactly the same thing as Network::getOutput(real*). */ VECTOR RadialBasisNetwork::getOutput(real *input) { return Network::getOutput(input); } const char * RadialBasisNetwork::getClassName() { return "RadialBasisNetwork"; } /// The number of centers in the network int RadialBasisNetwork::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 gradient descent training */ void RadialBasisNetwork::setCenterActivationFunction(ActivationFunction1 f) //RadialBasisNetwork::setCenterActivationFunction(ActivationFunction f,ActivationFunction df) { int i; for (i=0;i<getCenterCount();i++) { CenterNeuron &c = (CenterNeuron&)_centerLayer->getNeuron(i); //c.setActivationFunction(f,df); c.setActivationFunction(f); } } /** Prevents the ith output from having any bias. * @param i The index of the output (0<=i<getOutputCount()). * @throws Exception if the index given is invalid */ void RadialBasisNetwork::removeBias(int i) { try { SimpleNeuron &o = (SimpleNeuron&)_outputLayer->getNeuron(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 which input/desired-output pairs will be obtained */ void RadialBasisNetwork::trainWeights(TrainingSet &T) { if (T.getInputSize() != getInputCount())//getInputCount()继承于Network { string error(getClassName()); error = error + "::trainWeights() - Invalid TrainingSet provided."; throw Exception(error); } int output; int i,j; int p = T.getSize(); //number of training patters int h = getCenterCount(); //number of centers VECTOR in,y; //do for each output for (output=0; output<getOutputCount(); output++)// getInputCount()继承于Network { T.initialize(); bool hasBias; int effectiveH = h; SimpleNeuron &outNrn = (SimpleNeuron&)_outputLayer->getNeuron(output); //为何要强制类型转换? if (hasBias = outNrn.hasBias()) effectiveH++; //setup matrices Matrix *Y = new Matrix(p, 1);//存放p个模式下输出层各个节点的输出值 Matrix *W = NULL;//存放权值 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;i<p;i++) { T.getNextPair(in,y); getOutput(in); for (j=0;j<h;j++) { Hidden_num = j; V->elementAt(i,j) = _centerLayer->getNeuron(j).getOutput();//为第i,j个元素赋值 if (VT) VT->elementAt(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) = y[output]; } // for i=[0..p) if (VT) { Matrix *VTVinv, *VTY; try { Matrix *VTV; VTV = VT->multiply(V); VTVinv = VTV->inverse(); delete VTV; } catch (Exception &e) { string error(getClassName()); error = error + "::trainWeights() - " + e.what(); throw Exception(error); } VTY = VT->multiply(Y); W = VTVinv->multiply(VTY); delete VTVinv; delete VTY; } // if VT else { Matrix *Vinv; try { Vinv = V->inverse(); } catch (Exception &e) { string error(getClassName()); error = error + "::trainWeights() - " + e.what(); throw Exception(error); } W = Vinv->multiply(Y); delete Vinv; } //set the
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服