/// <summary> /// Adds the Edge to the underlying graph. /// </summary> /// <param name="edge">Edge to add.</param> /// <returns></returns> public Edge AddEdge(Edge edge) { base.AddEdge(edge); return(edge); }
/// <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) { var e = new Edge { Source = source, Target = target }; source.Out.Add(e); target.In.Add(e); return e; }
/// <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); }