Exemple #1
0
        public Network createNetwork(int inputVectorLength, int outputVectorLength, int numHiddenLayers, bool addBias)
        {
            Network network = new Network();

            // create input layer
            Layer inpLayer = new Layer();

            for (int i = 0; i < inputVectorLength; i++)
            {
                Node n = new Node(nodeNamePrefix[0] + i.ToString());
                inpLayer.AddNode(n);
            }
            network.AddLayer(inpLayer);
            // create hidden layers
            for (int numLayer = 0; numLayer < numHiddenLayers; numLayer++)
            {
                Layer hiddenLayer = new Layer();

                string res = new String(nodeNamePrefix[(numLayer + 1) % nodeNamePrefix.Length], (numLayer + 1) / nodeNamePrefix.Length + 1);
                for (int i = 0; i < inputVectorLength; i++)
                {
                    Node n = new Node(res + i.ToString());
                    hiddenLayer.AddNode(n);
                }
                network.AddLayer(hiddenLayer);
            }
            // create connectors from input to hidden layers, and from hidden to hidden layers
            for (int i = 0; i < numHiddenLayers; i++)
            {
                foreach (Node n in network.layers[i].nodes)
                {
                    foreach (Node n2 in network.layers[i + 1].nodes)
                    {
                        if (n2.isBiasNode == false)
                        {
                            Connector con = new Connector(n, n2, 0.5d);
                            n.AddForwardConnector(con);
                            n2.AddBackwardConnector(con);
                        }
                    }
                }
            }
            // create output layer
            Layer outLayer = new Layer();

            string reso = new String(nodeNamePrefix[(numHiddenLayers + 1) % nodeNamePrefix.Length], (numHiddenLayers + 1) / nodeNamePrefix.Length + 1);

            for (int i = 0; i < outputVectorLength; i++)
            {
                Node n = new Node(reso + i.ToString());
                outLayer.AddNode(n);
            }
            network.AddLayer(outLayer);
            // create connectors from hidden layer to output layer
            foreach (Node n in network.layers[numHiddenLayers].nodes)
            {
                foreach (Node n2 in outLayer.nodes)
                {
                    Connector con = new Connector(n, n2, 0.5d);
                    n.AddForwardConnector(con);
                    n2.AddBackwardConnector(con);
                }
            }

            if (addBias == true)
            {
                Node biasnode = new Node("bias", 1d, true);
                // connect bias node to other nodes excluding input layer
                for (int i = 1; i < network.layers.Count; i++)
                {
                    foreach (Node n in network.layers[i].nodes)
                    {
                        Connector con = new Connector(biasnode, n, 0.5d);
                        biasnode.AddForwardConnector(con);
                        n.AddBackwardConnector(con);
                    }
                }
                network.biasnode = biasnode;
            }
            else
            {
                network.biasnode = new Node("bias", 0d, true);
            }

            return(network);
        }