Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            FeedforwardNetwork network = new FeedforwardNetwork();

            network.AddLayer(new FeedforwardLayer(2));
            network.AddLayer(new FeedforwardLayer(3));
            network.AddLayer(new FeedforwardLayer(1));
            network.Reset();

            // train the neural network
            NeuralSimulatedAnnealing train = new NeuralSimulatedAnnealing(
                network, XOR_INPUT, XOR_IDEAL, 10, 2, 100);

            int epoch = 1;

            do
            {
                train.Iteration();
                Console.WriteLine("Epoch #" + epoch + " Error:" + train.Error);
                epoch++;
            } while ((epoch < 5000) && (train.Error > 0.001));

            network = train.Network;

            // test the neural network
            Console.WriteLine("Neural Network Results:");
            for (int i = 0; i < XOR_IDEAL.Length; i++)
            {
                double[] actual = network.ComputeOutputs(XOR_INPUT[i]);
                Console.WriteLine(XOR_INPUT[i][0] + "," + XOR_INPUT[i][1]
                                  + ", actual=" + actual[0] + ",ideal=" + XOR_IDEAL[i][0]);
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            FeedforwardNetwork network = new FeedforwardNetwork();
            network.AddLayer(new FeedforwardLayer(2));
            network.AddLayer(new FeedforwardLayer(3));
            network.AddLayer(new FeedforwardLayer(1));
            network.Reset();

            // train the neural network
            NeuralSimulatedAnnealing train = new NeuralSimulatedAnnealing(
                    network, XOR_INPUT, XOR_IDEAL, 10, 2, 100);

            int epoch = 1;

            do
            {
                train.Iteration();
                Console.WriteLine("Epoch #" + epoch + " Error:" + train.Error);
                epoch++;
            } while ((epoch < 5000) && (train.Error > 0.001));

            network = train.Network;

            // test the neural network
            Console.WriteLine("Neural Network Results:");
            for (int i = 0; i < XOR_IDEAL.Length; i++)
            {
                double[] actual = network.ComputeOutputs(XOR_INPUT[i]);
                Console.WriteLine(XOR_INPUT[i][0] + "," + XOR_INPUT[i][1]
                        + ", actual=" + actual[0] + ",ideal=" + XOR_IDEAL[i][0]);
            }
        }
Ejemplo n.º 3
0
        private void trainNetworkAnneal()
        {
            // train the neural network
            NeuralSimulatedAnnealing train = new NeuralSimulatedAnnealing(
                this.network, this.input, this.ideal, 10, 2, 100);

            int epoch = 1;

            do
            {
                train.Iteration();
                Console.WriteLine("Iteration #" + epoch + " Error:"
                                  + train.Error);
                epoch++;
            } while ((train.Error > 0.01));
        }
        private void trainNetworkAnneal()
        {
            Console.WriteLine("Training with simulated annealing for 5 iterations");
            // train the neural network
            NeuralSimulatedAnnealing train = new NeuralSimulatedAnnealing(
                this.network, this.input, this.ideal, 10, 2, 100);

            int epoch = 1;

            for (int i = 1; i <= 5; i++)
            {
                train.Iteration();
                Console.WriteLine("Iteration(Anneal) #" + epoch + " Error:"
                                  + train.Error);
                epoch++;
            }
        }
Ejemplo n.º 5
0
        private void trainNetworkAnneal()
        {
            // train the neural network
            NeuralSimulatedAnnealing train = new NeuralSimulatedAnnealing(
                   this.network, this.input, this.ideal, 10, 2, 100);

            int epoch = 1;

            do
            {
                train.Iteration();
                Console.WriteLine("Iteration #" + epoch + " Error:"
                        + train.Error);
                epoch++;
            } while ((train.Error > 0.01));
        }
Ejemplo n.º 6
0
        private void trainNetworkAnneal()
        {
            Console.WriteLine("Training with simulated annealing for 5 iterations");
            // train the neural network
            NeuralSimulatedAnnealing train = new NeuralSimulatedAnnealing(
                   this.network, this.input, this.ideal, 10, 2, 100);

            int epoch = 1;

            for (int i = 1; i <= 5; i++)
            {
                train.Iteration();
                Console.WriteLine("Iteration(Anneal) #" + epoch + " Error:"
                        + train.Error);
                epoch++;
            }
        }
Ejemplo n.º 7
0
    /// <summary>
    /// Trains the network
    /// </summary>
    public virtual void trainNetwork()
    {
      INeuralDataSet trainingSet = new BasicNeuralDataSet(networkInput, networkIdealOutput);
      //ITrain trainBackProp = new Backpropagation(network, trainingSet, BACKPROP_LEARN_RATE, BACKPROP_MOMENTUM);

      ITrain trainBackProp = new ScaledConjugateGradient(network, trainingSet);

      double error = Double.MaxValue;
      double lastError = Double.MaxValue;
      int epoch = 1;

      int lastAnneal = 0;
      int errorExit = 0;

      double errorOnLastAnnealStart = double.MaxValue;
      double sameErrorOnLastAnnealStartCount = 0;

      double currentAnnealInterval = MIN_BACKPROP_ITERATIONS_ANNEAL_START;
      double annealStartError = 0;

      do
      {
        trainBackProp.Iteration();
        error = trainBackProp.Error;

        if (lastError - error < MAX_RMS_ITERATION_NETWORK_ERROR)
          errorExit++;
        else
          errorExit = 0;

        Console.WriteLine("Iteration(SC) #{0} Error: {1}", epoch, error.ToString("0.00000000"));

        if (error > ANNEAL_MIN_ERROR)
        {
          if ((lastAnneal > currentAnnealInterval) && (lastError - error < MAX_ANNEAL_START_ERROR))
          {
            if (error == errorOnLastAnnealStart)
              sameErrorOnLastAnnealStartCount++;
            else if (error < errorOnLastAnnealStart)
            {
              sameErrorOnLastAnnealStartCount = 0;
              errorOnLastAnnealStart = error;
            }

            ICalculateScore score = new TrainingSetScore(trainingSet);
            NeuralSimulatedAnnealing trainAnneal = new NeuralSimulatedAnnealing(network, score, ANNEAL_STARTTEMP, ANNEAL_ENDTEMP, ANNEAL_ITERATIONS);

            for (int i = 1; i <= ANNEAL_ATTEMPTS; i++)
            {
              trainAnneal.Iteration();

              if (i == 1)
                annealStartError = trainAnneal.Error;

              Console.WriteLine("Iteration(Anneal) #{0}-{1} Error: {2}", epoch, i, trainAnneal.Error.ToString("0.00000000"));
              //WebLogging.AddLog("WinRatioNeural", WebLogging.LogCategory.WinRatioNeural, "Iteration(Anneal) #" + i + " Error: " + trainAnneal.Error.ToString("0.00000000"));
            }

            if (annealStartError == trainAnneal.Error)
            {
              if (currentAnnealInterval < 200)
              {
                currentAnnealInterval *= 1.5;
                Console.WriteLine("Iteration(Anneal) # No improvment. Increasing anneal interval to " + currentAnnealInterval);
              }
              else
                Console.WriteLine("Iteration(Anneal) # No improvment. Anneal interval at max.");
            }

            lastAnneal = 0;

            trainBackProp = new ScaledConjugateGradient(network, trainingSet);
            trainBackProp.Iteration();
            error = trainBackProp.Error;
            //saveNetwork(correctPredictions.ToString("##0.0")+ "_" + epoch.ToString() + "_nerualPokerAI_LA.nnDAT");
          }
        }

        //Every 50 epochs we can test the network accuracy
        //#if DEBUG
        //if (epoch % 50 == 0)
        //{
        //    //We want to switch to the testing set if we are not using all data for training
        //    if (TRAIN_DATA_PERCENT < 1.0) createTestingSets();

        //    Console.WriteLine("    Network accuracy is currently {0}%",getNetworkAccuracy());

        //    //Wait for 1 second so that we can read the output
        //    Thread.Sleep(1000);

        //    //Likewise we want to switch back before continuing
        //    if (TRAIN_DATA_PERCENT < 1.0) createTrainingSets();
        //}
        //#endif

        lastError = trainBackProp.Error;
        epoch++;
        lastAnneal++;

        //} while (error > MAX_RMS_TOTAL_NETWORK_ERROR && errorExit < 10 && epoch < MAX_ITERATIONS);
      } while (trainBackProp.Error > MAX_RMS_TOTAL_NETWORK_ERROR && epoch < MAX_ITERATIONS && sameErrorOnLastAnnealStartCount < 2);
    }