www.pudn.com > 基于VC的神经网络开发程序包(源码).rar > KohonenMap.cpp
#include "../include/KohonenMap.h" #include "../include/Exception.h" #include "../include/File.h" #include#include #include using namespace std; namespace annie { KohonenMap::KohonenMap(int nWeights, int nNeurons) : Network(nWeights,nWeights) { int i; _time = 0; _sigma = 1.0; _epsilon = 0.0; _nNeurons = nNeurons; _inputs = new InputNeuron*[_nInputs]; _neurons = new SimpleNeuron*[nNeurons]; for (i = 0; i < _nInputs; i++) _inputs[i] = new InputNeuron(i); for (i = 0; i < nNeurons; i++) { _neurons[i] = new SimpleNeuron(i,false); _neurons[i]->setActivationFunction(identity,didentity); for (int j=0; j<_nInputs; j++) _neurons[i]->connect(_inputs[j],random2());//WEIGHTS[i][j]); } } KohonenMap::~KohonenMap() { int i; for (i=0; i<_nInputs; i++) delete _inputs[i]; delete _inputs; for (i=0; i<_nNeurons; i++) delete _neurons[i]; delete _neurons; } const char* KohonenMap::getClassName() { return "KohonenMap"; } real KohonenMap::getCurrEpsilon() { return _epsilon; } real KohonenMap::getCurrSigma() { return _sigma; } void KohonenMap::init(real e, real em, real s, real sm) { _time = 0; _epsilon = e; _epsilonMomentum = em; _sigma = s; _sigmaMomentum = sm; } int KohonenMap::getTime() { return _time; } void KohonenMap::setInput(VECTOR &input) { //check exception on length of input for (int i=0; i<_nInputs; i++) _inputs[i]->setValue(input[i]); } int KohonenMap::updateWeights(VECTOR &input) { if (_sigma == 0 || _epsilon == 0) return -1; setInput(input); int i,j; int winner = getWinner(); for (i=0; i<_nNeurons; i++) { SimpleNeuron *n = _neurons[i]; for (j=0; j<_nInputs; j++) { real wt = n->getWeight(_inputs[j]); real dwt = _inputs[j]->getOutput() - wt; real r = getR(winner,i); real h = (real)exp(-r*r/(2*_sigma*_sigma)); n->connect(_inputs[j],wt+_epsilon*h*dwt); } } _time++; _epsilon *= _epsilonMomentum; _sigma *= _sigmaMomentum; return winner; } int KohonenMap::updateWeights(real input[]) { VECTOR in; for (int i=0; i<_nInputs; i++) in.push_back(input[i]); return updateWeights(in); } void KohonenMap::save(const char *filename) { //throw Exception("KohonenMap::save() - Not net implemented"); cout<<"WEIGHTS:"< getWeight(_inputs[j])); } void KohonenMap::setInput(real input[]) { for (int i=0; i<_nInputs; i++) _inputs[i]->setValue(input[i]); } VECTOR KohonenMap::getOutput(VECTOR &input) { setInput(input); return getOutput(getWinner()); } VECTOR KohonenMap::getOutput(int w) { VECTOR ans; for (int i=0; i<_nInputs; i++) ans.push_back(_neurons[w]->getWeight(_inputs[i])); return ans; } int KohonenMap::getNeuronCount() { return _nNeurons; } int KohonenMap::getWinner() { int w=0; real minDist = getEuclideanDistance(0); for (int i=1; i<_nNeurons; i++) { real d = getEuclideanDistance(i); if (d getWeight(_inputs[i]) - _inputs[i]->getOutput(); ans += t*t; } ans = (real)sqrt(ans); return ans; } real KohonenMap::getWeight(int nrn, int input) { //throw Exception return _neurons[nrn]->getWeight(_inputs[input]); } void KohonenMap::setWeight(int nrn, real wt[]) { for (int i=0; i<_nInputs; i++) _neurons[nrn]->connect(_inputs[i],wt[i]); } }; //namespace annie