Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Neural Networks on C#

, 19 Nov 2006 GPL3
The articles describes a C# library for neural network computations, and their application for several problem solving.
neuro_demo.zip
neuro_demo
Back Propagation
Approximation
AForge.Controls.dll
AForge.dll
AForge.Neuro.dll
Approximation.exe
Data Samples
sample1.csv
sample2.csv
TimeSeries
AForge.Controls.dll
AForge.dll
AForge.Neuro.dll
Data Samples
exponent.csv
growing sinusoid.csv
parabola.csv
sigmoid.csv
sinusoid.csv
TimeSeries.exe
XORProblem
AForge.Controls.dll
AForge.dll
AForge.Neuro.dll
XORProblem.exe
Simple
Delta Rule Learning
AForge.Controls.dll
AForge.dll
AForge.Neuro.dll
Classifier.exe
Data Samples
and.csv
cube.csv
or.csv
sample1.csv
sample2.csv
One-Layer Perceptron Classifier
AForge.Controls.dll
AForge.dll
AForge.Neuro.dll
Classifier.exe
Data Samples
sample1.csv
sample2.csv
Perceptron Classifier
AForge.Controls.dll
AForge.dll
AForge.Neuro.dll
Classifier.exe
Data Samples
and.csv
cube.csv
or.csv
SOM
2DOrganizing
2DOrganizing.exe
AForge.dll
AForge.Neuro.dll
Color
AForge.dll
AForge.Neuro.dll
Color.exe
TSP
AForge.Controls.dll
AForge.dll
AForge.Neuro.dll
TSP.exe
neuro_src.zip
neuro_src
Docs
AForge.Core.chm
AForge.Neuro.chm
Release
AForge.Controls.dll
AForge.dll
AForge.Neuro.dll
Samples
Neuro
Back Propagation
Approximation
App.ico
Data Samples
sample1.csv
sample2.csv
TimeSeries
App.ico
Data Samples
exponent.csv
growing sinusoid.csv
parabola.csv
sigmoid.csv
sinusoid.csv
XORProblem
App.ico
Simple
Delta Rule Learning
App.ico
Data Samples
and.csv
cube.csv
or.csv
sample1.csv
sample2.csv
One-Layer Perceptron Classifier
App.ico
Data Samples
sample1.csv
sample2.csv
Perceptron Classifier
App.ico
Data Samples
and.csv
cube.csv
or.csv
SOM
2DOrganizing
App.ico
Color
App.ico
TSP
App.ico
Sources
Controls
Core
Neuro
Activation Functions
Images
sigmoid.bmp
sigmoid_bipolar.bmp
threshold.bmp
Layers
Learning
Networks
Neurons
// AForge Neural Net Library
//
// Copyright � Andrew Kirillov, 2005-2006
// andrew.kirillov@gmail.com
//

namespace AForge.Neuro.Learning
{
	using System;

	/// <summary>
	/// Elastic network learning algorithm
	/// </summary>
	///
	/// <remarks>This class implements elastic network's learning algorithm and
	/// allows to train <see cref="DistanceNetwork">Distance Networks</see>.
	/// </remarks> 
	///
	public class ElasticNetworkLearning : IUnsupervisedLearning
	{
		// neural network to train
		private DistanceNetwork	network;

		// array of distances between neurons
		private double[] distance;

		// learning rate
		private double	learningRate = 0.1;
		// learning radius
		private double	learningRadius = 0.5;

		// squared learning radius multiplied by 2 (precalculated value to speed up computations)
		private double	squaredRadius2 = 2 * 7 * 7;
		
		/// <summary>
		/// Learning rate
		/// </summary>
		/// 
		/// <remarks>Determines speed of learning. Value range is [0, 1].
		/// Default value equals to 0.1.</remarks>
		/// 
		public double LearningRate
		{
			get { return learningRate; }
			set
			{
				learningRate = Math.Max( 0.0, Math.Min( 1.0, value ) );
			}
		}

		/// <summary>
		/// Learning radius
		/// </summary>
		/// 
		/// <remarks>Determines the amount of neurons to be updated around
		/// winner neuron. Neurons, which are in the circle of specified radius,
		/// are updated during the learning procedure. Neurons, which are closer
		/// to the winner neuron, get more update.<br /><br />
		/// Default value equals to 0.5.</remarks>
		/// 
		public double LearningRadius
		{
			get { return learningRadius; }
			set
			{
				learningRadius = Math.Max( 0, Math.Min( 1.0, value ) );
				squaredRadius2 = 2 * learningRadius * learningRadius;
			}
		}

		
		/// <summary>
		/// Initializes a new instance of the <see cref="ElasticNetworkLearning"/> class
		/// </summary>
		/// 
		/// <param name="network">Neural network to train</param>
		/// 
		public ElasticNetworkLearning( DistanceNetwork network )
		{
			this.network = network;

			// precalculate distances array
			int		neurons = network[0].NeuronsCount;
			double	deltaAlpha = Math.PI * 2.0 / neurons;
			double	alpha = deltaAlpha;

			distance = new double[neurons];
			distance[0] = 0.0;

			// calculate all distance values
			for ( int i = 1; i < neurons; i++ )
			{
				double dx = 0.5 * Math.Cos( alpha ) - 0.5;
				double dy = 0.5 * Math.Sin( alpha );

				distance[i] = dx * dx + dy * dy;

				alpha += deltaAlpha;
			}
		}


		/// <summary>
		/// Runs learning iteration
		/// </summary>
		/// 
		/// <param name="input">input vector</param>
		/// 
		/// <returns>Returns learning error - summary absolute difference between updated
		/// weights and according inputs. The difference is measured according to the neurons
		/// distance to the winner neuron.</returns>
		/// 
		public double Run( double[] input )
		{
			double error = 0.0;

			// compute the network
			network.Compute( input );
			int winner = network.GetWinner( );

			// get layer of the network
			Layer layer = network[0];

			// walk through all neurons of the layer
			for ( int j = 0, m = layer.NeuronsCount; j < m; j++ )
			{
				Neuron neuron = layer[j];

				// update factor
				double factor = Math.Exp( - distance[Math.Abs( j - winner )] / squaredRadius2 );

				// update weight of the neuron
				for ( int i = 0, n = neuron.InputsCount; i < n; i++ )
				{
					// calculate the error
					double e = ( input[i] - neuron[i] ) * factor;
					error += Math.Abs( e );
					// update weight
					neuron[i] += e * learningRate;
				}
			}
			return error;
		}

		/// <summary>
		/// Runs learning epoch
		/// </summary>
		/// 
		/// <param name="input">array of input vectors</param>
		/// 
		/// <returns>Returns summary learning error for the epoch. See <see cref="Run"/>
		/// method for details about learning error calculation.</returns>
		/// 
		public double RunEpoch( double[][] input )
		{
			double error = 0.0;

			// walk through all training samples
			foreach ( double[] sample in input )
			{
				error += Run( sample );
			}

			// return summary error
			return error;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

Andrew Kirillov
Software Developer (Senior) Cisco Systems
United Kingdom United Kingdom
Started software development at about 15 years old and it seems like now it lasts most part of my life. Fortunately did not spend too much time with Z80 and BK0010 and switched to 8086 and further. Similar with programming languages – luckily managed to get away from BASIC and Pascal to things like Assembler, C, C++ and then C#. Apart from daily programming for food, do it also for hobby, where mostly enjoy areas like Computer Vision, Robotics and AI. This led to some open source stuff like AForge.NET.
 
Going out of computers I am just a man loving his family, enjoying traveling, a bit of books, a bit of movies and a mixture of everything else. Always wanted to learn playing guitar, but it seems like 6 strings are much harder than few dozens of keyboard’s keys. Will keep progressing ...

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141220.1 | Last Updated 19 Nov 2006
Article Copyright 2006 by Andrew Kirillov
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid