/// <summary> /// Creates a new deep neural network based on the supplied inputs and layers. /// </summary> /// <param name="d">Descriptor object.</param> /// <param name="X">Training examples</param> /// <param name="y">Training labels</param> /// <param name="activationFunction">Activation Function for each output layer.</param> /// <param name="outputFunction">Ouput Function for each output layer.</param> /// <param name="hiddenLayers">The intermediary (hidden) layers / ensembles in the network.</param> /// <returns>A Deep Neural Network</returns> public static Network Create(this Network network, Descriptor d, Matrix X, Vector y, IFunction activationFunction, IFunction outputFunction = null, params NetworkLayer[] hiddenLayers) { // 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 function for bias nodes IFunction ident = new Ident(); // creating input nodes network.In = new Neuron[X.Cols + 1]; network.In[0] = new Neuron { Label = "B0", ActivationFunction = ident }; for (int i = 1; i < X.Cols + 1; i++) { network.In[i] = new Neuron { Label = d.ColumnAt(i - 1), ActivationFunction = ident } } ; // creating output nodes network.Out = new Neuron[output]; for (int i = 0; i < output; i++) { network.Out[i] = new Neuron { Label = Network.GetLabel(i, d), ActivationFunction = activationFunction, OutputFunction = outputFunction } } ; for (int layer = 0; layer < hiddenLayers.Count(); layer++) { if (layer == 0 && hiddenLayers[layer].IsAutoencoder) { // init and train it. } // connect input with previous layer or input layer // connect last layer with output layer } // 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(network); }
/// <summary>Defaults.</summary> /// <param name="network">The network.</param> /// <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="activationFunction">The activation.</param> /// <param name="outputFunction">The ouput function for hidden nodes (Optional).</param> /// <param name="epsilon">epsilon</param> /// <returns>A Network.</returns> public static Network Create( this Network network, Descriptor d, Matrix x, Vector y, IFunction activationFunction, IFunction outputFunction = null, double epsilon = double.NaN) { // 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)System.Math.Ceiling((x.Cols + output) * 2.0 / 3.0); return(network.Create( x.Cols, output, activationFunction, outputFunction, fnNodeInitializer: (l, i) => { if (l == 0) { return new Neuron(false) { Label = d.ColumnAt(i - 1), ActivationFunction = activationFunction, NodeId = i, LayerId = l } } ; if (l == 2) { return new Neuron(false) { Label = Network.GetLabel(i, d), ActivationFunction = activationFunction, NodeId = i, LayerId = l } } ; return new Neuron(false) { ActivationFunction = activationFunction, NodeId = i, LayerId = l }; }, hiddenLayers: hidden)); }
/// <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="activationFunction">The activation.</param> /// <param name="outputFunction">The ouput function for hidden nodes (Optional).</param> /// <param name="epsilon">epsilon</param> /// <returns>A Network.</returns> public static Network Create(this Network network, Descriptor d, Matrix x, Vector y, IFunction activationFunction, IFunction outputFunction = null, double epsilon = double.NaN, ILossFunction lossFunction = null) { // 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((double)(x.Cols + output) * 2.0 / 3.0); return(network.Create(x.Cols, output, activationFunction, outputFunction, fnNodeInitializer: new Func <int, int, NodeType, Neuron>((l, i, type) => { if (type == NodeType.Input) { return new Neuron(false) { Label = d.ColumnAt(i - 1), ActivationFunction = activationFunction, NodeId = i, LayerId = l } } ; else if (type == NodeType.Output) { return new Neuron(false) { Label = Network.GetLabel(i, d), ActivationFunction = activationFunction, NodeId = i, LayerId = l } } ; else { return new Neuron(false) { ActivationFunction = activationFunction, NodeId = i, LayerId = l } }; }), lossFunction: lossFunction, hiddenLayers: hidden)); }