⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kohonenmap.cpp

📁 基于VC开发的神经网络工具箱
💻 CPP
字号:
#include "../include/KohonenMap.h"
#include "../include/Exception.h"
#include "../include/File.h"
#include <cmath>
#include <fstream>
#include <cstdio>

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:"<<endl;
	for (int i = 0; i<_nNeurons; i++)
		for (int j=0; j<_nInputs; j++)
			printf("(%d,%d) = %f\n",i,j,_neurons[i]->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<minDist)
		{
			w=i;
			minDist = d;
		}
	}
	return w;
}

real
KohonenMap::getEuclideanDistance(int nrn)
{
	real ans = 0.0;
	for (int i=0; i<_nInputs; i++)
	{
		real t = _neurons[nrn]->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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -