C# 将一层中的所有节点连接到下一层中的所有节点的算法
我正试图使用本教程编写一个使用反向传播的神经网络 我正在尝试用C#来做这件事,并且正在改变一些细节 他的和我的主要区别在于,我不知道从一开始每个节点上会有多少个神经元。下面是我的API在代码中的工作方式C# 将一层中的所有节点连接到下一层中的所有节点的算法,c#,algorithm,neural-network,nodes,C#,Algorithm,Neural Network,Nodes,我正试图使用本教程编写一个使用反向传播的神经网络 我正在尝试用C#来做这件事,并且正在改变一些细节 他的和我的主要区别在于,我不知道从一开始每个节点上会有多少个神经元。下面是我的API在代码中的工作方式 class main { static void Main(string[] args) { List<bool> testData = new List<bool>() {true}; System.Console.Wri
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();
}
}
主类
{
静态void Main(字符串[]参数)
{
List testData=new List(){true};
System.Console.WriteLine(“主!\n”);
int输入神经元=64;
int输出神经元=2;
双eta=0.1;//0.0..1.0培训率
双α=0.5;//0.0..n动量
NeuralNet NeuralNet=新的NeuralNet(eta,α);
//顺序很重要,第一层用于输入,最后一层用于输出
neuralNet.AddLayer(inputNeurons);//输入层
//这里可以有多个隐藏层
neuralNet.AddLayer(outputNeurons);//输出层
neuralNet.MakeConnections();
neuralNet.Train(测试数据);
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; }
}
}
名称空间NeuralNetJO
{
班级计划
{
静态void Main(字符串[]参数)
{
}
}
公共类神经网络
{
公共神经网络(双倍newEta,双倍newAlpha)
{
numLayers=-1;//numLayers跟踪神经网络有多少层神经元
埃塔=新埃塔;
阿尔法=新阿尔法;
}
/**
*GetResults()
*返回结果和权重的数组
*/
public void GetResults()
{
foreach(n_层中的层l)
{
foreach(l.n_神经元中的神经元n)
{
}
}
}
公共连接()
{
//每层
如果(numLayers>0)
{
对于(int i=0;i0)//如果第一层
{
图层=新图层(numNeurons、numLayers、n_图层[numLayers-1]);
n_层。添加(层);
}
其他的
{
图层=新图层(numneuron、numLayers);
n_层。添加(层);
}
}
公共无效列车(列出测试数据)
{
控制台。WriteLine(“培训…”);
if(testData[0]==false)
{
Console.WriteLine(“\t False”);
}
其他的
{
Console.WriteLine(“\t True”);
}
}
//--------------成员变量--------------//
私有列表n_layer=new List();//层列表,层由神经元组成
私人国际公寓;
双eta;
双α;
}
公共类层
{
//mumLayer仅用于调试目的
公共层(int numNeurons,int numLayer,Layer prevLayer=null)
{
myLayer=numLayer;
对于(int i=0;i 0;
int-myIndex;
双eta;//训练率
双α;//动量
双梯度;
私人双坑(下一层)
{
返回1;
}
}
公共类连接
{
公共双权重{get;set;}
公共双DeltaWeight{get;set;}
}
}
在教程中,他给每个神经元下一层的神经元数量。在我的代码中,我不能这样做,因为我一次添加一层。添加的第一层是输入层,第二层到n-1层是隐藏层,最后一层是输出层
我很难理解一个好的算法,它可以循环通过第一层中的每个神经元,并将它们链接到第二层中的每个神经元,以此类推。我假设它需要递归,并以某种方式使用我的“numLayers”变量
正如您在我的使用代码中看到的,我调用了neuralNet.MakeConnections()
,我愿意在添加附加层的同时添加连接;如果有人能看到这样做的好地方,我会这样做
这是我最初的想法,但当我陷入困境时,我给自己画了一张图,并决定用这个函数来做可能更简单
提前感谢您的帮助!re:
我假设它需要递归
不需要。也不需要额外的变量numLayers
,您已经知道添加了多少层n\u层。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());
}
}
}
}
及
//在类层中
公共层(int numNeurons,
int numLayer,
层(层=空)
{
对于(int i=0;我会注意使用numLayers
。即使n\u layer.Count==0
,也要从-1
开始。然后使用它来决定这是否是第一层。为什么不直接使用if(n\u layer.Count==0).
本身来决定何时添加第一层?
// 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() ) );
}
}
}
}