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

📄 neuron.cpp

📁 一个简单灵活的数据挖掘实验平台
💻 CPP
字号:
#include "..\\Core\\Exception.h"
#include "Neuron.h"
#include <cmath>
//for test
#include <ctime>
#include <iostream>
using namespace std;

Neuron::Neuron(int aft)
{
	_aft = aft;
}
Neuron::~Neuron()
{
}
//: gets the type of the neuron
int Neuron::type() const
{
	return _aft;
}
//: gets the activity of the neuron
double Neuron::activity() const
{
	return _activity;
}
//: gets the copy of the input synapses 
vector<Synapse*> Neuron::in() const
{
	return _in;
}
//: gets the copy of the output synapses
vector<Synapse*> Neuron::out() const
{
	return _out;
}
//: calculate the activity of the input synapsed
double Neuron::activate()
{
	return 0;
}

//////////////////////////////////////////////////////////////////////////
//: Layer
Layer::Layer(int size,int aft)
	: _neuron_group(size),
	  _aft(aft)
{
	for(int i=0;i<size;i++)
		_neuron_group.push_back(new Neuron(aft));
}
Layer::Layer(int aft)
	: _neuron_group(),
	  _aft(aft)
{
}
//: gets the type of the layer
int Layer::type() const
{
	return _aft;
}
//: gets the copy vector of the current neuron group
vector<Neuron*> Layer:: neuron_group() const
{
	return _neuron_group;
}
//: adds a compatible neuron node to the neuron group
void Layer::add(Neuron* neuron)
{
	if(neuron->type() != this->type())
		throw new Exception("Layer::add, neuron type not compatible!");
	_neuron_group.push_back(neuron);
}
//: removes a neuron by the index of the neuron in the layer
Neuron* Layer::remove(int index)
{
	if(index<0 || index>=_neuron_group.size())
		throw new Exception("Layer::remove, index out of bound!");
	vector<Neuron*>::iterator it;
	int i=0;
	for(it=_neuron_group.begin();it!=_neuron_group.end();it++)
	{
		if(i++==index)
		{
			_neuron_group.erase(it);
			return *it;
		}
	}
	return NULL;
}

//////////////////////////////////////////////////////////////////////////
//: Synapse
Synapse::Synapse(Neuron* forward,Neuron* backward)
{
	_forward = forward;
	_backward = backward;
	_w = random();
	_dw = 0;
}
double Synapse::random(double range)
{
	return range*(rand()-RAND_MAX/2)/RAND_MAX*2;
}
double Synapse::weight() const
{
	return _w;
}
Neuron* Synapse::forward() const
{
	return _forward;
}
Neuron* Synapse::backward() const
{
	return _backward;
}
//========================================================================
/************************************************************************
void main()
{
	clock_t start,end;
	start = clock();
	try
	{
	}
	catch(Exception* e)
	{
		cout<<e->message()<<endl;
		delete e;
	}
	end = clock();
	cout<<"Time eclipsed[s]:"<<(double)(end-start)/CLOCKS_PER_SEC<<endl;
}
/************************************************************************/

⌨️ 快捷键说明

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