├── .gitignore ├── 88x31.png ├── CNAME ├── LICENSE ├── README.md ├── _config.yml ├── banner_livros2.png ├── benchmarks.png ├── binary-step.png ├── bwimage ├── .classpath ├── .project ├── .settings │ ├── org.eclipse.core.resources.prefs │ ├── org.eclipse.jdt.core.prefs │ └── org.eclipse.m2e.core.prefs ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── neuraljava │ └── samples │ └── bwimage │ ├── BwImageShow.java │ └── RGBImageShow.java ├── cnn_java ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.core.resources.prefs │ ├── org.eclipse.jdt.core.prefs │ └── org.eclipse.m2e.core.prefs ├── README.md ├── cleuton_001.jpg ├── cleuton_0010.jpg ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── neuraljava │ └── demos │ └── imageutil │ ├── FacesClassifier.java │ ├── LabelGen.java │ ├── PrepareFaces.java │ └── TrainModel.java ├── cuda ├── README.md └── matmul.cu ├── english ├── README.md ├── cnn_java │ └── README.md └── gpu_cpu │ └── README.md ├── formula-sigmoid.png ├── forward.png ├── gpu_cpu ├── README.md ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── neuraljava │ │ └── demos │ │ └── gpu │ │ └── MatMul.java │ └── resources │ └── log4j.properties ├── gradient-descent.png ├── icone.png ├── iris.png ├── iris ├── README.md ├── planilha.png ├── pom.xml ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── neuraljava │ │ │ └── samples │ │ │ ├── classificationIris │ │ │ ├── ClassificationIris.java │ │ │ ├── ClassificationIrisSave.java │ │ │ ├── ClassificationIrisScaled.java │ │ │ └── IrisLoader.java │ │ │ └── mlpgen │ │ │ └── api │ │ │ ├── Activation.java │ │ │ ├── IrisClassifier.java │ │ │ ├── Layer.java │ │ │ ├── Model.java │ │ │ ├── Node.java │ │ │ ├── ReLU.java │ │ │ ├── Sigmoid.java │ │ │ └── Sinapse.java │ │ └── resources │ │ └── iris.data └── teste.ods ├── linearmenteseparavel.png ├── mlp.png ├── multilayerperceptron ├── pom.xml ├── src │ └── main │ │ └── java │ │ └── com │ │ └── neuraljava │ │ └── samples │ │ └── mlp │ │ ├── MLP.java │ │ └── TestMLP.java └── treino_teste_sucesso.txt ├── nao-linearmente-separavel.png ├── neuronio.png ├── non-linear.gif ├── non-linear.png ├── percep.png ├── perceptron ├── planilha_perceptron.ods ├── planilha_perceptron.xlsx ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── datalearninghub │ │ └── samples │ │ └── perceptron │ │ ├── Classifier.java │ │ └── Perceptron.java │ └── resources │ ├── dados.csv │ └── dados_teste.csv ├── playground.png ├── python_java.png ├── python_java_keras ├── .ipynb_checkpoints │ └── python_keras-checkpoint.ipynb ├── README.md ├── conda_env.yml ├── dois.png ├── icone.png ├── java │ ├── .classpath │ ├── .gitignore │ ├── .project │ ├── .settings │ │ ├── org.eclipse.core.resources.prefs │ │ ├── org.eclipse.jdt.core.prefs │ │ └── org.eclipse.m2e.core.prefs │ ├── dois.png │ ├── pom.xml │ ├── resultado.png │ └── src │ │ └── main │ │ ├── java │ │ └── com │ │ │ └── neuraljava │ │ │ └── demos │ │ │ └── kerasmodel │ │ │ └── RunModel.java │ │ └── resources │ │ └── meu_mnist.h5 └── python_keras.ipynb ├── redeneural.png ├── regressionmpg ├── .classpath ├── .project ├── .settings │ ├── org.eclipse.core.resources.prefs │ ├── org.eclipse.jdt.core.prefs │ └── org.eclipse.m2e.core.prefs ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── neuraljava │ │ │ └── samples │ │ │ └── regressionmpg │ │ │ ├── RegressionMPG.java │ │ │ ├── RegressionMPG2L1L2.java │ │ │ └── RegressionMPGES.java │ └── resources │ │ └── auto-mpg.csv │ └── test │ └── java │ └── com │ └── neuraljava │ └── samples │ └── regressionmpg │ └── AppTest.java ├── regressionmpgCPU ├── .classpath ├── .project ├── .settings │ ├── org.eclipse.core.resources.prefs │ ├── org.eclipse.jdt.core.prefs │ └── org.eclipse.m2e.core.prefs ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── neuraljava │ │ │ └── samples │ │ │ └── regressionmpgcpu │ │ │ ├── RegressionMPG.java │ │ │ ├── RegressionMPG2L1L2.java │ │ │ └── RegressionMPGES.java │ └── resources │ │ └── auto-mpg.csv │ └── test │ └── java │ └── com │ └── neuraljava │ └── samples │ └── regressionmpg │ └── AppTest.java ├── sigmoid.png └── weight.png /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | -------------------------------------------------------------------------------- /88x31.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cleuton/neuraljava/6e5c3f6da4fb43ecf13cc380b7e3bc8d4b371e3f/88x31.png -------------------------------------------------------------------------------- /CNAME: -------------------------------------------------------------------------------- 1 | neuraljava.com -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-hacker -------------------------------------------------------------------------------- /banner_livros2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cleuton/neuraljava/6e5c3f6da4fb43ecf13cc380b7e3bc8d4b371e3f/banner_livros2.png -------------------------------------------------------------------------------- /benchmarks.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cleuton/neuraljava/6e5c3f6da4fb43ecf13cc380b7e3bc8d4b371e3f/benchmarks.png -------------------------------------------------------------------------------- /binary-step.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cleuton/neuraljava/6e5c3f6da4fb43ecf13cc380b7e3bc8d4b371e3f/binary-step.png -------------------------------------------------------------------------------- /bwimage/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /bwimage/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | bwimage 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /bwimage/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java=UTF-8 3 | encoding//src/test/java=UTF-8 4 | encoding/=UTF-8 5 | -------------------------------------------------------------------------------- /bwimage/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 3 | org.eclipse.jdt.core.compiler.compliance=1.5 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.release=disabled 6 | org.eclipse.jdt.core.compiler.source=1.5 7 | -------------------------------------------------------------------------------- /bwimage/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /bwimage/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.neuraljava.samples 6 | bwimage 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | bwimage 11 | http://maven.apache.org 12 | 13 | 14 | UTF-8 15 | 16 | 17 | 18 | 19 | junit 20 | junit 21 | 3.8.1 22 | test 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /bwimage/src/main/java/com/neuraljava/samples/bwimage/BwImageShow.java: -------------------------------------------------------------------------------- 1 | package com.neuraljava.samples.bwimage; 2 | 3 | import java.awt.Graphics; 4 | import java.awt.Graphics2D; 5 | import java.awt.image.BufferedImage; 6 | import java.io.File; 7 | import java.io.IOException; 8 | import java.util.Arrays; 9 | 10 | import javax.imageio.ImageIO; 11 | import javax.swing.ImageIcon; 12 | import javax.swing.JFrame; 13 | import javax.swing.JLabel; 14 | import javax.swing.JOptionPane; 15 | import javax.swing.JPanel; 16 | 17 | public class BwImageShow { 18 | 19 | /** 20 | * Mostra a composição de uma imagem monocromática 21 | * @param args String - Path da imagem 22 | * @throws IOException 23 | */ 24 | public static void main(String [] args) throws IOException { 25 | BufferedImage image = ImageIO.read(new File(args[0])); 26 | drawImage(image); 27 | int[][] pixels = new int[image.getWidth()][]; 28 | 29 | for (int x = 0; x < image.getWidth(); x++) { 30 | pixels[x] = new int[image.getHeight()]; 31 | 32 | for (int y = 0; y < image.getHeight(); y++) { 33 | pixels[x][y] = (int)(image.getRGB(y, x) & 0xFF); 34 | } 35 | System.out.println(Arrays.toString(pixels[x])); 36 | } 37 | } 38 | 39 | 40 | private static void drawImage(BufferedImage image) { 41 | JLabel picLabel = new JLabel(new ImageIcon(image)); 42 | JOptionPane.showMessageDialog(null, picLabel, "Imagem", JOptionPane.PLAIN_MESSAGE, null); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /bwimage/src/main/java/com/neuraljava/samples/bwimage/RGBImageShow.java: -------------------------------------------------------------------------------- 1 | package com.neuraljava.samples.bwimage; 2 | 3 | import java.awt.Color; 4 | import java.awt.Graphics; 5 | import java.awt.Graphics2D; 6 | import java.awt.image.BufferedImage; 7 | import java.io.File; 8 | import java.io.IOException; 9 | import java.util.Arrays; 10 | 11 | import javax.imageio.ImageIO; 12 | import javax.swing.ImageIcon; 13 | import javax.swing.JFrame; 14 | import javax.swing.JLabel; 15 | import javax.swing.JOptionPane; 16 | import javax.swing.JPanel; 17 | 18 | public class RGBImageShow { 19 | 20 | /** 21 | * Mostra a composição de uma imagem monocromática 22 | * @param args String - Path da imagem 23 | * @throws IOException 24 | */ 25 | public static void main(String [] args) throws IOException { 26 | BufferedImage image = ImageIO.read(new File(args[0])); 27 | drawImage(image); 28 | Color[][] pixels = new Color[image.getWidth()][]; 29 | 30 | for (int x = 0; x < image.getWidth(); x++) { 31 | pixels[x] = new Color[image.getHeight()]; 32 | 33 | for (int y = 0; y < image.getHeight(); y++) { 34 | pixels[x][y] = new Color(image.getRGB(y,x)); 35 | } 36 | } 37 | for (int x=0; x < pixels.length; x++) { 38 | for (int y = 0; y < pixels[x].length; y++) { 39 | System.out.print( 40 | "[" 41 | + String.format("%3d", pixels[x][y].getRed()) 42 | + "-" 43 | + String.format("%3d", pixels[x][y].getGreen()) 44 | + "-" 45 | + String.format("%3d", pixels[x][y].getBlue()) 46 | + "]" 47 | ); 48 | } 49 | System.out.println(""); 50 | } 51 | } 52 | 53 | 54 | private static void drawImage(BufferedImage image) { 55 | JLabel picLabel = new JLabel(new ImageIcon(image)); 56 | JOptionPane.showMessageDialog(null, picLabel, "Imagem", JOptionPane.PLAIN_MESSAGE, null); 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /cnn_java/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /cnn_java/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /cnn_java/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | imageutil 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /cnn_java/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java=UTF-8 3 | encoding//src/test/java=UTF-8 4 | encoding/=UTF-8 5 | -------------------------------------------------------------------------------- /cnn_java/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 3 | org.eclipse.jdt.core.compiler.compliance=1.8 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.release=disabled 6 | org.eclipse.jdt.core.compiler.source=1.8 7 | -------------------------------------------------------------------------------- /cnn_java/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /cnn_java/cleuton_001.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cleuton/neuraljava/6e5c3f6da4fb43ecf13cc380b7e3bc8d4b371e3f/cnn_java/cleuton_001.jpg -------------------------------------------------------------------------------- /cnn_java/cleuton_0010.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cleuton/neuraljava/6e5c3f6da4fb43ecf13cc380b7e3bc8d4b371e3f/cnn_java/cleuton_0010.jpg -------------------------------------------------------------------------------- /cnn_java/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.neuraljava.demos 6 | imageutil 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | imageutil 11 | http://maven.apache.org 12 | 13 | 14 | UTF-8 15 | 1.8 16 | 1.8 17 | 18 | 19 | 20 | 21 | junit 22 | junit 23 | 4.12 24 | test 25 | 26 | 27 | image-processing 28 | org.openimaj 29 | 1.3.8 30 | compile 31 | 32 | 33 | faces 34 | org.openimaj 35 | 1.3.8 36 | compile 37 | 38 | 39 | org.deeplearning4j 40 | deeplearning4j-core 41 | 1.0.0-beta4 42 | 43 | 44 | org.deeplearning4j 45 | deeplearning4j-modelimport 46 | 1.0.0-beta4 47 | 48 | 49 | org.nd4j 50 | nd4j-cuda-10.1 51 | 1.0.0-beta4 52 | 53 | 54 | org.deeplearning4j 55 | deeplearning4j-cuda-10.0 56 | 1.0.0-beta4 57 | 58 | 59 | org.deeplearning4j 60 | deeplearning4j-ui_2.10 61 | 1.0.0-beta4 62 | 63 | 64 | 65 | 66 | 67 | org.apache.maven.plugins 68 | maven-assembly-plugin 69 | 3.1.1 70 | 71 | 72 | jar-with-dependencies 73 | 74 | 75 | 76 | com.neuraljava.demos.imageutil.FacesClassifier 77 | 78 | 79 | 80 | 81 | 82 | make-assembly 83 | package 84 | 85 | single 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | -------------------------------------------------------------------------------- /cnn_java/src/main/java/com/neuraljava/demos/imageutil/FacesClassifier.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 Cleuton Sampaio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, 11 | software distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and limitations under the License. 14 | 15 | There is also the complete Apache License v2 available in HTML and TXT formats. 16 | */ 17 | package com.neuraljava.demos.imageutil; 18 | 19 | import java.awt.image.BufferedImage; 20 | import java.io.File; 21 | import java.io.IOException; 22 | import java.util.List; 23 | import java.util.Set; 24 | 25 | import javax.imageio.ImageIO; 26 | 27 | import org.openimaj.image.DisplayUtilities; 28 | import org.openimaj.image.FImage; 29 | import org.openimaj.image.ImageUtilities; 30 | import org.openimaj.image.processing.face.alignment.RotateScaleAligner; 31 | import org.openimaj.image.processing.face.detection.keypoints.FKEFaceDetector; 32 | import org.openimaj.image.processing.face.detection.keypoints.KEDetectedFace; 33 | 34 | public class FacesClassifier { 35 | 36 | public static void main (String [] args) throws IOException { 37 | /* 38 | * args 0: raw images path 39 | * args 1: processed images path 40 | * args 2: model save path 41 | */ 42 | Set classes = PrepareFaces.prepareImages(args[0], args[1]); 43 | System.out.println("Classes: " + classes.size()); 44 | TrainModel.train(args[1], args[2], classes.size()); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /cnn_java/src/main/java/com/neuraljava/demos/imageutil/LabelGen.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 Cleuton Sampaio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, 11 | software distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and limitations under the License. 14 | 15 | There is also the complete Apache License v2 available in HTML and TXT formats. 16 | */ 17 | package com.neuraljava.demos.imageutil; 18 | 19 | import java.io.File; 20 | import java.net.URI; 21 | 22 | import org.datavec.api.io.labels.PathLabelGenerator; 23 | import org.datavec.api.writable.Text; 24 | import org.datavec.api.writable.Writable; 25 | 26 | public class LabelGen implements PathLabelGenerator { 27 | 28 | /** 29 | * 30 | */ 31 | private static final long serialVersionUID = 1L; 32 | 33 | public Writable getLabelForPath(String path) { 34 | File file = new File(path); 35 | int dot = file.getName().lastIndexOf("_"); 36 | return new Text(file.getName().substring(0, dot)); 37 | } 38 | 39 | public Writable getLabelForPath(URI uri) { 40 | return getLabelForPath(new File(uri).toString()); 41 | } 42 | 43 | public boolean inferLabelClasses() { 44 | return true; 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /cnn_java/src/main/java/com/neuraljava/demos/imageutil/PrepareFaces.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 Cleuton Sampaio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, 11 | software distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and limitations under the License. 14 | 15 | There is also the complete Apache License v2 available in HTML and TXT formats. 16 | */ 17 | package com.neuraljava.demos.imageutil; 18 | 19 | import java.awt.image.BufferedImage; 20 | import java.io.File; 21 | import java.io.IOException; 22 | import java.util.ArrayList; 23 | import java.util.HashSet; 24 | import java.util.List; 25 | import java.util.Set; 26 | 27 | import javax.imageio.ImageIO; 28 | 29 | import org.datavec.api.io.labels.ParentPathLabelGenerator; 30 | import org.datavec.api.io.labels.PathLabelGenerator; 31 | import org.openimaj.image.FImage; 32 | import org.openimaj.image.ImageUtilities; 33 | import org.openimaj.image.processing.face.alignment.RotateScaleAligner; 34 | import org.openimaj.image.processing.face.detection.keypoints.FKEFaceDetector; 35 | import org.openimaj.image.processing.face.detection.keypoints.KEDetectedFace; 36 | 37 | /* 38 | This example uses Labeled Faces in the Wild - LFW database: 39 | http://vis-www.cs.umass.edu/lfw/ 40 | 41 | Each image is preprocessed to align the faces, convert to B&W and crop to 80 x 80 pixels. 42 | This is done by OpenIMAJ package. 43 | */ 44 | public class PrepareFaces { 45 | public static List getFaces(String imagePath) throws IOException { 46 | List facesList = new ArrayList(); 47 | File arq = new File(imagePath); 48 | BufferedImage imagem = ImageIO.read(arq); 49 | FImage fimage = ImageUtilities.createFImage(imagem); 50 | FKEFaceDetector detector = new FKEFaceDetector(1); 51 | List faces = detector.detectFaces(fimage); 52 | RotateScaleAligner aligner = new RotateScaleAligner(); 53 | for (KEDetectedFace face : faces) { 54 | FImage aligned = aligner.align(face); 55 | BufferedImage bfi = ImageUtilities.createBufferedImage(aligned); 56 | facesList.add(bfi); 57 | } 58 | return facesList; 59 | } 60 | 61 | public static Set prepareImages(String sourcePath, String targetPath) throws IOException { 62 | Set names = new HashSet(); 63 | File[] files = new File(sourcePath).listFiles(); 64 | for (File file : files) { 65 | String name = getNameFromFile(file); 66 | names.add(name); 67 | List faces = getFaces(file.getAbsolutePath()); 68 | int faceCount = 0; 69 | for (BufferedImage img : faces) { 70 | int pos = file.getName().indexOf('.'); 71 | String fileName = file.getName().substring(0,pos) + faceCount + ".jpg"; 72 | File outputfile = new File(targetPath + "/" + fileName); 73 | ImageIO.write(img, "jpg", outputfile); 74 | faceCount++; 75 | } 76 | System.out.println("Name: " + name); 77 | } 78 | return names; 79 | } 80 | 81 | private static String getNameFromFile(File file) { 82 | int dot = file.getName().lastIndexOf("_"); 83 | return file.getName().substring(0, dot); 84 | } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /cnn_java/src/main/java/com/neuraljava/demos/imageutil/TrainModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 Cleuton Sampaio 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, 11 | software distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and limitations under the License. 14 | 15 | There is also the complete Apache License v2 available in HTML and TXT formats. 16 | */ 17 | package com.neuraljava.demos.imageutil; 18 | 19 | import java.io.File; 20 | import java.io.IOException; 21 | import java.util.List; 22 | import java.util.Random; 23 | import java.util.concurrent.TimeUnit; 24 | import java.util.logging.Logger; 25 | 26 | import org.datavec.api.io.filters.BalancedPathFilter; 27 | import org.datavec.api.split.FileSplit; 28 | import org.datavec.api.split.InputSplit; 29 | import org.datavec.image.loader.NativeImageLoader; 30 | import org.datavec.image.recordreader.ImageRecordReader; 31 | import org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator; 32 | import org.deeplearning4j.earlystopping.EarlyStoppingConfiguration; 33 | import org.deeplearning4j.earlystopping.EarlyStoppingResult; 34 | import org.deeplearning4j.earlystopping.saver.LocalFileModelSaver; 35 | import org.deeplearning4j.earlystopping.scorecalc.DataSetLossCalculator; 36 | import org.deeplearning4j.earlystopping.termination.MaxEpochsTerminationCondition; 37 | import org.deeplearning4j.earlystopping.termination.MaxTimeIterationTerminationCondition; 38 | import org.deeplearning4j.earlystopping.termination.ScoreImprovementEpochTerminationCondition; 39 | import org.deeplearning4j.earlystopping.trainer.EarlyStoppingTrainer; 40 | import org.deeplearning4j.nn.conf.MultiLayerConfiguration; 41 | import org.deeplearning4j.nn.conf.NeuralNetConfiguration; 42 | import org.deeplearning4j.nn.conf.inputs.InputType; 43 | import org.deeplearning4j.nn.conf.layers.ConvolutionLayer; 44 | import org.deeplearning4j.nn.conf.layers.DenseLayer; 45 | import org.deeplearning4j.nn.conf.layers.OutputLayer; 46 | import org.deeplearning4j.nn.conf.layers.PoolingType; 47 | import org.deeplearning4j.nn.conf.layers.SubsamplingLayer; 48 | import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; 49 | import org.deeplearning4j.nn.weights.WeightInit; 50 | import org.nd4j.linalg.activations.Activation; 51 | import org.nd4j.linalg.dataset.DataSet; 52 | import org.nd4j.linalg.dataset.SplitTestAndTrain; 53 | import org.nd4j.linalg.dataset.api.iterator.DataSetIterator; 54 | import org.nd4j.linalg.dataset.api.preprocessor.DataNormalization; 55 | import org.nd4j.linalg.dataset.api.preprocessor.ImagePreProcessingScaler; 56 | import org.nd4j.linalg.learning.config.Adam; 57 | import org.nd4j.linalg.lossfunctions.LossFunctions; 58 | 59 | public class TrainModel { 60 | private static final Logger log = Logger.getLogger(TrainModel.class.getName()); 61 | public static void train(String imagePath, String modelPath, int numLabels) throws IOException { 62 | 63 | // Random 64 | int seed = 42; 65 | Random random = new Random(seed); 66 | 67 | // Hiperparametros 68 | 69 | int height = 80; 70 | int width = 80; 71 | int channels = 1; 72 | int batchSize = 10; 73 | double trainTestRatio = 0.6; 74 | int iterations = 1; 75 | int epochs = 500; 76 | double learningRate = 0.01; 77 | 78 | // Preparar dataset com o nosso label generator: 79 | 80 | LabelGen labelMaker = new LabelGen(); 81 | File mainPath = new File(imagePath); 82 | FileSplit fileSplit = new FileSplit(mainPath, NativeImageLoader.ALLOWED_FORMATS, random); 83 | int numExamples = Math.toIntExact(fileSplit.length()); 84 | BalancedPathFilter pathFilter = new BalancedPathFilter(random, labelMaker, numExamples, numLabels, 0); 85 | 86 | // Separação treino e teste: 87 | 88 | InputSplit[] inputSplit = fileSplit.sample(pathFilter, trainTestRatio, 1 - trainTestRatio); 89 | InputSplit trainData = inputSplit[0]; 90 | InputSplit testData = inputSplit[1]; 91 | 92 | // Configuração da rede: 93 | 94 | MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder() 95 | .seed(seed) 96 | .l2(0.0005) 97 | .weightInit(WeightInit.XAVIER) 98 | .updater(new Adam(1e-3)) 99 | .list() 100 | .layer(new ConvolutionLayer.Builder(5, 5) 101 | .nIn(channels) 102 | .stride(1,1) 103 | .nOut(32) 104 | .activation(Activation.IDENTITY) 105 | .build()) 106 | .layer(new SubsamplingLayer.Builder(PoolingType.MAX) 107 | .kernelSize(2,2) 108 | .stride(2,2) 109 | .build()) 110 | .layer(new ConvolutionLayer.Builder(5, 5) 111 | .stride(1,1) 112 | .nOut(50) 113 | .activation(Activation.IDENTITY) 114 | .build()) 115 | .layer(new SubsamplingLayer.Builder(PoolingType.MAX) 116 | .kernelSize(2,2) 117 | .stride(2,2) 118 | .build()) 119 | .layer(new DenseLayer.Builder().activation(Activation.RELU) 120 | .nOut(500).build()) 121 | .layer(new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD) 122 | .nOut(numLabels) 123 | .activation(Activation.SOFTMAX) 124 | .build()) 125 | .setInputType(InputType.convolutionalFlat(height,width,1)) 126 | .build(); 127 | 128 | // Carga dos dados: 129 | 130 | DataNormalization scaler = new ImagePreProcessingScaler(0, 1); 131 | 132 | // Iterador de treino: 133 | 134 | ImageRecordReader trainRR = new ImageRecordReader(height, width, channels, labelMaker); 135 | DataSetIterator trainIter; 136 | trainRR.initialize(trainData, null); 137 | trainIter = new RecordReaderDataSetIterator(trainRR, batchSize, 1, numLabels); 138 | scaler.fit(trainIter); 139 | trainIter.setPreProcessor(scaler); 140 | 141 | // Iterador de teste: 142 | 143 | ImageRecordReader testRR = new ImageRecordReader(height, width, channels, labelMaker); 144 | testRR.initialize(testData); 145 | DataSetIterator testIter = new RecordReaderDataSetIterator(testRR, batchSize, 1, numLabels); 146 | scaler.fit(testIter); 147 | testIter.setPreProcessor(scaler); 148 | 149 | // Configuração de Early Stopping: 150 | 151 | int maxEpochsWithNoImprovement = 10; 152 | EarlyStoppingConfiguration esConf = new EarlyStoppingConfiguration.Builder() 153 | .epochTerminationConditions(new MaxEpochsTerminationCondition(epochs),new ScoreImprovementEpochTerminationCondition(maxEpochsWithNoImprovement) ) 154 | .iterationTerminationConditions(new MaxTimeIterationTerminationCondition(20, TimeUnit.MINUTES)) 155 | .scoreCalculator(new DataSetLossCalculator(testIter, true)) 156 | .evaluateEveryNEpochs(1) 157 | .modelSaver(new LocalFileModelSaver(modelPath)) 158 | .build(); 159 | 160 | EarlyStoppingTrainer trainer = new EarlyStoppingTrainer(esConf,conf,trainIter); 161 | 162 | // Treinamento da rede: 163 | 164 | EarlyStoppingResult result = trainer.fit(); 165 | System.out.println("Termination reason: " + result.getTerminationReason()); 166 | System.out.println("Termination details: " + result.getTerminationDetails()); 167 | System.out.println("Total epochs: " + result.getTotalEpochs()); 168 | System.out.println("Best epoch number: " + result.getBestModelEpoch()); 169 | System.out.println("Score at best epoch: " + result.getBestModelScore()); 170 | 171 | // Recupera o modelo com melhor desempenho: 172 | 173 | MultiLayerNetwork net = (MultiLayerNetwork) result.getBestModel(); 174 | 175 | // Predições: 176 | 177 | trainIter.reset(); 178 | DataSet testDataSet = trainIter.next(); 179 | List allClassLabels = trainRR.getLabels(); 180 | int labelIndex = testDataSet.getLabels().argMax(1).getInt(0); 181 | int[] predictedClasses = net.predict(testDataSet.getFeatures()); 182 | String expectedResult = allClassLabels.get(labelIndex); 183 | String modelPrediction = allClassLabels.get(predictedClasses[0]); 184 | System.out.print("\nNome da pessoa: " + expectedResult + " predição: " + modelPrediction + "\n\n"); 185 | 186 | 187 | } 188 | 189 | } 190 | -------------------------------------------------------------------------------- /cuda/README.md: -------------------------------------------------------------------------------- 1 | ![](../icone.png) 2 | # Redes neurais e Deep Learning utilizando Java 3 | [**Cleuton Sampaio**](https://github.com/cleuton) - [**LinkedIn**](https://www.linkedin.com/in/cleutonsampaio/) 4 | 5 | ![](../88x31.png) 6 | 7 | [**English version**](../english/gpu_cpu) 8 | 9 | ## Programação CUDA 10 | 11 | Sei que é sobre **Java**, mas, para entender como funciona a programação **CUDA** para GPUs nVidia, é preciso mergulhar primeiro no C++. Vou mostrar um pequeno exemplo de programação paralela, para você entender as diferenças entre a programação sequencial da CPU e a paralela da GPU. 12 | 13 | Na verdade, este exemplo é muito utilizado em redes neurais e **deep learning**: Produto de matrizes! 14 | 15 | ## Produto de matrizes 16 | 17 | Em álgebra linear, o produto de duas matrizes, **A** e **B**, é obtido pela soma dos produtos de cada linha de **A** e cada coluna de **B**. Exemplo, o produto das matrizes: 18 | 19 | **A**: 20 | 21 | ``` 22 | -1 2 4 23 | 0 5 3 24 | 6 2 1 25 | ``` 26 | **B**: 27 | 28 | ``` 29 | 3 0 2 30 | 3 4 5 31 | 4 7 2 32 | ``` 33 | Resultará na seguinte matriz: 34 | 35 | ``` 36 | 19 36 16 37 | 27 41 31 38 | 28 15 24 39 | ``` 40 | 41 | Como? Vejamos... Cada elemento de cada linha de **A** deve ser multiplicado por cada elemento de cada coluna de **B** e o produto somado será o elemento da nova linha da matriz resultante. Vejamos a multiplicação da primeira linha: 42 | 43 | ``` 44 | A(1,1) * B(1,1) + A(1,2) * B(2,1) + A(1,3) * B(3,1) = 19 45 | A(1,1) * B(1,2) + A(1,2) * B(2,2) + A(1,3) * B(3,2) = 36 46 | A(1,1) * B(1,3) + A(1,2) * B(2,3) + A(1,3) * B(3,3) = 16 47 | ``` 48 | 49 | O resultado terá a forma: Linhas de **A** e colunas de **B**. Para que seja possível o produto, o número de colunas de **A** deve ser igual ao número de linhas de **B**. 50 | 51 | ## Programação GPU 52 | 53 | Já mostramos programação de **GPU** no exemplo [**"GPU ou CPU?"**](../gpu_cpu), portanto, só vou falar aqui um pouco sobre como o **kernel** é executado pela GPU. 54 | 55 | A GPU é composta de **cores** ou núcleos paralelos, cada um executando um **thread**. Os **threads** são organizados em **blocks** (blocos) e estes em **grids** (grade). Quando disparamos um **kernel** os vários **threads** o executarão em paralelo, portanto, não há como garantir a ordem de execução. 56 | 57 | Para multiplicar as matrizes **A** e **B** deste exemplo, podemos trabalhar com 9 threads, cada um processando a multiplicação de uma linha de **A** por uma coluna de **B**. Neste caso podemos pensar em cada **block** contendo 9 **threads** e cada **grid** contendo um **block**. Isso fica mais fácil de ver no exemplo [**matmul.cu**](./matmul.cu): 58 | 59 | ``` 60 | // Blocks & grids: 61 | dim3 blocks(size,size); 62 | dim3 grid(1,1); 63 | 64 | // Call the kernel: 65 | matmul<<>>(gpu_A,gpu_B,gpu_C,size); 66 | ``` 67 | O **size** das minhas matrizes é 3, pois ambas são **3 x 3**. Neste trecho de código eu determinei a quantidade e a distribuição dos **threads** e **blocks** que eu usarei e estou invocando o meu **kernel**. 68 | 69 | É confuso, eu sei, mas há um [**tutorial muito bom da nVidia**](https://www.tutorialspoint.com/cuda/cuda_quick_guide.htm) sobre o assunto. 70 | 71 | As etapas da programação básica **CUDA** são estas: 72 | 73 | 1. Declare o seu **kernel**; 74 | - *A função marcada com **__global__** que será executada pela GPU;* 75 | 2. Crie suas variáveis **CPU**; 76 | - *Declare e aloque espaço para seus vetores na memória da CPU;* 77 | 3. Crie suas variáveis **GPU** e copie os valores; 78 | - *Declare e aloque espaço na **GPU** para seus vetores, copiando os valores para lá;* 79 | 4. Determine o formato do **grid** e dos **blocks** de **threads**; 80 | - *Calcule quantos **threads** serão necessários e arrume a distribuição em **blocks**;* 81 | 5. Invoque o **kernel**; 82 | - *Chame sua função **kernel** passando as variáveis GPU;* 83 | 6. Copie o resultado de volta para a **CPU**; 84 | - *Copie os dados do vetor calculado na **GPU** para a **CPU**;* 85 | 7. Libere a memória da **GPU**; 86 | - *A memória da **GPU** é limitada, portanto, precisamos liberá-la.* 87 | 88 | Veja o [**código-fonte**](./matmul.cu) e identifique todas essas etapas. 89 | 90 | ## Kernel 91 | 92 | A multiplicação de matrizes na GPU é um processo diferente. Para começar, nós "desnormalizamos" as matrizes, criando vetores. Uma forma de fazer isso é **row first**. Por exemplo, pegamos a primeira matriz e colocamos cada linha depois da outra, "achatando" a estrutura: 93 | 94 | ``` 95 | float cpu_A[] = {-1,2,4,0,5,3,6,2,1}; 96 | ``` 97 | 98 | Depois, quando invocarmos o **kernel** precisaremos usar um *truque* para saber qual é a linha e qual é a coluna que queremos trabalhar: 99 | 100 | ``` 101 | // Row and Column indexes: 102 | int row = blockIdx.y*blockDim.y+threadIdx.y; 103 | int col = blockIdx.x*blockDim.x+threadIdx.x; 104 | 105 | ``` 106 | Usamos os objetos da GPU **blockIdx**, **blockDim** e **threadIdx** para calcular a linha e a coluna da nossa matriz "achatada". Os dois objetos possuem valores **x** e **y**, correspondendo ao modelo de **grid** e **blocks** que passamos ao invocar o **kernel**. 107 | 108 | Cada **thread** calculará um valor da matriz de resultado, portanto, precisa multiplicar uma linha por uma coluna: 109 | 110 | ``` 111 | // Are they bellow the maximum? 112 | if (col < size && row < size) { 113 | float result = 0; 114 | for(int ix=0;ix 24 | #include 25 | 26 | // CUDA Kernel function: 27 | 28 | __global__ void matmul(float *A, float* B, float *C, int size) 29 | { 30 | 31 | // Row and Column indexes: 32 | int row = blockIdx.y*blockDim.y+threadIdx.y; 33 | int col = blockIdx.x*blockDim.x+threadIdx.x; 34 | 35 | // Are they bellow the maximum? 36 | if (col < size && row < size) { 37 | float result = 0; 38 | for(int ix=0;ix>>(gpu_A,gpu_B,gpu_C,size); 72 | 73 | // Get the result Matrix: 74 | cudaMemcpy(cpu_C,gpu_C,msize,cudaMemcpyDeviceToHost); 75 | std::cout << cpu_C[0] << '\t' << cpu_C[1] << '\t' << cpu_C[2] << std::endl 76 | << cpu_C[3] << '\t' << cpu_C[4] << '\t' << cpu_C[5] << std::endl 77 | << cpu_C[6] << '\t' << cpu_C[7] << '\t' << cpu_C[8] << std::endl; 78 | 79 | //Free device matrices 80 | cudaFree(gpu_A); 81 | cudaFree(gpu_B); 82 | cudaFree(gpu_C); 83 | 84 | } 85 | -------------------------------------------------------------------------------- /english/README.md: -------------------------------------------------------------------------------- 1 | ![](../icone.png) 2 | 3 | # Neural network and Deep Learning with Java 4 | [**Cleuton Sampaio**](https://github.com/cleuton) - [**LinkedIn**](https://www.linkedin.com/in/cleutonsampaio/) 5 | 6 | ![](../88x31.png) 7 | 8 | 9 | All content, if not explicitly stated, is released under [Creative Commons Attribution 4.0 International] (http://creativecommons.org/licenses/by/4.0/). The source code is released under [Apache 2.0 license] (https://www.apache.org/licenses/LICENSE-2.0). 10 | 11 | 12 | [**VERSÃO EM PORTUGUÊS**](./) 13 | 14 | *I am translating all the content to english. By now, this is what I have translated.* 15 | 16 | # Contents 17 | 18 | - [**GPU vs CPU** in **Java**](./gpu_cpu): Is GPU really necessary to train *Deep Learning* models? How to run code in GPU using Java? 19 | 20 | -------------------------------------------------------------------------------- /english/cnn_java/README.md: -------------------------------------------------------------------------------- 1 | ![](../../icone.png) 2 | 3 | # Neural Networks and Deep Learning using Java 4 | 5 | [**Cleuton Sampaio**](https://github.com/cleuton) - [**LinkedIn**](https://www.linkedin.com/in/cleutonsampaio/) 6 | 7 | ![](../../88x31.png) 8 | 9 | ## Convolutional Neural Network 10 | 11 | [**Original source code here**](../../cnn_java) 12 | 13 | It is a double Neural Network model composed by some convolutional layers (not all neurons are connected), dedicated to extract image features, and some dense connected layers, which classify the image by its features. 14 | 15 | [**Here you have my post**](https://github.com/cleuton/facerec_cnn) explining how a **CNN** works. 16 | 17 | ## Java implementation 18 | 19 | We can create any neural network model with Java, including **CNN**. A CNN requires images pre-processing in order to raise the accuracy. 20 | 21 | In this demo we'll use: 22 | 23 | - [**OpenIMAJ**](http://openimaj.org/): Image pre-processing and face alignment; 24 | - [**Deeplearning4J**](https://deeplearning4j.org/): To create the model. 25 | 26 | ## Image database 27 | 28 | The [**Labeled Faces in the Wild**](http://vis-www.cs.umass.edu/lfw/) is a public image database that can be used to train a CNN. 29 | 30 | We need to pre-process the imagens, extracting and aligning the faces, and also converting them to grayscale. For example:emplo: 31 | 32 | Original image: 33 | 34 | ![](../../cnn_java/cleuton_001.jpg) 35 | 36 | Prepared image: 37 | 38 | ![](../../cnn_java/cleuton_0010.jpg) 39 | 40 | ## Image preprocessing 41 | 42 | Class [**PrepareFaces.java**](../../cnn_java/src/main/java/com/neuraljava/demos/imageutil/PrepareFaces.java) has method **getFaces()**, which uses **OpenIMAJ** to detect, extract and align faces, creating new images. 43 | 44 | ``` 45 | public class PrepareFaces { 46 | public static List getFaces(String imagePath) throws IOException { 47 | List facesList = new ArrayList(); 48 | File arq = new File(imagePath); 49 | BufferedImage imagem = ImageIO.read(arq); 50 | FImage fimage = ImageUtilities.createFImage(imagem); 51 | FKEFaceDetector detector = new FKEFaceDetector(1); 52 | List faces = detector.detectFaces(fimage); 53 | RotateScaleAligner aligner = new RotateScaleAligner(); 54 | for (KEDetectedFace face : faces) { 55 | FImage aligned = aligner.align(face); 56 | BufferedImage bfi = ImageUtilities.createBufferedImage(aligned); 57 | facesList.add(bfi); 58 | } 59 | return facesList; 60 | } 61 | 62 | public static Set prepareImages(String sourcePath, String targetPath) throws IOException { 63 | Set names = new HashSet(); 64 | File[] files = new File(sourcePath).listFiles(); 65 | for (File file : files) { 66 | String name = getNameFromFile(file); 67 | names.add(name); 68 | List faces = getFaces(file.getAbsolutePath()); 69 | int faceCount = 0; 70 | for (BufferedImage img : faces) { 71 | int pos = file.getName().indexOf('.'); 72 | String fileName = file.getName().substring(0,pos) + faceCount + ".jpg"; 73 | File outputfile = new File(targetPath + "/" + fileName); 74 | ImageIO.write(img, "jpg", outputfile); 75 | faceCount++; 76 | } 77 | System.out.println("Name: " + name); 78 | } 79 | return names; 80 | } 81 | 82 | ``` 83 | 84 | ## Model creation 85 | 86 | I created the model using **Deeplearning4J** in class [**TrainModel**](../../cnn_java/src/main/java/com/neuraljava/demos/imageutil/TrainModel.java): 87 | 88 | ``` 89 | private static final Logger log = Logger.getLogger(TrainModel.class.getName()); 90 | public static void train(String imagePath, String modelPath, int numLabels, String testImage) throws IOException { 91 | 92 | // Random 93 | int seed = 42; 94 | Random random = new Random(seed); 95 | 96 | // Hiperparameters 97 | int height = 80; 98 | int width = 80; 99 | int channels = 1; 100 | int batchSize = 10; 101 | double trainTestRatio = 0.7; 102 | int iterations = 1; 103 | int epochs = 50; 104 | double learningRate = 0.01; 105 | 106 | // Prepare dataset: 107 | 108 | LabelGen labelMaker = new LabelGen(); 109 | File mainPath = new File(imagePath); 110 | FileSplit fileSplit = new FileSplit(mainPath, NativeImageLoader.ALLOWED_FORMATS, random); 111 | int numExamples = Math.toIntExact(fileSplit.length()); 112 | 113 | // Network configuration: 114 | 115 | MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder() 116 | .seed(seed) 117 | .l2(0.0005) 118 | .weightInit(WeightInit.XAVIER) 119 | .updater(new Adam(1e-3)) 120 | .list() 121 | .layer(new ConvolutionLayer.Builder(5, 5) 122 | .nIn(channels) 123 | .stride(1,1) 124 | .nOut(32) 125 | .activation(Activation.IDENTITY) 126 | .build()) 127 | .layer(new SubsamplingLayer.Builder(PoolingType.MAX) 128 | .kernelSize(2,2) 129 | .stride(2,2) 130 | .build()) 131 | .layer(new ConvolutionLayer.Builder(5, 5) 132 | .stride(1,1) 133 | .nOut(50) 134 | .activation(Activation.IDENTITY) 135 | .build()) 136 | .layer(new SubsamplingLayer.Builder(PoolingType.MAX) 137 | .kernelSize(2,2) 138 | .stride(2,2) 139 | .build()) 140 | .layer(new DenseLayer.Builder().activation(Activation.RELU) 141 | .nOut(500).build()) 142 | .layer(new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD) 143 | .nOut(numLabels) 144 | .activation(Activation.SOFTMAX) 145 | .build()) 146 | .setInputType(InputType.convolutionalFlat(height,width,1)) 147 | .build(); 148 | MultiLayerNetwork network = new MultiLayerNetwork(conf); 149 | 150 | network.init(); 151 | ``` 152 | 153 | To get the classes from the file name, I had to create class [**LabelGen**](../../cnn_java/src/main/java/com/neuraljava/demos/imageutil/LabelGen.java), implementing interface **PathLabelGenerator**. 154 | 155 | ## GPU usage 156 | 157 | This code uses the **GPU** to train the Neural Network. To use the GPU I had to include this in the **pom.xml**: 158 | 159 | ``` 160 | 161 | org.nd4j 162 | nd4j-cuda-10.1 163 | 1.0.0-beta4 164 | 165 | 166 | org.deeplearning4j 167 | deeplearning4j-cuda-10.0 168 | 1.0.0-beta4 169 | 170 | ``` 171 | 172 | ## How to run the code 173 | 174 | When you run a ```mvn clean package``` it will create an **uber jar**. Just run it with the arguments: 175 | 176 | 1. raw images path 177 | 2. processed images path 178 | 3. model save path 179 | 4. test image path -------------------------------------------------------------------------------- /formula-sigmoid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cleuton/neuraljava/6e5c3f6da4fb43ecf13cc380b7e3bc8d4b371e3f/formula-sigmoid.png -------------------------------------------------------------------------------- /forward.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cleuton/neuraljava/6e5c3f6da4fb43ecf13cc380b7e3bc8d4b371e3f/forward.png -------------------------------------------------------------------------------- /gpu_cpu/README.md: -------------------------------------------------------------------------------- 1 | ![](../icone.png) 2 | # Redes neurais e Deep Learning utilizando Java 3 | [**Cleuton Sampaio**](https://github.com/cleuton) - [**LinkedIn**](https://www.linkedin.com/in/cleutonsampaio/) 4 | 5 | ![](../88x31.png) 6 | 7 | [**English version**](../english/gpu_cpu) 8 | 9 | ## GPU ou CPU? 10 | 11 | Uma grande dúvida dos desenolvedores de aplicações de Machine Learning e de Deep Learning é sobre a necessidade ou não de usar um computador com GPU, afinal de contas, GPUs ainda são muito caras. Para ter uma ideia, veja o preço de uma GPU típica para processamento de IA no Brasil custa algo entre US$ 1.000,00 e US$ 7.000,00 (ou mais). 12 | 13 | O objetivo deste tutorial é demonstrar a necessidade do uso de GPU para processamento de **Deep Learning**. 14 | 15 | A Amazon possui instâncias apropriadas para processamento com GPU. Veja só a comparação de preços de duas configurações: 16 | 17 | | Instância | vCPUs | GPUs | RAM | Preço por hora (US$) | 18 | | --- | --- | --- | --- | --- | 19 | | c5.2xlarge | 8 | 0 | 16 GiB | 0,34 | 20 | | p3.2xlarge | 8 | 1 | 61 GiB | 3,06 | 21 | | p3.8xlarge | 32 | 4 | 244 GiB | 12,24 | 22 | 23 | Qualquer um que já tenha treinado um modelo de Machine ou Deep Learning, sabe que o uso de GPU pode diminuir o tempo de treinamento de dias / horas para minutos / segundos, certo? 24 | 25 | Mas, é realmente necessário? Não podemos usar um **cluster** de máquinas baratas, como fazemos com **Bigdata**? 26 | 27 | A resposta mais simples e direta é: **SIM**, GPUs são necessárias para treinar modelos e nada as substituirá. Porém, você tem que programar direito, de modo a obter o melhor proveito do uso de **GPU**, e nem todas as bibliotecas e frameworks fazem isso com eficiência. 28 | 29 | ## Como funciona a GPU 30 | 31 | Vamos começar por uma analogia, adaptada do que eu vi no treinamento da [**Data Science Academy**](http://www.datascienceacademy.com.br) e que gostei muito. 32 | 33 | Imagine uma moto sinistra, tipo... 1000 CC... Sei lá, uma Kawazaki. É uma moto bem veloz, certo? Agora, imagine que você tenha 8 dessas motos e faz entrega de pizzas. Cada moto pode levar 1 pedido até o cliente, logo, se houver mais de 8 pedidos, alguém terá que esperar uma das motos ficar disponível para entrega. 34 | 35 | É assim que funciona a CPU: Muito rápida e voltada para processamento sequencial. Cada núcleo é uma moto muito veloz. É claro que você pode adaptar para que cada moto entregue mais de uma pizza por vez, mas, de qualquer forma, será um processamento sequencial: Entrega uma pizza, entrega a próxima etc. 36 | 37 | Agora, vamos pensar que você tenha 2000 bicicletas e 2000 entregadores. Embora as motos sejam muito mais rápidas, você tem muito mais bicicletas e pode entregar vários pedidos de uma só vez, evitando filas de atendimento. A lentidão das bikes é compensada pelo paralelismo. 38 | 39 | Assim é a GPU: Orientada para processamento em paralelo. 40 | 41 | Se compararmos o tempo de processamento de tarefas, a CPU ganha, mas, se considerarmos o paralelismo, a GPU é imbatível. É por isso que é utilizada para tarefas de processamento intensivo e cálculos, como: Mineração de moedas virtuais e Deep Learning. 42 | 43 | ## Como podemos programar para a GPU 44 | 45 | A programação para GPU não é simples. Para começar, você tem que considerar que existe mais de um fornecedor de GPUs e que há dois frameworks de programação mais conhecidos: 46 | 47 | - **CUDA**: Compute Unified Device Architecture, dos chips **Nvidia**; 48 | - **OpenCL**: Utilizado em GPUs de outros fornecedores, como a **AMD**. 49 | 50 | A interface de programação CUDA é feita em C, mas existem *bindings* para **Python**, como o [**PyCuda**](https://documen.tician.de/pycuda/) e para **Java**, como o [**JCuda**](http://www.jcuda.org/). Mas são um pouco mais difíceis de aprender e programar. 51 | 52 | E você precisa entender bem da plataforma [**CUDA**](https://developer.nvidia.com/cuda-zone), assim como de seus componentes individuais, como o [**cuDNN**](https://developer.nvidia.com/cudnn) ou o [**cuBLAS**](https://docs.nvidia.com/cuda/cublas/index.html). 53 | 54 | Porém, há alternativas mais fáceis e interessantes que utilizam a GPU, como o [**Deeplearning4J**](https://deeplearning4j.org/docs/latest/nd4j-overview) e seu projeto associado, o [**ND4J**](https://deeplearning4j.org/docs/latest/nd4j-overview). O **ND4J** é como se fosse o **numpy** do Java, só que com **esteróides**! Ele é capaz de sozinho permitir que você use a(s) GPU(s) disponíveis de maneira simples e prática, e é o que vamos usar neste tutorial. 55 | 56 | ## Antes de mais nada 57 | 58 | Você deve ter uma GPU **NVidia** em seu equipamento, com os drivers apropriados instalados. Procure saber qual é a GPU que você possui. Depois, certifique-se de haver instalado o [**driver correto, da Nvidia**](https://www.nvidia.com.br/Download/index.aspx?lang=br). Depois, instale o [**CUDA Toolkit**](https://www.nvidia.com.br/Download/index.aspx?lang=br). Se tudo estiver correto, você poderá executar o comando abaixo: 59 | 60 | ``` 61 | nvidia-smi 62 | 63 | +-----------------------------------------------------------------------------+ 64 | | NVIDIA-SMI 418.39 Driver Version: 418.39 CUDA Version: 10.1 | 65 | |-------------------------------+----------------------+----------------------+ 66 | | GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC | 67 | | Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. | 68 | |===============================+======================+======================| 69 | | 0 GeForce MX110 On | 00000000:01:00.0 Off | N/A | 70 | | N/A 50C P0 N/A / N/A | 666MiB / 2004MiB | 4% Default | 71 | +-------------------------------+----------------------+----------------------+ 72 | 73 | +-----------------------------------------------------------------------------+ 74 | | Processes: GPU Memory | 75 | | GPU PID Type Process name Usage | 76 | |=============================================================================| 77 | | 0 1078 G /usr/lib/xorg/Xorg 302MiB | 78 | | 0 1979 G /usr/bin/gnome-shell 125MiB | 79 | | 0 2422 G ...quest-channel-token=7684432457067004974 108MiB | 80 | | 0 19488 G ...-token=7864D1BD51E7DFBD5D19F40F0E37669D 47MiB | 81 | | 0 20879 G ...-token=8B052333281BD2F7FF0CBFF6F185BA98 1MiB | 82 | | 0 24967 G ...-token=62FCB4B2D2AE1DC66B4AF1A0693122BE 40MiB | 83 | | 0 25379 G ...equest-channel-token=587023958284958671 35MiB | 84 | +-----------------------------------------------------------------------------+ 85 | 86 | ``` 87 | 88 | ## Trabalhos de IA 89 | 90 | O que é um trabalho de IA? De Deep Learning? É baseado em duas operações matemáticas complexas: 91 | 92 | - **Feedforward**: Basicamente a combinação linear das matrizes de pesos com os valores em cada camada, da entrada até o final; 93 | - **Backpropagation**: Cálculo diferencial de cada gradiente de cada neurônio (incluindo os BIAS), da última camada até o início, de modo a ajustar os pesos. 94 | O Feedforward é repetido para cada registro do conjunto de entrada e multiplicado pela quantidade de iterações ou epochs que desejamos treinar, ou seja, muitas vezes. E o Backpropagation pode ser feito na mesma frequência, ou a intervalos regulares, dependendo o algoritmo de aprendizado utilizado. 95 | 96 | Em resumo: Cálculos vetoriais e diferenciais de múltiplos valores simultâneos. 97 | 98 | É por isto que as GPUs são necessárias para desenvolvimento, treinamento e também para inferências, dependendo da complexidade do modelo. 99 | 100 | ## Demonstração 101 | 102 | O projeto deste tutorial é uma aplicação **Java** que executa uma multiplicação de matrizes (produto escalar), uma operação comum em trabalhos de deep learning. Ele multiplica as matrizes uma única vez, primeiro na CPU, depois na GPU (utilizando ND4J e CUDA Toolkit). Notem que nem chega a ser um modelo de machine learning, mas apenas uma única operação básica. 103 | 104 | O arquivo [**pom.xml**](./pom.xml) configura o **ND4J** para usar a GPU com a plataforma **CUDA**: 105 | 106 | ``` 107 | 108 | org.nd4j 109 | nd4j-cuda-10.1 110 | 1.0.0-beta4 111 | 112 | ``` 113 | 114 | A classe principal [**MatMul**](./src/main/java/com/neuraljava/demos/gpu/MatMul.java) é uma aplicação simples, que define duas matrizes e calcula seu produto escalar, primeiramente na CPU, depois na GPU, utilizando o **ND4J**. 115 | 116 | Estou trabalhando com 2 matrizes de 500 x 300 e 300 x 400, nada demais para uma rede neural típica. 117 | 118 | Meu laptop é um **I7, oitava geração**, e tem um chipset **Nvidia MX110**, que é bem "entry level", com 256 cuda cores e Cuda Capability 5, ou seja, nada demais... Uma placa K80 tem mais de 3.500 cuda cores e cuda capability 8 ou superior. 119 | 120 | Vejamos a execução do programa: 121 | 122 | ``` 123 | CPU Interativo (nanossegundos): 111.203.589 124 | 125 | ... 126 | 1759 [main] INFO org.nd4j.linalg.jcublas.ops.executioner.CudaExecutioner - Device Name: [GeForce MX110]; CC: [5.0]; Total/free memory: [2101870592] 127 | 128 | 129 | GPU Paralelo (nanossegundos): 9.905.426 130 | 131 | 132 | Percentual de tempo no cálculo da GPU com relação à CPU: 8.907469704057842 133 | ``` 134 | 135 | ## Conclusão 136 | 137 | Em uma GPU *entry level* como a minha, o produto escalar de matrizes rodou na GPU em apenas 8,9% do tempo que rodou na CPU. Uma diferença abissal. Vejam só: 138 | - Tempo CPU: **111.203.589** nanossegundos; 139 | - Tempo GPU: **9.905.426** nanossegundos. 140 | Considerando que o produto escalar é apenas UMA operação, e que o Feedforward envolve milhares de vezes esta operação, é razoável acreditar que essa diferença deve ser muito maior, se realmente estivéssemos treinando uma rede neural. 141 | 142 | E não adianta cluster, nem RDMA, pois nada, NADA é capaz de igualar o desempenho de uma única GPU. 143 | 144 | Bom, espero ter demonstrado aqui duas coisas: **GPU é essencial** e como podemos utilizá-la diretamente de uma aplicação **Java**. Se você quiser, pode até converter aquele modelo de [**MLP** que fizemos](../multilayerperceptron) para rodar na **GPU** e tirar uma tremenda onda. 145 | -------------------------------------------------------------------------------- /gpu_cpu/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.neuraljava.demos 6 | gpu 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | gpu 11 | http://maven.apache.org 12 | 13 | 14 | UTF-8 15 | 1.8 16 | 1.8 17 | 18 | 19 | 20 | 21 | junit 22 | junit 23 | 4.12 24 | test 25 | 26 | 27 | 28 | org.slf4j 29 | slf4j-api 30 | 1.7.5 31 | 32 | 33 | org.slf4j 34 | slf4j-log4j12 35 | 1.7.5 36 | 37 | 38 | 39 | 40 | org.nd4j 41 | nd4j-cuda-10.1 42 | 1.0.0-beta4 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /gpu_cpu/src/main/java/com/neuraljava/demos/gpu/MatMul.java: -------------------------------------------------------------------------------- 1 | package com.neuraljava.demos.gpu; 2 | 3 | import java.text.DecimalFormat; 4 | import java.text.NumberFormat; 5 | import java.util.Arrays; 6 | import java.util.Random; 7 | 8 | import org.apache.log4j.BasicConfigurator; 9 | import org.nd4j.linalg.api.ndarray.INDArray; 10 | import org.nd4j.linalg.factory.Nd4j; 11 | import org.nd4j.linalg.util.ArrayUtil; 12 | 13 | public class MatMul { 14 | 15 | 16 | 17 | private static float [][] iterativo(float[][] A, float [][] B) { 18 | 19 | float [][] AB = new float[A.length][B[0].length]; 20 | int linAB=0; 21 | int colAB=0; 22 | for (int linA=0; linA= AB[0].length) { 29 | colAB = 0; 30 | linAB++; 31 | } 32 | } 33 | } 34 | 35 | return AB; 36 | } 37 | 38 | 39 | public static void main (String [] args) { 40 | 41 | int seed = 42; 42 | int linhasA = 500; 43 | int colunasA = 300; 44 | int linhasB = 300; 45 | int colunasB = 400; 46 | int linhasC = linhasA; 47 | int colunasC = colunasB; 48 | Random random = new Random(seed); 49 | BasicConfigurator.configure(); 50 | 51 | float [][] A = new float[linhasA][colunasA]; 52 | for(int x=0;x 3 | 4.0.0 4 | 5 | com.neuraljava.samples 6 | classificationIris 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | classificationIris 11 | http://maven.apache.org 12 | 13 | 14 | UTF-8 15 | 1.8 16 | 1.8 17 | 18 | 19 | 20 | 21 | junit 22 | junit 23 | 4.12 24 | test 25 | 26 | 27 | org.deeplearning4j 28 | deeplearning4j-core 29 | 1.0.0-beta4 30 | 31 | 32 | org.deeplearning4j 33 | deeplearning4j-modelimport 34 | 1.0.0-beta4 35 | 36 | 37 | org.nd4j 38 | nd4j-cuda-10.1 39 | 1.0.0-beta4 40 | 41 | 42 | org.deeplearning4j 43 | deeplearning4j-cuda-10.0 44 | 1.0.0-beta4 45 | 46 | 47 | org.deeplearning4j 48 | deeplearning4j-ui_2.10 49 | 1.0.0-beta4 50 | 51 | 52 | 53 | 54 | 55 | org.apache.maven.plugins 56 | maven-assembly-plugin 57 | 3.1.1 58 | 59 | 60 | jar-with-dependencies 61 | 62 | 63 | 64 | com.neuraljava.samples.classificationIris.ClassificationIris 65 | 66 | 67 | 68 | 69 | 70 | make-assembly 71 | package 72 | 73 | single 74 | 75 | 76 | 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /iris/src/main/java/com/neuraljava/samples/classificationIris/ClassificationIris.java: -------------------------------------------------------------------------------- 1 | package com.neuraljava.samples.classificationIris; 2 | 3 | import java.io.IOException; 4 | import java.util.Arrays; 5 | import java.util.logging.Logger; 6 | 7 | import org.datavec.api.records.reader.RecordReader; 8 | import org.datavec.api.records.reader.impl.csv.CSVRecordReader; 9 | import org.datavec.api.records.reader.impl.transform.TransformProcessRecordReader; 10 | import org.datavec.api.split.FileSplit; 11 | import org.datavec.api.transform.TransformProcess; 12 | import org.datavec.api.transform.schema.Schema; 13 | import org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator; 14 | import org.deeplearning4j.datasets.iterator.DataSetIteratorSplitter; 15 | import org.deeplearning4j.nn.conf.NeuralNetConfiguration; 16 | import org.deeplearning4j.nn.conf.layers.DenseLayer; 17 | import org.deeplearning4j.nn.conf.layers.OutputLayer; 18 | import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; 19 | import org.deeplearning4j.nn.weights.WeightInit; 20 | import org.deeplearning4j.optimize.listeners.ScoreIterationListener; 21 | import org.nd4j.evaluation.classification.Evaluation; 22 | import org.nd4j.linalg.activations.Activation; 23 | import org.nd4j.linalg.api.ndarray.INDArray; 24 | import org.nd4j.linalg.dataset.DataSet; 25 | import org.nd4j.linalg.dataset.SplitTestAndTrain; 26 | import org.nd4j.linalg.dataset.api.iterator.DataSetIterator; 27 | import org.nd4j.linalg.factory.Nd4j; 28 | import org.nd4j.linalg.io.ClassPathResource; 29 | import org.nd4j.linalg.learning.config.Adam; 30 | import org.nd4j.linalg.lossfunctions.LossFunctions; 31 | 32 | 33 | 34 | public class ClassificationIris { 35 | private static final Logger log = Logger.getLogger(ClassificationIris.class.getName()); 36 | private void modelBuild() throws IOException, InterruptedException { 37 | 38 | // Carrega o arquivo CSV diretamente do Resource: 39 | RecordReader rr = new CSVRecordReader(0,','); // Não há header, logo, não vamos pular a primeira linha! 40 | rr.initialize(new FileSplit(new ClassPathResource("iris.data").getFile())); 41 | 42 | // Vamos criar o esquema para transformação do dataset: 43 | Schema schema = new Schema.Builder() 44 | .addColumnFloat("sepal_length") 45 | .addColumnFloat("sepal_width") 46 | .addColumnFloat("petal_length") 47 | .addColumnFloat("petal_width") 48 | .addColumnCategorical("class", Arrays.asList("Iris-setosa","Iris-versicolor", "Iris-virginica")) 49 | .build(); 50 | 51 | // Transformando atributos discretos em one-hot-encodeds: 52 | TransformProcess transformProcess = new TransformProcess.Builder(schema) 53 | .categoricalToInteger("class") 54 | .build(); 55 | 56 | RecordReader tRR = new TransformProcessRecordReader(rr,transformProcess); 57 | int labelIndex = 4; // O índice do label é 4 (começa de zero) 58 | int numClasses = 3; 59 | int batchSize = 150; 60 | 61 | DataSetIterator iterator = new RecordReaderDataSetIterator(tRR,batchSize,labelIndex,numClasses); 62 | DataSet allData = iterator.next(); 63 | allData.shuffle(); 64 | SplitTestAndTrain testAndTrain = allData.splitTestAndTrain(0.65); //Use 65% of data for training 65 | 66 | DataSet trainingData = testAndTrain.getTrain(); 67 | DataSet testData = testAndTrain.getTest(); 68 | // Criando a arquitetura da Rede: 69 | int seed = 124; 70 | int colunasInput = 4; 71 | int colunasOutput = 3; 72 | int nHidden = 8; 73 | float learningRate = 0.01f; 74 | int epochs = 500; 75 | 76 | MultiLayerNetwork net = new MultiLayerNetwork(new NeuralNetConfiguration.Builder() 77 | .seed(seed) 78 | .weightInit(WeightInit.XAVIER) 79 | .updater(new Adam(learningRate)) 80 | .list() 81 | .layer(0, new DenseLayer.Builder().nIn(colunasInput).nOut(nHidden) 82 | .activation(Activation.RELU) 83 | .build()) 84 | .layer(1, new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT) // Multiclass Cross Entropy 85 | .activation(Activation.SOFTMAX) 86 | .nIn(nHidden).nOut(colunasOutput).build()) 87 | .build() 88 | ); 89 | 90 | net.setListeners(new ScoreIterationListener(8)); 91 | 92 | // Treinamento da rede: 93 | for( int i=0; i0) { 89 | linhas[x] = linha; 90 | } 91 | } 92 | List registros = Arrays.asList(linhas); 93 | Collections.shuffle(registros); 94 | int reg = 0; 95 | for (String linha2 : registros) { 96 | String [] vetor1 = linha2.split(","); 97 | for (int v=0; v nodes; 30 | public int number; 31 | Node bias; 32 | 33 | public Layer(int numNodes, Activation activation, Model model) { 34 | super(); 35 | this.activation = activation; 36 | this.nodes = new ArrayList(); 37 | this.bias = new Node(); 38 | this.bias.sinapses = new ArrayList(); 39 | this.number = model.layers.size() + 1; 40 | this.bias.layerNumber = this.number; 41 | for (int x=0; x(); 44 | this.nodes.add(node); 45 | node.layerNumber = this.number; 46 | node.nodeNumber = x+1; 47 | } 48 | // Criamos as sinapses da camada anterior, conectando esta camada à ela. 49 | if (model.layers.size() > 0) { 50 | // A input layer não tem camada anterior 51 | Layer previous = model.layers.get(model.layers.size()-1); // Pega a última inserida 52 | for (Node nprev : previous.nodes) { 53 | for (Node natu : this.nodes) { 54 | Sinapse sinapse = new Sinapse(); 55 | sinapse.finalNode = natu; 56 | sinapse.weight = model.getRandom(); 57 | nprev.sinapses.add(sinapse); 58 | } 59 | } 60 | // Bias da camada anterior (um pouco de repetição, mas dá para entender bem) 61 | for (Node natu : this.nodes) { 62 | Sinapse sinapse = new Sinapse(); 63 | sinapse.finalNode = natu; 64 | sinapse.weight = model.getRandom(); 65 | previous.bias.sinapses.add(sinapse); 66 | } 67 | } 68 | else { 69 | model.firstLayer = this; 70 | } 71 | model.lastLayer = this; 72 | } 73 | 74 | @Override 75 | public boolean equals(Object obj) { 76 | return ((Layer)obj).number == this.number; 77 | } 78 | 79 | @Override 80 | public String toString() { 81 | String saida = "\n[Layer. Number : " + this.number 82 | + "\nBias: " 83 | + this.bias 84 | + "\nnodes:\n" 85 | + this.nodes 86 | + "\n]"; 87 | return saida; 88 | } 89 | 90 | 91 | } 92 | -------------------------------------------------------------------------------- /iris/src/main/java/com/neuraljava/samples/mlpgen/api/Model.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2018 Cleuton Sampaio 4 | 5 | Licensed under the Apache License, Version 2.0 (the "License"); 6 | you may not use this file except in compliance with the License. 7 | You may obtain a copy of the License at 8 | 9 | http://www.apache.org/licenses/LICENSE-2.0 10 | 11 | Unless required by applicable law or agreed to in writing, software 12 | distributed under the License is distributed on an "AS IS" BASIS, 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | See the License for the specific language governing permissions and 15 | limitations under the License. 16 | 17 | Este trabalho é para demonstração de redes neurais e não tem objetivo 18 | de desempenho ou precisão. O Autor não se responsabiliza pelo seu uso e 19 | não fornecerá suporte. 20 | */ 21 | package com.neuraljava.samples.mlpgen.api; 22 | 23 | import java.util.ArrayList; 24 | import java.util.List; 25 | import java.util.Random; 26 | 27 | import org.slf4j.Logger; 28 | import org.slf4j.LoggerFactory; 29 | 30 | /** 31 | * Esta implementação de modelo utiliza Gradient Descent e MSE. 32 | * A intenção é demonstração e aprendizado, e não desempenho. 33 | * @author Cleuton Sampaio 34 | * 35 | */ 36 | public class Model { 37 | public List layers; 38 | public double lossValue; 39 | Random random; 40 | Layer firstLayer; 41 | Layer lastLayer; 42 | Logger logger = LoggerFactory.getLogger(IrisClassifier.class); 43 | 44 | public double [] forwardPass(double [] input) { 45 | double [] outputValues = new double [layers.get(layers.size()-1).nodes.size()]; 46 | for (Layer layer : this.layers) { 47 | if (layer.equals(this.firstLayer)) { 48 | int i = 0; 49 | for (Node node : layer.nodes) { 50 | node.input = input[i]; 51 | node.value = input[i]; 52 | i++; 53 | } 54 | } 55 | else { 56 | for (Node node : layer.nodes) { 57 | double finalValue = 0.0; 58 | int idx = this.layers.indexOf(layer); 59 | idx--; 60 | Layer previous = this.layers.get(idx); 61 | for (Node nprev : previous.nodes) { 62 | Sinapse sinapse = this.getSinapse(nprev, node); 63 | finalValue += (nprev.value * sinapse.weight); 64 | } 65 | // Bias anterior: 66 | Sinapse sinapse = this.getSinapse(previous.bias, node); 67 | finalValue += (sinapse.weight); 68 | node.input = finalValue; 69 | node.value = layer.activation.exec(node.input); 70 | } 71 | } 72 | } 73 | for (int x=0; x=0; l--) { 101 | Layer layer = this.layers.get(l); 102 | Layer proxima = this.layers.get(l+1); 103 | for (Node node : layer.nodes) { 104 | if (l == (indiceUltima - 1)) { 105 | for (Sinapse sinapse : node.sinapses) { 106 | double erro = outputErrors[sinapse.finalNode.nodeNumber-1]; 107 | sinapse.gradient = erro * proxima.activation.calcularDerivada(sinapse.finalNode.value) * node.value; 108 | } 109 | } 110 | else { 111 | // Da antepenúltima para trás, somamos os deltaz 112 | for (Sinapse sinapse : node.sinapses) { 113 | double valorFinal = 0.0; 114 | for (Sinapse s2 : sinapse.finalNode.sinapses) { 115 | double deltaz = outputErrors[s2.finalNode.nodeNumber-1]*outputs[s2.finalNode.nodeNumber-1]*(1-outputs[s2.finalNode.nodeNumber-1]); 116 | valorFinal += (deltaz * s2.weight); 117 | } 118 | sinapse.gradient = valorFinal * proxima.activation.calcularDerivada(sinapse.finalNode.value) * node.value; 119 | } 120 | } 121 | } 122 | // Peso do Bias 123 | if (l == (indiceUltima - 1)) { 124 | for (Sinapse sinapse : layer.bias.sinapses) { 125 | double erro = sinapse.finalNode.value - target[sinapse.finalNode.nodeNumber-1]; 126 | sinapse.gradient = erro * layer.activation.calcularDerivada(sinapse.finalNode.value); 127 | } 128 | } 129 | else { 130 | for (Sinapse sinapse : layer.bias.sinapses) { 131 | double valorFinal = 0.0; 132 | for (Sinapse s2 : sinapse.finalNode.sinapses) { 133 | double deltaz = outputErrors[s2.finalNode.nodeNumber-1]*outputs[s2.finalNode.nodeNumber-1]*(1-outputs[s2.finalNode.nodeNumber-1]); 134 | valorFinal += (deltaz * s2.weight); 135 | } 136 | sinapse.gradient = valorFinal * proxima.activation.calcularDerivada(sinapse.finalNode.value); 137 | } 138 | } 139 | } 140 | // Update weights 141 | for (int la=0;la(); 177 | this.random = new Random(); 178 | } 179 | 180 | public Model(int seed) { 181 | this(); 182 | this.random = new Random(seed); 183 | } 184 | 185 | public double calcSquaredErrors(double [] target, double [] estimated) { 186 | double retorno = 0.0; 187 | for (int y=0;y sinapses; 34 | public double input; // net value before activation 35 | public double value; // output or current value 36 | @Override 37 | public boolean equals(Object obj) { 38 | Node other = (Node) obj; 39 | return other.input == this.input && other.layerNumber == this.layerNumber; 40 | } 41 | @Override 42 | public String toString() { 43 | String saida = "\n\t[Node. Layer: " 44 | + this.layerNumber 45 | + ", node: " 46 | + this.nodeNumber 47 | + ", input: " 48 | + this.input 49 | + ", value: " 50 | + this.value 51 | + " sinapses: " 52 | + sinapses 53 | + "]"; 54 | return saida; 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /iris/src/main/java/com/neuraljava/samples/mlpgen/api/ReLU.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2018 Cleuton Sampaio 4 | 5 | Licensed under the Apache License, Version 2.0 (the "License"); 6 | you may not use this file except in compliance with the License. 7 | You may obtain a copy of the License at 8 | 9 | http://www.apache.org/licenses/LICENSE-2.0 10 | 11 | Unless required by applicable law or agreed to in writing, software 12 | distributed under the License is distributed on an "AS IS" BASIS, 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | See the License for the specific language governing permissions and 15 | limitations under the License. 16 | 17 | Este trabalho é para demonstração de redes neurais e não tem objetivo 18 | de desempenho ou precisão. O Autor não se responsabiliza pelo seu uso e 19 | não fornecerá suporte. 20 | */ 21 | package com.neuraljava.samples.mlpgen.api; 22 | 23 | public class ReLU extends Activation { 24 | 25 | @Override 26 | public double exec(double input) { 27 | return Math.max(0, input); 28 | } 29 | 30 | @Override 31 | public double calcularDerivada(double parametro) { 32 | double retorno = parametro > 0 ? 1 : 0; 33 | return retorno; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /iris/src/main/java/com/neuraljava/samples/mlpgen/api/Sigmoid.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2018 Cleuton Sampaio 4 | 5 | Licensed under the Apache License, Version 2.0 (the "License"); 6 | you may not use this file except in compliance with the License. 7 | You may obtain a copy of the License at 8 | 9 | http://www.apache.org/licenses/LICENSE-2.0 10 | 11 | Unless required by applicable law or agreed to in writing, software 12 | distributed under the License is distributed on an "AS IS" BASIS, 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | See the License for the specific language governing permissions and 15 | limitations under the License. 16 | 17 | Este trabalho é para demonstração de redes neurais e não tem objetivo 18 | de desempenho ou precisão. O Autor não se responsabiliza pelo seu uso e 19 | não fornecerá suporte. 20 | */ 21 | package com.neuraljava.samples.mlpgen.api; 22 | 23 | public class Sigmoid extends Activation { 24 | 25 | @Override 26 | public double exec(double input) { 27 | return (1/( 1 + Math.pow(Math.E,(-1 * input)))); 28 | } 29 | 30 | @Override 31 | public double calcularDerivada(double saida) { 32 | return saida * (1 - saida); 33 | } 34 | 35 | 36 | } 37 | -------------------------------------------------------------------------------- /iris/src/main/java/com/neuraljava/samples/mlpgen/api/Sinapse.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2018 Cleuton Sampaio 4 | 5 | Licensed under the Apache License, Version 2.0 (the "License"); 6 | you may not use this file except in compliance with the License. 7 | You may obtain a copy of the License at 8 | 9 | http://www.apache.org/licenses/LICENSE-2.0 10 | 11 | Unless required by applicable law or agreed to in writing, software 12 | distributed under the License is distributed on an "AS IS" BASIS, 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | See the License for the specific language governing permissions and 15 | limitations under the License. 16 | 17 | Este trabalho é para demonstração de redes neurais e não tem objetivo 18 | de desempenho ou precisão. O Autor não se responsabiliza pelo seu uso e 19 | não fornecerá suporte. 20 | */ 21 | package com.neuraljava.samples.mlpgen.api; 22 | 23 | public class Sinapse { 24 | public Node finalNode; 25 | public double weight; 26 | public double gradient; 27 | @Override 28 | public String toString() { 29 | String saida = "\n\t[Sinapse. Final node number: " 30 | + finalNode.layerNumber + "/" + finalNode.nodeNumber 31 | + ", weight: " 32 | + this.weight 33 | + "]"; 34 | return saida; 35 | } 36 | @Override 37 | public boolean equals(Object obj) { 38 | Sinapse sinapse = (Sinapse) obj; 39 | Node outro = sinapse.finalNode; 40 | Node node = this.finalNode; 41 | return node.layerNumber == outro.layerNumber 42 | && node.nodeNumber == outro.nodeNumber; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /iris/src/main/resources/iris.data: -------------------------------------------------------------------------------- 1 | 5.1,3.5,1.4,0.2,Iris-setosa 2 | 4.9,3.0,1.4,0.2,Iris-setosa 3 | 4.7,3.2,1.3,0.2,Iris-setosa 4 | 4.6,3.1,1.5,0.2,Iris-setosa 5 | 5.0,3.6,1.4,0.2,Iris-setosa 6 | 5.4,3.9,1.7,0.4,Iris-setosa 7 | 4.6,3.4,1.4,0.3,Iris-setosa 8 | 5.0,3.4,1.5,0.2,Iris-setosa 9 | 4.4,2.9,1.4,0.2,Iris-setosa 10 | 4.9,3.1,1.5,0.1,Iris-setosa 11 | 5.4,3.7,1.5,0.2,Iris-setosa 12 | 4.8,3.4,1.6,0.2,Iris-setosa 13 | 4.8,3.0,1.4,0.1,Iris-setosa 14 | 4.3,3.0,1.1,0.1,Iris-setosa 15 | 5.8,4.0,1.2,0.2,Iris-setosa 16 | 5.7,4.4,1.5,0.4,Iris-setosa 17 | 5.4,3.9,1.3,0.4,Iris-setosa 18 | 5.1,3.5,1.4,0.3,Iris-setosa 19 | 5.7,3.8,1.7,0.3,Iris-setosa 20 | 5.1,3.8,1.5,0.3,Iris-setosa 21 | 5.4,3.4,1.7,0.2,Iris-setosa 22 | 5.1,3.7,1.5,0.4,Iris-setosa 23 | 4.6,3.6,1.0,0.2,Iris-setosa 24 | 5.1,3.3,1.7,0.5,Iris-setosa 25 | 4.8,3.4,1.9,0.2,Iris-setosa 26 | 5.0,3.0,1.6,0.2,Iris-setosa 27 | 5.0,3.4,1.6,0.4,Iris-setosa 28 | 5.2,3.5,1.5,0.2,Iris-setosa 29 | 5.2,3.4,1.4,0.2,Iris-setosa 30 | 4.7,3.2,1.6,0.2,Iris-setosa 31 | 4.8,3.1,1.6,0.2,Iris-setosa 32 | 5.4,3.4,1.5,0.4,Iris-setosa 33 | 5.2,4.1,1.5,0.1,Iris-setosa 34 | 5.5,4.2,1.4,0.2,Iris-setosa 35 | 4.9,3.1,1.5,0.1,Iris-setosa 36 | 5.0,3.2,1.2,0.2,Iris-setosa 37 | 5.5,3.5,1.3,0.2,Iris-setosa 38 | 4.9,3.1,1.5,0.1,Iris-setosa 39 | 4.4,3.0,1.3,0.2,Iris-setosa 40 | 5.1,3.4,1.5,0.2,Iris-setosa 41 | 5.0,3.5,1.3,0.3,Iris-setosa 42 | 4.5,2.3,1.3,0.3,Iris-setosa 43 | 4.4,3.2,1.3,0.2,Iris-setosa 44 | 5.0,3.5,1.6,0.6,Iris-setosa 45 | 5.1,3.8,1.9,0.4,Iris-setosa 46 | 4.8,3.0,1.4,0.3,Iris-setosa 47 | 5.1,3.8,1.6,0.2,Iris-setosa 48 | 4.6,3.2,1.4,0.2,Iris-setosa 49 | 5.3,3.7,1.5,0.2,Iris-setosa 50 | 5.0,3.3,1.4,0.2,Iris-setosa 51 | 7.0,3.2,4.7,1.4,Iris-versicolor 52 | 6.4,3.2,4.5,1.5,Iris-versicolor 53 | 6.9,3.1,4.9,1.5,Iris-versicolor 54 | 5.5,2.3,4.0,1.3,Iris-versicolor 55 | 6.5,2.8,4.6,1.5,Iris-versicolor 56 | 5.7,2.8,4.5,1.3,Iris-versicolor 57 | 6.3,3.3,4.7,1.6,Iris-versicolor 58 | 4.9,2.4,3.3,1.0,Iris-versicolor 59 | 6.6,2.9,4.6,1.3,Iris-versicolor 60 | 5.2,2.7,3.9,1.4,Iris-versicolor 61 | 5.0,2.0,3.5,1.0,Iris-versicolor 62 | 5.9,3.0,4.2,1.5,Iris-versicolor 63 | 6.0,2.2,4.0,1.0,Iris-versicolor 64 | 6.1,2.9,4.7,1.4,Iris-versicolor 65 | 5.6,2.9,3.6,1.3,Iris-versicolor 66 | 6.7,3.1,4.4,1.4,Iris-versicolor 67 | 5.6,3.0,4.5,1.5,Iris-versicolor 68 | 5.8,2.7,4.1,1.0,Iris-versicolor 69 | 6.2,2.2,4.5,1.5,Iris-versicolor 70 | 5.6,2.5,3.9,1.1,Iris-versicolor 71 | 5.9,3.2,4.8,1.8,Iris-versicolor 72 | 6.1,2.8,4.0,1.3,Iris-versicolor 73 | 6.3,2.5,4.9,1.5,Iris-versicolor 74 | 6.1,2.8,4.7,1.2,Iris-versicolor 75 | 6.4,2.9,4.3,1.3,Iris-versicolor 76 | 6.6,3.0,4.4,1.4,Iris-versicolor 77 | 6.8,2.8,4.8,1.4,Iris-versicolor 78 | 6.7,3.0,5.0,1.7,Iris-versicolor 79 | 6.0,2.9,4.5,1.5,Iris-versicolor 80 | 5.7,2.6,3.5,1.0,Iris-versicolor 81 | 5.5,2.4,3.8,1.1,Iris-versicolor 82 | 5.5,2.4,3.7,1.0,Iris-versicolor 83 | 5.8,2.7,3.9,1.2,Iris-versicolor 84 | 6.0,2.7,5.1,1.6,Iris-versicolor 85 | 5.4,3.0,4.5,1.5,Iris-versicolor 86 | 6.0,3.4,4.5,1.6,Iris-versicolor 87 | 6.7,3.1,4.7,1.5,Iris-versicolor 88 | 6.3,2.3,4.4,1.3,Iris-versicolor 89 | 5.6,3.0,4.1,1.3,Iris-versicolor 90 | 5.5,2.5,4.0,1.3,Iris-versicolor 91 | 5.5,2.6,4.4,1.2,Iris-versicolor 92 | 6.1,3.0,4.6,1.4,Iris-versicolor 93 | 5.8,2.6,4.0,1.2,Iris-versicolor 94 | 5.0,2.3,3.3,1.0,Iris-versicolor 95 | 5.6,2.7,4.2,1.3,Iris-versicolor 96 | 5.7,3.0,4.2,1.2,Iris-versicolor 97 | 5.7,2.9,4.2,1.3,Iris-versicolor 98 | 6.2,2.9,4.3,1.3,Iris-versicolor 99 | 5.1,2.5,3.0,1.1,Iris-versicolor 100 | 5.7,2.8,4.1,1.3,Iris-versicolor 101 | 6.3,3.3,6.0,2.5,Iris-virginica 102 | 5.8,2.7,5.1,1.9,Iris-virginica 103 | 7.1,3.0,5.9,2.1,Iris-virginica 104 | 6.3,2.9,5.6,1.8,Iris-virginica 105 | 6.5,3.0,5.8,2.2,Iris-virginica 106 | 7.6,3.0,6.6,2.1,Iris-virginica 107 | 4.9,2.5,4.5,1.7,Iris-virginica 108 | 7.3,2.9,6.3,1.8,Iris-virginica 109 | 6.7,2.5,5.8,1.8,Iris-virginica 110 | 7.2,3.6,6.1,2.5,Iris-virginica 111 | 6.5,3.2,5.1,2.0,Iris-virginica 112 | 6.4,2.7,5.3,1.9,Iris-virginica 113 | 6.8,3.0,5.5,2.1,Iris-virginica 114 | 5.7,2.5,5.0,2.0,Iris-virginica 115 | 5.8,2.8,5.1,2.4,Iris-virginica 116 | 6.4,3.2,5.3,2.3,Iris-virginica 117 | 6.5,3.0,5.5,1.8,Iris-virginica 118 | 7.7,3.8,6.7,2.2,Iris-virginica 119 | 7.7,2.6,6.9,2.3,Iris-virginica 120 | 6.0,2.2,5.0,1.5,Iris-virginica 121 | 6.9,3.2,5.7,2.3,Iris-virginica 122 | 5.6,2.8,4.9,2.0,Iris-virginica 123 | 7.7,2.8,6.7,2.0,Iris-virginica 124 | 6.3,2.7,4.9,1.8,Iris-virginica 125 | 6.7,3.3,5.7,2.1,Iris-virginica 126 | 7.2,3.2,6.0,1.8,Iris-virginica 127 | 6.2,2.8,4.8,1.8,Iris-virginica 128 | 6.1,3.0,4.9,1.8,Iris-virginica 129 | 6.4,2.8,5.6,2.1,Iris-virginica 130 | 7.2,3.0,5.8,1.6,Iris-virginica 131 | 7.4,2.8,6.1,1.9,Iris-virginica 132 | 7.9,3.8,6.4,2.0,Iris-virginica 133 | 6.4,2.8,5.6,2.2,Iris-virginica 134 | 6.3,2.8,5.1,1.5,Iris-virginica 135 | 6.1,2.6,5.6,1.4,Iris-virginica 136 | 7.7,3.0,6.1,2.3,Iris-virginica 137 | 6.3,3.4,5.6,2.4,Iris-virginica 138 | 6.4,3.1,5.5,1.8,Iris-virginica 139 | 6.0,3.0,4.8,1.8,Iris-virginica 140 | 6.9,3.1,5.4,2.1,Iris-virginica 141 | 6.7,3.1,5.6,2.4,Iris-virginica 142 | 6.9,3.1,5.1,2.3,Iris-virginica 143 | 5.8,2.7,5.1,1.9,Iris-virginica 144 | 6.8,3.2,5.9,2.3,Iris-virginica 145 | 6.7,3.3,5.7,2.5,Iris-virginica 146 | 6.7,3.0,5.2,2.3,Iris-virginica 147 | 6.3,2.5,5.0,1.9,Iris-virginica 148 | 6.5,3.0,5.2,2.0,Iris-virginica 149 | 6.2,3.4,5.4,2.3,Iris-virginica 150 | 5.9,3.0,5.1,1.8,Iris-virginica 151 | -------------------------------------------------------------------------------- /iris/teste.ods: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cleuton/neuraljava/6e5c3f6da4fb43ecf13cc380b7e3bc8d4b371e3f/iris/teste.ods -------------------------------------------------------------------------------- /linearmenteseparavel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cleuton/neuraljava/6e5c3f6da4fb43ecf13cc380b7e3bc8d4b371e3f/linearmenteseparavel.png -------------------------------------------------------------------------------- /mlp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cleuton/neuraljava/6e5c3f6da4fb43ecf13cc380b7e3bc8d4b371e3f/mlp.png -------------------------------------------------------------------------------- /multilayerperceptron/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.neuraljava.samples 6 | mlp 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | mlp 11 | http://maven.apache.org 12 | 13 | 14 | UTF-8 15 | 1.10 16 | 1.10 17 | 18 | 19 | 20 | 21 | 22 | org.apache.commons 23 | commons-math3 24 | 3.6.1 25 | 26 | 27 | 28 | junit 29 | junit 30 | 4.12 31 | test 32 | 33 | 34 | 35 | 36 | 37 | maven-compiler-plugin 38 | 3.7.0 39 | 40 | maven.compiler.source 41 | maven.compiler.target 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /multilayerperceptron/src/main/java/com/neuraljava/samples/mlp/MLP.java: -------------------------------------------------------------------------------- 1 | package com.neuraljava.samples.mlp; 2 | 3 | import java.util.Random; 4 | 5 | import org.apache.commons.math3.linear.MatrixUtils; 6 | import org.apache.commons.math3.linear.RealMatrix; 7 | import org.apache.commons.math3.linear.RealVector; 8 | 9 | /** 10 | * Multi Layer perceptron. 11 | * Este código implementa um MLP (aproximador universal) simples, com o propósito de demonstrar Redes Neurais multicamadas. 12 | * Há só um nó na camada de saída. 13 | * @author Cleuton Sampaio 14 | * 15 | */ 16 | public class MLP { 17 | public RealVector input; 18 | public RealMatrix hidden; 19 | public RealVector hiddenValues; 20 | public RealVector biasHidden; 21 | public RealVector output; 22 | public double outputValue; 23 | public double biasOutputWeight; 24 | public double learningRate; 25 | public int iterations; 26 | public double MSE; 27 | private Random random = new Random(); 28 | 29 | public MLP() { 30 | double [][] wMatrix = new double [2][2]; 31 | wMatrix[0] = random.doubles(2,-2,2.01).toArray(); 32 | wMatrix[1] = random.doubles(2,-2,2.01).toArray(); 33 | this.hidden = MatrixUtils.createRealMatrix(wMatrix); 34 | this.biasHidden = MatrixUtils.createRealVector(random.doubles(2,-2,2.01).toArray()); 35 | this.output = MatrixUtils.createRealVector(random.doubles(2,-2,2.01).toArray()); 36 | biasOutputWeight = random.doubles(-2, 2.01).findFirst().getAsDouble(); 37 | this.learningRate = 0.2; 38 | this.iterations = 100; 39 | } 40 | 41 | /** 42 | * Ativação da Rede com um só conjunto de dados de entrada. Usa Sigmoid como função de ativação 43 | * @param x double[] vetor de características de um registro. 44 | */ 45 | public void forwardPropagation(double [] x) { 46 | this.input = MatrixUtils.createRealVector(x); 47 | hiddenValues = hidden.operate(input).add(biasHidden).map(v -> sigmoid(v)); 48 | outputValue = sigmoid(output.dotProduct(hiddenValues) + biasOutputWeight); 49 | } 50 | 51 | /** 52 | * Backpropagation usando Gradient Descent 53 | * @param y double saída esperada de um exemplo 54 | * @param deltaz2 55 | */ 56 | public void backPropagation(double t) { 57 | double z = this.outputValue; 58 | double erro = Math.pow((t-z), 2); 59 | double deltaz = (z - t) * z * (1 - z); 60 | this.MSE += erro; 61 | for(int i=0;i<2;i++) { 62 | this.output.setEntry(i, 63 | this.output.getEntry(i) 64 | - this.learningRate 65 | * deltaz 66 | * this.hiddenValues.getEntry(i) 67 | ); 68 | } 69 | this.biasOutputWeight = this.biasOutputWeight 70 | - this.learningRate * deltaz; 71 | for(int i=0;i<2;i++) { 72 | for(int j=0;j<2;j++) { 73 | this.hidden.setEntry(i, j, 74 | this.hidden.getEntry(i, i) 75 | - this.learningRate 76 | * deltaz 77 | * this.output.getEntry(j) 78 | * this.hiddenValues.getEntry(j) 79 | * (1 - this.hiddenValues.getEntry(j)) 80 | * this.input.getEntry(i) 81 | ); 82 | } 83 | } 84 | for(int i=0;i<2;i++) { 85 | this.biasHidden.setEntry(i, 86 | this.biasHidden.getEntry(i) 87 | - this.learningRate 88 | * deltaz 89 | * this.output.getEntry(i) 90 | * this.hiddenValues.getEntry(i) 91 | * (1 - this.hiddenValues.getEntry(i)) 92 | ); 93 | } 94 | 95 | } 96 | 97 | private double derivPesosBiasHidden(double t, double z, int i) { 98 | double deriv = (z-t) * z * (1 - z) 99 | * this.output.getEntry(i) 100 | * this.hiddenValues.getEntry(i) 101 | * (1 - this.hiddenValues.getEntry(i)); 102 | return deriv; 103 | } 104 | 105 | private double derivPesosHidden(double t, double z, int i, int j) { 106 | double deriv = (z-t) * z * (1 - z) * this.output.getEntry(j) 107 | * this.hiddenValues.getEntry(j) * (1 - this.hiddenValues.getEntry(j)) 108 | * this.input.getEntry(i); 109 | return deriv; 110 | } 111 | 112 | private double derivPesosBiasOutput(double t, double z) { 113 | return (z-t) * z * (1 - z); 114 | } 115 | 116 | private double derivPesosOutput(double t, double z, int i) { 117 | double deriv = (z-t) * z * (1 - z) * this.hiddenValues.getEntry(i); 118 | return deriv; 119 | } 120 | 121 | public void treinar(double [][] X, double [] y) { 122 | for(int it=0; it 3 | 4.0.0 4 | 5 | com.datalearninghub.samples 6 | perceptron 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | perceptron 11 | http://maven.apache.org 12 | 13 | 14 | UTF-8 15 | 16 | 17 | 18 | 19 | 20 | junit 21 | junit 22 | 4.12 23 | test 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /perceptron/src/main/java/com/datalearninghub/samples/perceptron/Classifier.java: -------------------------------------------------------------------------------- 1 | package com.datalearninghub.samples.perceptron; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.InputStream; 5 | import java.io.InputStreamReader; 6 | 7 | /** 8 | * Usa o Perceptron para classificar as amostras de um dataset com 2 variáveis. 9 | * @author Cleuton Sampaio 10 | * 11 | */ 12 | public class Classifier { 13 | 14 | class Datasetds { 15 | double [][] valores; 16 | double [] rotulo; 17 | } 18 | 19 | Perceptron perceptron; 20 | 21 | void processa() throws Exception { 22 | treinar(); 23 | testar(); 24 | } 25 | 26 | void treinar() throws Exception { 27 | Datasetds treino = preprocessar("dados.csv",149); 28 | perceptron = new Perceptron(10,0.2); 29 | perceptron.treinar(treino.valores, treino.rotulo); 30 | } 31 | 32 | void testar() throws Exception { 33 | Datasetds teste = preprocessar("dados_teste.csv",91); 34 | int erros = 0; 35 | for(int i=0; i 64 | 4.0.0 65 | 66 | com.neuraljava.demos 67 | kerasmodel 68 | 0.0.1-SNAPSHOT 69 | jar 70 | 71 | kerasmodel 72 | http://maven.apache.org 73 | 74 | 75 | UTF-8 76 | 1.8 77 | 1.8 78 | 79 | 80 | 81 | 82 | junit 83 | junit 84 | 4.12 85 | test 86 | 87 | 88 | org.deeplearning4j 89 | deeplearning4j-core 90 | 1.0.0-beta4 91 | 92 | 93 | org.deeplearning4j 94 | deeplearning4j-modelimport 95 | 1.0.0-beta4 96 | 97 | 98 | org.nd4j 99 | nd4j-native-platform 100 | 1.0.0-beta4 101 | 102 | 103 | 104 | 105 | ``` 106 | 107 | **Classe principal** 108 | 109 | A classe principal, aliás a única que eu criei, vai fazer o seguinte: 110 | 111 | 1. Recuperar o modelo criado pelo Keras/Python, a partir dos **resources**; 112 | 2. Ler um arquivo de teste (dois.png) com uma imagem do algarismo dois, desenhado à mão; 113 | 3. Formatar a imagem em uma matriz apropriada para dar entrada no modelo do Keras; 114 | 4. Executar a inferência e exibir o resultado. 115 | 116 | ``` 117 | public class RunModel { 118 | 119 | public static void main(String[] args) throws IOException, InvalidKerasConfigurationException, UnsupportedKerasConfigurationException { 120 | 121 | // Carrega o modelo pré-treinado: 122 | String txtModel = new ClassPathResource("meu_mnist.h5").getFile().getPath(); 123 | MultiLayerNetwork model = KerasModelImport.importKerasSequentialModelAndWeights(txtModel); 124 | System.out.println("OK: " + model.getnLayers()); 125 | 126 | // Carrega um arquivo de imagem tipo MNIST: 127 | BufferedImage imagem = ImageIO.read(new File(args[0])); 128 | byte[] pixels = ((DataBufferByte) imagem.getRaster().getDataBuffer()).getData(); 129 | double [] vetorImagem = new double[pixels.length]; 130 | for (int x=0;x 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /python_java_keras/java/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /python_java_keras/java/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | kerasmodel 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /python_java_keras/java/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java=UTF-8 3 | encoding//src/main/resources=UTF-8 4 | encoding//src/test/java=UTF-8 5 | encoding/=UTF-8 6 | -------------------------------------------------------------------------------- /python_java_keras/java/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 3 | org.eclipse.jdt.core.compiler.compliance=1.8 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.release=disabled 6 | org.eclipse.jdt.core.compiler.source=1.8 7 | -------------------------------------------------------------------------------- /python_java_keras/java/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /python_java_keras/java/dois.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cleuton/neuraljava/6e5c3f6da4fb43ecf13cc380b7e3bc8d4b371e3f/python_java_keras/java/dois.png -------------------------------------------------------------------------------- /python_java_keras/java/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.neuraljava.demos 6 | kerasmodel 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | kerasmodel 11 | http://maven.apache.org 12 | 13 | 14 | UTF-8 15 | 1.8 16 | 1.8 17 | 18 | 19 | 20 | 21 | junit 22 | junit 23 | 4.12 24 | test 25 | 26 | 27 | org.deeplearning4j 28 | deeplearning4j-core 29 | 1.0.0-beta4 30 | 31 | 32 | org.deeplearning4j 33 | deeplearning4j-modelimport 34 | 1.0.0-beta4 35 | 36 | 37 | org.nd4j 38 | nd4j-native-platform 39 | 1.0.0-beta4 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /python_java_keras/java/resultado.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cleuton/neuraljava/6e5c3f6da4fb43ecf13cc380b7e3bc8d4b371e3f/python_java_keras/java/resultado.png -------------------------------------------------------------------------------- /python_java_keras/java/src/main/java/com/neuraljava/demos/kerasmodel/RunModel.java: -------------------------------------------------------------------------------- 1 | package com.neuraljava.demos.kerasmodel; 2 | 3 | import java.awt.image.BufferedImage; 4 | import java.awt.image.DataBufferByte; 5 | import java.io.File; 6 | import java.io.IOException; 7 | 8 | import javax.imageio.ImageIO; 9 | 10 | import org.deeplearning4j.datasets.iterator.impl.MnistDataSetIterator; 11 | import org.deeplearning4j.datasets.mnist.MnistImageFile; 12 | import org.deeplearning4j.nn.conf.preprocessor.FeedForwardToCnnPreProcessor; 13 | import org.deeplearning4j.nn.modelimport.keras.KerasModelImport; 14 | import org.deeplearning4j.nn.modelimport.keras.exceptions.InvalidKerasConfigurationException; 15 | import org.deeplearning4j.nn.modelimport.keras.exceptions.UnsupportedKerasConfigurationException; 16 | import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; 17 | import org.nd4j.linalg.api.ndarray.INDArray; 18 | import org.nd4j.linalg.dataset.api.iterator.DataSetIterator; 19 | import org.nd4j.linalg.factory.Nd4j; 20 | import org.nd4j.linalg.io.ClassPathResource; 21 | 22 | public class RunModel { 23 | 24 | public static void main(String[] args) throws IOException, InvalidKerasConfigurationException, UnsupportedKerasConfigurationException { 25 | 26 | // Carrega o modelo pré-treinado: 27 | String txtModel = new ClassPathResource("meu_mnist.h5").getFile().getPath(); 28 | MultiLayerNetwork model = KerasModelImport.importKerasSequentialModelAndWeights(txtModel); 29 | System.out.println("OK: " + model.getnLayers()); 30 | 31 | // Carrega um arquivo de imagem tipo MNIST: 32 | BufferedImage imagem = ImageIO.read(new File(args[0])); 33 | byte[] pixels = ((DataBufferByte) imagem.getRaster().getDataBuffer()).getData(); 34 | double [] vetorImagem = new double[pixels.length]; 35 | for (int x=0;x 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /regressionmpg/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | regressionmpg 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /regressionmpg/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java=UTF-8 3 | encoding//src/test/java=UTF-8 4 | encoding/=UTF-8 5 | -------------------------------------------------------------------------------- /regressionmpg/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 3 | org.eclipse.jdt.core.compiler.compliance=1.5 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.release=disabled 6 | org.eclipse.jdt.core.compiler.source=1.5 7 | -------------------------------------------------------------------------------- /regressionmpg/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /regressionmpg/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.neuraljava.samples 6 | regressionmpg 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | regressionmpg 11 | http://maven.apache.org 12 | 13 | 14 | UTF-8 15 | 1.8 16 | 1.8 17 | 18 | 19 | 20 | 21 | junit 22 | junit 23 | 4.12 24 | test 25 | 26 | 27 | org.deeplearning4j 28 | deeplearning4j-core 29 | 1.0.0-beta4 30 | 31 | 32 | org.deeplearning4j 33 | deeplearning4j-modelimport 34 | 1.0.0-beta4 35 | 36 | 37 | org.nd4j 38 | nd4j-cuda-10.1 39 | 1.0.0-beta4 40 | 41 | 42 | org.deeplearning4j 43 | deeplearning4j-cuda-10.0 44 | 1.0.0-beta4 45 | 46 | 47 | org.deeplearning4j 48 | deeplearning4j-ui_2.10 49 | 1.0.0-beta4 50 | 51 | 52 | 53 | 54 | 55 | org.apache.maven.plugins 56 | maven-assembly-plugin 57 | 3.1.1 58 | 59 | 60 | jar-with-dependencies 61 | 62 | 63 | 64 | com.neuraljava.samples.regressionmpg.RegressionMPG 65 | 66 | 67 | 68 | 69 | 70 | make-assembly 71 | package 72 | 73 | single 74 | 75 | 76 | 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /regressionmpg/src/main/java/com/neuraljava/samples/regressionmpg/RegressionMPG.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2018 Cleuton Sampaio 4 | 5 | Licensed under the Apache License, Version 2.0 (the "License"); 6 | you may not use this file except in compliance with the License. 7 | You may obtain a copy of the License at 8 | 9 | http://www.apache.org/licenses/LICENSE-2.0 10 | 11 | Unless required by applicable law or agreed to in writing, software 12 | distributed under the License is distributed on an "AS IS" BASIS, 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | See the License for the specific language governing permissions and 15 | limitations under the License. 16 | 17 | Este trabalho é para demonstração de redes neurais e não tem objetivo 18 | de desempenho ou precisão. O Autor não se responsabiliza pelo seu uso e 19 | não fornecerá suporte. 20 | */ 21 | package com.neuraljava.samples.regressionmpg; 22 | 23 | import java.io.IOException; 24 | import java.util.Arrays; 25 | 26 | import org.datavec.api.records.Record; 27 | import org.datavec.api.records.reader.RecordReader; 28 | import org.datavec.api.records.reader.impl.csv.CSVRecordReader; 29 | import org.datavec.api.records.reader.impl.transform.TransformProcessRecordReader; 30 | import org.datavec.api.split.FileSplit; 31 | import org.datavec.api.transform.TransformProcess; 32 | import org.datavec.api.transform.condition.column.InvalidValueColumnCondition; 33 | import org.datavec.api.transform.schema.Schema; 34 | import org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator; 35 | import org.deeplearning4j.datasets.iterator.DataSetIteratorSplitter; 36 | import org.deeplearning4j.nn.conf.NeuralNetConfiguration; 37 | import org.deeplearning4j.nn.conf.layers.DenseLayer; 38 | import org.deeplearning4j.nn.conf.layers.OutputLayer; 39 | import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; 40 | import org.deeplearning4j.nn.weights.WeightInit; 41 | import org.deeplearning4j.optimize.listeners.ScoreIterationListener; 42 | import org.nd4j.evaluation.regression.RegressionEvaluation; 43 | import org.nd4j.linalg.activations.Activation; 44 | import org.nd4j.linalg.api.ndarray.INDArray; 45 | import org.nd4j.linalg.dataset.api.iterator.DataSetIterator; 46 | import org.nd4j.linalg.factory.Nd4j; 47 | import org.nd4j.linalg.io.ClassPathResource; 48 | import org.nd4j.linalg.learning.config.Adam; 49 | import org.nd4j.linalg.lossfunctions.LossFunctions; 50 | import java.util.logging.Logger; 51 | 52 | 53 | public class RegressionMPG { 54 | private static final Logger log = Logger.getLogger(RegressionMPG.class.getName()); 55 | private void modelBuild() throws IOException, InterruptedException { 56 | 57 | // Carrega o arquivo CSV diretamente do Resource: 58 | RecordReader rr = new CSVRecordReader(1,','); 59 | rr.initialize(new FileSplit(new ClassPathResource("auto-mpg.csv").getFile())); 60 | 61 | // Vamos criar o esquema para transformação do dataset: 62 | Schema schema = new Schema.Builder() 63 | .addColumnInteger("mpg") 64 | .addColumnCategorical("cylinder", Arrays.asList("3","4", "5","6","8")) 65 | .addColumnInteger("displacement") 66 | .addColumnInteger("horsepower") 67 | .addColumnInteger("weight") 68 | .addColumnFloat("acceleration") 69 | .addColumnCategorical("modelyear",Arrays.asList("70","71","72","73", 70 | "74","75","76","77","78","79","80","81","82")) 71 | .addColumnCategorical("origin",Arrays.asList("1","2","3")) 72 | .addColumnsString("carname") 73 | .build(); 74 | 75 | // Temos que eliminar valores inválidos na coluna "horsepower" e converter os valores categóricos: 76 | TransformProcess transformProcess = new TransformProcess.Builder(schema) 77 | .removeColumns("carname") 78 | .categoricalToOneHot("cylinder") 79 | .categoricalToOneHot("modelyear") 80 | .categoricalToOneHot("origin") 81 | .filter(new InvalidValueColumnCondition("horsepower")) 82 | .build(); 83 | 84 | RecordReader tRR = new TransformProcessRecordReader(rr,transformProcess); 85 | 86 | // Agora, vamos criar um Iterator e separar os dados de treino e teste (80% e 20%): 87 | DataSetIterator iter = new RecordReaderDataSetIterator.Builder(tRR, 8) 88 | .regression(0, 0) 89 | .build(); 90 | 91 | DataSetIteratorSplitter splitter = new DataSetIteratorSplitter(iter,49,0.8); 92 | DataSetIterator treino = splitter.getTrainIterator(); 93 | DataSetIterator test = splitter.getTestIterator(); 94 | 95 | // Criando a arquitetura da Rede: 96 | int seed = 124; 97 | int colunasInput = 25; // São 7, mas depois do "one-hot-encoding" viram 25 98 | int colunasOutput = 1; 99 | int nHidden1 = 10; 100 | int nHidden2 = 5; 101 | float learningRate = 0.01f; 102 | int epochs = 400; 103 | MultiLayerNetwork net = new MultiLayerNetwork(new NeuralNetConfiguration.Builder() 104 | .seed(seed) 105 | .weightInit(WeightInit.XAVIER) 106 | .updater(new Adam(learningRate)) 107 | .list() 108 | .layer(0, new DenseLayer.Builder().nIn(colunasInput).nOut(nHidden1) 109 | .activation(Activation.RELU) 110 | .build()) 111 | .layer(1, new DenseLayer.Builder().nIn(nHidden1).nOut(nHidden2) 112 | .activation(Activation.RELU) 113 | .build()) 114 | .layer(2, new OutputLayer.Builder(LossFunctions.LossFunction.MSE) 115 | .activation(Activation.IDENTITY) 116 | .nIn(nHidden2).nOut(colunasOutput).build()) 117 | .build() 118 | ); 119 | net.setListeners(new ScoreIterationListener(8)); 120 | 121 | // Treinamento da rede: 122 | for( int i=0; i 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /regressionmpgCPU/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | regressionmpg 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /regressionmpgCPU/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java=UTF-8 3 | encoding//src/main/resources=UTF-8 4 | encoding//src/test/java=UTF-8 5 | encoding/=UTF-8 6 | -------------------------------------------------------------------------------- /regressionmpgCPU/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 3 | org.eclipse.jdt.core.compiler.compliance=1.8 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.release=disabled 6 | org.eclipse.jdt.core.compiler.source=1.8 7 | -------------------------------------------------------------------------------- /regressionmpgCPU/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /regressionmpgCPU/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.neuraljava.samples 6 | regressionmpgcpu 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | regressionmpg 11 | http://maven.apache.org 12 | 13 | 14 | UTF-8 15 | 1.8 16 | 1.8 17 | 18 | 19 | 20 | 21 | junit 22 | junit 23 | 4.12 24 | test 25 | 26 | 27 | org.deeplearning4j 28 | deeplearning4j-core 29 | 1.0.0-beta4 30 | 31 | 32 | org.deeplearning4j 33 | deeplearning4j-modelimport 34 | 1.0.0-beta4 35 | 36 | 37 | org.nd4j 38 | nd4j-native 39 | 1.0.0-beta4 40 | 41 | 53 | 54 | org.deeplearning4j 55 | deeplearning4j-ui_2.10 56 | 1.0.0-beta4 57 | 58 | 59 | 60 | 61 | 62 | org.apache.maven.plugins 63 | maven-assembly-plugin 64 | 3.1.1 65 | 66 | 67 | jar-with-dependencies 68 | 69 | 70 | 71 | com.neuraljava.samples.regressionmpgcpu.RegressionMPG 72 | 73 | 74 | 75 | 76 | 77 | make-assembly 78 | package 79 | 80 | single 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /regressionmpgCPU/src/main/java/com/neuraljava/samples/regressionmpgcpu/RegressionMPG.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2018 Cleuton Sampaio 4 | 5 | Licensed under the Apache License, Version 2.0 (the "License"); 6 | you may not use this file except in compliance with the License. 7 | You may obtain a copy of the License at 8 | 9 | http://www.apache.org/licenses/LICENSE-2.0 10 | 11 | Unless required by applicable law or agreed to in writing, software 12 | distributed under the License is distributed on an "AS IS" BASIS, 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | See the License for the specific language governing permissions and 15 | limitations under the License. 16 | 17 | Este trabalho é para demonstração de redes neurais e não tem objetivo 18 | de desempenho ou precisão. O Autor não se responsabiliza pelo seu uso e 19 | não fornecerá suporte. 20 | */ 21 | package com.neuraljava.samples.regressionmpgcpu; 22 | 23 | import java.io.IOException; 24 | import java.util.Arrays; 25 | 26 | import org.datavec.api.records.Record; 27 | import org.datavec.api.records.reader.RecordReader; 28 | import org.datavec.api.records.reader.impl.csv.CSVRecordReader; 29 | import org.datavec.api.records.reader.impl.transform.TransformProcessRecordReader; 30 | import org.datavec.api.split.FileSplit; 31 | import org.datavec.api.transform.TransformProcess; 32 | import org.datavec.api.transform.condition.column.InvalidValueColumnCondition; 33 | import org.datavec.api.transform.schema.Schema; 34 | import org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator; 35 | import org.deeplearning4j.datasets.iterator.DataSetIteratorSplitter; 36 | import org.deeplearning4j.nn.conf.NeuralNetConfiguration; 37 | import org.deeplearning4j.nn.conf.layers.DenseLayer; 38 | import org.deeplearning4j.nn.conf.layers.OutputLayer; 39 | import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; 40 | import org.deeplearning4j.nn.weights.WeightInit; 41 | import org.deeplearning4j.optimize.listeners.ScoreIterationListener; 42 | import org.nd4j.evaluation.regression.RegressionEvaluation; 43 | import org.nd4j.linalg.activations.Activation; 44 | import org.nd4j.linalg.api.ndarray.INDArray; 45 | import org.nd4j.linalg.dataset.api.iterator.DataSetIterator; 46 | import org.nd4j.linalg.factory.Nd4j; 47 | import org.nd4j.linalg.io.ClassPathResource; 48 | import org.nd4j.linalg.learning.config.Adam; 49 | import org.nd4j.linalg.lossfunctions.LossFunctions; 50 | import java.util.logging.Logger; 51 | 52 | 53 | public class RegressionMPG { 54 | private static final Logger log = Logger.getLogger(RegressionMPG.class.getName()); 55 | private void modelBuild() throws IOException, InterruptedException { 56 | 57 | // Carrega o arquivo CSV diretamente do Resource: 58 | RecordReader rr = new CSVRecordReader(1,','); 59 | rr.initialize(new FileSplit(new ClassPathResource("auto-mpg.csv").getFile())); 60 | 61 | // Vamos criar o esquema para transformação do dataset: 62 | Schema schema = new Schema.Builder() 63 | .addColumnInteger("mpg") 64 | .addColumnCategorical("cylinder", Arrays.asList("3","4", "5","6","8")) 65 | .addColumnInteger("displacement") 66 | .addColumnInteger("horsepower") 67 | .addColumnInteger("weight") 68 | .addColumnFloat("acceleration") 69 | .addColumnCategorical("modelyear",Arrays.asList("70","71","72","73", 70 | "74","75","76","77","78","79","80","81","82")) 71 | .addColumnCategorical("origin",Arrays.asList("1","2","3")) 72 | .addColumnsString("carname") 73 | .build(); 74 | 75 | // Temos que eliminar valores inválidos na coluna "horsepower" e converter os valores categóricos: 76 | TransformProcess transformProcess = new TransformProcess.Builder(schema) 77 | .removeColumns("carname") 78 | .categoricalToOneHot("cylinder") 79 | .categoricalToOneHot("modelyear") 80 | .categoricalToOneHot("origin") 81 | .filter(new InvalidValueColumnCondition("horsepower")) 82 | .build(); 83 | 84 | RecordReader tRR = new TransformProcessRecordReader(rr,transformProcess); 85 | 86 | // Agora, vamos criar um Iterator e separar os dados de treino e teste (80% e 20%): 87 | DataSetIterator iter = new RecordReaderDataSetIterator.Builder(tRR, 8) 88 | .regression(0, 0) 89 | .build(); 90 | 91 | DataSetIteratorSplitter splitter = new DataSetIteratorSplitter(iter,49,0.8); 92 | DataSetIterator treino = splitter.getTrainIterator(); 93 | DataSetIterator test = splitter.getTestIterator(); 94 | 95 | // Criando a arquitetura da Rede: 96 | int seed = 124; 97 | int colunasInput = 25; // São 7, mas depois do "one-hot-encoding" viram 25 98 | int colunasOutput = 1; 99 | int nHidden1 = 10; 100 | int nHidden2 = 5; 101 | float learningRate = 0.01f; 102 | int epochs = 400; 103 | MultiLayerNetwork net = new MultiLayerNetwork(new NeuralNetConfiguration.Builder() 104 | .seed(seed) 105 | .weightInit(WeightInit.XAVIER) 106 | .updater(new Adam(learningRate)) 107 | .list() 108 | .layer(0, new DenseLayer.Builder().nIn(colunasInput).nOut(nHidden1) 109 | .activation(Activation.RELU) 110 | .build()) 111 | .layer(1, new DenseLayer.Builder().nIn(nHidden1).nOut(nHidden2) 112 | .activation(Activation.RELU) 113 | .build()) 114 | .layer(2, new OutputLayer.Builder(LossFunctions.LossFunction.MSE) 115 | .activation(Activation.IDENTITY) 116 | .nIn(nHidden2).nOut(colunasOutput).build()) 117 | .build() 118 | ); 119 | net.setListeners(new ScoreIterationListener(8)); 120 | 121 | // Treinamento da rede: 122 | for( int i=0; i