├── .gitattributes ├── .gitignore ├── .travis.yml ├── LICENSE.txt ├── NOTICE.txt ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── src └── main ├── assembly └── release.xml └── java └── org └── encog └── examples ├── clustering └── kmeans │ └── SimpleKMeans.java ├── gui ├── elementary │ ├── DisplayPanel.java │ └── ElementaryExample.java └── life │ ├── GameOfLife.java │ └── WorldPanel.java ├── guide ├── classification │ └── IrisClassification.java ├── regression │ └── AutoMPGRegression.java └── timeseries │ ├── SunSpotTimeseries.java │ └── package-info.java ├── ml ├── bayesian │ ├── BayesianSpam.java │ ├── SimpleBayesian.java │ ├── SimpleK2.java │ └── words │ │ ├── BayesianSpam.java │ │ └── BayesianWordAnalyzer.java ├── hmm │ ├── HMMSimpleContinuous.java │ ├── HMMSimpleDiscrete.java │ └── HMMSimpleKMeans.java ├── prg │ ├── Benchmark.java │ └── SimpleExpression.java ├── sampling │ └── SimpleSampling.java ├── tsp │ ├── City.java │ ├── anneal │ │ ├── SolveTSP.java │ │ └── TSPSimulatedAnnealing.java │ └── genetic │ │ ├── SolveTSP.java │ │ └── TSPScore.java └── world │ ├── QLearningPanel.java │ └── QLearningPattern.java ├── neural ├── adaline │ └── AdalineDigits.java ├── analyst │ ├── AnalystExample.java │ └── AnalystNormalize.java ├── art │ └── art1 │ │ └── NeuralART1.java ├── bam │ └── BidirectionalAssociativeMemory.java ├── benchmark │ ├── ActivationFunctions.java │ ├── Benchmark.java │ ├── BenchmarkErrorFunctions.java │ ├── BinaryVsMemory.java │ ├── ElliottBenchmark.java │ ├── ErrorMethod.java │ ├── FahlmanEncoder.java │ ├── FreeformBenchmark.java │ ├── MatrixBenchmark.java │ ├── MultiBench.java │ ├── PSOBenchmark.java │ ├── SimpleBenchmark.java │ ├── TestConverge.java │ ├── ThreadCount.java │ └── WeightInitialization.java ├── boltzmann │ └── BoltzTSP.java ├── cpn │ └── RocketCPN.java ├── cross │ └── CrossValidateSunspot.java ├── csv │ └── XORCSV.java ├── forest │ ├── Evaluate.java │ ├── ForestConfig.java │ ├── ForestCover.java │ ├── GenerateData.java │ └── TrainNetwork.java ├── freeform │ ├── ConvertToFreeform.java │ ├── ElmanFreeform.java │ ├── FreeformCompare.java │ ├── FreeformOnlineXOR.java │ ├── FreeformXOR.java │ └── SkipNeuralNetwork.java ├── gui │ ├── hopfield │ │ ├── HopfieldPanel.java │ │ └── HopfieldPattern.java │ ├── ocr │ │ ├── Entry.java │ │ ├── OCR.java │ │ ├── Sample.java │ │ └── SampleData.java │ ├── predict │ │ ├── GraphPanel.java │ │ └── PredictSIN.java │ └── som │ │ ├── MapPanel.java │ │ └── SomColors.java ├── hopfield │ └── HopfieldAssociate.java ├── image │ └── ImageNeuralNetwork.java ├── lunar │ ├── LanderSimulator.java │ ├── LunarLander.java │ ├── NeuralPilot.java │ └── PilotScore.java ├── neat │ ├── XORNEAT.java │ └── boxes │ │ ├── BoxTrialCase.java │ │ ├── BoxesScore.java │ │ ├── DisplayBoxes.java │ │ ├── DisplayBoxesPanel.java │ │ ├── TrialEvaluation.java │ │ └── VisualizeBoxesMain.java ├── normalize │ ├── NormalizeFile.java │ └── SimpleNormalize.java ├── persist │ ├── EncogPersistence.java │ └── Serial.java ├── predict │ ├── market │ │ ├── Config.java │ │ ├── MarketBuildTraining.java │ │ ├── MarketEvaluate.java │ │ ├── MarketPredict.java │ │ ├── MarketPrune.java │ │ └── MarketTrain.java │ └── sunspot │ │ ├── MultiSunspot.java │ │ ├── PredictSunspot.java │ │ ├── PredictSunspotElman.java │ │ └── PredictSunspotSVM.java ├── radial │ └── MultiRadial.java ├── recurrent │ ├── elman │ │ └── ElmanXOR.java │ └── jordan │ │ └── JordanXOR.java ├── resume │ └── TrainResume.java ├── som │ └── SimpleSOM.java ├── util │ ├── TemporalXOR.java │ └── XOR.java ├── xor │ ├── XORConst.java │ ├── XORFactory.java │ ├── XORHelloWorld.java │ ├── XOROnline.java │ └── XORPSO.java ├── xorpartial │ ├── XORPartial.java │ └── XORPartialAuto.java └── xorsql │ └── XORSQL.java └── proben ├── BenchmarkDefinition.java ├── EncogBenchmarkDefinition.java ├── ProBen.java ├── ProBenData.java ├── ProBenError.java ├── ProBenEvaluate.java ├── ProBenResult.java ├── ProBenResultAccumulator.java └── ProBenRunner.java /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs eol=crlf 6 | *.java eol=lf 7 | *.sln merge=union 8 | *.csproj merge=union 9 | *.vbproj merge=union 10 | *.fsproj merge=union 11 | *.dbproj merge=union 12 | 13 | # Standard to msysgit 14 | *.doc diff=astextplain 15 | *.DOC diff=astextplain 16 | *.docx diff=astextplain 17 | *.DOCX diff=astextplain 18 | *.dot diff=astextplain 19 | *.DOT diff=astextplain 20 | *.pdf diff=astextplain 21 | *.PDF diff=astextplain 22 | *.rtf diff=astextplain 23 | *.RTF diff=astextplain 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | target/ 3 | build/ 4 | 5 | *.iml 6 | .DS_Store 7 | .gradle 8 | .idea 9 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: 3 | - oraclejdk8 4 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffheaton/encog-java-examples/c4372d20f9b62a111c78d068ca2b3926b829ad1a/LICENSE.txt -------------------------------------------------------------------------------- /NOTICE.txt: -------------------------------------------------------------------------------- 1 | Encog 3.2 Machine Learning Framework 2 | Copyright 2008-2013 Heaton Research, Inc. 3 | 4 | This product includes software developed at 5 | Heaton Research, Inc. (http://www.heatonresearch.com/encog/). 6 | 7 | ==================================================================== 8 | LibSVM Copyright 9 | Copyright (c) 2000-2011 Chih-Chung Chang and Chih-Jen Lin 10 | All rights reserved. 11 | 12 | Redistribution and use in source and binary forms, with or without 13 | modification, are permitted provided that the following conditions 14 | are met: 15 | 16 | 1. Redistributions of source code must retain the above copyright 17 | notice, this list of conditions and the following disclaimer. 18 | 19 | 2. Redistributions in binary form must reproduce the above copyright 20 | notice, this list of conditions and the following disclaimer in the 21 | documentation and/or other materials provided with the distribution. 22 | 23 | 3. Neither name of copyright holders nor the names of its contributors 24 | may be used to endorse or promote products derived from this software 25 | without specific prior written permission. 26 | 27 | 28 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 29 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 30 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 31 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR 32 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 33 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 34 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 35 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 36 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 37 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 38 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 39 | ======================================================================= -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/encog/encog-java-examples.svg)](https://travis-ci.org/encog/encog-java-examples) 2 | 3 | Encog Examples 3.3 4 | 5 | The following links will be helpful getting started with Encog. 6 | 7 | Getting Started: 8 | 9 | http://www.heatonresearch.com/wiki/Getting_Started 10 | 11 | Important Links: 12 | 13 | http://www.heatonresearch.com/encog 14 | http://www.heatonresearch.com/wiki -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffheaton/encog-java-examples/c4372d20f9b62a111c78d068ca2b3926b829ad1a/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Sep 29 18:40:51 CDT 2014 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.1-bin.zip 7 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /src/main/assembly/release.xml: -------------------------------------------------------------------------------- 1 | 4 | release 5 | 6 | zip 7 | 8 | 9 | 10 | 11 | ${project.basedir} 12 | / 13 | true 14 | 15 | .settings/** 16 | src/** 17 | .classpath 18 | .project 19 | LICENSE.txt 20 | README.txt 21 | NOTICE.txt 22 | pom.xml 23 | build.xml 24 | 25 | 26 | 27 | ${project.build.directory} 28 | / 29 | 30 | apidocs/** 31 | 32 | 33 | 34 | ${project.build.directory} 35 | /lib 36 | 37 | *-jar-with-dependencies* 38 | 39 | 40 | 41 | 42 | 43 | 44 | /lib 45 | true 46 | false 47 | runtime 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /src/main/java/org/encog/examples/clustering/kmeans/SimpleKMeans.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Encog(tm) Java Examples v3.4 3 | * http://www.heatonresearch.com/encog/ 4 | * https://github.com/encog/encog-java-examples 5 | * 6 | * Copyright 2008-2017 Heaton Research, Inc. 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | * 20 | * For more information on Heaton Research copyrights, licenses 21 | * and trademarks visit: 22 | * http://www.heatonresearch.com/copyright 23 | */ 24 | package org.encog.examples.clustering.kmeans; 25 | 26 | import java.util.Arrays; 27 | 28 | import org.encog.ml.MLCluster; 29 | import org.encog.ml.data.MLDataPair; 30 | import org.encog.ml.data.MLDataSet; 31 | import org.encog.ml.data.basic.BasicMLData; 32 | import org.encog.ml.data.basic.BasicMLDataPair; 33 | import org.encog.ml.data.basic.BasicMLDataSet; 34 | import org.encog.ml.kmeans.KMeansClustering; 35 | 36 | /** 37 | * This example performs a simple KMeans cluster. The numbers are clustered 38 | * into two groups. 39 | */ 40 | public class SimpleKMeans { 41 | 42 | /** 43 | * The data to be clustered. 44 | */ 45 | public static final double[][] DATA = { { 28, 15, 22 }, { 16, 15, 32 }, 46 | { 32, 20, 44 }, { 1, 2, 3 }, { 3, 2, 1 } }; 47 | 48 | /** 49 | * The main method. 50 | * @param args Arguments are not used. 51 | */ 52 | public static void main(final String args[]) { 53 | 54 | final BasicMLDataSet set = new BasicMLDataSet(); 55 | 56 | for (final double[] element : SimpleKMeans.DATA) { 57 | set.add(new BasicMLData(element)); 58 | } 59 | 60 | final KMeansClustering kmeans = new KMeansClustering(2, set); 61 | 62 | kmeans.iteration(100); 63 | //System.out.println("Final WCSS: " + kmeans.getWCSS()); 64 | 65 | // Display the cluster 66 | int i = 1; 67 | for (final MLCluster cluster : kmeans.getClusters()) { 68 | System.out.println("*** Cluster " + (i++) + " ***"); 69 | final MLDataSet ds = cluster.createDataSet(); 70 | final MLDataPair pair = BasicMLDataPair.createPair( 71 | ds.getInputSize(), ds.getIdealSize()); 72 | for (int j = 0; j < ds.getRecordCount(); j++) { 73 | ds.getRecord(j, pair); 74 | System.out.println(Arrays.toString(pair.getInputArray())); 75 | 76 | } 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/org/encog/examples/gui/elementary/DisplayPanel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Encog(tm) Java Examples v3.4 3 | * http://www.heatonresearch.com/encog/ 4 | * https://github.com/encog/encog-java-examples 5 | * 6 | * Copyright 2008-2017 Heaton Research, Inc. 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | * 20 | * For more information on Heaton Research copyrights, licenses 21 | * and trademarks visit: 22 | * http://www.heatonresearch.com/copyright 23 | */ 24 | package org.encog.examples.gui.elementary; 25 | 26 | import java.awt.Dimension; 27 | import java.awt.Graphics; 28 | import java.awt.Image; 29 | 30 | import javax.swing.JPanel; 31 | 32 | public class DisplayPanel extends JPanel { 33 | private Image currentImage; 34 | 35 | public Image getCurrentImage() { 36 | return currentImage; 37 | } 38 | 39 | public void setCurrentImage(Image currentImage) { 40 | this.currentImage = currentImage; 41 | setPreferredSize(new Dimension(this.currentImage.getWidth(null), this.currentImage.getHeight(null))); 42 | repaint(); 43 | } 44 | 45 | public void paint(Graphics g) 46 | { 47 | super.paint(g); 48 | if( this.currentImage!=null ) { 49 | g.drawImage(this.currentImage, 0, 0, null); 50 | } 51 | } 52 | 53 | 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/org/encog/examples/gui/life/WorldPanel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Encog(tm) Java Examples v3.4 3 | * http://www.heatonresearch.com/encog/ 4 | * https://github.com/encog/encog-java-examples 5 | * 6 | * Copyright 2008-2017 Heaton Research, Inc. 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | * 20 | * For more information on Heaton Research copyrights, licenses 21 | * and trademarks visit: 22 | * http://www.heatonresearch.com/copyright 23 | */ 24 | package org.encog.examples.gui.life; 25 | 26 | import java.awt.Dimension; 27 | import java.awt.Graphics; 28 | import java.awt.Image; 29 | 30 | import javax.swing.JPanel; 31 | 32 | public class WorldPanel extends JPanel { 33 | private Image currentImage; 34 | 35 | public Image getCurrentImage() { 36 | return currentImage; 37 | } 38 | 39 | public void setCurrentImage(Image currentImage) { 40 | this.currentImage = currentImage; 41 | setPreferredSize(new Dimension(this.currentImage.getWidth(null), this.currentImage.getHeight(null))); 42 | repaint(); 43 | } 44 | 45 | public void paint(Graphics g) 46 | { 47 | super.paint(g); 48 | if( this.currentImage!=null ) { 49 | g.drawImage(this.currentImage, 0, 0, null); 50 | } 51 | } 52 | 53 | 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/org/encog/examples/guide/timeseries/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Encog(tm) Java Examples v3.4 3 | * http://www.heatonresearch.com/encog/ 4 | * https://github.com/encog/encog-java-examples 5 | * 6 | * Copyright 2008-2017 Heaton Research, Inc. 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | * 20 | * For more information on Heaton Research copyrights, licenses 21 | * and trademarks visit: 22 | * http://www.heatonresearch.com/copyright 23 | */ 24 | package org.encog.examples.guide.timeseries; 25 | -------------------------------------------------------------------------------- /src/main/java/org/encog/examples/ml/bayesian/SimpleBayesian.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Encog(tm) Java Examples v3.4 3 | * http://www.heatonresearch.com/encog/ 4 | * https://github.com/encog/encog-java-examples 5 | * 6 | * Copyright 2008-2017 Heaton Research, Inc. 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | * 20 | * For more information on Heaton Research copyrights, licenses 21 | * and trademarks visit: 22 | * http://www.heatonresearch.com/copyright 23 | */ 24 | package org.encog.examples.ml.bayesian; 25 | 26 | import org.encog.ml.bayesian.BayesianEvent; 27 | import org.encog.ml.bayesian.BayesianNetwork; 28 | import org.encog.ml.bayesian.EventType; 29 | import org.encog.ml.bayesian.query.enumerate.EnumerationQuery; 30 | import org.encog.ml.bayesian.query.sample.SamplingQuery; 31 | 32 | public class SimpleBayesian { 33 | public static void main(String[] args) { 34 | 35 | // build the bayesian network structure 36 | BayesianNetwork network = new BayesianNetwork(); 37 | BayesianEvent rained = network.createEvent("rained"); 38 | BayesianEvent evenTemperatures = network.createEvent("temperature"); 39 | BayesianEvent gardenGrew = network.createEvent("gardenGrew"); 40 | BayesianEvent plentyOfCarrots = network.createEvent("carrots"); 41 | BayesianEvent plentyOfTomatoes = network.createEvent("Tomatoes"); 42 | network.createDependency(rained, gardenGrew); 43 | network.createDependency(evenTemperatures, gardenGrew); 44 | network.createDependency(gardenGrew, plentyOfCarrots); 45 | network.createDependency(gardenGrew, plentyOfTomatoes); 46 | network.finalizeStructure(); 47 | 48 | // build the truth tales 49 | rained.getTable().addLine(0.2, true); 50 | evenTemperatures.getTable().addLine(0.5, true); 51 | gardenGrew.getTable().addLine(0.9, true, true, true); 52 | gardenGrew.getTable().addLine(0.7, true, false, true); 53 | gardenGrew.getTable().addLine(0.5, true, true, false); 54 | gardenGrew.getTable().addLine(0.1, true, false, false); 55 | plentyOfCarrots.getTable().addLine(0.8, true, true); 56 | plentyOfCarrots.getTable().addLine(0.2, true, false); 57 | plentyOfTomatoes.getTable().addLine(0.6, true, true); 58 | plentyOfTomatoes.getTable().addLine(0.1, true, false); 59 | 60 | // validate the network 61 | network.validate(); 62 | 63 | // display basic stats 64 | System.out.println(network.toString()); 65 | System.out.println("Parameter count: " + network.calculateParameterCount()); 66 | 67 | EnumerationQuery query = new EnumerationQuery(network); 68 | //SamplingQuery query = new SamplingQuery(network); 69 | query.defineEventType(rained, EventType.Evidence); 70 | query.defineEventType(evenTemperatures, EventType.Evidence); 71 | query.defineEventType(plentyOfCarrots, EventType.Outcome); 72 | query.setEventValue(rained, true); 73 | query.setEventValue(evenTemperatures, true); 74 | query.setEventValue(plentyOfCarrots, true); 75 | query.execute(); 76 | System.out.println(query.toString()); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/main/java/org/encog/examples/ml/bayesian/SimpleK2.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Encog(tm) Java Examples v3.4 3 | * http://www.heatonresearch.com/encog/ 4 | * https://github.com/encog/encog-java-examples 5 | * 6 | * Copyright 2008-2017 Heaton Research, Inc. 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | * 20 | * For more information on Heaton Research copyrights, licenses 21 | * and trademarks visit: 22 | * http://www.heatonresearch.com/copyright 23 | */ 24 | package org.encog.examples.ml.bayesian; 25 | 26 | import org.encog.ml.bayesian.BayesianNetwork; 27 | import org.encog.ml.bayesian.training.BayesianInit; 28 | import org.encog.ml.bayesian.training.TrainBayesian; 29 | import org.encog.ml.bayesian.training.search.k2.SearchK2; 30 | import org.encog.ml.data.MLDataSet; 31 | import org.encog.ml.data.basic.BasicMLDataSet; 32 | 33 | public class SimpleK2 { 34 | 35 | public static final double DATA[][] = { 36 | { 1, 0, 0 }, // case 1 37 | { 1, 1, 1 }, // case 2 38 | { 0, 0, 1 }, // case 3 39 | { 1, 1, 1 }, // case 4 40 | { 0, 0, 0 }, // case 5 41 | { 0, 1, 1 }, // case 6 42 | { 1, 1, 1 }, // case 7 43 | { 0, 0, 0 }, // case 8 44 | { 1, 1, 1 }, // case 9 45 | { 0, 0, 0 }, // case 10 46 | }; 47 | 48 | public static void main(String[] args) { 49 | //String[] labels = { "available", "not" }; 50 | 51 | MLDataSet data = new BasicMLDataSet(DATA,null); 52 | BayesianNetwork network = new BayesianNetwork(); 53 | network.createEvent("x1"); 54 | network.createEvent("x2"); 55 | network.createEvent("x3"); 56 | network.finalizeStructure(); 57 | 58 | TrainBayesian train = new TrainBayesian(network,data,10); 59 | train.setInitNetwork(BayesianInit.InitEmpty); 60 | train.iteration(); 61 | 62 | double p = network.performQuery("P(+x2|+x1)");// 0.71 63 | System.out.println("x2 probability : " + network.getEvent("x2").getTable().findLine(1, new int[] {1})); 64 | System.out.println("Calculated P(+x2|+x1): " + p); 65 | System.out.println("Final network structure: " + network.toString()); 66 | 67 | //EncogDirectoryPersistence.saveObject(new File("d:\\test.eg"), network); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/org/encog/examples/ml/bayesian/words/BayesianSpam.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Encog(tm) Java Examples v3.4 3 | * http://www.heatonresearch.com/encog/ 4 | * https://github.com/encog/encog-java-examples 5 | * 6 | * Copyright 2008-2017 Heaton Research, Inc. 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | * 20 | * For more information on Heaton Research copyrights, licenses 21 | * and trademarks visit: 22 | * http://www.heatonresearch.com/copyright 23 | */ 24 | package org.encog.examples.ml.bayesian.words; 25 | 26 | import org.encog.util.Format; 27 | 28 | public class BayesianSpam { 29 | 30 | public final static String[] SPAM_DATA = { 31 | "offer is secret", 32 | "click secret link", 33 | "secret sports link" 34 | }; 35 | 36 | public final static String[] HAM_DATA = { 37 | "play sports today", 38 | "went play sports", 39 | "secret sports event", 40 | "sports is today", 41 | "sports costs money" 42 | }; 43 | 44 | public static void test(BayesianWordAnalyzer a, String message) { 45 | double d = a.probability(message); 46 | System.out.println("Probability of \"" + message + "\" being " + a.getClassName() + " is " + Format.formatPercent(d) + " ; " + a.getLastProblem()); 47 | } 48 | 49 | public static void testWordClass(BayesianWordAnalyzer a, String word) { 50 | double d = a.probabilityWordClass(word); 51 | System.out.println(a.getLastProblem() + " = " + Format.formatPercent(d)); 52 | } 53 | 54 | public static void testWordNotClass(BayesianWordAnalyzer a, String word) { 55 | double d = a.probabilityWordNotClass(word); 56 | System.out.println(a.getLastProblem() + " = " + Format.formatPercent(d)); 57 | 58 | } 59 | 60 | public static final void main(String[] args) { 61 | BayesianWordAnalyzer a1 = new BayesianWordAnalyzer(0,"spam",SPAM_DATA,"ham",HAM_DATA); 62 | System.out.println("Using Laplace of 0"); 63 | System.out.println("P(" + a1.getClassName() + "): " + Format.formatPercent(a1.getClassProbability())); 64 | System.out.println("P(" + a1.getNotClassName() + "): " + Format.formatPercent(a1.getNotClassProbability())); 65 | testWordClass(a1,"secret"); 66 | testWordNotClass(a1,"secret"); 67 | test(a1,"secret"); // 0.0 68 | test(a1,"today"); // 0.0 69 | test(a1,"sports"); // 16.67 70 | test(a1,"today is secret"); // 0.0 71 | test(a1,"secret is secret"); // 96.15 72 | 73 | BayesianWordAnalyzer a2 = new BayesianWordAnalyzer(1,"spam",SPAM_DATA,"ham",HAM_DATA); 74 | System.out.println("Using Laplace of 1"); 75 | System.out.println("P(" + a2.getClassName() + "): " + Format.formatPercent(a2.getClassProbability())); 76 | System.out.println("P(" + a2.getNotClassName() + "): " + Format.formatPercent(a2.getNotClassProbability())); 77 | testWordClass(a2,"today"); 78 | testWordNotClass(a2,"today"); 79 | test(a2,"secret"); // 0.0 80 | test(a2,"today"); 81 | test(a2,"sports"); 82 | test(a2,"today is secret"); // 48.58 83 | test(a2,"secret is secret"); 84 | } 85 | 86 | 87 | 88 | } 89 | -------------------------------------------------------------------------------- /src/main/java/org/encog/examples/ml/hmm/HMMSimpleContinuous.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Encog(tm) Java Examples v3.4 3 | * http://www.heatonresearch.com/encog/ 4 | * https://github.com/encog/encog-java-examples 5 | * 6 | * Copyright 2008-2017 Heaton Research, Inc. 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | * 20 | * For more information on Heaton Research copyrights, licenses 21 | * and trademarks visit: 22 | * http://www.heatonresearch.com/copyright 23 | */ 24 | package org.encog.examples.ml.hmm; 25 | 26 | import org.encog.ml.data.MLSequenceSet; 27 | import org.encog.ml.hmm.HiddenMarkovModel; 28 | import org.encog.ml.hmm.alog.KullbackLeiblerDistanceCalculator; 29 | import org.encog.ml.hmm.alog.MarkovGenerator; 30 | import org.encog.ml.hmm.distributions.ContinousDistribution; 31 | import org.encog.ml.hmm.train.bw.TrainBaumWelch; 32 | 33 | /** 34 | * This class is a very simple example of a HMM for a continuous input. 35 | * First, a known HMM is created and output is generated from it. We then 36 | * create a second initial HMM, and use the data generated from the first 37 | * HMM to train it to match the first. 38 | */ 39 | public class HMMSimpleContinuous { 40 | 41 | static HiddenMarkovModel buildContHMM() 42 | { 43 | double [] mean1 = {0.25, -0.25}; 44 | double [][] covariance1 = { {1, 2}, {1, 4} }; 45 | 46 | double [] mean2 = {0.5, 0.25}; 47 | double [][] covariance2 = { {4, 2}, {3, 4} }; 48 | 49 | HiddenMarkovModel hmm = new HiddenMarkovModel(2); 50 | 51 | hmm.setPi(0, 0.8); 52 | hmm.setPi(1, 0.2); 53 | 54 | hmm.setStateDistribution(0, new ContinousDistribution(mean1,covariance1)); 55 | hmm.setStateDistribution(1, new ContinousDistribution(mean2,covariance2)); 56 | 57 | hmm.setTransitionProbability(0, 1, 0.05); 58 | hmm.setTransitionProbability(0, 0, 0.95); 59 | hmm.setTransitionProbability(1, 0, 0.10); 60 | hmm.setTransitionProbability(1, 1, 0.90); 61 | 62 | return hmm; 63 | } 64 | 65 | static HiddenMarkovModel buildContInitHMM() 66 | { 67 | double [] mean1 = {0.20, -0.20}; 68 | double [][] covariance1 = { {1.3, 2.2}, {1.3, 4.3} }; 69 | 70 | double [] mean2 = {0.5, 0.25}; 71 | double [][] covariance2 = { {4.1, 2.1}, {3.2, 4.4} }; 72 | 73 | HiddenMarkovModel hmm = new HiddenMarkovModel(2); 74 | 75 | hmm.setPi(0, 0.9); 76 | hmm.setPi(1, 0.1); 77 | 78 | hmm.setStateDistribution(0, new ContinousDistribution(mean1,covariance1)); 79 | hmm.setStateDistribution(1, new ContinousDistribution(mean2,covariance2)); 80 | 81 | hmm.setTransitionProbability(0, 1, 0.10); 82 | hmm.setTransitionProbability(0, 0, 0.90); 83 | hmm.setTransitionProbability(1, 0, 0.15); 84 | hmm.setTransitionProbability(1, 1, 0.85); 85 | 86 | return hmm; 87 | } 88 | 89 | public static void main(String[] args) { 90 | 91 | HiddenMarkovModel hmm = buildContHMM(); 92 | HiddenMarkovModel learntHmm = buildContInitHMM(); 93 | 94 | MarkovGenerator mg = new MarkovGenerator(hmm); 95 | MLSequenceSet training = mg.generateSequences(200,100); 96 | 97 | TrainBaumWelch bwl = new TrainBaumWelch(learntHmm,training); 98 | 99 | KullbackLeiblerDistanceCalculator klc = 100 | new KullbackLeiblerDistanceCalculator(); 101 | 102 | System.out.println("Training Continuous Hidden Markov Model with Baum Welch"); 103 | 104 | for(int i=1;i<=10;i++) { 105 | double e = klc.distance(learntHmm, hmm); 106 | System.out.println("Iteration #"+i+": Difference: " + e); 107 | bwl.iteration(); 108 | learntHmm = (HiddenMarkovModel)bwl.getMethod(); 109 | } 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /src/main/java/org/encog/examples/ml/hmm/HMMSimpleDiscrete.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Encog(tm) Java Examples v3.4 3 | * http://www.heatonresearch.com/encog/ 4 | * https://github.com/encog/encog-java-examples 5 | * 6 | * Copyright 2008-2017 Heaton Research, Inc. 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | * 20 | * For more information on Heaton Research copyrights, licenses 21 | * and trademarks visit: 22 | * http://www.heatonresearch.com/copyright 23 | */ 24 | package org.encog.examples.ml.hmm; 25 | 26 | import org.encog.ml.data.MLSequenceSet; 27 | import org.encog.ml.hmm.HiddenMarkovModel; 28 | import org.encog.ml.hmm.alog.KullbackLeiblerDistanceCalculator; 29 | import org.encog.ml.hmm.alog.MarkovGenerator; 30 | import org.encog.ml.hmm.distributions.DiscreteDistribution; 31 | import org.encog.ml.hmm.train.bw.TrainBaumWelch; 32 | 33 | /** 34 | * This class is a very simple example of a HMM for a discrete input. 35 | * First, a known HMM is created and output is generated from it. We then 36 | * create a second initial HMM, and use the data generated from the first 37 | * HMM to train it to match the first. 38 | */ 39 | public class HMMSimpleDiscrete { 40 | static HiddenMarkovModel buildDiscHMM() 41 | { 42 | HiddenMarkovModel hmm = 43 | new HiddenMarkovModel(2, 2); 44 | 45 | hmm.setPi(0, 0.95); 46 | hmm.setPi(1, 0.05); 47 | 48 | hmm.setStateDistribution(0, new DiscreteDistribution(new double[][] { { 0.95, 0.05 } })); 49 | hmm.setStateDistribution(1, new DiscreteDistribution(new double[][] { { 0.20, 0.80 } })); 50 | 51 | hmm.setTransitionProbability(0, 1, 0.05); 52 | hmm.setTransitionProbability(0, 0, 0.95); 53 | hmm.setTransitionProbability(1, 0, 0.10); 54 | hmm.setTransitionProbability(1, 1, 0.90); 55 | 56 | return hmm; 57 | } 58 | 59 | static HiddenMarkovModel buildDiscInitHMM() 60 | { 61 | HiddenMarkovModel hmm = new HiddenMarkovModel(2,2); 62 | 63 | hmm.setPi(0, 0.50); 64 | hmm.setPi(1, 0.50); 65 | 66 | hmm.setStateDistribution(0, new DiscreteDistribution(new double[][] { { 0.8, 0.2 } })); 67 | hmm.setStateDistribution(1, new DiscreteDistribution(new double[][] { { 0.1, 0.9 } })); 68 | 69 | hmm.setTransitionProbability(0, 1, 0.2); 70 | hmm.setTransitionProbability(0, 0, 0.8); 71 | hmm.setTransitionProbability(1, 0, 0.2); 72 | hmm.setTransitionProbability(1, 1, 0.8); 73 | 74 | return hmm; 75 | } 76 | 77 | public static void main(String[] args) { 78 | HiddenMarkovModel hmm = buildDiscHMM(); 79 | HiddenMarkovModel learntHmm = buildDiscInitHMM(); 80 | 81 | MarkovGenerator mg = new MarkovGenerator(hmm); 82 | MLSequenceSet training = mg.generateSequences(200,100); 83 | 84 | TrainBaumWelch bwl = new TrainBaumWelch(learntHmm,training); 85 | 86 | KullbackLeiblerDistanceCalculator klc = 87 | new KullbackLeiblerDistanceCalculator(); 88 | 89 | System.out.println("Training Discrete Hidden Markov Model with Baum Welch"); 90 | 91 | for(int i=1;i<=10;i++) { 92 | double e = klc.distance(learntHmm, hmm); 93 | System.out.println("Iteration #"+i+": Difference: " + e); 94 | bwl.iteration(); 95 | learntHmm = (HiddenMarkovModel)bwl.getMethod(); 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/main/java/org/encog/examples/ml/hmm/HMMSimpleKMeans.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Encog(tm) Java Examples v3.4 3 | * http://www.heatonresearch.com/encog/ 4 | * https://github.com/encog/encog-java-examples 5 | * 6 | * Copyright 2008-2017 Heaton Research, Inc. 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | * 20 | * For more information on Heaton Research copyrights, licenses 21 | * and trademarks visit: 22 | * http://www.heatonresearch.com/copyright 23 | */ 24 | package org.encog.examples.ml.hmm; 25 | 26 | import org.encog.ml.data.MLSequenceSet; 27 | import org.encog.ml.hmm.HiddenMarkovModel; 28 | import org.encog.ml.hmm.alog.KullbackLeiblerDistanceCalculator; 29 | import org.encog.ml.hmm.alog.MarkovGenerator; 30 | import org.encog.ml.hmm.distributions.DiscreteDistribution; 31 | import org.encog.ml.hmm.train.kmeans.TrainKMeans; 32 | 33 | /** 34 | * This is a simple example of KMeans training for a Hidden Markov Model (HMM). 35 | * First, a known HMM is created and output generated for it. Using this output 36 | * and KMeans we can generate a 2nd hiddem markov model that is very similar to the 37 | * first. 38 | */ 39 | public class HMMSimpleKMeans { 40 | 41 | static HiddenMarkovModel buildDiscHMM() 42 | { 43 | HiddenMarkovModel hmm = 44 | new HiddenMarkovModel(2, 2); 45 | 46 | hmm.setPi(0, 0.95); 47 | hmm.setPi(1, 0.05); 48 | 49 | hmm.setStateDistribution(0, new DiscreteDistribution(new double[][] { { 0.95, 0.05 } })); 50 | hmm.setStateDistribution(1, new DiscreteDistribution(new double[][] { { 0.20, 0.80 } })); 51 | 52 | hmm.setTransitionProbability(0, 1, 0.05); 53 | hmm.setTransitionProbability(0, 0, 0.95); 54 | hmm.setTransitionProbability(1, 0, 0.10); 55 | hmm.setTransitionProbability(1, 1, 0.90); 56 | 57 | return hmm; 58 | } 59 | 60 | 61 | public static void main(String[] args) { 62 | HiddenMarkovModel hmm = buildDiscHMM(); 63 | HiddenMarkovModel learntHmm = null; 64 | 65 | MarkovGenerator mg = new MarkovGenerator(hmm); 66 | MLSequenceSet training = mg.generateSequences(200,100); 67 | 68 | TrainKMeans trainer = new TrainKMeans(hmm,training); 69 | 70 | KullbackLeiblerDistanceCalculator klc = 71 | new KullbackLeiblerDistanceCalculator(); 72 | 73 | System.out.println("Training Hidden Markov Model with KMeans"); 74 | 75 | for(int i=1;i<=10;i++) { 76 | trainer.iteration(); 77 | learntHmm = (HiddenMarkovModel)trainer.getMethod(); 78 | double e = klc.distance(learntHmm, hmm); 79 | System.out.println("Iteration #"+i+": Difference: " + e); 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/org/encog/examples/ml/prg/Benchmark.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Encog(tm) Java Examples v3.4 3 | * http://www.heatonresearch.com/encog/ 4 | * https://github.com/encog/encog-java-examples 5 | * 6 | * Copyright 2008-2017 Heaton Research, Inc. 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | * 20 | * For more information on Heaton Research copyrights, licenses 21 | * and trademarks visit: 22 | * http://www.heatonresearch.com/copyright 23 | */ 24 | package org.encog.examples.ml.prg; 25 | 26 | import org.encog.ml.prg.EncogProgram; 27 | import org.encog.util.Stopwatch; 28 | 29 | public class Benchmark { 30 | public static final int MILLION = 1000000; 31 | public static final int COUNT = 10 * MILLION; 32 | 33 | public static void testDirect() { 34 | Stopwatch sw = new Stopwatch(); 35 | sw.start(); 36 | double d = 0; 37 | 38 | for(double a = 0;a speciesClasses = new ArrayList(); 47 | speciesClasses.add(new ClassItem("Iris-setosa",0)); 48 | speciesClasses.add(new ClassItem("Iris-versicolor",1)); 49 | speciesClasses.add(new ClassItem("Iris-virginica",2)); 50 | 51 | analyst.getScript().getProperties().setProperty(ScriptProperties.SETUP_CONFIG_INPUT_HEADERS, true); 52 | analyst.getScript().getProperties().setProperty(ScriptProperties.SETUP_CONFIG_CSV_FORMAT, AnalystFileFormat.DECPNT_COMMA); 53 | 54 | analyst.getScript().setDefaultNormalizedRange(0,1); 55 | analyst.getScript().defineField("sepal_l", FieldDirection.Input, NormalizationAction.Normalize, 0, 100); 56 | analyst.getScript().defineField("sepal_w", FieldDirection.Input, NormalizationAction.Normalize, 0, 100); 57 | analyst.getScript().defineField("petal_l", FieldDirection.Input, NormalizationAction.Normalize, 0, 100); 58 | analyst.getScript().defineField("petal_w", FieldDirection.Input, NormalizationAction.Normalize, 0, 100); 59 | analyst.getScript().defineClass("species", FieldDirection.Output, NormalizationAction.Equilateral, speciesClasses); 60 | 61 | MLDataSet training = analyst.getUtility().loadCSV("/Users/jheaton/iris.csv"); 62 | /*for(MLDataPair pair: training) { 63 | System.out.println(pair.toString()); 64 | }*/ 65 | System.out.println(training.size()); 66 | 67 | analyst.save("/Users/jheaton/test.ega"); 68 | 69 | ReadCSV csv = new ReadCSV("/Users/jheaton/iris.csv",true,CSVFormat.ENGLISH); 70 | MLData input = new BasicMLData(analyst.determineInputCount()); 71 | double[] rawInput = new double[analyst.determineInputFieldCount()]; 72 | 73 | while(csv.next()) { 74 | rawInput[0] = csv.getDouble(0); 75 | rawInput[1] = csv.getDouble(1); 76 | rawInput[2] = csv.getDouble(2); 77 | rawInput[3] = csv.getDouble(3); 78 | //analyst.getUtility().prepareInput(rawInput, input); 79 | System.out.println(input.toString()); 80 | } 81 | 82 | 83 | } catch(Exception ex) { 84 | ex.printStackTrace(); 85 | } 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/main/java/org/encog/examples/neural/art/art1/NeuralART1.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Encog(tm) Java Examples v3.4 3 | * http://www.heatonresearch.com/encog/ 4 | * https://github.com/encog/encog-java-examples 5 | * 6 | * Copyright 2008-2017 Heaton Research, Inc. 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | * 20 | * For more information on Heaton Research copyrights, licenses 21 | * and trademarks visit: 22 | * http://www.heatonresearch.com/copyright 23 | */ 24 | package org.encog.examples.neural.art.art1; 25 | 26 | import org.encog.Encog; 27 | import org.encog.ml.data.specific.BiPolarNeuralData; 28 | import org.encog.neural.art.ART1; 29 | 30 | /** 31 | * This example presents a series of 5-value images to an ART1 network. 32 | * ART1 learns new patterns as it goes, and classifies them into groups. 33 | * 34 | * This is based on a an example by Karsten Kutza, 35 | * written in C on 1996-01-24. 36 | * http://www.neural-networks-at-your-fingertips.com 37 | */ 38 | public class NeuralART1 { 39 | 40 | public static final int INPUT_NEURONS = 5; 41 | public static final int OUTPUT_NEURONS = 10; 42 | 43 | public static final String[] PATTERN = { 44 | " O ", 45 | " O O", 46 | " O", 47 | " O O", 48 | " O", 49 | " O O", 50 | " O", 51 | " OO O", 52 | " OO ", 53 | " OO O", 54 | " OO ", 55 | "OOO ", 56 | "OO ", 57 | "O ", 58 | "OO ", 59 | "OOO ", 60 | "OOOO ", 61 | "OOOOO", 62 | "O ", 63 | " O ", 64 | " O ", 65 | " O ", 66 | " O", 67 | " O O", 68 | " OO O", 69 | " OO ", 70 | "OOO ", 71 | "OO ", 72 | "OOOO ", 73 | "OOOOO" }; 74 | 75 | private boolean[][] input; 76 | 77 | public void setupInput() { 78 | this.input = new boolean[PATTERN.length][INPUT_NEURONS]; 79 | for (int n = 0; n < PATTERN.length; n++) { 80 | for (int i = 0; i < INPUT_NEURONS; i++) { 81 | this.input[n][i] = (PATTERN[n].charAt(i) == 'O'); 82 | } 83 | } 84 | } 85 | 86 | public void run() { 87 | this.setupInput(); 88 | ART1 logic = new ART1(INPUT_NEURONS,OUTPUT_NEURONS); 89 | 90 | for (int i = 0; i < PATTERN.length; i++) { 91 | BiPolarNeuralData in = new BiPolarNeuralData(this.input[i]); 92 | BiPolarNeuralData out = new BiPolarNeuralData(OUTPUT_NEURONS); 93 | logic.compute(in, out); 94 | if (logic.hasWinner()) { 95 | System.out.println(PATTERN[i] + " - " + logic.getWinner()); 96 | } else { 97 | System.out.println(PATTERN[i] 98 | + " - new Input and all Classes exhausted"); 99 | } 100 | } 101 | } 102 | 103 | public static void main(String[] args) { 104 | NeuralART1 art = new NeuralART1(); 105 | art.run(); 106 | Encog.getInstance().shutdown(); 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/main/java/org/encog/examples/neural/benchmark/Benchmark.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Encog(tm) Java Examples v3.4 3 | * http://www.heatonresearch.com/encog/ 4 | * https://github.com/encog/encog-java-examples 5 | * 6 | * Copyright 2008-2017 Heaton Research, Inc. 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | * 20 | * For more information on Heaton Research copyrights, licenses 21 | * and trademarks visit: 22 | * http://www.heatonresearch.com/copyright 23 | */ 24 | package org.encog.examples.neural.benchmark; 25 | 26 | import org.encog.ConsoleStatusReportable; 27 | import org.encog.Encog; 28 | import org.encog.util.benchmark.EncogBenchmark; 29 | 30 | /** 31 | * Simple console app that uses the Encog benchmarking class. 32 | * This will print out a number that shows how fast your computer is 33 | * with Encog. The lower the better. 34 | * @author jeff 35 | * 36 | */ 37 | public class Benchmark { 38 | 39 | public static void main(final String args[]) { 40 | 41 | final Benchmark b = new Benchmark(); 42 | System.out.println("Benchmark result: " + b.run()); 43 | 44 | Encog.getInstance().shutdown(); 45 | } 46 | 47 | public String run() { 48 | final EncogBenchmark mark = new EncogBenchmark(new ConsoleStatusReportable()); 49 | String result = mark.process(); 50 | 51 | return result; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/encog/examples/neural/benchmark/BinaryVsMemory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Encog(tm) Java Examples v3.4 3 | * http://www.heatonresearch.com/encog/ 4 | * https://github.com/encog/encog-java-examples 5 | * 6 | * Copyright 2008-2017 Heaton Research, Inc. 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | * 20 | * For more information on Heaton Research copyrights, licenses 21 | * and trademarks visit: 22 | * http://www.heatonresearch.com/copyright 23 | */ 24 | package org.encog.examples.neural.benchmark; 25 | 26 | import java.io.File; 27 | 28 | import org.encog.Encog; 29 | import org.encog.ml.data.MLDataPair; 30 | import org.encog.ml.data.basic.BasicMLDataPair; 31 | import org.encog.ml.data.basic.BasicMLDataSet; 32 | import org.encog.ml.data.buffer.BufferedMLDataSet; 33 | import org.encog.util.Format; 34 | import org.encog.util.benchmark.Evaluate; 35 | import org.encog.util.benchmark.RandomTrainingFactory; 36 | 37 | public class BinaryVsMemory { 38 | private static int evalMemory() 39 | { 40 | final BasicMLDataSet training = RandomTrainingFactory.generate(1000, 41 | 10000, 10, 10, -1, 1); 42 | 43 | final long start = System.currentTimeMillis(); 44 | final long stop = start + (10*Evaluate.MILIS); 45 | int record = 0; 46 | 47 | MLDataPair pair = BasicMLDataPair.createPair(10, 10); 48 | 49 | int iterations = 0; 50 | while( System.currentTimeMillis()=training.getRecordCount() ) 54 | record = 0; 55 | } 56 | 57 | System.out.println("In 10 seconds, the memory dataset read " + 58 | Format.formatInteger( iterations) + " records."); 59 | 60 | return iterations; 61 | } 62 | 63 | private static int evalBinary() 64 | { 65 | File file = new File("temp.egb"); 66 | 67 | final BasicMLDataSet training = RandomTrainingFactory.generate(1000, 68 | 10000, 10, 10, -1, 1); 69 | 70 | // create the binary file 71 | 72 | file.delete(); 73 | BufferedMLDataSet training2 = new BufferedMLDataSet(file); 74 | training2.load(training); 75 | 76 | final long start = System.currentTimeMillis(); 77 | final long stop = start + (10*Evaluate.MILIS); 78 | int record = 0; 79 | 80 | MLDataPair pair = BasicMLDataPair.createPair(10, 10); 81 | 82 | int iterations = 0; 83 | while( System.currentTimeMillis()=training2.getRecordCount() ) 87 | record = 0; 88 | } 89 | 90 | System.out.println("In 10 seconds, the disk(binary) dataset read " + 91 | Format.formatInteger( iterations) + " records."); 92 | file.delete(); 93 | return iterations; 94 | } 95 | 96 | public static void main(String[] args) 97 | { 98 | int memory = evalMemory(); 99 | int binary = evalBinary(); 100 | System.out.println( "Memory is " + Format.formatInteger(memory/binary) + " times the speed of disk."); 101 | Encog.getInstance().shutdown(); 102 | } 103 | 104 | } 105 | -------------------------------------------------------------------------------- /src/main/java/org/encog/examples/neural/benchmark/ErrorMethod.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Encog(tm) Java Examples v3.4 3 | * http://www.heatonresearch.com/encog/ 4 | * https://github.com/encog/encog-java-examples 5 | * 6 | * Copyright 2008-2017 Heaton Research, Inc. 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | * 20 | * For more information on Heaton Research copyrights, licenses 21 | * and trademarks visit: 22 | * http://www.heatonresearch.com/copyright 23 | */ 24 | package org.encog.examples.neural.benchmark; 25 | 26 | import java.util.Arrays; 27 | 28 | import org.encog.mathutil.error.ErrorCalculation; 29 | import org.encog.mathutil.error.ErrorCalculationMode; 30 | import org.encog.mathutil.randomize.RangeRandomizer; 31 | import org.encog.util.Format; 32 | 33 | public class ErrorMethod { 34 | 35 | public static double[] generate(int size, double low, double high) { 36 | double[] result = new double[size]; 37 | for(int i=0;i