示例#1
0
文件: Edge.cs 项目: m-abubakar/numl
 /// <summary>Creates a new Edge.</summary>
 /// <param name="source">Source for the.</param>
 /// <param name="target">Target for the.</param>
 /// <returns>An Edge.</returns>
 public static Edge Create(Node source, Node target)
 {
     Edge e = new Edge { Source = source, Target = target };
     source.Out.Add(e);
     target.In.Add(e);
     return e;
 }
示例#2
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)
        {
            Network nn = new Network();
            // set output to number of choices of available
            // 1 if only two choices
            int distinct = y.Distinct().Count();
            int 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. 
            int hidden = (int)System.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 (int i = 1; i < x.Cols + 1; i++)
                nn.In[i] = new Node { Label = d.ColumnAt(i - 1), Activation = ident };

            // creating hidden nodes
            Node[] h = new Node[hidden + 1];
            h[0] = new Node { Label = "B1", Activation = ident };
            for (int 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 (int 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 (int i = 1; i < h.Length; i++)
                for (int j = 0; j < nn.In.Length; j++)
                    Edge.Create(nn.In[j], h[i]);

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

            return nn;
        }
示例#3
0
 /// <summary>Gets the nodes in this collection.</summary>
 /// <param name="n">The Node to process.</param>
 /// <returns>
 ///     An enumerator that allows foreach to be used to process the nodes in this collection.
 /// </returns>
 private IEnumerable<Node> GetNodes(Node n)
 {
     foreach (var edge in n.In)
     {
         yield return edge.Source;
         foreach (var node in this.GetNodes(edge.Source))
         {
             yield return node;
         }
     }
 }
示例#4
0
 /// <summary>Gets the edges in this collection.</summary>
 /// <param name="n">The Node to process.</param>
 /// <returns>
 ///     An enumerator that allows foreach to be used to process the edges in this collection.
 /// </returns>
 private IEnumerable<Edge> GetEdges(Node n)
 {
     foreach (var edge in n.In)
     {
         yield return edge;
         foreach (var e in this.GetEdges(edge.Source))
         {
             yield return e;
         }
     }
 }
示例#5
0
        /// <summary>Generates an object from its XML representation.</summary>
        /// <param name="reader">The <see cref="T:System.Xml.XmlReader" /> stream from which the object is
        /// deserialized.</param>
        public void ReadXml(XmlReader reader)
        {
            XmlSerializer nSerializer = new XmlSerializer(typeof(Node));
            XmlSerializer eSerializer = new XmlSerializer(typeof(Edge));

            reader.MoveToContent();


            Dictionary<string, Node> nodes = new Dictionary<string, Node>();
            int length = 0;
            reader.ReadStartElement();
            length = int.Parse(reader.GetAttribute("Length"));
            reader.ReadStartElement("Nodes");
            for (int i = 0; i < length; i++)
            {
                var node = (Node)nSerializer.Deserialize(reader);
                nodes.Add(node.Id, node);
                reader.Read();
            }
            reader.ReadEndElement();

            length = int.Parse(reader.GetAttribute("Length"));
            reader.ReadStartElement("Edges");
            for (int i = 0; i < length; i++)
            {
                var edge = (Edge)eSerializer.Deserialize(reader);
                reader.Read();

                edge.Source = nodes[edge.SourceId];
                edge.Target = nodes[edge.TargetId];

                edge.Source.Out.Add(edge);
                edge.Target.In.Add(edge);
            }
            reader.ReadEndElement();

            length = int.Parse(reader.GetAttribute("Length"));
            reader.ReadStartElement("In");
            In = new Node[length];
            for (int i = 0; i < length; i++)
            {
                reader.MoveToContent();
                In[i] = nodes[reader.GetAttribute("Id")];
                reader.Read();

            }
            reader.ReadEndElement();

            length = int.Parse(reader.GetAttribute("Length"));
            reader.ReadStartElement("Out");
            Out = new Node[length];
            for (int i = 0; i < length; i++)
            {
                reader.MoveToContent();
                Out[i] = nodes[reader.GetAttribute("Id")];
                reader.Read();

            }
            reader.ReadEndElement();
        }