Example #1
0
        public Network(int[] neuronCounts, IFunction activator, IDataset dataset, double dataRatio, double?weight = null)
        {
            // throw some errors first
            if (neuronCounts.Length < 2)
            {
                throw new ArgumentOutOfRangeException("Network must have at least 2 layers");
            }
            if (neuronCounts[0] != dataset.GetDataSize())
            {
                throw new ArgumentOutOfRangeException("Input neuron count must match 1:1 with input data size. Current neuron count: " + neuronCounts[0] + ", current input size: " + dataset.GetDataSize());
            }
            if (neuronCounts[neuronCounts.Length - 1] != dataset.GetLabelSize())
            {
                throw new ArgumentOutOfRangeException("Output neuron count must match 1:1 with output data size. Current neuron count: " + neuronCounts[neuronCounts.Length - 1] + ", current input size: " + dataset.GetLabelSize());
            }

            // init new network based on neuroncounts
            Layers = new Layer[neuronCounts.Length];

            Layers[0] = new Layer(neuronCounts[0]);
            for (int c = 1; c < neuronCounts.Length; c++)
            {
                Layers[c] = new Layer(neuronCounts[c], Layers[c - 1], activator, weight);
            }

            // create testdata and trainingdata
            double counter = Math.Round(1 / (1 - dataRatio)); // if data ratio is 0.8 then 80% of the data will be for training.

            // this means the counter should be 5, eg. every 5th entry will be test data rather than training data
            double[][] inputSet  = dataset.GetDataset();
            double[][] outputSet = dataset.GetLabelset();

            List <double[]> tr_i = new List <double[]> {
            };
            List <double[]> tr_o = new List <double[]> {
            };
            List <double[]> te_i = new List <double[]> {
            };
            List <double[]> te_o = new List <double[]> {
            };


            for (int i = 0; i < inputSet.Length; i++)
            {
                if (i % counter == 0) // how we test whether to add something to test data or training data
                {
                    // test data goes in here
                    te_i.Add(inputSet[i]);
                    te_o.Add(outputSet[i]);
                }
                else
                {
                    // training data goes in here
                    tr_i.Add(inputSet[i]);
                    tr_o.Add(outputSet[i]);
                }
            }

            TrainingInputs  = tr_i.ToArray();
            TrainingOutputs = tr_o.ToArray();
            TestingInputs   = te_i.ToArray();
            TestingOutputs  = te_o.ToArray();
            GlobalError     = 1337;
        }