Esempio n. 1
0
        /// <summary>Defaults.</summary>
        /// <param name="d">The Descriptor to process.</param>
        /// <param name="x">The Vector to process.</param>
        /// <param name="y">The Vector to process.</param>
        /// <param name="activation">The activation.</param>
        /// <returns>A Network.</returns>
        public static Network Default(Descriptor d, Matrix x, Vector y, IFunction activation)
        {
            var nn = new Network();

            // set output to number of choices of available
            // 1 if only two choices
            var distinct = y.Distinct().Count();
            var output = distinct > 2 ? distinct : 1;

            // identity funciton for bias nodes
            IFunction ident = new Ident();

            // set number of hidden units to (Input + Hidden) * 2/3 as basic best guess.
            var hidden = (int)Math.Ceiling((decimal)(x.Cols + output) * 2m / 3m);

            // creating input nodes
            nn.In = new Node[x.Cols + 1];
            nn.In[0] = new Node { Label = "B0", Activation = ident };
            for (var i = 1; i < x.Cols + 1; i++)
            {
                nn.In[i] = new Node { Label = d.ColumnAt(i - 1), Activation = ident };
            }

            // creating hidden nodes
            var h = new Node[hidden + 1];
            h[0] = new Node { Label = "B1", Activation = ident };
            for (var i = 1; i < hidden + 1; i++)
            {
                h[i] = new Node { Label = string.Format("H{0}", i), Activation = activation };
            }

            // creating output nodes
            nn.Out = new Node[output];
            for (var i = 0; i < output; i++)
            {
                nn.Out[i] = new Node { Label = GetLabel(i, d), Activation = activation };
            }

            // link input to hidden. Note: there are
            // no inputs to the hidden bias node
            for (var i = 1; i < h.Length; i++)
            {
                for (var j = 0; j < nn.In.Length; j++)
                {
                    Edge.Create(nn.In[j], h[i]);
                }
            }

            // link from hidden to output (full)
            for (var i = 0; i < nn.Out.Length; i++)
            {
                for (var j = 0; j < h.Length; j++)
                {
                    Edge.Create(h[j], nn.Out[i]);
                }
            }

            return nn;
        }
        public static void AreEqual(Network n1, Network n2)
        {
            // recursive test
            Assert.Equal(n1.In.Length, n2.In.Length);
            Assert.Equal(n1.Out.Length, n2.Out.Length);
            for (int i = 0; i < n1.In.Length; i++)
                AreEqual(n1.In[i], n2.In[i]);

            for (int i = 0; i < n1.Out.Length; i++)
                AreEqual(n1.Out[i], n2.Out[i]);
        }
        /// <summary>
        /// Creates a new Network Properties object for use in training a neural network model.
        /// </summary>
        /// <param name="network">Network being optimized.</param>
        /// <param name="examples">Number of training examples.</param>
        /// <param name="features">Number of features in the training examples.</param>
        /// <param name="learningRate">Learning rate.</param>
        /// <param name="lambda">Lambda (weight decay).</param>
        /// <param name="maxIterations">Maximum number of iterations.</param>
        /// <param name="parameters">Optional parameters object used when training networks.
        ///     <para>Usage: parameters = new { Obj1, Obj2, Obj3... }</para>
        /// </param>
        /// <returns></returns>
        public static NetworkTrainingProperties Create(Network network, int examples, int features, double learningRate, 
            double lambda, int maxIterations, object parameters = null)
        {
            Dictionary<string, object> props = new Dictionary<string, object>();
            if (parameters != null)
            {
                foreach (var prop in parameters.GetType().GetProperties())
                {
                    props[prop.Name] = prop.GetValue(parameters);
                }
            }

            return new NetworkTrainingProperties(props)
            {
                Network = network,
                Examples = examples,
                Features = features,
                Lambda = lambda,
                LearningRate = learningRate
            };
        }
        private static int VerifyNetwork(Network network)
        {
            int errors = 0;

            var edges = network.GetEdges();

            for (int layer = 0; layer < network.Layers; layer++)
            {
                var nodes = network.GetNodes(layer);

                foreach (var node in nodes)
                {
                    foreach (var iedge in node.In)
                    {
                        if (!edges.Any(a => a.ParentId == iedge.ParentId && a.ChildId == iedge.ChildId))
                            errors++;
                    }
                    foreach (var oedge in node.Out)
                    {
                        if (!edges.Any(a => a.ParentId == oedge.ParentId && a.ChildId == oedge.ChildId))
                            errors++;
                    }

                    if (layer == 0 && node.In.Count > 0) errors++;
                    if (layer == network.Layers - 1 && node.Out.Count > 0) errors++;
                }
            }

            network.Forward(Vector.Ones(network.In.Length - 1));

            if (network.Out.Any(a => double.IsNaN(a.Output) || double.IsInfinity(a.Output))) errors++;

            return errors;
        }