Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

How to use ActivationNetwork of Accord.NET properly to do machine learning tasks such as BackPropagationLearning #2232

Open
FurkanGozukara opened this issue Nov 13, 2020 · 0 comments

Comments

@FurkanGozukara
Copy link

Source of the question : https://stackoverflow.com/questions/64827817/how-to-use-activationnetwork-of-accord-net-properly-to-do-machine-learning-tasks

It is sad that the examples of Accord.NET is extremely primitive and insufficient. Therefore, I am having hard time to figure out how to use the system properly.

The entire source code of my application is uploaded to here : https://github.com/FurkanGozukara/CSE419-Artificial-Intelligence-and-Machine-Learning-2020/tree/master/source%20codes/lecture%206%20perceptron%20example

The video of this lecture is here (3 hours 11 minutes) : https://youtu.be/qrklFBewlJA

My biggest question is about how to provide output classes?

Lets say for the dataset abalone (http://archive.ics.uci.edu/ml/datasets/Abalone) there are 28 output classes which are the age of abalone : https://github.com/FurkanGozukara/CSE419-Artificial-Intelligence-and-Machine-Learning-2020/blob/master/source%20codes/lecture%206%20perceptron%20example/lecture%206%20perceptron%20example/bin/Debug/netcoreapp3.1/abalone.data

How to compose the output class?

Like this way?

            int irNumberOfExamples = File.ReadAllLines(srFileName).Count();

        double[][] input = new double[irNumberOfExamples][];
        double[][] output = new double[irNumberOfExamples][];

        List<double> lstOutPutClasses = new List<double>();

        NumberFormatInfo formatProvider = new NumberFormatInfo();
        formatProvider.NumberDecimalSeparator = ".";
        formatProvider.NumberGroupSeparator = ",";

        foreach (var vrPerLine in File.ReadAllLines(srFileName))
        {
            var vrOutPut = Convert.ToDouble(vrPerLine.Split(',').Last(), formatProvider);

            if (lstOutPutClasses.Contains(vrOutPut) == false)
            {
                lstOutPutClasses.Add(vrOutPut);
            }
        }

        int irCounter = 0;
        foreach (var vrPerLine in File.ReadAllLines(srFileName))
        {
            input[irCounter] = vrPerLine.Split(',').SkipLast(1).
                Select(pr => Convert.ToDouble(pr.Replace("I", "0.0").Replace("M", "0.5").Replace("F", "1.0"), formatProvider)).ToArray();

            var vrCurrentOutClass = Convert.ToDouble(vrPerLine.Split(',').Last(), formatProvider);

            output[irCounter][lstOutPutClasses.IndexOf(vrCurrentOutClass)] = 1;

            irCounter++;
        }

This generates the output class like below

enter image description here

And here the code of training part

   int irFinalClassCount = lstOutPutClasses.Count;

        double learningRate = 0.1;
        int irNumberOfFeatures = input[0].Length;

        ActivationNetwork network3 = new ActivationNetwork(
      new SigmoidFunction(2),//activation function
       irNumberOfFeatures,//input layer equal number of features
       12,// 12 neurons at the hidden layer_1
       irFinalClassCount); //output layer equal to number of output classes
                           
        BackPropagationLearning bpteacher = new BackPropagationLearning(network3);
        bpteacher.LearningRate = 0.1;
        bpteacher.Momentum = 0.5;

        for (int i = 0; i < 200000; i++)
        {
            double error = bpteacher.RunEpoch(input, output);//train the algorithm

            var vrAcc = calculateAcurracy(network3, input, output);

            Console.WriteLine("BackPropagationLearning -> " + i + ", Error = " + error.ToString("N2") + "\t\t accuracy: " + vrAcc);
        }

So my second question is about calculating accuracy. The BackPropagationLearning algorithm generates weights for each output neuron. So I counted the highest one as the prediction. Is my approach and code correct?

enter image description here

I am believe I am on the right track but I want to be sure by getting feedback of an Accord.NET library expert

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant