2016-08-13 116 views
1

我正在尝试使用这个https://www.youtube.com/watch?v=KkwX7FkLfug教程来使用反向传播编写NeuralNet。用于将一个层中的所有节点连接到下一层中的所有节点的算法

我想在C#中做到这一点,并正在改变一些细节。

他和我的主要区别是,我不知道从一开始就有多少神经元会在每个节点上。这是我的API将如何在代码中工作。

class main 
{ 
    static void Main(string[] args) 
    { 
     List<bool> testData = new List<bool>() {true}; 
     System.Console.WriteLine("In MAIN! \n"); 
     int inputNeurons = 64; 
     int outputNeurons = 2; 

     double eta = 0.1; // 0.0..1.0 training rate 
     double alpha = 0.5; // 0.0..n  momentum 

     NeuralNet neuralNet = new NeuralNet(eta, alpha); 
     // order matters, first layer is for input, last layer is for output 
     neuralNet.AddLayer(inputNeurons); // input layer 
              // multiple hidden layers could go here 
     neuralNet.AddLayer(outputNeurons); // output Layer 
     neuralNet.MakeConnections(); 

     neuralNet.Train(testData); 

     neuralNet.GetResults(); 
     Console.ReadLine(); 
    } 
} 

这里是我的NeuralNet代码的其余部分,还有一部分是伪代码或只是打印报表,现在当我尝试并获得的连接工作。

namespace NeuralNetJO 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

     } 
    } 

    public class NeuralNet 
    { 
     public NeuralNet(double newEta, double newAlpha) 
     { 
      numLayers = -1; // numLayers tracks how many layers of neurons the neural net has 
      eta = newEta; 
      alpha = newAlpha; 
     } 

     /** 
     * GetResults() 
     *  returns array of results and weights 
     */ 
     public void GetResults() 
     { 
      foreach (Layer l in n_layer) 
      { 
       foreach (Neuron n in l.n_neurons) 
       { 

       } 
      } 
     } 

     public void MakeConnections() 
     { 
      // For each layer 
      if (numLayers > 0) 
      { 
       for (int i = 0; i < n_layer.Count; i++) 
       { 
        for (int j = 0; j < n_layer[i].n_neurons.Count; j++) 
        { 
         //For each Node in Layer that isn't final layer, connect it to every node in the next layer 

        } 
       } 
      } 
     } 

     public void FeedForward(List<int> inputVals) 
     { 

     } 

     public void BackProp(List<int> targetVals) 
     { 

     } 

     public void AddLayer(int numNeurons) 
     { 
      numLayers++; 
      if (numLayers > 0) //If first layer 
      { 
       Layer layer = new Layer(numNeurons, numLayers, n_layer[numLayers - 1]); 
       n_layer.Add(layer); 
      } 
      else 
      { 
       Layer layer = new Layer(numNeurons, numLayers); 
       n_layer.Add(layer); 
      } 
     } 

     public void Train(List<bool> testData) 
     { 
      Console.WriteLine("Training..."); 
      if (testData[0] == false) 
      { 
       Console.WriteLine("\t False"); 
      } 
      else 
      { 
       Console.WriteLine("\t True"); 
      } 
     } 



     //-------------- Member Variables --------------// 

     private List<Layer> n_layer = new List<Layer>(); // List of layers, layers are comprised of Neurons 
     private int numLayers; 
     double eta; 
     double alpha; 
    } 


    public class Layer 
    { 
     // mumLayer is for debug purposes only 
     public Layer(int numNeurons, int numLayer, Layer prevLayer = null) 
     { 
      myLayer = numLayer; 
      for (int i = 0; i <= numNeurons; ++i) // Add a bias Neuron 
      { 
       System.Console.Write(i + ": "); // Show line number for accurate Neuron count 
       Neuron neuron = new Neuron(i); 

       Console.WriteLine(" in layer #" + numLayer); 
       n_neurons.Add(neuron); 
      } 

      if (prevLayer != null) 
      { 
       foreach (Neuron n in prevLayer) 
       { 

       } 
      } 
     } 

     public List<Neuron> n_neurons = new List<Neuron>(); 
     int myLayer; 
    } 

    /** 
    * Neuron is a class that holds public information about Neurons 
    * This include weights, value, input and output locations. 
    */ 
    public class Neuron 
    { 
     public Neuron(int index) // Constructor 
     { 
      myIndex = index; 
      System.Console.Write("Creating Neuron " + myIndex); 
     } 



     private double transferFunction(double x) 
     { 
      return x; 
     } 

     private double transferFunctionDerivative(double x) 
     { 
      return x; 
     } 

     double randomWeight() 
     { 
      // set weights random 
      Random r = new Random(0); 
      return r.NextDouble() * 2 - 1.0; 
     } 

     public double Value { get; set; } // Output value 
     List<Connection> outPutWeights;  // Fpr each connection for the layer to the right 
     public int numOutputs { set; get; } // This will be set when numLayers > 0; 
     int myIndex; 
     double eta;  // training rate 
     double alpha; // momentum 
     double gradient; 

     private double sumDOW(Layer nextLayer) 
     { 
      return 1; 
     } 
    } 

    public class Connection 
    { 
     public double Weight { get; set; } 
     public double DeltaWeight { get; set; } 
    } 
} 

在教程中,他给每个神经元的下一层神经元的数量。在我的代码中,我无法做到这一点,因为我一次只添加一个图层。第一层添加的是输入层,第二层到n-1层是隐藏层,最后一层是输出层。

我在绕过一个很好的算法时会遇到很多麻烦,这些算法可以循环遍历第一层中的每个神经元并将它们链接到第二个神经元等等。我假设它将需要递归并以某种方式使用我的'numLayers'变量。

正如您在我的使用代码中看到的,我拨打电话neuralNet.MakeConnections(),我愿意添加连接,因为我添加了其他图层;如果任何人都能看到这样做的好地方。

这是我最初的想法,但是当我被困住时,我画了一张照片,并决定用这个函数来做它可能会更简单。

在此先感谢您的帮助!

+0

小心使用'numLayers'。即使'n_layer.Count == 0',你也可以在'-1'开始。然后你用它来决定这是否是你的第一层。为什么不使用'if(n_layer.Count == 0)...'来决定何时添加第一层? –

回答

1

re:I am assuming it will need to be recursive

根本不是。对于额外变量numLayers也没有必要,您已经知道您添加了多少个图层n_layers.Count

// in class NeuralNet 
public void MakeConnections() 
{ 
    // start at input layer, stop at last hidden layer 
    for(int i=0; i<(n_layer.Count()-1); ++i) 
    { 
    Layer thisLayer = n_layer[i]; // only for typing convenience 
    Layer nextLayer = n_layer[i+1]; 
    for(int n1=0; n1<thisLayer.n_neurons.Count(); ++n1) 
    { 
     Neuron thisNeuron = thisLayer.n_neurons[n1]; 
     for(int n2=0; n2<nextLayer.n_neurons.Count(); ++n2) 
     { 
     thisNeuron.outPutWeights.Add(
      new Connection(randomWeight(),randomWeight()); 
     } 
    } 
    } 
} 

如果你愿意,当你添加图层添加这些连接,那么它可能是这个样子:

// in class NeuralNet 
public void AddLayer(int numNeurons) 
{ 
    Layer layer = new Layer(numNeurons, 
          n_layers.Count(), 
          (n_layers.Count > 0) 
          ? n_layer[n_layers.Count-1] 
          : null); 
    n_layers.Add(layer); 
} 

// in class Layer 
public Layer(int numNeurons, 
      int numLayer, 
      Layer prevLayer = null) 
{ 
    for (int i = 0; i <= numNeurons; ++i) 
    { 
    Neuron neuron = new Neuron(i); 
    n_neurons.Add(neuron); 
    } 
    if (prevLayer != null) 
    { 
    for(int i=0; i<prevLayer.n_neurons.Count(); ++i) 
    { 
     for(int j=0; j<n_neurons.Count(); ++j) 
     { 
     prevLayer.n_neurons[i].outputWeights.Add(
      new Connection(randomWeight(), randomWeight())); 
     } 
    } 
    } 
} 

有,当然,没有需要以两种风格进行连接。你会有这样的双连接。只要一起用MakeConnections立即全部做,或者在飞行中做。

相关问题