Encog Java Examples

From Encog Machine Learning Framework
Jump to: navigation, search

There are a variety of ways to compile and execute the Encog Examples. However, the first thing that you will need to do is download the Encog Examples for Java. These files can be found here:

Encog Java Downloads

You should download both the core and example archives. The core archive contains the Encog Javadocs, source code, and compiled release JAR's. The examples archive contains the complete source to the examples. Both the core and example archives are self contained, however, you will likely want the core download as well because it contains the Encog Javadocs.

There are a variety of ways you can compile and execute the examples in Java.

Using the Encog Examples in an IDE

Encog should work with any modern IDE available for Java. Tutorials are provided for the following IDE's.

We will likely add an IntelliJ tutorial soon.

Using the Encog Examples with ANT

Ant is a tool used to build projects. All Encog releases are build using Maven, however, we provide information for ANT as well. To use ANT, you must be sure that it is installed. To determine if ant is installed, execute ant command, as you can see here:

C:\Users\jheaton>ant
Buildfile: build.xml does not exist!
Build failed
C:\Users\jheaton>

Despite the fact that I got an error, ANT is properly installed. The error simply indicates that I do not have a build.xml file in my path.

To compile the examples first move to the directory that you have the examples installed into. For this example, I am assuming that it is C:\encog-examples-3.0.0-SNAPSHOT. Enter the ant command and you should see something similar to the following.

C:\encog-examples-3.0.0-SNAPSHOT>ant
Buildfile: build.xml
 
init:
    [mkdir] Created dir: C:\encog-examples-3.0.0-SNAPSHOT\build
 
compile:
    [javac] Compiling 86 source files to C:\encog-examples-3.0.0-SNAPSHOT\build
 
dist:
      [jar] Building jar: C:\encog-examples-3.0.0-SNAPSHOT\lib\examples.jar
 
BUILD SUCCESSFUL
Total time: 2 seconds
C:\encog-examples-3.0.0-SNAPSHOT>dir
 Volume in drive C is OS
 Volume Serial Number is E419-2B65
 
 Directory of C:\encog-examples-3.0.0-SNAPSHOT
 
04/22/2011  10:21 AM    <DIR>          .
04/22/2011  10:21 AM    <DIR>          ..
04/21/2011  08:08 PM               730 .classpath
04/21/2011  08:08 PM               462 .project
04/22/2011  09:19 AM    <DIR>          .settings
04/22/2011  09:19 AM    <DIR>          apidocs
04/22/2011  10:21 AM    <DIR>          build
04/22/2011  10:20 AM             1,443 build.xml
04/22/2011  10:17 AM    <DIR>          dist
04/22/2011  10:19 AM           903,398 examples.jar
04/22/2011  10:21 AM    <DIR>          lib
04/21/2011  08:08 PM                 0 LICENSE.txt
04/21/2011  08:08 PM                 0 NOTICE.txt
04/21/2011  08:08 PM             5,625 pom.xml
04/21/2011  08:08 PM                 0 README.txt
04/22/2011  09:19 AM    <DIR>          src
               8 File(s)        911,658 bytes
               8 Dir(s)  800,699,785,216 bytes free
 
C:\encog-examples-3.0.0-SNAPSHOT>ant
Buildfile: build.xml
 
init:
    [mkdir] Created dir: C:\encog-examples-3.0.0-SNAPSHOT\build
 
compile:
    [javac] Compiling 86 source files to C:\encog-examples-3.0.0-SNAPSHOT\build
 
dist:
      [jar] Building jar: C:\encog-examples-3.0.0-SNAPSHOT\lib\examples.jar
 
BUILD SUCCESSFUL
Total time: 2 seconds
C:\encog-examples-3.0.0-SNAPSHOT>

Your output might be slightly different, depending on version numbers. Now that the examples have been compiled, we can execute them.

You will find the compiled JAR files in the lib directory. Move into that directory and issue a dir command. You should see the two JAR files. The names of the JAR files might be slightly different, depending on the Encog version you are using. Both files must be included in the java command to run the examples.

C:\encog-examples-3.0.0-SNAPSHOT>cd lib
 
C:\encog-examples-3.0.0-SNAPSHOT\lib>dir
 Volume in drive C is OS
 Volume Serial Number is E419-2B65
 
 Directory of C:\encog-examples-3.0.0-SNAPSHOT\lib
 
04/22/2011  10:28 AM    <DIR>          .
04/22/2011  10:28 AM    <DIR>          ..
04/21/2011  08:09 PM           810,579 encog-core-3.0.0-SNAPSHOT.jar
04/22/2011  10:24 AM           145,936 examples.jar
               2 File(s)        956,515 bytes
               2 Dir(s)  800,696,946,688 bytes free
 
C:\encog-examples-3.0.0-SNAPSHOT\lib>java -cp encog-core-3.0.0-SNAPSHOT.jar;examples.jar org.encog.examples.neural.xor.XORHelloWorld

In the above example, I used the following command to execute the XORHelloWorld example.

java -cp encog-core-3.0.0-SNAPSHOT.jar;examples.jar org.encog.examples.neural.xor.XORHelloWorld

This will produce output similar to this.

Epoch #1 Error:0.2598572497324211
Epoch #2 Error:0.2551690969552338
Epoch #3 Error:0.2507910354358059
Epoch #4 Error:0.2500840286336642
Epoch #5 Error:0.2518387635794088
Epoch #6 Error:0.25298111425292485
Epoch #7 Error:0.25226189027640933
Epoch #8 Error:0.2507189068512614
Epoch #9 Error:0.2498719920218717
Epoch #10 Error:0.25008371868633794
Epoch #11 Error:0.2505962232487143
Epoch #12 Error:0.2506690068275491
Epoch #13 Error:0.25027689511250945
Epoch #14 Error:0.2498676691982638
Epoch #15 Error:0.24976102352257937
Epoch #16 Error:0.24988228029556947
Epoch #17 Error:0.2499731319723439
...
Epoch #262 Error:0.01010542704768214
Epoch #263 Error:0.01000488353149423
Epoch #264 Error:0.009906312614907957
Neural Network Results:
0.0,0.0, actual=0.10247678448233427,ideal=0.0
1.0,0.0, actual=0.9034784225379703,ideal=1.0
0.0,1.0, actual=0.8950890030205028,ideal=1.0
1.0,1.0, actual=0.09173004960694356,ideal=0.0

You might see more, or fewer, Epoch's depending on how successful the initial random weights of the neural network were.

Using the Encog Examples with Maven

Begin by downloading the examples to a directory. For this example I am assuming that the examples are located in the directory C:\encog-examples-3.0.0-SNAPSHOT. Your filename will likely be different. Just use what ever directory is included in the zip that was downloaded. To compile the examples with Maven you should issue the command mvn package as you see here.

C:\encog-examples-3.0.0-SNAPSHOT>mvn package
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Encog Examples 3.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
...
[INFO] 
[INFO] --- maven-assembly-plugin:2.2.1:single (make-assembly) @ encog-examples ---
[INFO] Reading assembly descriptor: src/main/assembly/release.xml
[INFO] Building zip: C:\encog-examples-3.0.0-SNAPSHOT\target\encog-examples-3.0.0-SNAPSHOT-release.zip
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 18.611s
[INFO] Finished at: Sun Apr 24 20:34:08 CDT 2011
[INFO] Final Memory: 21M/197M
[INFO] ------------------------------------------------------------------------

Maven outputs pages of text to the console. Only the beginning and completion are shown above.

Now that the examples have been compiled, they can be executed. The following command can be used to execute an example. Note: if your JAR files have different names, because they are from a different version, make sure to update the command. The JAR files will be located in the lib directory.

java -cp .\lib\encog-core-3.0.0-SNAPSHOT.jar;.\lib\examples.jar org.encog.examples.neural.xor.XORHelloWorld

You can see an example of running the above example here.

C:\encog-examples-3.0.0-SNAPSHOT>java -cp .\lib\encog-core-3.0.0-SNAPSHOT.jar;.\
lib\examples.jar org.encog.examples.neural.xor.XORHelloWorld
Epoch #1 Error:0.2527228916643881
Epoch #2 Error:0.25139577938633284
Epoch #3 Error:0.2502031913784494
Epoch #4 Error:0.25002384343137424
Epoch #5 Error:0.2505025820800253
Epoch #6 Error:0.25081870698039244
Epoch #7 Error:0.25062902265100645
Epoch #8 Error:0.25021007654084965
Epoch #9 Error:0.24997210951662388
Epoch #10 Error:0.25002074771728006
...
Epoch #314 Error:0.010783407592568538
Epoch #315 Error:0.010668875112447376
Epoch #316 Error:0.010556741766347591
Epoch #317 Error:0.010446932523331598
Epoch #318 Error:0.010339375464944445
Epoch #319 Error:0.010234001625087643
Epoch #320 Error:0.010130744839667825
Epoch #321 Error:0.010029541605334737
Epoch #322 Error:0.009930330946676457
Neural Network Results:
0.0,0.0, actual=0.10095734090225009,ideal=0.0
1.0,0.0, actual=0.9029167569353723,ideal=1.0
0.0,1.0, actual=0.903486986538461,ideal=1.0
1.0,1.0, actual=0.1019799717186347,ideal=0.0

You might see more, or fewer, Epoch's depending on how successful the initial random weights of the neural network were.

See Also