A neural network class that represents a network with recurrent (cyclic) connections. This is a much faster implementation of CyclicNetwork. The speedup is approximately 5x depending on hardware and CLR platform, see http://sharpneat.sourceforge.net/network_optimization.html for detailed info. The speedup is achieved by compactly storing all required data in arrays and in a way that maximizes in-order memory accesses; This allows us to maximize use of CPU caches. In contrast the CyclicNetwork class represents the network directly, that is, as a network of neuron/node objects; This has additional overhead such as the standard data associated with each object in dotNet which results in less efficient packing of the true neural net data in memory, which in turns results in less efficient use of CPU memory caches. Finally, representing the network directly as a graph of connected nodes is not conducive to writing code with in-order memory accesses. Algorithm Overview. 1) Loop connections. Each connection gets its input signal from its source neuron, applies its weight and stores its output value./ Connections are ordered by source neuron index, thus all memory accesses here are sequential/in-order. 2) Loop connections (again). Each connection adds its output value to its target neuron, thus each neuron accumulates or 'collects' its input signal in its pre-activation variable. Because connections are sorted by source neuron index and not target index, this loop generates out-of order memory accesses, but is the only loop to do so. 3) Loop neurons. Pass each neuron's pre-activation signal through the activation function and set its post-activation signal value. The activation loop is now complete and we can go back to (1) or stop.
Inheritance: IBlackBox
コード例 #1
0
        public static CFastCyclicNetwork Convert(FastCyclicNetwork fcn) {
            var cfcn = new CFastCyclicNetwork();
            var numberOfNeurons = fcn._neuronActivationFnArray.Length;
            // Copy connections
            cfcn.Connections = new List<CConnection>(fcn._connectionArray.Length);
            foreach (var c in fcn._connectionArray)
            {
                var cc = new CConnection
                             {
                                 ToNeuronId = c._tgtNeuronIdx,
                                 FromNeuronId = c._srcNeuronIdx,
                                 Weight = c._weight
                             };
                cfcn.Connections.Add(cc);
            }

            // Copy activation functions
            cfcn.ActivationFunctions = new List<string>(numberOfNeurons);
            foreach (var s in fcn._neuronActivationFnArray)
            {
                if (s == null)
                    cfcn.ActivationFunctions.Add("");
                else
                    cfcn.ActivationFunctions.Add(s.FunctionId);
            }

            // Copy auxiliary arguments
            cfcn.NeuronAuxArgs = new List<List<double>>(numberOfNeurons);
            foreach (var aux in fcn._neuronAuxArgsArray)
            {
                if (aux == null)
                    cfcn.NeuronAuxArgs.Add(new List<double>());
                else
                    cfcn.NeuronAuxArgs.Add(new List<double>(aux));
            }
            cfcn.NeuronCount = fcn._neuronCount;
            cfcn.InputNeuronCount = fcn._inputNeuronCount;
            cfcn.OutputNeuronCount = fcn._outputNeuronCount;
            cfcn.TimestepsPerActivation = fcn._timestepsPerActivation;

            return cfcn;
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: tansey/grid-games
        private static double[][] TrainXOR(int epochs, FastCyclicNetwork network)
        {
            double[][] inputs = new double[][] { 
                new double[] { 0, 0 },
                new double[] { 1, 0 },
                new double[] { 0, 1 },
                new double[] { 1, 1 }
            };
            double[][] outputs = new double[][] { 
                new double[] { 0 }, 
                new double[] { 1 }, 
                new double[] { 1 }, 
                new double[] { 0 } };

            for (int i = 0; i < epochs; i++)
                network.Train(inputs[i % inputs.Length], outputs[i % outputs.Length]);
            return inputs;
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: tansey/grid-games
 static void PrintWeights(FastCyclicNetwork network)
 {
     foreach (var conn in network.ConnectionArray)
         Console.WriteLine("[{0}] -> [{1}]: {2:N4}", conn._srcNeuronIdx, conn._tgtNeuronIdx, conn._weight);
 }
コード例 #4
0
ファイル: Program.cs プロジェクト: tansey/grid-games
        private static void RunXor(FastCyclicNetwork network, double[][] inputs)
        {
            for (int i = 0; i < inputs.Length; i++)
            {
                network.ResetState();

                // Convert the sensors into an input array for the network
                for (int j = 0; j < inputs[i].Length; j++)
                    network.InputSignalArray[j] = inputs[i][j];

                // Activate the network
                network.Activate();

                Console.WriteLine("[{0},{1}] -> {2:N4}", inputs[i][0], inputs[i][1], network.OutputSignalArray[0]);
            }
        }