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

📄 ne-neuron.h

📁 neural network 一个演示原理的代码
💻 H
字号:
/*
	          _/_/_/_/   _/_/_/  _/       _/   _/_/_/_/_/
	        _/      _/    _/    _/_/     _/   _/         
	       _/      _/    _/    _/ _/    _/   _/      
	      _/      _/    _/    _/  _/   _/   _/      
	     _/      _/    _/    _/   _/  _/   _/_/_/
	    _/      _/    _/    _/    _/ _/   _/   
	   _/_/_/_/_/    _/    _/     _/_/   _/    
	  _/      _/    _/    _/       _/   _/      
	 _/      _/    _/    _/       _/   _/      
	_/      _/  _/_/_/  _/       _/   _/_/_/_/_/


	\author Hannosset Christophe
	\author hannosset@skynet.be
	\version 1.0
	\date	18 - 09 - 2004

*/

#if!defined( __AINE_NEURON_H__ )
#define __AINE_NEURON_H__

typedef double (*SK_callBack)( const double );


namespace aine
{
	/*!
		\author Hannosset Christophe
		\author c.hannosset@ainenn.org
		\version 1.0.2
		\date	26 - 07 - 2004

		\brief Neuron base objects
		\todo 
		\bug
		\warning
	*/

	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	//	NEURONOBJECTCOMPONENT
	/*!	\class NeuronObjectComponent
	 *	Container for the unique identifier of the object during instanciation and an accesor.
	 *	Maintains a map of all the valid objects .
	 */
	class NeuronObjectComponent	{

		static unsigned int counter;
		
		unsigned int	id;

			static map<unsigned int,NeuronObjectComponent *>	objList;

		double value;

	protected:
	public:
		/*!	standard constructor.
			*	increament the counter and assign a unique id
			*	record the object in the objList container
			*/
		NeuronObjectComponent( void );

		/*!	standard destructor
			*	remove the object fropm the objList
			*/
		~NeuronObjectComponent( void );

		/*!	virtual function used for a generalizationprinciple	*/
		virtual bool Save( ostream *out )	{	*out << value << " ";	return true;	}

		/*!	virtual function used for a generalizationprinciple	*/
		virtual bool Load( istream *in )	{	*in >> value;	return true;	}

	public:

		/*!	\return unique identifier of the object	*/
		unsigned int GetId( void ) const	{	return id;	}

		/*!	\return the value of the base object.	*/
		virtual double GetValue( void ) const	{	return value;	}

		/*!	As the object does not make any processing with the value, it allows exetrnal object to modify it	*/
		virtual void SetValue( const double anv )	{	value = anv;	}

		/*!	Unversal object accessor
		*	\return a pointer to the object associated with the id or 0 if none found
		*/
		static NeuronObjectComponent *GetObject( const unsigned int id );
	};

	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	//	BASELINK
	/*!	\class BaseLink
	 *	Once a link is established, it should not be modified
	 */
	class BaseLink : public NeuronObjectComponent	{
	private:
		
		unsigned int linkId;

	protected:

		BaseLink( void ) : linkId( 0 )	{}

		/*!	Assign a NeuronObjectCoponent with the BaseLink	*/
		void SetLinkId( const int id )	{	linkId = id;	}

	public:

		/*!	standard accessor to the linkid	*/
		unsigned int GetLinkId( void ) const	{	return linkId;	}

		/*!	The only public functoin allowing to establish a link
		 *	\param bl	base link that will be also linked with out object
		 */
		void Link( BaseLink *bl )
		{
			SetLinkId( bl->GetId() );
		}

		virtual bool ReceptorSite( const double v )	{	return true;	}

		virtual bool Trace( ostream *out )	{	return true;	}
	};

	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	//	DENDRITE
	class Soma;

	/*! \class Dendrite
	 *	conceptual object container of an id with alink and a value.
	 *	The member NeuronObjectComponent::value correspond to the Dendrite of the learning filter
	 */
	class Dendrite : public BaseLink	{
		friend class SynapticKnob;
		friend class Neuron;
		friend class Adjusting;

		Soma	&soma;

		double valReceived;	//<!	must maintain in memory the last value transfered

	protected:
		unsigned int ddConnectId;	//!	Needed in the back propagation leaning method...

		/*!	default constructor of the Dendrite.
		 *	by default the learing filter is set to 1.0
		 */
		Dendrite( Soma &aSoma )
		: soma( aSoma ) , valReceived( DBL_MAX ) , ddConnectId( 0 )
		{
			SetValue( 1.0 );
		}

		virtual bool ReceptorSite( const double v );

		/*	Starts the process of learning to the dendrites by minimizing the error
		 *	\param	v	expected value
		 */
		bool Adjust( const double v );

		/*!	As the learning filter */
		virtual void SetValue( const double anv )	{	BaseLink::SetValue( max( 0.0 , anv ) );	}

	public:
	
		bool ValReceivedNotNull( void ) const	{	return valReceived != 0.0;	}

		virtual bool Trace( ostream *out );
	};

	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	//	SYNAPTICKNOB
	/*! \class SynapticKnob
	 *	conceptual object container of the value the neuron transmits
	 */
	class SynapticKnob : public BaseLink {
		friend class Soma;
		friend class Neuron;
		friend class Adjusting;

		SK_callBack	cb;

	protected:

		Soma	&soma;

		unsigned int skConnectId;

		/*	Default constructor of the SynapticKnob
		 *	value will contain 0.0
		 */
		SynapticKnob( Soma &aSoma )
		: soma( aSoma ) , skConnectId( 0 )
		{
			cb = (SK_callBack)SynapticKnob::cbDefault;
		}

		/*!	The depolarization phenomenon starts at the soma to the Axon and the Synaptic knob
		 *	It is the result of the action potential of the soma.
		 *	At this level, the information is passed to the next neuron
		 *	\note the amount of information transfrered to the next neuron is between [0 , 1]
		 *	In case we have two sk connected to each other, the sk connected to another,
		 *	sk will produce inhibitory neuro-transmittors if the sk he is connected is emitting excitatory neuro-transmittors
		 */
		bool Depolarization( const double anv );

		static double cbDefault( const double aValue )	{	return aValue;	}

		/*!	in case we have two synaptic knob connected	*/
		virtual bool ReceptorSite( const double v )	{	SetValue( v );	return true;	}

		/*!	Links a synaptic knob to another. and resume with the normal link procedure	*/
		void LinkSK( SynapticKnob *bl )
		{
			BaseLink::Link( bl );
			bl->skConnectId = GetId();
		}

		/*!	The only public functoin allowing to establish a link
		 *	\param bl	base link that will be also linked with out object
		 */
		void LinkD( Dendrite *bl )
		{
			BaseLink::Link( bl );
			bl->ddConnectId = GetId();
		}

		/*!	Measure the amount of excitatory neurotransmitters, the inhibitory quantity being (1 - GetValue())
		 *	we must ensure the value is really part of [0 ; 1]	
		 */
		virtual void SetValue( const double anv )	{	BaseLink::SetValue( max( 0.0 , anv ) );	}

	public:

		virtual bool Trace( ostream *out );

		void SetCallBack( SK_callBack f )	{	cb = f;	}
	};

	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	//	SOMA
	/*! class soma
	 *	container of the link wit the dendrites and the axon
	 *	The values it processes is the threshold value above which the depolarization will be
	 */
	class Soma : public BaseLink , public vector<Dendrite *>	{
		
		friend class Dendrite;
		friend class Adjusting;

		double threshold;

	protected:

		/*!	standard constructor
		 *	by default the threshold is 0
		 */
		Soma( const double ath = 0.0 )
		: threshold( ath )
		{
			SetValue( 0.0 );
		}

		virtual bool Trace( ostream *out )	
		{	
			*out << "N" << GetId() << " [ ";
			GetSpecialTrace( out );
			*out << "]" << endl;
			for_each( begin() , end() , bind2nd( mem_fun( &Dendrite::Trace ) , out ) );
			*out << endl;

			return true;
		}

		/*!	According to the lmFlag
		 *	\return the size or the value of the soma
		 */
		double GetRatio( void );

		/*!	Allow the trhreshold of a neuron to be dynamic by allowing a connection with a Synaptic Knob	*/
		virtual bool ReceptorSite( const double v )
		{
			threshold = v;	
			return true;
		}

		/*!	Refractory period is used to reset correctly the value	*/
		virtual void Refractory( void )	
		{	
			SetValue( 0.0 );
		}

	public:

		vector<SynapticKnob *>	axon;

		/*!	sum up the new value of the soma
		 *	\return true if the depolarization will take place.
		 */
		bool ActionPotential( const double nv = 0.0 );

		/*!	The depolarization  will take place iif the ActionPotential is true	
		 *	\return true if the ActionPotential was true
	 	 */
		virtual bool Depolarization( void );
		
		/*!	Allow via inheritance to printout specific values form the inheritor of the Neuron	*/
		virtual void GetSpecialTrace( ostream *out )	{	*out << "value = " << GetValue() << ", threshold = " << GetThreshold();	}

		/*!	Assign a new threshold	*/
		void SetThreshold( const double anv )	{	threshold = anv;	}

		/*!	\return the threshold value	*/
		double GetThreshold( void ) const	{	return threshold;	}

	};

	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	//	NEURON
	/*!	\class Neuron
	 *	Cell containing the conjunction of the soma and the dendrite. The axon is also present in the algorithm
	 *	the main task of the neuron is to process the links with other neurons, to Depolarise and Refract
	 */
	class Neuron : public Soma	{
		friend class Adjusting;
	protected:

	public:
		
		/*!	standard constructor
		 *	\param alr learning rate quantifier
		 *	\param ath threshold values of the membrane of this neuron
		 */
		Neuron( const double ath = 0.0 )
		: Soma( ath ) 
		{}

		~Neuron( void );

		/*!	the conect understand that the n1 will connect with n2.
		 *	the axon of this neuron will create a new SynapticKnob that will link with newly created dendrite of n2
		 *	\return the synaptci knob of the connection
		 */
		SynapticKnob *ConnectWith( Neuron *n2 );

		/*!	Connect to a synaptic knob.
		 *	If the connected synaptic knob has a value, 
		 *	this synaptic knob will produce inhibitor neuron transmittors.
		 *	Otherwise it produces exitatory to the target the sk is connected to
		 *	\return the synaptci knob of the connection
		 */
		SynapticKnob *ConnectTo( SynapticKnob *sk );

		/*!	Connect this neuron to modify the threshold value of the target
		 *	We will create a connection to alter the treshold
		 *	\return the synaptci knob of the connection
		 */
		SynapticKnob *ConnectTo( Neuron *n2 );

		/*!	Printout on the degub stream the neuron information	*/
		virtual bool Trace( ostream *out )	
		{	
			Soma::Trace( out );
			return true;
		}
		
		/*!	The input cannot be more than 1 nor less than 0	
		 *	Start up a processing of inputs...
		 *	\return true if a depolarization can take place
		 */
		bool SetInputValue( const double anv )	{	Refractory();	return Soma::ActionPotential( max( 0.0 , anv ) );	}

		/*!	Access the value of the neuron provoking a spike on the action potential 
		 *	\return the amount that would have been spiked
		 */
		virtual double GetResult( void );

		/*!	Stars up a learning proces for this neuron
		 *	\param v the value that is expected this neuron to have if the value is not specified (DBL_MAX) then the weights are slightly adjusted
		 */
		virtual bool Adjust( const double v = DBL_MAX );

		Dendrite *GetDendrite( const unsigned int id = 0 );

		SynapticKnob *GetSynapticKnob( const unsigned int id = 0 );

		/*!	Clean up the possible residue of this neuron
		 *	We can do it as the neuron can lower slowly its threshold to zero (and spike).
		 *	We consider that for a so negligeable amount, the Synaptic knob will produce as much neurop transmitters excitatory than inhibitory.
		 */
		void Refractory( void )	{	Soma::Refractory();	}	
	};

	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	//	ADJUSTING
	/*!	\class Learning
	 *	Functor for the STL
	 */
	class Adjusting	{
	private:

		const double expected , somaValue;
		Soma *soma;

	public:
		Adjusting( Soma *asoma , const double v , const double aSomaValue )
			: soma( asoma ) , expected( v ) , somaValue( aSomaValue )
		{
		}

		
		bool operator()( Dendrite *d );

	private:

		double GetRatio( void );
	};

}

#endif

⌨️ 快捷键说明

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