Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » General » Downloads
 
Add your own
alternative version

Neural Network OCR

, 11 Aug 2005
Some ideas about optical character recognition using neural networks.
Prize winner in Competition "C# Jul 2005"
// AForge Neural Net Library
//
// Copyright � Andrew Kirillov, 2005
// andrew.kirillov@gmail.com
//

namespace AForge.NeuralNet
{
	using System;

	/// <summary>
	/// Neuron
	/// </summary>
	public class Neuron
	{
		protected int		inputsCount = 1;
		protected float[]	weights = new float[1];	// synapses weights
		protected float		threshold = 0.0f;
		protected IActivationFunction	function = new SigmoidFunction();

		protected float		sum;		// weighted input's sum
		protected float		output;		// neuron's output value

		protected static Random	rand = new Random();

		// Inputs count property
		public int InputsCount
		{
			get { return inputsCount; }
			set
			{
				inputsCount = Math.Max(1, value);
				weights = new float[inputsCount];
			}
		}
		// Threshold property
		public float Threshold
		{
			get { return threshold; }
			set { threshold = value; }
		}
		// Activation function property
		public IActivationFunction ActivationFunction
		{
			get { return function; }
			set { function = value; }
		}
		// Output value
		public float Output
		{
			get { return output; }
		}
		// Get/Set weight value
		public float this[int index]
		{
			get { return weights[index]; }
			set { weights[index] = value; }
		}


		// Constructors
		public Neuron()
		{ }
		public Neuron(int inputs) : this(inputs, new SigmoidFunction())
		{ }
		public Neuron(int inputs, IActivationFunction function)
		{
			this.function = function;
			InputsCount =  inputs;
		}


		// Compute the output value of the neuron
		public float Compute(float[] input)
		{
			if (input.Length != inputsCount)
				throw new ArgumentException();

			sum = 0.0f;

			// compute weighted sum of input
			for (int i = 0; i < inputsCount; i++)
			{
				sum += weights[i] * input[i];
			}
			sum -= threshold;

			return (output = function.Output(sum));
		}

		// Randomize weights
		public void Randomize()
		{
			for (int i = 0; i < inputsCount; i++)
				weights[i] = (float)(rand.NextDouble());

			threshold = (float)(rand.NextDouble());
		}
	}
}

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)

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 | Mobile
Web01 | 2.8.140721.1 | Last Updated 11 Aug 2005
Article Copyright 2005 by Andrew Kirillov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid