天天看點

Encog3Java-User.pdf翻譯:第四章 使用Java建構神經網絡

第三章是關于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 implement 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

Encog3Java-User.pdf翻譯:第四章 使用Java建構神經網絡

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激活函數使用一個基于對數函數的算法。下面展示了如何計算這個激活函數。

Encog3Java-User.pdf翻譯:第四章 使用Java建構神經網絡

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

Encog3Java-User.pdf翻譯:第四章 使用Java建構神經網絡

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。

Encog3Java-User.pdf翻譯:第四章 使用Java建構神經網絡

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

Encog3Java-User.pdf翻譯:第四章 使用Java建構神經網絡

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中看到。

Encog3Java-User.pdf翻譯:第四章 使用Java建構神經網絡

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

Encog3Java-User.pdf翻譯:第四章 使用Java建構神經網絡

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 implements 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.

此誤差水準應與網絡最初訓練時的誤差水準相比對。