2015-05-16 31 views
0

我正在嘗試學習如何實現神經網絡。我似乎無法在任何地方發現的一個主要問題是如何應用和存儲權重。我通過手動完成每個輸出的所有乘法,在一個簡單的[2輸入,4輸出]網絡中工作。但現在我有4個輸入,4個隱藏在2層,4個輸出,並將它們存儲在4x4x2陣列中。我似乎無法掌握如何將這個應用於循環。下面的當前代碼顯示了我目前所擁有的部分以及作爲神經網絡評論的部分是我目前試圖找出的部分。如何在神經網絡中應用權重?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
//Import SFML 
using SFML.Graphics; 
using SFML.System; 
using SFML.Window; 
namespace GeneticAlgorithims 
{ 
    class Program 
    { 
     //Neural Network Variables 
     public static int Inputs = 4; 
     public static int Outputs = 4; 
     public static int Hiddens = 2; 
     //End Variables 
     public static Random rand = new Random(); 
     public static List<Organism> organisms = new List<Organism>(); 
     public static RenderWindow window = new RenderWindow(new VideoMode(800,600), "Neural Networks", Styles.Default, new ContextSettings(512, 512, 16)); 
     static void Main(string[] args) 
     { 
      Generate(); 
      while(window.IsOpen) 
      { 
       window.DispatchEvents(); 
       Update(); 
       window.Clear(Color.Cyan); 
       Draw(); 
       window.Display(); 
      } 
     } 

     static void Generate() 
     { 
      for (int x = 0; x < 2; x++) 
      { 
       organisms.Add(new Organism(new Vector2f(rand.Next(0, (int)window.Size.X), rand.Next(0, (int)window.Size.Y)), new Matrix(new double[Inputs, Inputs, Hiddens]))); 
      } 

      foreach (Organism organism in organisms) 
      { 
       organism.color = new Color((byte)rand.Next(0, 255),(byte)rand.Next(0, 255),(byte)rand.Next(0, 255)); 
       organism.rotation = rand.Next(1, 360); 
      } 
     } 

     static void Update() 
     { 
      //Image map = window.Capture(); 
      foreach(Organism organism in organisms) 
      { 
       //Get Near Organism 
       Organism near = new Organism(); 
       foreach (Organism check in organisms) 
       { 
        if (check != organism) 
        { 
         if (Math.Abs(check.position.X - organism.position.X) < 128) 
         { 
          if (Math.Abs(check.position.Y - organism.position.Y) < 128) 
          { 
           near = check; 
          } 
         } 
        } 
       } 
       //Begin Neural Network 
       int CurrentColor = near.color.R; 
       float DistanceX = (near.position != null) ? Math.Abs(near.position.X - organism.position.X) : 0; 
       float DistanceY = (near.position != null) ? Math.Abs(near.position.Y - organism.position.Y) : 0; 
       float Health = organism.Health; 
       Console.WriteLine(CurrentColor); 
       for (int A = 0; A < Inputs; A++) 
       { 
        for (int B = 0; B < Outputs; B += 4) 
        { 
         for (int C = 0; C < Hiddens; C++) 
         { 
          organism.rotation += 
         } 
        } 
       } 
        //End Neural Network 
        organism.Age++; 
       organism.position.X += organism.Speed * (float)Math.Cos(organism.rotation * 0.0174f); 
       organism.position.Y += organism.Speed * (float)Math.Sin(organism.rotation * 0.0174f); 

       float X = organism.position.X; 
       float Y = organism.position.Y; 
       if (organism.position.X > window.Size.X) organism.position.X = 0; 
       if (organism.position.X < 0) organism.position.X = window.Size.X; 
       if (organism.position.Y > window.Size.Y) organism.position.Y = 0; 
       if (organism.position.Y < 0) organism.position.Y = window.Size.Y; 

       if(organism.Age % 2500 == 0) 
       { 
        Organism newOrganism = new Organism(organism.position, organism.brain); 
        newOrganism.rotation = rand.Next(0, 360); 
        newOrganism.color = organism.color; 
        //organisms.Add(newOrganism); 
        //return; 
       } 
       if (organism.Age > 3000) 
       { 
        //organisms.Remove(organism); 
        //return; 
       } 
      } 
     } 

     static void Draw() 
     { 
      Sprite bufferSprite = new Sprite(new Texture("img.png")); 
      foreach (Organism organism in organisms) 
      { 

       Vertex[] line = new Vertex[2]; 
       line[0] = new Vertex(organism.position, Color.Red); 
       line[1] = new Vertex(new Vector2f(organism.position.X + (float)(120 * Math.Cos((organism.rotation - 30) * 0.0174)), organism.position.Y + (float)(120 * Math.Sin((organism.rotation - 30) * 0.0174))), Color.Red); 
       window.Draw(line, PrimitiveType.Lines); 

       line[0] = new Vertex(organism.position, Color.Red); 
       line[1] = new Vertex(new Vector2f(organism.position.X + (float)(120 * Math.Cos((organism.rotation + 30) * 0.0174)), organism.position.Y + (float)(120 * Math.Sin((organism.rotation + 30) * 0.0174))), Color.Red); 
       window.Draw(line, PrimitiveType.Lines); 

       bufferSprite.Origin = new Vector2f(8, 8); 
       bufferSprite.Color = organism.color; 
       bufferSprite.Rotation = organism.rotation; 
       bufferSprite.Position = organism.position; 
       window.Draw(bufferSprite); 

      } 
     } 
    } 
} 
+0

SO神經網絡自己的典型方法是讓層對象具有一維權重數組進入該層。然後你有一個包含所有圖層的一維數組的網絡對象。在網絡中,您遍歷輸入中傳遞的圖層,並將該圖層的輸出傳遞到下一圖層。在圖層內部,您可以在該圖層的權重上循環乘以輸入並對其進行求和。然後最後一層輸出是網絡輸出。 – user1646196

+0

謝謝,這有助於瞭解它是如何工作的,你會介意在僞代碼 –

+0

中顯示這​​個問題我會用它寫出一個答案:) – user1646196

回答

0

所以正如我在評論中說的,你會有一個帶有權重和網絡類的圖層類。在大多數情況下,沒有必要將事情分解得小一些。

所以對於層類,我們有N權重加偏置:

class Layer 
{ 
    double[] weights; 
    int nOut, nIn; 
    Layer(int inputs, int outputs) 
    { 
     nIn=inputs; nOut=outputs; 
     weights=new double[(inputs+1)*outputs]; //Assume random weights chosen 
    } 

    double[] processInput(double[] in) 
    { 
     double[] out=new double[nOut]; //initialise all to zero 
     for(int i=0; i<nOut; i++) 
     { 
      out[i] = weights[(i+1)*nIn] //bias always present 
      for(int j=0; j<nIn; j++) 
      { 
       out[i] +=in[j]*weights[(i+1)*nIn+j +1] //skips bias 
      } 
      out[i]=activationFunction(out[i]) 
     } 
     return out 
    } 
} 

那麼對於網絡類

class Network 
{ 
    List<Layer> 
    //skipping adding and setting up layers 

    double[] processInput(double[] in) 
    { 
     double[] temp=in; 
     for(Layer l:layers) 
     { 
      temp=layer.processInput(temp); 
      //input for next layer is output from previous 
     } 
     return temp; 
    } 
} 

,如果你想實現一個希望是十分明顯的