[译]C#Encog神经网络简介
By robot-v1.0
本文链接 https://www.kyfws.com/ai/an-introduction-to-encog-neural-networks-for-c-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 9 分钟阅读 - 4194 个词 阅读量 0C#Encog神经网络简介(译文)
原文地址:https://www.codeproject.com/Articles/54575/An-Introduction-to-Encog-Neural-Networks-for-C
原文作者:JeffHeaton
译文由本站 robot-v1.0 翻译
前言
An introduction to creating neural networks with the Encog Framework for C#.
介绍使用Encog Framework for C#创建神经网络的方法.
介绍(Introduction)
本文对使用Encog人工智能框架的神经网络和神经网络编程进行了基本介绍. Encog是可用于Java和Microsoft .NET的AI框架.在本文中,我将向您展示如何使用Encog for C#创建和训练一个非常基本的神经网络.将来,我可能会使用更复杂的神经网络示例进行跟进.但是,到目前为止,本文向您展示了如何开始使用Encog.(This article provides a basic introduction to neural networks and neural network programming using the Encog Artificial Intelligence Framework. Encog is an AI framework that is available for both Java and Microsoft .NET. In this article, I will show you how to create and train a very basic neural network with Encog for C#. I will likely follow this up with more complex neural network examples in the future. However, for now, this article shows you how to get started with Encog.)
类似的文章显示了如何针对Java执行相同的操作.(There is a similar article that shows how to do this same thing for Java.) 您也可以在代码项目中找到它(You can find it here at The Code Project, as well) .(.)
背景(Background)
Encog是先进的人工智能框架.使用Encog,您可以创建高级神经网络应用程序.尽管Encog支持人工智能编程的其他方面,但本文将重点介绍神经网络编程.从2.3版开始,神经网络编程是Encog的主要重点. Encog是根据较小的GNU公共许可证(LGPL)发布的.可以从以下URL下载Encog:(Encog is an advanced Artificial Intelligence Framework. Using Encog, you can create advanced neural network applications. Though Encog supports other aspects of Artificial Intelligence programming, this article will focus on neural network programming. Neural Network programming is the primary focus of Encog, as of version 2.3. Encog is released under the Lesser GNU Public License (LGPL). Encog can be downloaded from the following URL:) http://www.heatonresearch.com/encog/(http://www.heatonresearch.com/encog/) .(.)
这是一个简单的介绍性示例,显示了如何创建可识别XOR运算符的神经网络. XOR运算符实质上是神经网络世界的" Hello World".它通常用于演示新的神经网络.(This is a simple introductory example that shows how to create a neural network that recognizes the XOR operator. The XOR operator is essentially the “Hello World” of the neural network world. It is often used to demonstrate a new neural network.)
在向您展示如何在Encog中创建神经网络之前,了解神经网络的工作原理非常重要.几乎所有的神经网络都包含层.一层是一组行为相似的神经元. Encog支持的不同类型的神经网络使用了许多不同的层类型.但是,几乎每个神经网络都具有两个非常重要的层.这些是输入和输出层.输入层是如何将数据馈送到神经网络.输出层是您如何从神经网络获取响应的方法.(Before I show you how to create a neural network in Encog, it is important to understand how a neural network works. Nearly all neural networks contain layers. A layer is a group of neurons that behave similarly. There are many different layer types used by the different types of neural networks that are supported by Encog. However, there are two very important layers that nearly every neural network will have. These are the input and output layers. The input layer is how you feed data to the neural network. The output layer is how you get the response back from the neural network.)
神经网络的输入和输出都是C#(The input and output from the neural network are both C#) double
值,普通的浮点数.输入层和输出层都将具有一定数量的神经元.这确定了该层将处理多少个浮点数.输入和输出层通常将具有不同数量的神经元.确定输入和输出层的结构是如何定义要解决的神经网络"问题".(values, ordinary floating-point numbers. Both the input and output layers will have some number of neurons. This determines how many floating-point numbers the layer will deal with. The input and output layers will typically have a different number of neurons. Deciding the structure of the input and output layer is how you define the “problem” that you are trying to solve to the neural network.)
让我们看看如何创建一个神经网络以用作XOR运算符. XOR运算符要求两个输入不同,才能使输出为真. XOR运算符的真值表可以表示为:(Let’s see how we could create a neural network to perform as an XOR operator. The XOR operator requires that the two inputs be different, for the output to be true. The XOR operator’s truth table can be expressed as follows:)
0 XOR 0 = 0
1 XOR 0 = 1
0 XOR 1 = 1
1 XOR 1 = 0
我现在想创建一个可以执行此运算符的神经网络.这样的网络将具有两个输入神经元和一个输出神经元.它将接受两个操作数并返回结果.(I would like to now create a neural network that can perform this operator. Such a network will have two input neurons and one output neuron. It will accept the two operands and return the result.)
使用代码(Using the Code)
现在,我们将看一下如何构造此示例的代码.此示例是使用Encog v2.3创建的,该版本是撰写本文时Encog的当前版本.此示例的下载随附了Encog 2.3 DLL文件.您还需要包含其中的log4net文件.您可能希望从本文前面提供的URL中获取最新的Encog JAR.(We will now take a look at how the code for this example is constructed. This example was created with Encog v2.3, which is the current version of Encog at the time of this writing. The Encog 2.3 DLL file is provided with the download for this example. You also need the log4net file which is included, as well. You may wish to grab the latest Encog JAR from the URL provided earlier in this article.)
此示例打包为一个简单的Visual Studio项目.(This example is packaged as a simple Visual Studio project.)
神经网络在使用前必须经过培训.要训练该神经网络,我必须提供训练数据.训练数据是XOR运算符的真值表. XOR具有以下输入:(Neural networks must be trained before they are of any use. To train this neural network, me must provide training data. The training data is the truth table for the XOR operator. The XOR has the following inputs:)
public static double[][] XOR_INPUT ={
new double[2] { 0.0, 0.0 },
new double[2] { 1.0, 0.0 },
new double[2] { 0.0, 1.0 },
new double[2] { 1.0, 1.0 } };
这些都是XOR运算符的所有可能输入.同样,预期输出也存储为数组.(These are all of the possible inputs to the XOR operator. Likewise, the expected outputs are also stored as an array.)
public static double[][] XOR_IDEAL = {
new double[1] { 0.0 },
new double[1] { 1.0 },
new double[1] { 1.0 },
new double[1] { 0.0 } };
这两个数组将合并以创建XOR运算符的训练数据.以下代码行结合了这两个数组以创建训练数据:(These two arrays will be combined to create the training data for the XOR operator. The following line of code combines these two arrays to create training data:)
INeuralDataSet trainingSet = new BasicNeuralDataSet(XOR_INPUT, XOR_IDEAL);
现在我们必须创建一个神经网络.下面的代码行执行此操作:(We must now create a neural network. The following lines of code do this:)
BasicNetwork network = new BasicNetwork();
network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 2));
network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 6));
network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 1));
network.Structure.FinalizeStructure();
network.Reset();
您可以看到正在创建的三个图层.他们三个(You can see the three layers being created. The three) AddLayer
上面的方法创建了三层.第一层是输入层,它具有两个神经元.创建的最后一层是输出层,它具有单个神经元.在中间创建的层称为隐藏层.隐藏层有助于神经网络的其余部分学习.必须添加更多隐藏的神经元,以使神经网络学习更复杂的模式.对于XOR运算符,四个隐藏的神经元绰绰有余.挑选隐藏神经元的数量通常是一个反复试验的过程.(methods above create the three layers. The first layer is the input layer, it has two neurons. The final layer created is the output layer, it has a single neuron. The layer created in the middle is called the hidden layer. The hidden layer helps the rest of the neural network learn. More hidden neurons must be added to allow the neural network to learn more complex patterns. Four hidden neurons is more than enough for the XOR operator. Picking the number of hidden neurons is usually a process of trial and error.)
呼吁(The call to the) Reset
上面的方法随机化网络中所有神经元之间的权重.这些随机权重将不允许神经网络充当XOR运算符.但是,我们将很快看到的训练过程将优化这些权重,并鼓励神经网络产生类似于XOR运算符的输出.(method above randomizes the weights between all of the neurons in the network. These random weights will not allow the neural network to function as an XOR operator. However, the training process, that we will see shortly, will refine these weights and encourage the neural network to produce output similar to the XOR operator.)
的(The) ActivationSigmoid
class指定要使用的激活类型. S型函数在这里是一个不错的选择,因为我们在XOR运算符上不使用任何负值.如果网络需要识别负值,则双曲正切激活函数将更为合适. Encog支持许多不同的激活功能,所有这些功能都有其独特的用途.(class specifies the type of activation that is to be used. The sigmoid function is a good choice here because we use no negative values on the XOR operator. If the network needs to recognize negative, the hyperbolic tangent activation function would be more appropriate. Encog supports a number of different activation functions, all of which have their unique uses.)
必须创建一个训练对象来训练神经网络. Encog支持许多不同的培训方法.有些用于特定的神经网络类型.其他人在不同类型的数据上表现更好.这种神经网络的最佳"通用"训练器称为弹性传播(RPROP).以下代码创建了一个RPROP训练器:(A training object must be created to train the neural network. Encog supports a number of different training methods. Some are used for specific neural network types. Others perform better on different types of data. The best ‘general purpose’ trainer for this type of neural network is called resilient propagation (RPROP). The following code creates an RPROP trainer:)
ITrain train = new ResilientPropagation(network, trainingSet);
创建培训师后,我们应该循环进行一系列迭代.每次迭代将使神经网络更接近能够用作XOR运算符.(Once the trainer has been created, we should loop through a series of iterations. Each iteration will take the neural network closer to being able to function as an XOR operator.)
do
{
train.Iteration();
Console.WriteLine("Epoch #" + epoch + " Error:" + train.Error);
epoch++;
} while ((epoch < 5000) && (train.Error > 0.001));
在每次迭代或时代,我们都会检查错误.我们继续操作,直到误差小于1%.误差定义了神经网络的输出与之前定义的理想输出的距离.(At each iteration, or epoch, we check the error. We continue until the error is less than 1%. The error defines how far the output of the neural network is from the ideal output defined earlier.)
一旦对神经网络进行了令人满意的训练,我们将利用它.我们将每个输入馈入神经网络并观察输出.以下代码执行此操作:(Once the neural network has been trained satisfactorily, we will make use of it. We will feed each of the inputs into the neural network and observe the output. The following code does this:)
Console.WriteLine("Neural Network Results:");
foreach (INeuralDataPair pair in trainingSet)
{
INeuralData output = network.Compute(pair.Input);
Console.WriteLine(pair.Input[0] + "," + pair.Input[1]
+ ", actual=" + output[0] + ",ideal=" + pair.Ideal[0]);
}
我们遍历每个训练数据项,并呈现神经网络的输出以及实际输出.程序的输出如下所示:(We loop through each of the training data items and present the output from the neural network as well as the actual output. The output from the program is shown here:)
Epoch #1 Error:0.577117664847525
Epoch #2 Error:0.577117664847525
Epoch #3 Error:0.533613696022388
Epoch #4 Error:0.503211450802659
Epoch #5 Error:0.503000549513747
Epoch #6 Error:0.502288740107349
...
Epoch #40 Error:0.0206168956797096
Epoch #41 Error:0.00842597610945368
Epoch #42 Error:0.0011283298276187
Epoch #43 Error:0.000212804226087766
Neural Network Results:
0,0, actual=7.79044815631158E-07,ideal=0
1,0, actual=0.999959847028804,ideal=1
0,1, actual=0.999974904416701,ideal=1
1,1, actual=0.000101466036536172,ideal=0
如您所见,在误差百分比降至1%以下之前,神经网络循环进行了多次迭代.经过43次迭代,对网络进行了训练.当您运行应用程序时,它可能会花费更多或更少的时间,因为神经网络以随机权重开始.(As you can see, the neural network loops through a number of iterations before the error percent drops below 1%. After 43 iterations, the network is trained. When you run the application, it may take more or less, as the neural network starts with random weights.)
完成后,您将看到神经网络的实际输出.神经网络的输出与理想输出不完全匹配.这是预期的.但是,值0.99非常接近1.0.同样,为第一个模式显示的科学符号数接近于零.(Once it is done, you see the actual output from the neural network. The output from the neural network does not exactly match the ideal output. This is expected. However, the value 0.99 is very close to 1.0. Likewise, the scientific notation number displayed for the first pattern is close to zero.)
兴趣点(Points of Interest)
本文演示了一个非常简单的神经网络应用程序.使用神经网络创建XOR运算符实在太大了. Encog提供了许多更复杂的示例.我可能会在这里写更多文章,展示Encog的一些更高级的功能.(This article demonstrated a very simple neural network application. It is massive overkill to use a neural network to create an XOR operator. There are many more complex examples provided with Encog. I will likely write further articles here demonstrating some of the more advanced features of Encog.)
神经网络的有趣特征之一是它的创建与传统程序完全不同.对于传统程序,您将考虑如何实现XOR运算符并创建所有必要的编程逻辑来实现.对于神经网络,您只需提供输入示例和预期的输出.取决于神经网络来学习如何提供这些预期的输出.通常,您真的不知道它如何实际学习提供输出.对于可能具有数百个神经元的大型神经网络尤其如此.(One of the interesting features of a neural network is that it is created completely differently from a traditional program. For a traditional program, you would think about how to implement the XOR operator and create all of the necessary programming logic to do it. For a neural network, you just provide input examples and the expected outputs. It is up to the neural network to learn how to provide these expected outputs. Usually, you really have no idea how it actually learns to provide its output. This is especially true with large neural networks that may have hundreds of neurons.)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# Windows Dev Architect AI 新闻 翻译