Search code examples
c#machine-learningencog

c# Encog Framework, neural network, Why do I get an internal error when I train my network?


I´m following the instructions given in the book 'Use Encog c#' where I´ve had to redesign some of the code to fit my needs.

I´m working with image Datasets, I load one 'category' into the network at a time, train it, save it and proceed with the next 'category' in a console program.

This is how I create my neural network:

  public BasicNetwork CreateNetwork(ImageMLDataSet training)
    {
        var network = EncogUtility.SimpleFeedForward(training.InputSize, 100, 0, training.IdealSize, true);

        return network;
    }

and my trainingset: The imageDict is a Dictionary variable

 public ImageMLDataSet CreateTraining()
    {
        var downsample = new RGBDownsample();
        var training = new ImageMLDataSet(downsample, false, -1, 1);

        foreach (var item in imageDict)
        {
            ImageMLData data = new ImageMLData(item.Value);
            training.Add(data);
        }
        Console.WriteLine("Training set created");

        return training;
    }

after this I assign ID's to my images using 'ImagePair' (in my case as a dictionary instead of a class) And process it to ImageMLData.

The Error Occurs within this method, when the 'EncogUtility.TrainConsole(train, network, training, minutes);' is called.

public void TrainNetwork(BasicNetwork network, IMLDataSet training)
    {
        float minutes = 1;
        double strategyError = 0.25;
        int strategyCycles = 50;

        Console.WriteLine("Training initiated...");
        var train = new ResilientPropagation(network, training);

        try
        {                
            train.AddStrategy(new ResetStrategy(strategyError, strategyCycles));

            EncogUtility.TrainConsole(train, network, training, minutes);
        }
        catch(Exception e)
        {
            Console.WriteLine("Error at: " + e);
        }

        Console.WriteLine("Training stopped");

    }

When I run this I catch the exception:

Error at: Encog.EncogError: Nested Exception ---> System.NullReferenceException: Object reference is not set to an instance of an object at Encog.MathUtil.Error.ErrorCalculation.UpdateError(Double[] actual, IMLData ideal, Double significance) ved Encog.Neural.Networks.Training.Propagation.GradientWorker.Process(IMLDataPair pair) at Encog.Neural.Networks.Training.Propagation.GradientWorker.Run() --- Slut på staksporing af indre undtagelser --- at Encog.Neural.Networks.Training.Propagation.Propagation.Iteration()
at Encog.Util.Simple.EncogUtility.TrainConsole(IMLTrain train, BasicNetwork network, IMLDataSet trainingSet, Double seconds) at TreeSorting.NeuralNetwork.TrainNetwork(BasicNetwork network, IMLDataSet training) in C:\Dokumenter\Monosoft\Monosoft Project\ConsoleApp1\NeuralNetwork.cs:line 180

Thanks in advance :)


Solution

  • For others searching for solutions to this type of problem:

    I solved this by changeing the 'var' when I declared new variables and that seemed to solve my issue. thanks to GunnarSigfusson for the answer that fixed it c: