AForge.Neuro.Learning.SOMLearning.Run C# (CSharp) Method

Run() public method

Runs learning iteration.

The method runs one learning iterations - finds winner neuron (the neuron which has weights with values closest to the specified input vector) and updates its weight (as well as weights of neighbor neurons) in the way to decrease difference with the specified input vector.

public Run ( double input ) : double
input double Input vector.
return double
        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.Layers[0];

            // check learning radius
            if ( learningRadius == 0 )
            {
                Neuron neuron = layer.Neurons[winner];

                // update weight of the winner only
                for ( int i = 0; i < neuron.Weights.Length; i++ )
                {
                    // calculate the error
                    double e = input[i] - neuron.Weights[i];
                    error += Math.Abs( e );
                    // update weights
                    neuron.Weights[i] += e * learningRate;
                }
            }
            else
            {
                // winner's X and Y
                int wx = winner % width;
                int wy = winner / width;

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

                    int dx = ( j % width ) - wx;
                    int dy = ( j / width ) - wy;

                    // update factor ( Gaussian based )
                    double factor = Math.Exp( -(double) ( dx * dx + dy * dy ) / squaredRadius2 );

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

Usage Example

		// Worker thread
		void SearchSolution( )
		{
			// create learning algorithm
			SOMLearning	trainer = new SOMLearning( network );

			// input
			double[] input = new double[3];

			double	fixedLearningRate = learningRate / 10;
			double	driftingLearningRate = fixedLearningRate * 9;

			// iterations
			int i = 0;

			// loop
			while ( !needToStop )
			{
				trainer.LearningRate = driftingLearningRate * ( iterations - i ) / iterations + fixedLearningRate;
				trainer.LearningRadius = (double) radius * ( iterations - i ) / iterations;

				input[0] = rand.Next( 256 );
				input[1] = rand.Next( 256 );
				input[2] = rand.Next( 256 );

				trainer.Run( input );

				// update map once per 50 iterations
				if ( ( i % 10 ) == 9 )
				{
					UpdateMap( );
				}

				// increase current iteration
				i++;

				// set current iteration's info
				this.BeginInvoke ( new MethodInvoker (() => currentIterationBox.Text = i.ToString()));

				// stop ?
				if ( i >= iterations )
					break;
			}

			// enable settings controls
			EnableControls( true );
		}