Exemplo n.º 1
0
        /// <summary>
        /// Prunes the network in the given direction for the specified number of layers.
        /// </summary>
        /// <param name="network">Current network.</param>
        /// <param name="backwards">If true, removes layer by layer in reverse order (i.e. output layer first).</param>
        /// <param name="layers">Number of layers to prune from the network.</param>
        public static Network Prune(this Network network, bool backwards = true, int layers = 1)
        {
            int count = layers;
            int layer = (backwards ? (network.Layers - 1) : 0);

            while (count > 0)
            {
                count--;

                var nodes = network.GetNodes(layer);

                for (int i = 0; i < nodes.Count(); i++)
                {
                    network.RemoveNode(nodes.ElementAt(i));
                }

                layer = (backwards ? --layer : ++layer);
            }

            network.Reindex();

            // remove bias outputs
            var outputs = network.GetNodes(network.Layers - 1).Where(w => w.IsBias);

            for (int x = 0; x < outputs.Count(); x++)
            {
                network.RemoveNode(outputs.ElementAt(x));
            }

            for (int i = 0; i < network.GetNodes(0).Count(); i++)
            {
                network.GetNodes(0).ElementAt(i).In.Clear();
            }

            network.In = network.GetNodes(0).ToArray();

            for (int i = 0; i < network.GetNodes(network.Layers - 1).Count(); i++)
            {
                network.GetNodes(network.Layers - 1).ElementAt(i).Out.Clear();
            }

            network.Out = network.GetNodes(network.Layers - 1).ToArray();

            return(network);
        }