您的位置:首页 > 理论基础 > 计算机网络

Encog3Java-User.pdf翻译:第四章 使用Java构建神经网络

2017-11-29 14:00 399 查看
第三章是关于Encog的图形界面应用程序,我没有下载下来。所以没翻译。

Chapter 4

第四章

Constructing Neural Networks in Java

使用Java构建神经网络

? Constructing a Neural Network

? Activation Functions

? Encog Persistence

? Using the Encog Analyst from Code

This chapter will show how to construct feedforward and simple recurrent neural networks with Encog and how to save these neural networks for later use. Both of these neural network types are created using the BasicNetwork and BasicLayer classes. In addition
to these two classes, activation functions are also used. The role of activation functions will be discussed as well.

本章将展示如何用Encog构建前馈和简单递归神经网络,以及如何保存这些神经网络,以便后续使用。这两个神经网络类型创建使用BasicNetwork和BasicLayer类。除了这两个类之外,还使用了激活函数。激活函数的作用也将会讨论。

Neural networks can take a considerable amount of time to train. Because of this it is important to save your neural networks. Encog neural networks can be persisted using Java’s built-in serialization. This persistence can also be achieved by writing the neural
network to an EG file, a cross-platform text file. This chapter will introduce both forms of persistence.

神经网络可能会花费相当多的时间来训练。正因为如此,保存你的神经网络是很重要的。Encog神经网络可以使用java内置的序列化来持久化。这种持久性也可以通过将神经网络写入一个跨平台文本文件EG文件来实现。本章将介绍持久性的两种形式。

In the last chapter, the Encog Analyst was used to automatically normalize data. The Encog Analyst can also automatically create neural networks based on CSV data. This chapter will show how to use the Encog analyst to create neural networks from code.

在上一章中,Encog Analyst进行自动规范数据。Encog Analyst还可以自动创建基于CSV数据神经网络。本章将展示如何使用Encog Analyst创建神经网络。

4.1 Constructing a Neural Network

4.1 构建神经网络

A simple neural network can quickly be created using BasicLayer and BasicNetwork objects. The following code creates several BasicLayer objects with a default hyperbolic tangent activation function.

一个简单的神经网络可以使用BasicNetwork和BasicLayer对象迅速创建。下面的代码创建一个默认的双曲正切函数的几个BasicLayer对象(我下载的是3.4版本的Encog代码,看代码里默认激活函数是ActivationSigmoid,不是双曲正切函数ActivationTANH)。

BasicNetwork network = new BasicNetwork();

network.addLayer(new BasicLayer(2));

network.addLayer(new BasicLayer(3));

network.addLayer(new BasicLayer(1));

network.getStructure().finalizeStructure();

network.reset();

This network will have an input layer of two neurons, a hidden layer with three neurons and an output layer with a single neuron. To use an activation function other than the hyperbolic tangent function, use code similar to the following:

这个网络将有两个神经元的输入层,三个神经元的隐藏层,和一个神经元的输出层。要使用除双曲正切函数以外的激活函数,使用与下面类似的代码:

BasicNetwork network = new BasicNetwork();

network.addLayer(new BasicLayer( null , true , 2 ));

network.addLayer(new BasicLayer(new ActivationSigmoid(), true , 3 ));

network.addLayer(new BasicLayer(new ActivationSigmoid(), false , 1 ));

network.getStructure().finalizeStructure();

network.reset();

The sigmoid activation function is passed to the AddLayer calls for the hidden and output layer. The true value that was also introduced specifies that the BasicLayer should have a bias neuron. The output layer does not have bias neurons, and the input layer
does not have an activation function. This is because the bias neuron affects the next layer, and the activation function affects data coming from the previous layer.

调用addlayer方法,Sigmoid函数传递给隐层和输出层。指定的BasicLayer要有一个偏置神经元。输出层没有偏置神经元,输入层不具有激活功能。这是因为偏置神经元影响下一层,激活函数影响来自前一层的数据。

Unless Encog is being used for something very experimental, always use a bias neuron. Bias neurons allow the activation function to shift off the origin of zero. This allows the neural network to produce a zero value even when the inputs are not zero. The following
URL provides a more mathematical justification for the importance of bias neurons:

除非encog被用于一些实验,否则总是用一个偏置神经元。偏置神经元允许激活函数转移原点零点。这使得神经网络即使在输入不为零时也能产生零值。下面的URL为偏置神经元的重要性提供了更多的数学依据:
http://www.heatonresearch.com/wiki/Bias
Activation functions are attached to layers and used to scale data output from a layer. Encog applies a layer’s activation function to the data that the layer is about to output. If an activation function is not specified for BasicLayer, the hyperbolic tangent
activation will be defaulted.

激活函数连接到层,用于缩放来自层的数据输出。Encog将层的激活函数应用到此层将要输出的数据上。如果一个BasicLayer没有指定函数,双曲正切激活函数是缺省函数(我下载的是3.4版本的Encog代码,看代码里默认激活函数是ActivationSigmoid,不是双曲正切函数ActivationTANH)。

It is also possible to create context layers. A context layer can be used to create an Elman or Jordan style neural networks. The following code could be used to create an Elman neural network.

还可以创建上下文层。上下文层可以用来创建一个Elman或Jordan风格的神经网络。下面的代码可以用来创建一个Elman神经网络。

BasicLayer input , hidden ;

BasicNetwork network = new BasicNetwork();

network.addLayer( input = new BasicLayer(1));

network.addLayer( hidden = new BasicLayer(2));

network.addLayer(new BasicLayer(1));

input.setContextFedBy( hidden );

network.getStructure().finalizeStructure();

network.reset();

Notice the hidden.setContextFedBy line? This creates a context link from the output layer to the hidden layer. The hidden layer will always be fed the output from the last iteration. This creates an Elman style neural network.Elman and Jordan networks will
be introduced in Chapter 7.

注意到hidden.setContextFedBy那一行了吗?这将创建一个从输出层到隐藏层的上下文链接。隐藏层将始终被从最后一次迭代输出。这将创建一个Elman风格的神经网络。Elman和Jordan网络将在第7章中介绍。

4.2 The Role of Activation Functions

4.2 激活函数的角色

The last section illustrated how to assign activation functions to layers. Activation functions are used by many neural network architectures to scale the output from layers. Encog provides many different activation functions that can be used to construct neural
networks. The next sections will introduce these activation functions.

上一节演示了如何将激活函数分配给层。许多神经网络体系结构使用激活函数来缩放层的输出。Encog提供许多不同的激活函数,可用于构建神经网络。下一节将介绍这些激活函数。

Activation functions are attached to layers and are used to scale data output from a layer. Encog applies a layer’s activation function to the data that the layer is about to output. If an activation function is not specified for BasicLayer, the hyperbolic
tangent activation will be the defaulted. All classes that serve as activation functions must impl
4000
ement the ActivationFunction interface.

激活函数连接到层,用于缩放来自层的数据输出。Encog将层的激活函数应用到此层将要输出的数据上。如果一个BasicLayer没有指定函数,双曲正切激活函数是缺省函数。所有作为激活函数的类必须实现ActivationFunction接口。

Activation functions play a very important role in training neural networks. Propagation training, which will be covered in the next chapter, requires than an activation function have a valid derivative. Not all activation functions have valid derivatives.
Determining if an activation function has a derivative may be an important factor in choosing an activation function.

激活函数在训练神经网络中起着非常重要的作用。将在下一章中涉及的传播训练要求激活函数要有一个有效的导数。并非所有激活函数都有有效的导数。确定激活函数是否有导数可能是选择激活函数的一个重要因素。

4.3 Encog Activation Functions

4.3 Encog激活函数

The next sections will explain each of the activation functions supported by Encog. There are several factors to consider when choosing an activation function. Firstly, it is important to consider how the type of neural network being used dictates the activation
function required. Secondly, consider the necessity of training the neural network using propagation. Propagation training requires an activation function that provides a derivative. Finally, consider the range of numbers to be used. Some activation functions
deal with only positive numbers or numbers in a particular range.

下一节将介绍encog支持的激活函数。在选择激活函数时有几个因素需要考虑。首先,重要的是要考虑使用的神经网络类型如何决定所需的激活功能。其次,考虑利用传播训练神经网络的必要性。传播训练需要一个有导数的激活函数。最后,考虑要使用的数字的范围。某些激活函数只处理特定范围内的正数或数字。

4.3.1 ActivationBiPolar

The ActivationBiPolar activation function is used with neural networks that require bipolar values. Bipolar values are either true or false. A true value is represented by a bipolar value of 1; a false value is represented by a bipolar value of -1. The bipolar
activation function ensures that any numbers passed to it are either -1 or 1. The ActivationBiPolar function does this with the following code:

激活函数ActivationBiPolar采用神经网络需要双极值。双极值是真或假。一个真值用双极值1表示;假值用双极值-1表示。双极激活函数确保传递给它的任何数字都是- 1或1。ActivationBiPolar函数使用如下代码起作用:

if(d[ i ] > 0){

d[ i ] = 1;

} else {

d[ i ] = -1;

}

As shown above, the output from this activation is limited to either -1 or 1. This sort of activation function is used with neural networks that require bipolar output from one layer to the next. There is no derivative function for bipolar, so this activation
function cannot be used with propagation training.

如上所示,此激活的输出仅限于- 1或1。这种激活函数被用于需要从一层到下一层的双极输出的神经网络。双极性没有导数函数,所以这种激活函数不能用于传播训练。

4.3.2 ActivationCompetitive

The ActivationCompetitive function is used to force only a select group of neurons to win. The winner is the group of neurons with the highest output. The outputs of each of these neurons are held in the array passed to this function. The size of the winning
neuron group is definable. The function will first determine the winners. All non-winning neurons will be set to zero. The winners will all have the same value, which is an even division of the sum of the winning outputs.

activationcompetitive函数用于迫使只有一组神经元获胜。获胜者是产出最高的神经元群。每个神经元的输出被保存在传递给这个函数的数组中。获胜神经元组的大小是可定义的。函数首先决定获胜者。所有非获胜神经元将被设置为零。获奖者都有相同的价值,这是获胜产出总和的一个划分。

This function begins by creating an array that will track whether each neuron has already been selected as one of the winners. The number of winners is also counted.

这个函数从创建一个数组开始,该数组将跟踪每个神经元是否已经被选为获胜者之一。获胜人数也算在内。

final boolean [ ] winners = new boolean [ x.length ] ;

double sumWinners = 0 ;

First, loop maxWinners a number of times to find that number of winners.

// f i n d the d e s i r e d number o f winners

for( int i = 0 ; i < this.params [ 0 ] ; i++){

double maxFound = Double.NEGATIVE INFINITY ;

int winner = -1;

Now, one winner must be determined. Loop over all of the neuron outputs and find the one with the highest output.

for( int j = s t a r t ; j < s t a r t + s i z e ; j++){

If this neuron has not already won and it has the maximum output, it might be a winner if no other neuron has a higher activation.

i f( ! winners [ j ] &&( x [ j ] > maxFound)){

winner = j ;

maxFound = x [ j ] ;

}

}

Keep the sum of the winners that were found and mark this neuron as a winner. Marking it a winner will prevent it from being chosen again. The sum of the winning outputs will ultimately be divided among the winners.

保留发现的获胜者总数,并标记这个神经元为胜利者。把它标为胜利者将防止它再次被选中。获胜产出的总和最终将在优胜者中分配。

sumWinners += maxFound;

winners [ winner ] = true ;

}

Now that the correct number of winners is determined, the values must be adjusted for winners and non-winners. The non-winners will all be set to zero. The winners will share the sum of the values held by all winners.

现在确定了正确的获胜者数,必须对优胜者和非优胜者的值进行调整。非获奖者都将被设置为零。获胜者将分享所有获胜者保存的价值总和。

// adjust weights for winners and non-winners

for( int i = start ; i < start + size ; i++){

i f( winners [ i ]){

x[ i ] = x[ i ] / sumWinners ;

} e l s e {

x[ i ] = 0.0;

}

}

This sort of an activation function can be used with competitive, learning neural networks such as the self-organizing map. This activation function has no derivative, so it cannot be used with propagation training.

这种激活函数可用于具有竞争性的学习神经网络,如自组织映射图。这个激活函数没有导数,所以不能用于传播训练。

4.3.3 ActivationLinear

The ActivationLinear function is really no activation function at all. It simply implements the linear function. The linear function can be seen in Equation 4.1.

activationlinear功能真的是没有激活功能。它只实现线性函数。线性函数可以在方程4.1中看到。

f(x)= x(4.1)

The graph of the linear function is a simple line, as seen in Figure 4.1.
Figure 4.1: Graph of the Linear Activation Function



The Java implementation for the linear activation function is very simple. It does nothing. The input is returned as it was passed.

对于线性激活函数,java实现很简单。它什么也不做。输入通过时返回。

public final void activationFunction( final double [ ] x , final int start,final int size ){}

The linear function is used primarily for specific types of neural networks that have no activation function, such as the self-organizing map. The linear activation function has a constant derivative of one, so it can be used with propagation training. Linear
layers are sometimes used by the output layer of a propagation-trained feedforward neural network.

线性函数主要用于没有激活函数的特定类型的神经网络,如自组织映射。线性激活函数的常数导数为1,因此可以用于传播训练。传播训练前馈神经网络的输出层有时使用线性层。

4.3.4 ActivationLOG

The ActivationLog activation function uses an algorithm based on the log function. The following shows how this activation function is calculated.

activationlog激活函数使用一个基于对数函数的算法。下面展示了如何计算这个激活函数。



This produces a curve similar to the hyperbolic tangent activation function, which will be discussed later in this chapter. The graph for the logarithmic activation function is shown in Figure 4.2.

这将产生类似于双曲正切激活函数的曲线,将在本章后面讨论。对数激活函数的图如图4.2所示。

Figure 4.2: Graph of the Logarithmic Activation Function



The logarithmic activation function can be useful to prevent saturation. A hidden node of a neural network is considered saturated when, on a given set of inputs, the output is approximately 1 or -1 in most cases. This can slow training significantly. This
makes the logarithmic activation function a possible choice when training is not successful using the hyperbolic tangent activation function.

对数激活函数对于防止饱和是有用的。在给定的一组输入时,输出在大多数情况下大约为1或-1,这时一个神经网络的隐节点被认为是饱和的。这可以显著减缓训练速度。这使得当使用双曲正切激活函数训练不成功时,对数激活函数是一种可能的选择。

As illustrated in Figure 4.2, the logarithmic activation function spans both positive and negative numbers. This means it can be used with neural networks where negative number output is desired. Some activation functions, such as the sigmoid activation function
will only produce positive output. The logarithmic activation function does have a derivative, so it can be used with propagation training.

如图4.2所示,对数激活函数跨越正负两个数字。这意味着它可以与需要负数输出的神经网络一起使用。一些激活函数,如sigmoid激活函数只会产生正的输出。对数激活函数确实有一个导数,因此它可以用于传播训练。

4.3.5 ActivationSigmoid

The ActivationSigmoid activation function should only be used when positive number output is expected because the ActivationSigmoid function will only produce positive output. The equation for the ActivationSigmoid function can be seen in Equation 4.2.

激活函数ActivationSigmoid只能用于正数产值预计,因为ActivationSigmoid函数只会产生正输出。ActivationSigmoid函数方程是方程4.3。



The ActivationSigmoid function will move negative numbers into the positive range. This can be seen in Figure 4.3, which shows the graph of the sigmoid function.

激活函数ActivationSigmoid将移动负数到正数范围。这可以在图4.3中看到,它显示了S形函数的图。

Figure 4.3: Graph of the ActivationSigmoid Function



The ActivationSigmoid function is a very common choice for feedforward and simple recurrent neural networks. However, it is imperative that the training data does not expect negative output numbers. If negative numbers are required, the hyperbolic tangent activation
function may be a better solution.

对于前馈和简单的递归神经网络,激活函数ActivationSigmoid是一个很常见的选择。但是,训练数据不能有负输出数。如果需要负数,双曲正切激活函数可能是更好的解决方案。

4.3.6 ActivationSoftMax

The ActivationSoftMax activation function will scale all of the input values so that the sum will equal one. The ActivationSoftMax activation function is sometimes used as a hidden layer activation function. The activation function begins by summing the natural
exponent of all of the neuron outputs.

activationsoftmax激活函数将缩放所有的输入值,总和将等于一。activationsoftmax激活函数有时被用来作为一个隐含层的激活函数。激活函数首先将所有神经元输出的自然指数相加。

double sum = 0;

for( int i = 0; i < d. length ; i++){

d[ i ] = BoundMath. exp(d[ i ]);

sum += d[ i ] ;

}

The output from each of the neurons is then scaled according to this sum.

This produces outputs that will sum to 1.

for( int i = start ; i < start + size ; i++){

x[ i ] = x[ i ] / sum;

}

The ActivationSoftMax is typically used in the output layer of a neural network for classification.

activationsoftmax通常用在一个神经网络的输出层来分类。

4.3.7 ActivationTANH

The ActivationTANH activation function uses the hyperbolic tangent function. The hyperbolic tangent activation function is probably the most commonly used activation function as it works with both negative and positive numbers. The hyperbolic tangent function
is the default activation function for Encog. The equation for the hyperbolic tangent activation function can be seen in Equation 4.3.

activationtanh激活函数采用双曲正切函数。双曲正切激活函数可能是最常用的激活函数,它适用于负数和正数。双曲正切函数是encog默认激活功能。双曲正切激活函数的方程可以在方程4.4中看到。



The fact that the hyperbolic tangent activation function accepts both positive and negative numbers can be seen in Figure 4.4, which shows the graph of the hyperbolic tangent function.

双曲正切激活函数接受正负数可以在图4.4中看到,该图显示双曲正切函数的图形。

Figure 4.4: Graph of the Hyperbolic Tangent Activation Function



The hyperbolic tangent function is a very common choice for feedforward and simple recurrent neural networks. The hyperbolic tangent function has a derivative so it can be used with propagation training.

双曲正切函数是前馈和简单递归神经网络的常用选择。双曲正切函数有一个导数,因此可以用于传播训练。

4.4 Encog Persistence

4.4 Encog持久化

It can take considerable time to train a neural network and it is important to take measures to guarantee your work is saved once the network has been trained. Encog provides several means for this data to be saved, with two primary ways to store Encog data
objects. Encog offers file-based persistence or Java’s own persistence.

训练一个神经网络需要相当长的时间,重要的是采取措施保证你的工作在网络被训练后能被保存下来。Encog提供了多种手段保存数据,有两种主要的储存Encog数据对象的方式。Encog提供基于Java的持久性或基于文件的持久性。

Java provides its own means to serialize objects and is called Java serialization. Java serialization allows many different object types to be written to a stream, such as a disk file. Java serialization for Encog works the same way as with any Java object
using Java serialization. Every important Encog object that should support serialization impleme
c8e8
nts the Serializable interface.

java提供自己的手段序列化对象被称为java序列化。java序列化允许许多不同类型的对象被写入到流中,如磁盘文件。java序列化Encog和序列化其它java对象做的一样。每一个重要的encog对象支持序列化实现Serializable接口。

Java serialization is a quick way to store an Encog object. However, it has some important limitations. The files created with Java serialization can only be used by Encog for Java; they will be incompatible with Encog for .Net or Encog for Silverlight. Further,
Java serialization is directly tied to the underlying objects. As a result, future versions of Encog may not be compatible with your serialized files.

java序列化是一个快速的方法来存储一个encog对象。然而,它有一些限制。文件创建java序列化只能由encog java使用;他们将与其它encog不相容,如.Net或Encog Silverlight。此外,java序列化是直接依赖于底层的对象。作为一个结果,encog的未来版本可能与你的序列化文件不兼容。

To create universal files that will work with all Encog platforms, consider the Encog EG format. The EG format stores neural networks as flat text files ending in the extension .EG. This chapter will introduce both methods of Encog persistence, beginning with
Encog EG persistence. The chapter will end by exploring how a neural network is saved in an Encog persistence file.

创建所有encog平台的通用文件,考虑encog EG格式。EG格式将神经网络保存为文本文件,文件扩展名为.EG。这一章将介绍encog持久性的方法,用encog持久化开始。本章结尾将探讨神经网络如何保存在一个encog持久文件中。

4.5 Using Encog EG Persistence

4.5 使用Encog的EG持久化

Encog EG persistence files are the native file format for Encog and are stored with the extension .EG. The Encog Workbench uses the Encog EG to process files. This format can be exchanged over different operating systems and Encog platforms, making it the choice
format choice for an Encog application.

Encog EG持久性文件是为encog本地文件格式,存储文件扩展名为.EG。encog工作台采用encog EG处理文件。这种格式可以在不同的操作系统和encog平台上交换,这是选择encog应用的一个原因。

This section begins by looking at an XOR example that makes use of Encog’s EG files. Later, this same example will be used for Java serialization. We will begin with the Encog EG persistence example.

该部分首先看一个例子,利用异或encog的EG文件。后面,这个例子也会用于java序列化。我们将开始与encog EG持久化实例。

4.5.1 Using Encog EG Persistence

4.5.1 使用Encog的EG持久化

Encog EG persistence is very easy to use. The EncogDirectoryPersistence class is used to load and save objects from an Encog EG file. The following is a good example of Encog EG persistence:

encog EG的持久性是非常容易使用。encogdirectorypersistence类用于从一个encog EG文件加载对象和保存对象到一个encog EG文件中。以下是encog EG持久的一个很好的例子:

org.encog.examples.neural.persist.EncogPersistence

This example is made up of two primary methods. The first method, trainAndSave, trains a neural network and then saves it to an Encog EG file. The second method, loadAndEvaluate, loads the Encog EG file and evaluates it.

这个例子由两个主要方法组成。第一个方法,trainandsave,训练一个神经网络,然后将其保存到一个encog EG文件中。第二个方法,loadandevaluate,加载encog EG文件并评估它。

This proves that the Encog EG file was saved correctly. The main method simply calls these two in sequence. We will begin by examining the trainAndSave method.

这证明encog EG文件保存正确。main方法简单地按顺序调用这两个方法。我们将通过检查trainandsave方法开始。

public void trainAndSave( ){

System.out.println(

” Training XOR network to under 1% errorrate.” );

This method begins by creating a basic neural network to be trained with the XOR operator. It is a simple three-layer feedforward neural network. 

该方法首先创建一个基本的神经网络,用XOR运算符进行训练。它是一种简单的三层前馈神经网络。

BasicNetwork network = new BasicNetwork( );

network.addLayer(new BasicLayer( 2 ));

network.addLayer(new BasicLayer( 6 ));

network.addLayer(new BasicLayer( 1 ));

network.getStructure( ).finalizeStructure( );

network.reset( );

A training set is created that contains the expected outputs and inputs for the XOR operator.

MLDataSet trainingSet = new BasicMLDataSet(XOR INPUT, XOR IDEAL);

This neural network will be trained using resilient propagation(RPROP).

// train the neural network

final MLTrain train = new ResilientPropagation( network , trainingSet );

RPROP iterations are performed until the error rate is very small. Training will be covered in the next chapter. For now, training is a means to verify that the error remains the same after a network reload.

RPROP迭代执行直到错误率很小。训练将在下一章讨论。目前,训练是验证网络重新加载后错误保持不变的一种手段。

do {

train.iteration();

} while( train.getError()> 0.009);

Once the network has been trained, display the final error rate. The neural network can now be saved.

一旦网络被训练完,显示最终错误率。现在可以保存神经网络。

double e = network.calculateError( trainingSet );

System.out.println(”Network traiined to error : ”+ e);

System.out.println(”Saving network”);

The network can now be saved to a file. Only one Encog object is saved per file. This is done using the saveObject method of the EncogDirectoryPersistence class.

该网络现在可以保存到一个文件中。每个文件只保存一个encog对象。这是使用的encogdirectorypersistence类的saveobject方法。

System.out.println(”Saving network”);

EncogDirectoryPersistence.saveObject(new File(FILENAME), network);

Now that the Encog EG file has been created, load the neural network back from the file to ensure it still performs well using the loadAndEvaluate method.

现在,encog EG文件已经创建,加载神经网络从文件以确保它仍然表现良好,使用loadandevaluate方法。

public void loadAndEvaluate()

{

System.out.println(”Loading network”);

BasicNetwork network = (BasicNetwork)EncogDirectoryPersistence.loadObject(new File(FILENAME));

Now that the collection has been constructed, load the network named network that was saved earlier. It is important to evaluate the neural network to prove that it is still trained. To do this, create a training set for the XOR operator.

现在已经构建了集合,加载前面保存的网络。重要的是要评估的神经网络,以证明它仍然是训练过的。为此,为XOR运算符创建一个训练集。

MLDataSet trainingSet = new BasicMLDataSet(XOR INPUT, XOR IDEAL);

Calculate the error for the given training data.

double e = network.calculateError( trainingSet );

System.out.println( ” Loaded network ’serroris(should be same as above ): ” + e );}

This error is displayed and should be the same as before the network was saved.

此错误显示,与保存网络之前相同。

4.6 Using Java Serialization

4.6 使用Java序列化

It is also possible to use standard Java serialization with Encog neural networks and training sets. Encog EG persistence is much more flexible than Java serialization. However, there are cases a neural network can simply be saved to a platform-dependant binary
file. This example shows how to use Java serialization with Encog. The example begins by calling the trainAndSave method.

encog神经网络中也可以使用标准java序列化与训练集。encog EG持久性比java序列化更灵活。然而,有些情况下,一个神经网络可以简单地保存到依赖于平台的二进制文件中。这个例子显示了如何在Encog中使用java序列化。该示例首先调用trainandsave方法。

public void trainAndSave( )throws IOException {

System.out.println(” training XOR network to under 1% errorrate.” );

This method begins by creating a basic neural network to be trained with the XOR operator. It is a simple, three-layer feedforward neural network.

方法开始创建一个基本神经网络并使用XOR训练。这是个简单的三层前馈神经网络。

BasicNetwork network = new BasicNetwork( );

network.addLayer(new BasicLayer( 2 ));

network.addLayer(new BasicLayer( 6 ));

network.addLayer(new BasicLayer( 1 ));

network.getStructure( ).finalizeStructure( );

network.reset( );

MLDataSet trainingSet = new BasicMLDataSet(XOR INPUT, XOR IDEAL);

We will train this neural network using resilient propagation(RPROP).

// train the neural network

final MLTrain train = new ResilientPropagation( network , trainingSet );

The following code loops through training iterations until the error rate is below one percent(<0.01).

do {

train.iteration();

} while( train.getError()> 0.01);

The final error for the neural network is displayed.

double e = network.calculateError( trainingSet );

System.out.println(”Network traiined to error : ” + e);

System.out.println(”Saving network”);

Regular Java Serialization code can be used to save the network or the SerializeObject class can be used. This utility class provides a save method that will write any single serializable object to a binary file. Here the save method is used to save the neural
network.

普通java Serialization代码可以用于保存网络或使用serializeobject类。此实用工具类提供了一个保存方法,会将任何可序列化的对象写到二进制文件中。这里保存方法用于保存神经网络。

SerializeObject.save(FILENAME, network);}

Now that the binary serialization file is created, load the neural network back from the file to see if it still performs well. This is performed by the loadAndEvaluate method.

现在创建二进制序列化文件后,从文件中加载神经网络,看看它是否仍然执行得很好。这是由loadandevaluate方法进行。

public void loadAndEvaluate()

throws IOException , ClassNotFoundException {

System.out.println(”Loading network”);

The SerializeObject class also provides a load method that will read an object back from a binary serialization file.

serializeobject类还提供负载的方法,会从一个二进制序列化文件读回一个对象。

BasicNetwork network =

(BasicNetwork)SerializeObject.load(FILENAME);

MLDataSet trainingSet =

new BasicMLDataSet(XOR INPUT, XOR IDEAL);

Now that the network is loaded, the error level is reported.

现在,网络被加载完了,给出了误差水平。

double e = network.calculateError( trainingSet );

System.out.println(

”Loaded network ’ s error is( should be same as above): ” + e);

}

This error level should match the error level at the time the network was originally trained.

此误差水平应与网络最初训练时的误差水平相匹配。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Encog
相关文章推荐