如何在神经网络中应用权重?
How to apply weights in neural network?
我正在尝试学习如何实现神经网络。我似乎在任何地方都找不到的一件主要事情是如何应用和存储权重。我通过手动为每个输出执行所有乘法,使其在一个简单的 [2 输入,4 输出] 网络中工作。但现在我有 4 个输入、4 个隐藏在 2 层中和 4 个输出,并将它们存储在一个 4x4x2 数组中。我似乎无法掌握如何使用 for 循环来应用它。下面的当前代码显示了我目前拥有的内容,被注释为神经网络的部分是我目前正在尝试弄清楚的。
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);
}
}
}
}
因此,正如我在评论中所说,您将拥有一个带权重的层 class 和一个网络 class。在大多数情况下,没有必要将事情分解得更小。
所以对于层 class,我们有 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
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;
}
}
希望这很清楚
我正在尝试学习如何实现神经网络。我似乎在任何地方都找不到的一件主要事情是如何应用和存储权重。我通过手动为每个输出执行所有乘法,使其在一个简单的 [2 输入,4 输出] 网络中工作。但现在我有 4 个输入、4 个隐藏在 2 层中和 4 个输出,并将它们存储在一个 4x4x2 数组中。我似乎无法掌握如何使用 for 循环来应用它。下面的当前代码显示了我目前拥有的内容,被注释为神经网络的部分是我目前正在尝试弄清楚的。
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);
}
}
}
}
因此,正如我在评论中所说,您将拥有一个带权重的层 class 和一个网络 class。在大多数情况下,没有必要将事情分解得更小。
所以对于层 class,我们有 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
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;
}
}
希望这很清楚