Exemplo n.º 1
0
        /// <inheritdoc />
        public override BasicNetwork Calculate()
        {
            //Network
            var network           = new BasicNetwork();
            var inputNeuronCount  = inputs.GetLength(1);
            var outputNeuronCount = targets.GetLength(1);

            //Input Layer
            var inputLayer = new BasicLayer(inputActivationFunction, inputHasBias, inputNeuronCount);

            network.AddLayer(inputLayer);

            //Hidden Layer
            if (hiddenLayerConfig.GetLength(1) != 2)
            {
                throw new ArgumentException("Net Configuration is a 2 column table of values of neuron count and activation function type, with each row representing a separate layer");
            }

            for (var row = 0; row < hiddenLayerConfig.GetLength(0); row++)
            {
                var activationFunc = Util.GetActivationFunction(hiddenLayerConfig[row, 1].ToString());
                var layer          = new BasicLayer(activationFunc, true, Convert.ToInt32(hiddenLayerConfig[row, 0]));

                network.AddLayer(layer);
            }

            //Output layer
            var outputLayer = new BasicLayer(outputActivationFunction, outputHasBias, outputNeuronCount);

            network.AddLayer(outputLayer);

            //Training
            network.Structure.FinalizeStructure();
            network.Reset();
            var dataSet = new BasicMLDataSet(inputs.AsJagged(), targets.AsJagged());
            var trainlm = new LevenbergMarquardtTraining(network, dataSet);

            var epoch = 1;

            do
            {
                trainlm.Iteration();
                epoch++;
            } while (epoch < epochLimit && trainlm.Error > errorTolerance);

            return(network);
        }