예제 #1
0
        /// <summary>
        /// Determine the significance of the neuron. The higher the return value,
        /// the more significant the neuron is.
        /// </summary>
        ///
        /// <param name="layer">The layer to query.</param>
        /// <param name="neuron">The neuron to query.</param>
        /// <returns>How significant is this neuron.</returns>
        public double DetermineNeuronSignificance(int layer,
                                                  int neuron)
        {
            _network.ValidateNeuron(layer, neuron);

            // calculate the bias significance
            double result = 0;

            // calculate the inbound significance
            if (layer > 0)
            {
                int prevLayer = layer - 1;
                int prevCount = _network
                                .GetLayerTotalNeuronCount(prevLayer);
                for (int i = 0; i < prevCount; i++)
                {
                    result += _network.GetWeight(prevLayer, i, neuron);
                }
            }

            // calculate the outbound significance
            if (layer < _network.LayerCount - 1)
            {
                int nextLayer = layer + 1;
                int nextCount = _network.GetLayerNeuronCount(nextLayer);
                for (int i = 0; i < nextCount; i++)
                {
                    result += _network.GetWeight(layer, neuron, i);
                }
            }

            return(Math.Abs(result));
        }
예제 #2
0
        /// <summary>
        /// Connect layers from a BasicNetwork. Used internally only.
        /// </summary>
        /// <param name="network">The BasicNetwork.</param>
        /// <param name="fromLayerIdx">The from layer index.</param>
        /// <param name="source">The from layer.</param>
        /// <param name="target">The target.</param>
        private void ConnectLayersFromBasic(BasicNetwork network,
                                            int fromLayerIdx, IFreeformLayer source, IFreeformLayer target)
        {
            for (int targetNeuronIdx = 0; targetNeuronIdx < target.Count; targetNeuronIdx++)
            {
                for (int sourceNeuronIdx = 0; sourceNeuronIdx < source.Count; sourceNeuronIdx++)
                {
                    IFreeformNeuron sourceNeuron = source.Neurons[sourceNeuronIdx];
                    IFreeformNeuron targetNeuron = target.Neurons[targetNeuronIdx];

                    // neurons with no input (i.e. bias neurons)
                    if (targetNeuron.InputSummation == null)
                    {
                        continue;
                    }

                    IFreeformConnection connection = _connectionFactory
                                                     .Factor(sourceNeuron, targetNeuron);
                    sourceNeuron.AddOutput(connection);
                    targetNeuron.AddInput(connection);
                    double weight = network.GetWeight(fromLayerIdx,
                                                      sourceNeuronIdx, targetNeuronIdx);
                    connection.Weight = weight;
                }
            }
        }
        /// <summary>
        /// Randomize one level of a neural network.
        /// </summary>
        ///
        /// <param name="network">The network to randomize</param>
        /// <param name="fromLayer">The from level to randomize.</param>
        public override void Randomize(BasicNetwork network, int fromLayer)
        {
            int fromCount = network.GetLayerTotalNeuronCount(fromLayer);
            int toCount   = network.GetLayerNeuronCount(fromLayer + 1);

            for (int toNeuron = 0; toNeuron < toCount; toNeuron++)
            {
                double n = 0.0;
                for (int fromNeuron = 0; fromNeuron < fromCount; fromNeuron++)
                {
                    double w = network.GetWeight(fromLayer, fromNeuron, toNeuron);
                    n += w * w;
                }
                n = Math.Sqrt(n);


                for (int fromNeuron = 0; fromNeuron < fromCount; fromNeuron++)
                {
                    double w = network.GetWeight(fromLayer, fromNeuron, toNeuron);
                    w = _beta * w / n;
                    network.SetWeight(fromLayer, fromNeuron, toNeuron, w);
                }
            }
        }
예제 #4
0
        /// <summary>
        /// Randomize one level of a neural network.
        /// </summary>
        ///
        /// <param name="network">The network to randomize</param>
        /// <param name="fromLayer">The from level to randomize.</param>
        public virtual void Randomize(BasicNetwork network, int fromLayer)
        {
            int fromCount = network.GetLayerTotalNeuronCount(fromLayer);
            int toCount   = network.GetLayerNeuronCount(fromLayer + 1);

            for (int fromNeuron = 0; fromNeuron < fromCount; fromNeuron++)
            {
                for (int toNeuron = 0; toNeuron < toCount; toNeuron++)
                {
                    double v = network.GetWeight(fromLayer, fromNeuron, toNeuron);
                    v = Randomize(v);
                    network.SetWeight(fromLayer, fromNeuron, toNeuron, v);
                }
            }
        }
예제 #5
0
        /// <summary>
        ///     Apply a regularization penalty, such as that from L1/L2 regularization.
        /// </summary>
        /// <param name="fromLayer">The from layer.</param>
        /// <param name="l">The penalty.</param>
        public void LayerRegularizationPenalty(int fromLayer, double[] l)
        {
            var fromCount = _network.GetLayerTotalNeuronCount(fromLayer);
            var toCount   = _network.Layers[fromLayer + 1].Count;

            for (var fromNeuron = 0; fromNeuron < fromCount; fromNeuron++)
            {
                for (var toNeuron = 0; toNeuron < toCount; toNeuron++)
                {
                    var w = _network.GetWeight(fromLayer, fromNeuron, toNeuron);
                    l[0] += Math.Abs(w);
                    l[1] += w * w;
                }
            }
        }
        /// <summary>
        /// Construct a network analyze class. Analyze the specified network.
        /// </summary>
        ///
        /// <param name="network">The network to analyze.</param>
        public AnalyzeNetwork(BasicNetwork network)
        {
            int assignDisabled = 0;
            int assignedTotal = 0;
            IList<Double> biasList = new List<Double>();
            IList<Double> weightList = new List<Double>();
            IList<Double> allList = new List<Double>();

            for (int layerNumber = 0; layerNumber < network.LayerCount - 1; layerNumber++)
            {

                int fromCount = network.GetLayerNeuronCount(layerNumber);
                int fromBiasCount = network
                    .GetLayerTotalNeuronCount(layerNumber);
                int toCount = network.GetLayerNeuronCount(layerNumber + 1);

                // weights
                for (int fromNeuron = 0; fromNeuron < fromCount; fromNeuron++)
                {
                    for (int toNeuron = 0; toNeuron < toCount; toNeuron++)
                    {
                        double v = network.GetWeight(layerNumber, fromNeuron,
                                                     toNeuron);

                        if (network.Structure.ConnectionLimited )
                        {
                            if (Math.Abs(v) < network.Structure.ConnectionLimit )
                            {
                                assignDisabled++;
                            }
                        }

                        weightList.Add(v);
                        allList.Add(v);
                        assignedTotal++;
                    }
                }

                // bias
                if (fromCount != fromBiasCount)
                {
                    int biasNeuron = fromCount;
                    for (int toNeuron = 0; toNeuron < toCount; toNeuron++)
                    {
                        double v = network.GetWeight(layerNumber, biasNeuron,
                                                       toNeuron);
                        if (network.Structure.ConnectionLimited)
                        {
                            if (Math.Abs(v) < network.Structure.ConnectionLimit)
                            {
                                assignDisabled++;
                            }
                        }

                        biasList.Add(v);
                        allList.Add(v);
                        assignedTotal++;
                    }
                }
            }

            _disabledConnections = assignDisabled;
            _totalConnections = assignedTotal;
            _weights = new NumericRange(weightList);
            _bias = new NumericRange(biasList);
            _weightsAndBias = new NumericRange(allList);
            _weightValues = EngineArray.ListToDouble(weightList);
            _allValues = EngineArray.ListToDouble(allList);
            _biasValues = EngineArray.ListToDouble(biasList);
        }
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        private BasicNetwork ObtainNetwork()
        {
            BasicNetwork network = EncogUtility.SimpleFeedForward(2, 3, 0, 4, false);

            double[] weights = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 };
            NetworkCODEC.ArrayToNetwork(weights, network);

            Assert.AreEqual(1.0, network.GetWeight(1, 0, 0), 0.01);
            Assert.AreEqual(2.0, network.GetWeight(1, 1, 0), 0.01);
            Assert.AreEqual(3.0, network.GetWeight(1, 2, 0), 0.01);
            Assert.AreEqual(4.0, network.GetWeight(1, 3, 0), 0.01);

            Assert.AreEqual(5.0, network.GetWeight(1, 0, 1), 0.01);
            Assert.AreEqual(6.0, network.GetWeight(1, 1, 1), 0.01);
            Assert.AreEqual(7.0, network.GetWeight(1, 2, 1), 0.01);
            Assert.AreEqual(8.0, network.GetWeight(1, 3, 1), 0.01);

            Assert.AreEqual(9.0, network.GetWeight(1, 0, 2), 0.01);
            Assert.AreEqual(10.0, network.GetWeight(1, 1, 2), 0.01);
            Assert.AreEqual(11.0, network.GetWeight(1, 2, 2), 0.01);
            Assert.AreEqual(12.0, network.GetWeight(1, 3, 2), 0.01);

            Assert.AreEqual(13.0, network.GetWeight(1, 0, 3), 0.01);
            Assert.AreEqual(14.0, network.GetWeight(1, 1, 3), 0.01);
            Assert.AreEqual(15.0, network.GetWeight(1, 2, 3), 0.01);
            Assert.AreEqual(16.0, network.GetWeight(1, 3, 3), 0.01);

            Assert.AreEqual(17.0, network.GetWeight(0, 0, 0), 0.01);
            Assert.AreEqual(18.0, network.GetWeight(0, 1, 0), 0.01);
            Assert.AreEqual(19.0, network.GetWeight(0, 2, 0), 0.01);
            Assert.AreEqual(20.0, network.GetWeight(0, 0, 1), 0.01);
            Assert.AreEqual(21.0, network.GetWeight(0, 1, 1), 0.01);
            Assert.AreEqual(22.0, network.GetWeight(0, 2, 1), 0.01);

            Assert.AreEqual(20.0, network.GetWeight(0, 0, 1), 0.01);
            Assert.AreEqual(21.0, network.GetWeight(0, 1, 1), 0.01);
            Assert.AreEqual(22.0, network.GetWeight(0, 2, 1), 0.01);

            Assert.AreEqual(23.0, network.GetWeight(0, 0, 2), 0.01);
            Assert.AreEqual(24.0, network.GetWeight(0, 1, 2), 0.01);
            Assert.AreEqual(25.0, network.GetWeight(0, 2, 2), 0.01);


            return(network);
        }
예제 #8
0
        /// <summary>
        /// Construct a network analyze class. Analyze the specified network.
        /// </summary>
        ///
        /// <param name="network">The network to analyze.</param>
        public AnalyzeNetwork(BasicNetwork network)
        {
            int            assignDisabled = 0;
            int            assignedTotal  = 0;
            IList <Double> biasList       = new List <Double>();
            IList <Double> weightList     = new List <Double>();
            IList <Double> allList        = new List <Double>();

            for (int layerNumber = 0; layerNumber < network.LayerCount - 1; layerNumber++)
            {
                int fromCount     = network.GetLayerNeuronCount(layerNumber);
                int fromBiasCount = network
                                    .GetLayerTotalNeuronCount(layerNumber);
                int toCount = network.GetLayerNeuronCount(layerNumber + 1);

                // weights
                for (int fromNeuron = 0; fromNeuron < fromCount; fromNeuron++)
                {
                    for (int toNeuron = 0; toNeuron < toCount; toNeuron++)
                    {
                        double v = network.GetWeight(layerNumber, fromNeuron,
                                                     toNeuron);

                        if (network.Structure.ConnectionLimited)
                        {
                            if (Math.Abs(v) < network.Structure.ConnectionLimit)
                            {
                                assignDisabled++;
                            }
                        }

                        weightList.Add(v);
                        allList.Add(v);
                        assignedTotal++;
                    }
                }

                // bias
                if (fromCount != fromBiasCount)
                {
                    int biasNeuron = fromCount;
                    for (int toNeuron = 0; toNeuron < toCount; toNeuron++)
                    {
                        double v = network.GetWeight(layerNumber, biasNeuron,
                                                     toNeuron);
                        if (network.Structure.ConnectionLimited)
                        {
                            if (Math.Abs(v) < network.Structure.ConnectionLimit)
                            {
                                assignDisabled++;
                            }
                        }

                        biasList.Add(v);
                        allList.Add(v);
                        assignedTotal++;
                    }
                }
            }

            _disabledConnections = assignDisabled;
            _totalConnections    = assignedTotal;
            _weights             = new NumericRange(weightList);
            _bias           = new NumericRange(biasList);
            _weightsAndBias = new NumericRange(allList);
            _weightValues   = EngineArray.ListToDouble(weightList);
            _allValues      = EngineArray.ListToDouble(allList);
            _biasValues     = EngineArray.ListToDouble(biasList);
        }
예제 #9
0
        /// <summary>
        /// Construct a network analyze class. Analyze the specified network.
        /// </summary>
        ///
        /// <param name="network">The network to analyze.</param>
        public AnalyzeNetwork(BasicNetwork network)
        {
            IList<Double> biasList = new List<Double>();
            IList<Double> weightList = new List<Double>();
            IList<Double> allList = new List<Double>();

            for (int layerNumber = 0; layerNumber < network.LayerCount - 1; layerNumber++)
            {
                int fromCount = network.GetLayerNeuronCount(layerNumber);
                int fromBiasCount = network
                    .GetLayerTotalNeuronCount(layerNumber);
                int toCount = network.GetLayerNeuronCount(layerNumber + 1);

                // weights
                for (int fromNeuron = 0; fromNeuron < fromCount; fromNeuron++)
                {
                    for (int toNeuron = 0; toNeuron < toCount; toNeuron++)
                    {
                        double v = network.GetWeight(layerNumber, fromNeuron,
                                                     toNeuron);
                        weightList.Add(v);
                        allList.Add(v);
                    }
                }

                // bias
                if (fromCount != fromBiasCount)
                {
                    int biasNeuron = fromCount;
                    for (int toNeuron = 0; toNeuron < toCount; toNeuron++)
                    {
                        double v = network.GetWeight(layerNumber, biasNeuron,
                                                       toNeuron);
                        biasList.Add(v);
                        allList.Add(v);
                    }
                }
            }

            _disabledConnections = 0;
            _totalConnections = 0;
            _weights = new NumericRange(weightList);
            _bias = new NumericRange(biasList);
            _weightsAndBias = new NumericRange(allList);
            _weightValues = EngineArray.ListToDouble(weightList);
            _allValues = EngineArray.ListToDouble(allList);
            _biasValues = EngineArray.ListToDouble(biasList);
        }
예제 #10
0
        /// <summary>
        /// Calculate the derivatives for this training set element.
        /// </summary>
        ///
        /// <param name="pair">The training set element.</param>
        /// <returns>The sum squared of errors.</returns>
        private double CalculateDerivatives(IMLDataPair pair)
        {
            // error values
            double e   = 0.0d;
            double sum = 0.0d;

            _network.Compute(pair.Input);

            int fromLayer       = _network.LayerCount - 2;
            int toLayer         = _network.LayerCount - 1;
            int fromNeuronCount = _network.GetLayerTotalNeuronCount(fromLayer);
            int toNeuronCount   = _network.GetLayerNeuronCount(toLayer);

            double output = _network.Structure.Flat.LayerOutput[0];

            e = pair.Ideal[0] - output;

            for (int i = 0; i < fromNeuronCount; i++)
            {
                double lastOutput = _network.GetLayerOutput(fromLayer, i);

                _jacobian[_jacobianRow][_jacobianCol++] = CalcDerivative(
                    _network.GetActivation(toLayer), output) * lastOutput;
            }

            while (fromLayer > 0)
            {
                fromLayer--;
                toLayer--;
                fromNeuronCount = _network.GetLayerTotalNeuronCount(fromLayer);
                toNeuronCount   = _network.GetLayerNeuronCount(toLayer);

                // this.network.getLayerOutput(fromLayer, neuronNumber) holder.getResult().get(lastSynapse);

                // for each neuron in the input layer
                for (int neuron = 0; neuron < toNeuronCount; neuron++)
                {
                    output = _network.GetLayerOutput(toLayer, neuron);

                    IActivationFunction function = _network.GetActivation(toLayer);

                    double w   = _network.GetWeight(toLayer, neuron, 0);
                    double val = CalcDerivative(function, output)
                                 * CalcDerivative2(function, sum) * w;

                    // for each weight of the input neuron
                    for (int i = 0; i < fromNeuronCount; i++)
                    {
                        sum = 0.0d;
                        // for each neuron in the next layer
                        for (int j = 0; j < toNeuronCount; j++)
                        {
                            // for each weight of the next neuron
                            for (int k = 0; k < fromNeuronCount; k++)
                            {
                                sum += _network.GetWeight(fromLayer, k, j) * output;
                            }
                        }

                        _jacobian[_jacobianRow][_jacobianCol++] = val
                                                                  * _network.GetLayerOutput(fromLayer, i);
                    }
                }
            }

            // return error
            return(e);
        }