示例#1
0
        public static MLPNetwork CreateMLPNetwork(int[] layerSetup, double[] weights = null, Func <double, double> activationFunction = null)
        {
            var network = new MLPNetwork {
                Layers = new Node[layerSetup.Length][]
            };

            for (var i = 0; i < layerSetup.Length; i++)
            {
                var actualActivationFunc = i == layerSetup.Length - 1 ? Identity : (activationFunction ?? TanH);
                network.Layers[i] = Enumerable.Range(0, layerSetup[i]).Select(l => new Node(actualActivationFunc)).ToArray();
            }

            var weightCounter = 0;
            var rnd           = new Random();

            for (var i = 1; i < network.Layers.Length; i++)
            {
                foreach (var n in network.Layers[i - 1])
                {
                    foreach (var nextNode in network.Layers[i])
                    {
                        nextNode.InputLinks.Add(new Link(weights == null ? rnd.NextDouble() : weights[weightCounter++])
                        {
                            SourceNode = n, DestinationNode = nextNode
                        });
                    }
                }
            }

            return(network);
        }
示例#2
0
        public static double[] ExtractWeights(this MLPNetwork network)
        {
            var weights = new List <double>();

            foreach (var n in network.Layers)
            {
                foreach (var n2 in n)
                {
                    foreach (var l in n2.InputLinks)
                    {
                        weights.Add(l.Weight);
                    }
                }
            }

            return(weights.ToArray());
        }