├── .github └── ISSUE_TEMPLATE │ └── bug_report.md ├── 01_Introduction_to_Deep_Learning_in_Java ├── 01_Determine_the_right_network_type_to_solve_the_problem │ └── README.md ├── 02_Determine_the_right_activation_function │ └── README.md ├── 03_Combat_overfitting_problems │ └── README.md ├── 04_Determine_the_right_batch_size_and_learning_rates │ └── README.md ├── 05_Configuring_Maven _for _DL4J │ └── README.md ├── 06_Configuring_DL4J_for_GPU accelerated environment │ └── README.md ├── 07_Troubleshooting_Installation_issues │ └── README.md └── README.md ├── 02_Data_Extraction_Transform_and_Loading ├── README.md ├── dataset.zip ├── irisdata.txt ├── logdata.zip ├── sourceCode │ └── cookbook-app │ │ ├── .gitignore │ │ ├── pom.xml │ │ ├── src │ │ └── main │ │ │ ├── java │ │ │ └── com │ │ │ │ └── javadeeplearningcookbook │ │ │ │ └── app │ │ │ │ ├── CollectionInputSplitExample.java │ │ │ │ ├── FileSplitExample.java │ │ │ │ ├── NormalizationExample.java │ │ │ │ ├── NumberedFileInputSplitExample.java │ │ │ │ ├── SerializationExample.java │ │ │ │ ├── TransformSplitExample.java │ │ │ │ ├── executorexamples │ │ │ │ └── LocalExecuteExample.java │ │ │ │ └── recordreaderexamples │ │ │ │ ├── CSVRecordReaderExample.java │ │ │ │ ├── CodecReaderExample.java │ │ │ │ ├── ImageRecordReaderExample.java │ │ │ │ ├── JacksonLineRecordReaderExample.java │ │ │ │ ├── RegexSequenceRecordReaderExample.java │ │ │ │ ├── SequenceRecordReaderExample.java │ │ │ │ └── TransformProcessRecordReaderExample.java │ │ │ └── resources │ │ │ ├── irisdata.txt │ │ │ └── titanic.csv │ │ └── temp │ │ ├── n02131653_124.JPEG │ │ ├── n02131653_170.JPEG │ │ ├── n02131653_175.JPEG │ │ ├── n02131653_176.png │ │ ├── n02131653_82.JPEG │ │ ├── n02131653_86.JPEG │ │ └── n02131653_87.png ├── titanic.csv └── transform-data.csv ├── 03_Building_Deep_Neural_Networks_for_Binary_classification ├── README.md └── sourceCode │ ├── cookbookapp │ ├── .gitignore │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── com │ │ │ └── javadeeplearningcookbook │ │ │ ├── api │ │ │ └── CustomerRetentionPredictionApi.java │ │ │ └── examples │ │ │ └── CustomerRetentionPredictionExample.java │ │ └── resources │ │ ├── Churn_Modelling.csv │ │ └── test.csv │ └── spring-dl4j │ ├── .gitignore │ ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties │ ├── model.zip │ ├── mvnw │ ├── mvnw.cmd │ ├── pom.xml │ └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── springdl4j │ │ │ └── springdl4j │ │ │ ├── SpringDl4jApplication.java │ │ │ ├── controller │ │ │ └── CookBookController.java │ │ │ └── service │ │ │ ├── CookBookService.java │ │ │ └── CookBookServiceImpl.java │ └── resources │ │ ├── application.properties │ │ ├── static │ │ └── main.css │ │ └── templates │ │ └── welcome.html │ └── test │ └── java │ └── com │ └── springdl4j │ └── springdl4j │ └── SpringDl4jApplicationTests.java ├── 04_Building_Convolutional_Neural_Networks ├── README.md ├── dataset.zip └── sourceCode │ ├── cookbookapp │ ├── .gitignore │ ├── cnntrainedmodel.zip │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── com │ │ │ └── javadeeplearningcookbook │ │ │ ├── api │ │ │ └── ImageClassifierAPI.java │ │ │ └── examples │ │ │ └── AnimalClassifier.java │ │ └── resources │ │ └── cnntrainedmodel.zip │ └── spring-dl4j │ ├── .gitignore │ ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties │ ├── model.zip │ ├── mvnw │ ├── mvnw.cmd │ ├── pom.xml │ └── src │ ├── .gitignore │ ├── main │ ├── java │ │ └── com │ │ │ └── springdl4j │ │ │ └── springdl4j │ │ │ ├── SpringDl4jApplication.java │ │ │ ├── controller │ │ │ └── CookBookController.java │ │ │ └── service │ │ │ ├── CookBookService.java │ │ │ └── CookBookServiceImpl.java │ └── resources │ │ ├── application.properties │ │ ├── cnntrainedmodel.zip │ │ ├── static │ │ └── main.css │ │ └── templates │ │ └── welcome.html │ └── test │ └── java │ └── com │ └── springdl4j │ └── springdl4j │ └── SpringDl4jApplicationTests.java ├── 05_Implementing_NLP ├── README.md └── sourceCode │ └── cookbookapp │ ├── .gitignore │ ├── pom.xml │ ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── javadeeplearningcookbook │ │ │ └── examples │ │ │ ├── BasicLineIteratorExample.java │ │ │ ├── CnnWord2VecSentenceClassificationExample.java │ │ │ ├── CollectionSentenceIteratorExample.java │ │ │ ├── FileSentenceIteratorExample.java │ │ │ ├── GoogleNewsVectorExample.java │ │ │ ├── LineSentenceIteratorExample.java │ │ │ ├── ParagraphVectorExample.java │ │ │ ├── SentenceDataPreProcessor.java │ │ │ ├── TSNEVisualizationExample.java │ │ │ ├── UimaSentenceIteratorExample.java │ │ │ └── Word2VecModelExample.java │ │ └── resources │ │ ├── files │ │ ├── sentences1.txt │ │ ├── sentences2.txt │ │ └── sentences3.txt │ │ ├── label │ │ ├── finance │ │ │ ├── f01.txt │ │ │ ├── f02.txt │ │ │ ├── f03.txt │ │ │ ├── f04.txt │ │ │ ├── f05.txt │ │ │ ├── f06.txt │ │ │ ├── f07.txt │ │ │ ├── f08.txt │ │ │ ├── f09.txt │ │ │ └── f10.txt │ │ ├── health │ │ │ ├── f01.txt │ │ │ ├── f02.txt │ │ │ ├── f03.txt │ │ │ ├── f04.txt │ │ │ ├── f05.txt │ │ │ ├── f06.txt │ │ │ ├── f07.txt │ │ │ ├── f08.txt │ │ │ ├── f09.txt │ │ │ └── f10.txt │ │ └── science │ │ │ ├── f01.txt │ │ │ ├── f02.txt │ │ │ ├── f03.txt │ │ │ ├── f04.txt │ │ │ ├── f05.txt │ │ │ ├── f06.txt │ │ │ ├── f07.txt │ │ │ ├── f08.txt │ │ │ ├── f09.txt │ │ │ └── f10.txt │ │ ├── raw_sentences.txt │ │ ├── raw_sentences_large.txt │ │ └── unlabeled │ │ ├── document1 │ │ └── f01.txt │ │ └── document2 │ │ └── f02.txt │ └── words.txt ├── 06_Constructing_LSTM_Network_for_time_series ├── README.md └── sourceCode │ └── cookbookapp-lstm-time-series │ ├── .gitignore │ ├── pom.xml │ └── src │ └── main │ └── java │ └── LstmTimeSeriesExample.java ├── 07_Constructing_LSTM_Neural_network_for_sequence_classification ├── README.md └── sourceCode │ └── cookbookapp │ ├── .gitignore │ ├── pom.xml │ └── src │ └── main │ └── java │ └── UciSequenceClassificationExample.java ├── 08_Performing_Anomaly_detection_on_unsupervised_data ├── README.md └── sourceCode │ └── cookbook-app │ ├── .gitignore │ ├── pom.xml │ └── src │ └── main │ └── java │ └── MnistAnomalyDetectionExample.java ├── 09_Using_RL4J_for_Reinforcement_learning ├── README.md └── sourceCode │ └── cookbookapp │ ├── .gitignore │ ├── pom.xml │ └── src │ └── main │ ├── java │ └── MalmoExample.java │ └── resources │ └── cliff_walking_rl4j.xml ├── 10_Developing_applications_in_distributed_environment ├── README.md └── sourceCode │ └── cookbookapp │ ├── .gitignore │ ├── pom.xml │ └── src │ └── main │ └── java │ └── com │ └── javacookbook │ └── app │ ├── PreProcessLocal.java │ ├── PreprocessSpark.java │ └── SparkExample.java ├── 11_Applying_Transfer_Learning_to_network_models ├── README.md └── sourceCode │ └── cookbookapp │ ├── .gitignore │ ├── pom.xml │ └── src │ └── main │ ├── java │ ├── DataSetIteratorHelper.java │ ├── SaveFeaturizedDataExample.java │ └── TransferLearnChurnExample.java │ └── resources │ └── Churn_Modelling.csv ├── 12_Benchmarking_and_Neural_Network_Optimization ├── README.md └── sourceCode │ └── cookbookapp │ ├── .gitignore │ ├── pom.xml │ └── src │ └── main │ ├── java │ ├── HyperParameterTuning.java │ └── HyperParameterTuningArbiterUiExample.java │ └── resources │ └── Churn_Modelling.csv ├── CODE_OF_CONDUCT.md ├── LICENSE └── README.md /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Desktop (please complete the following information):** 27 | - OS: [e.g. iOS] 28 | - Browser [e.g. chrome, safari] 29 | - Version [e.g. 22] 30 | 31 | **Smartphone (please complete the following information):** 32 | - Device: [e.g. iPhone6] 33 | - OS: [e.g. iOS8.1] 34 | - Browser [e.g. stock browser, safari] 35 | - Version [e.g. 22] 36 | 37 | **Additional context** 38 | Add any other context about the problem here. 39 | -------------------------------------------------------------------------------- /01_Introduction_to_Deep_Learning_in_Java/01_Determine_the_right_network_type_to_solve_the_problem/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Although there are multiple ways to perform a single task, here are few of the commonly used network architectures for the mentioned use-cases: 5 | 6 | | Problem | Core Architecture | 7 | |--|--| 8 | | Image Classification | CNN | 9 | | Anomaly Detection | Autoencoder | 10 | | Time Series classification | RNN/LSTM/Computation graph 11 | | Prediction problems on sequence data | RNN/LSTM 12 | | Recommender Systems | RL 13 | 14 | 15 | Note that, the optimal architectural decision can vary upon the type of data dealt with and whether it is supervised/unsupervised. 16 | 17 | - For prediction problems with simple CSV data (non-sequential), MLP(Multilayer perceptron) would be just enough and will give best results compared to other complex architectures. MLP is nothing but the deep neural net with an input layer and output layer and multiple hidden layers in between these two layers. Hidden layers receive input from the input layer and output layers receive input from hidden layers. If there are multiple hidden layers, each layer will take inputs from preceding hidden layer. 18 | 19 | - Time series data or anything that involves sequential data is going to need a RNN or LSTM. RNN is optimal to handle sequential data. If we want to track long term dependencies in the data, an LSTM might be the best option. LSTM is a variant from RNN with a memory unit which is capable to hold long term dependencies. 20 | - Anomaly detection problems involve feature analysis of each and every sample. We dont need to have labels here. We basically try to encode the data and decode it back to see the outliers in the feature. An autoencoder will be perfect fit for this purpose and lot of better variants like VAE (Variational autoencoder) are possible to construct using DL4J. 21 | - DL4J have its on subsidiary library for reinforcement learning called RL4J. Recommender systems use reinforcement learning algorithms to solve recommendation problems. We can also feed the data in image/video/text format to a feed forward network/CNN and then generate classified actions. That is to chose the policy upon given action. 22 | 23 | -------------------------------------------------------------------------------- /01_Introduction_to_Deep_Learning_in_Java/02_Determine_the_right_activation_function/README.md: -------------------------------------------------------------------------------- 1 | 2 | Note that there are no implied rules about using an activation function at different layers. It all depends on your data and what you want to do from it. The only purpose of an activation function is to bring non-linearity in the network. 3 | 4 | | Constraint | Activation function | 5 | |--|--| 6 | | Hidden layers| ReLU | 7 | | Output layer (classification) | Sigmoid | 8 | | Output layer (non-classification problems) | Linear 9 | | 10 | 11 | 12 | -------------------------------------------------------------------------------- /01_Introduction_to_Deep_Learning_in_Java/03_Combat_overfitting_problems/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ### Here are the most common strategies used to combat over-fitting: 4 | 1) Dropouts 5 | 2) L1/L2 Regularization 6 | 3) Gather more data for training. 7 | 4) Create more samples using data augmentation and train your network on top of it. 8 | 5) Go for simple network architecture. 9 | -------------------------------------------------------------------------------- /01_Introduction_to_Deep_Learning_in_Java/04_Determine_the_right_batch_size_and_learning_rates/README.md: -------------------------------------------------------------------------------- 1 | 2 | ### Lesson: 3 | No batch size or learning rate that works well for all neural network models. 4 | 5 | ### What can be done ? 6 | Understand the data, Run multiple training sessions, Check evaluation metrics, experiment, experiment and experiment!! 7 | -------------------------------------------------------------------------------- /01_Introduction_to_Deep_Learning_in_Java/05_Configuring_Maven _for _DL4J/README.md: -------------------------------------------------------------------------------- 1 | Add below required maven dependencies: 2 | 3 | #### DL4J core: 4 | 5 | 6 | org.deeplearning4j 7 | deeplearning4j-core 8 | 1.0.0-beta3 9 | 10 | 11 | #### ND4J: 12 | 13 | 14 | org.nd4j 15 | nd4j-native-platform 16 | 1.0.0-beta3 17 | 18 | 19 | 20 | 21 | 22 | ### More details in cookbook. 23 | 24 | -------------------------------------------------------------------------------- /01_Introduction_to_Deep_Learning_in_Java/06_Configuring_DL4J_for_GPU accelerated environment/README.md: -------------------------------------------------------------------------------- 1 | 2 | ### DL4J dependencies for GPU powered hardware 3 | 4 | ND4J dependency for CUDA: 5 | 6 | 7 | org.nd4j 8 | nd4j-cuda-9.2 9 | 1.0.0-beta3 10 | 11 | 12 | DL4J CUDA dependency: 13 | 14 | 15 | org.deeplearning4j 16 | deeplearning4j-cuda-9.2 17 | 1.0.0-beta3 18 | 19 | -------------------------------------------------------------------------------- /01_Introduction_to_Deep_Learning_in_Java/07_Troubleshooting_Installation_issues/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | - For GPU hardware, make sure necessary configuration in place. Verify components (such as CUDA) are properly installed and configured. 4 | - Make sure necessary dependencies are downloaded and present in your local maven repository. 5 | - Make sure logging is enabled and configured to capture any runtime errors due to corrupted installations. 6 | 7 | -------------------------------------------------------------------------------- /01_Introduction_to_Deep_Learning_in_Java/README.md: -------------------------------------------------------------------------------- 1 | ## Chapter 1 : Introduction to Deep Learning in Java 2 | 3 | We will discuss about DL4J as a distinct deep learning solution and the significance of Java deep learning library. We will also showcase required deep learning concepts in a recipe-based approach. 4 | 5 | In this chapter, we have the following recipes: 6 | 7 | - [Determine the right network type to solve the problem](https://github.com/rahul-raj/Java-Deep-Learning-Cookbook/tree/master/01_Introduction_to_Deep_Learning_in_Java/01_Determine_the_right_network_type_to_solve_the_problem "Determine the right network type to solve the problem") 8 | - [Determine the right activation function](https://github.com/rahul-raj/Java-Deep-Learning-Cookbook/tree/master/01_Introduction_to_Deep_Learning_in_Java/02_Determine_the_right_activation_function "Determine the right activation function") 9 | - [Combat overfitting problems](https://github.com/rahul-raj/Java-Deep-Learning-Cookbook/tree/master/01_Introduction_to_Deep_Learning_in_Java/03_Combat_overfitting_problems "Combat overfitting problems") 10 | - [Determine the right batch size and learning rates](https://github.com/rahul-raj/Java-Deep-Learning-Cookbook/tree/master/01_Introduction_to_Deep_Learning_in_Java/04_Determine_the_right_batch_size_and_learning_rates "Determine the right batch size and learning rates") 11 | - [Configuring Maven for DL4J](https://github.com/rahul-raj/Java-Deep-Learning-Cookbook/tree/master/01_Introduction_to_Deep_Learning_in_Java/05_Configuring_Maven%20_for%20_DL4J "Configuring Maven for DL4J") 12 | - [Configuring DL4J for GPU accelerated environment](https://github.com/rahul-raj/Java-Deep-Learning-Cookbook/tree/master/01_Introduction_to_Deep_Learning_in_Java/06_Configuring_DL4J_for_GPU%20accelerated%20environment "Configuring DL4J for GPU accelerated environment") 13 | - [Troubleshooting Installation issues](https://github.com/rahul-raj/Java-Deep-Learning-Cookbook/tree/master/01_Introduction_to_Deep_Learning_in_Java/07_Troubleshooting_Installation_issues "Troubleshooting Installation issues") 14 | -------------------------------------------------------------------------------- /02_Data_Extraction_Transform_and_Loading/README.md: -------------------------------------------------------------------------------- 1 | 2 | ### Chapter 2 : Data Extraction, Transform and Loading 3 |   4 |   5 |   6 | 7 | #### Instructions 8 | 9 | Navigate to **sourceCode/cookbook-app** directory and import **pom.xml** 10 | 11 | There are multiple **DataVec** examples in this project demonstrating **DataVec** features 12 | -------------------------------------------------------------------------------- /02_Data_Extraction_Transform_and_Loading/dataset.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahul-raj/Java-Deep-Learning-Cookbook/ba8dbb3a174cfe7e02a46ef5f222cb22254df199/02_Data_Extraction_Transform_and_Loading/dataset.zip -------------------------------------------------------------------------------- /02_Data_Extraction_Transform_and_Loading/logdata.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahul-raj/Java-Deep-Learning-Cookbook/ba8dbb3a174cfe7e02a46ef5f222cb22254df199/02_Data_Extraction_Transform_and_Loading/logdata.zip -------------------------------------------------------------------------------- /02_Data_Extraction_Transform_and_Loading/sourceCode/cookbook-app/.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | target 3 | cookbook-app.iml 4 | cookbook-app.iws 5 | cookbook-app.ipr -------------------------------------------------------------------------------- /02_Data_Extraction_Transform_and_Loading/sourceCode/cookbook-app/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | com.javadeeplearningcookbook.app 8 | cookbook-app 9 | 1.0-SNAPSHOT 10 | 11 | cookbook-app 12 | 13 | http://www.packtpub.com 14 | 15 | 16 | UTF-8 17 | 1.8 18 | 1.8 19 | 20 | 21 | 22 | 23 | junit 24 | junit 25 | 4.11 26 | test 27 | 28 | 29 | org.deeplearning4j 30 | deeplearning4j-core 31 | 1.0.0-beta3 32 | 33 | 34 | org.nd4j 35 | nd4j-native-platform 36 | 1.0.0-beta3 37 | 38 | 39 | org.datavec 40 | datavec-api 41 | 1.0.0-beta3 42 | 43 | 44 | 45 | org.datavec 46 | datavec-data-codec 47 | 1.0.0-beta3 48 | 49 | 54 | 55 | org.bytedeco 56 | javacv-platform 57 | 1.4.4 58 | 59 | 60 | org.bytedeco 61 | javacpp 62 | 1.4.4 63 | 64 | 65 | org.slf4j 66 | slf4j-simple 67 | 1.7.25 68 | 69 | 70 | 71 | org.datavec 72 | datavec-local 73 | 1.0.0-beta3 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | maven-clean-plugin 83 | 3.1.0 84 | 85 | 86 | 87 | maven-resources-plugin 88 | 3.0.2 89 | 90 | 91 | maven-compiler-plugin 92 | 3.8.0 93 | 94 | 95 | maven-surefire-plugin 96 | 2.22.1 97 | 98 | 99 | maven-jar-plugin 100 | 3.0.2 101 | 102 | 103 | maven-install-plugin 104 | 2.5.2 105 | 106 | 107 | maven-deploy-plugin 108 | 2.8.2 109 | 110 | 111 | 112 | maven-site-plugin 113 | 3.7.1 114 | 115 | 116 | maven-project-info-reports-plugin 117 | 3.0.0 118 | 119 | 120 | 121 | 122 | 123 | -------------------------------------------------------------------------------- /02_Data_Extraction_Transform_and_Loading/sourceCode/cookbook-app/src/main/java/com/javadeeplearningcookbook/app/CollectionInputSplitExample.java: -------------------------------------------------------------------------------- 1 | package com.javadeeplearningcookbook.app; 2 | 3 | import org.datavec.api.split.CollectionInputSplit; 4 | import org.datavec.api.split.FileSplit; 5 | 6 | import java.io.File; 7 | 8 | public class CollectionInputSplitExample { 9 | public static void main(String[] args) { 10 | FileSplit fileSplit = new FileSplit(new File("temp")); 11 | CollectionInputSplit collectionInputSplit = new CollectionInputSplit(fileSplit.locations()); 12 | collectionInputSplit.locationsIterator().forEachRemaining(System.out::println); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /02_Data_Extraction_Transform_and_Loading/sourceCode/cookbook-app/src/main/java/com/javadeeplearningcookbook/app/FileSplitExample.java: -------------------------------------------------------------------------------- 1 | package com.javadeeplearningcookbook.app; 2 | 3 | import org.datavec.api.split.FileSplit; 4 | 5 | import java.io.File; 6 | 7 | public class FileSplitExample { 8 | public static void main(String[] args) { 9 | String[] allowedFormats=new String[]{".JPEG"}; 10 | //recursive -> true, so that it will check for all subdirectories 11 | FileSplit fileSplit = new FileSplit(new File("temp"),allowedFormats,true); 12 | fileSplit.locationsIterator().forEachRemaining(System.out::println); 13 | 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /02_Data_Extraction_Transform_and_Loading/sourceCode/cookbook-app/src/main/java/com/javadeeplearningcookbook/app/NormalizationExample.java: -------------------------------------------------------------------------------- 1 | package com.javadeeplearningcookbook.app; 2 | 3 | import org.datavec.api.records.reader.RecordReader; 4 | import org.datavec.api.records.reader.impl.csv.CSVRecordReader; 5 | import org.datavec.api.records.reader.impl.transform.TransformProcessRecordReader; 6 | import org.datavec.api.split.FileSplit; 7 | import org.datavec.api.transform.TransformProcess; 8 | import org.datavec.api.transform.schema.Schema; 9 | import org.datavec.api.transform.transform.doubletransform.ConvertToDouble; 10 | import org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator; 11 | import org.nd4j.linalg.dataset.api.iterator.DataSetIterator; 12 | import org.nd4j.linalg.dataset.api.preprocessor.DataNormalization; 13 | import org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize; 14 | 15 | import java.io.File; 16 | import java.io.IOException; 17 | import java.util.Arrays; 18 | 19 | public class NormalizationExample { 20 | public static void main(String[] args) throws IOException, InterruptedException { 21 | try { 22 | Schema schema = new Schema.Builder() 23 | .addColumnsString("Name", "Subject") 24 | .addColumnInteger("Score") 25 | .addColumnCategorical("Grade", Arrays.asList("A","B","C","D")) 26 | .addColumnInteger("Passed").build(); 27 | 28 | TransformProcess transformProcess = new TransformProcess.Builder(schema) 29 | .removeColumns("Name","Subject") 30 | .transform(new ConvertToDouble("Score")) 31 | .categoricalToInteger("Grade").build(); 32 | RecordReader recordReader = new CSVRecordReader(1, ','); 33 | recordReader.initialize(new FileSplit(new File("Path/to/transform-data.csv"))); 34 | RecordReader transformRecordReader = new TransformProcessRecordReader(recordReader,transformProcess); 35 | DataSetIterator iterator = new RecordReaderDataSetIterator(transformRecordReader,2); 36 | System.out.println("Before Applying Normalization"); 37 | System.out.println(iterator.next().getFeatures()); 38 | iterator.reset(); 39 | DataNormalization normalization = new NormalizerStandardize(); 40 | normalization.fit(iterator); 41 | iterator.setPreProcessor(normalization); 42 | System.out.println("After Applying Normalization"); 43 | System.out.println(iterator.next().getFeatures()); 44 | 45 | 46 | 47 | } catch(IllegalArgumentException e){ 48 | System.out.println("Please provide proper directory path to transform-data.csv in place of: Path/to/transform-data.csv"); 49 | } catch (IOException e) { 50 | e.printStackTrace(); 51 | } catch (InterruptedException e) { 52 | e.printStackTrace(); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /02_Data_Extraction_Transform_and_Loading/sourceCode/cookbook-app/src/main/java/com/javadeeplearningcookbook/app/NumberedFileInputSplitExample.java: -------------------------------------------------------------------------------- 1 | package com.javadeeplearningcookbook.app; 2 | 3 | import org.datavec.api.split.NumberedFileInputSplit; 4 | 5 | public class NumberedFileInputSplitExample { 6 | public static void main(String[] args) { 7 | NumberedFileInputSplit numberedFileInputSplit = new NumberedFileInputSplit("numberedfiles/file%d.txt",1,4); 8 | numberedFileInputSplit.locationsIterator().forEachRemaining(System.out::println); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /02_Data_Extraction_Transform_and_Loading/sourceCode/cookbook-app/src/main/java/com/javadeeplearningcookbook/app/SerializationExample.java: -------------------------------------------------------------------------------- 1 | package com.javadeeplearningcookbook.app; 2 | 3 | import org.datavec.api.transform.TransformProcess; 4 | import org.datavec.api.transform.schema.Schema; 5 | import org.datavec.api.transform.transform.doubletransform.ConvertToDouble; 6 | 7 | import java.util.Arrays; 8 | 9 | public class SerializationExample { 10 | public static void main(String[] args) { 11 | Schema schema = new Schema.Builder() 12 | .addColumnsString("Name", "Subject") 13 | .addColumnInteger("Score") 14 | .addColumnCategorical("Grade", Arrays.asList("A","B","C","D")) 15 | .addColumnInteger("Passed").build(); 16 | 17 | TransformProcess transformProcess = new TransformProcess.Builder(schema) 18 | .removeColumns("Name") 19 | .transform(new ConvertToDouble("Score")) 20 | .categoricalToInteger("Grade").build(); 21 | 22 | String json = transformProcess.toJson(); 23 | System.out.println(json); 24 | 25 | String yaml = transformProcess.toYaml(); 26 | System.out.println(yaml); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /02_Data_Extraction_Transform_and_Loading/sourceCode/cookbook-app/src/main/java/com/javadeeplearningcookbook/app/TransformSplitExample.java: -------------------------------------------------------------------------------- 1 | package com.javadeeplearningcookbook.app; 2 | 3 | import org.datavec.api.split.CollectionInputSplit; 4 | import org.datavec.api.split.TransformSplit; 5 | 6 | import java.net.URI; 7 | import java.net.URISyntaxException; 8 | import java.util.Arrays; 9 | import java.util.List; 10 | 11 | public class TransformSplitExample { 12 | public static void main(String[] args) throws URISyntaxException { 13 | TransformSplit.URITransform uriTransform = URI::normalize; 14 | List uriList = Arrays.asList(new URI("file://storage/examples/./cats.txt"), 15 | new URI("file://storage/examples//dogs.txt"), 16 | new URI("file://storage/./examples/bear.txt")); 17 | TransformSplit transformSplit = new TransformSplit(new CollectionInputSplit(uriList),uriTransform); 18 | transformSplit.locationsIterator().forEachRemaining(System.out::println); 19 | 20 | //search and replace example 21 | List uriReplaceList = Arrays.asList(new URI("file://storage/examples/0/inputs.txt"), 22 | new URI("file://storage/examples/1/inputs.txt"), 23 | new URI("file://storage/examples/2/inputs.txt")); 24 | TransformSplit transformReplaceSplit = TransformSplit.ofSearchReplace( 25 | new CollectionInputSplit(uriReplaceList), 26 | "inputs","outputs"); 27 | 28 | transformReplaceSplit.locationsIterator().forEachRemaining(System.out::println); 29 | 30 | 31 | 32 | 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /02_Data_Extraction_Transform_and_Loading/sourceCode/cookbook-app/src/main/java/com/javadeeplearningcookbook/app/executorexamples/LocalExecuteExample.java: -------------------------------------------------------------------------------- 1 | package com.javadeeplearningcookbook.app.executorexamples; 2 | 3 | import org.datavec.api.records.reader.RecordReader; 4 | import org.datavec.api.records.reader.impl.csv.CSVRecordReader; 5 | import org.datavec.api.records.writer.RecordWriter; 6 | import org.datavec.api.records.writer.impl.csv.CSVRecordWriter; 7 | import org.datavec.api.split.FileSplit; 8 | import org.datavec.api.split.partition.NumberOfRecordsPartitioner; 9 | import org.datavec.api.split.partition.Partitioner; 10 | import org.datavec.api.transform.TransformProcess; 11 | import org.datavec.api.transform.schema.Schema; 12 | import org.datavec.api.writable.Writable; 13 | import org.datavec.local.transforms.LocalTransformExecutor; 14 | 15 | import java.io.File; 16 | import java.util.ArrayList; 17 | import java.util.Arrays; 18 | import java.util.List; 19 | 20 | public class LocalExecuteExample { 21 | public static void main(String[] args) throws Exception { 22 | try { 23 | int numClasses = 2; 24 | int batchSize = 8; 25 | 26 | File file = new File("Path/to/titanic.csv-file"); 27 | RecordReader recordReader = new CSVRecordReader(1,','); 28 | recordReader.initialize(new FileSplit(file)); 29 | // WritableConverter writableConverter = new SelfWritableConverter(); 30 | 31 | Schema schema = new Schema.Builder() 32 | .addColumnInteger("Survived") 33 | .addColumnCategorical("Pclass", Arrays.asList("1","2","3")) 34 | .addColumnString("Name") 35 | .addColumnCategorical("Sex", Arrays.asList("male","female")) 36 | .addColumnsInteger("Age","Siblings/Spouses Aboard","Parents/Children Aboard") 37 | .addColumnDouble("Fare") 38 | .build(); 39 | TransformProcess transformProcess = new TransformProcess.Builder(schema) 40 | .removeColumns("Name","Fare") 41 | .categoricalToInteger("Sex") 42 | .categoricalToOneHot("Pclass") 43 | .removeColumns("Pclass[1]") 44 | .build(); 45 | 46 | List> outputData = new ArrayList<>(); 47 | 48 | RecordWriter recordWriter = new CSVRecordWriter(); 49 | Partitioner partitioner = new NumberOfRecordsPartitioner(); 50 | recordWriter.initialize(new FileSplit(new File("/Path/To/LocalExecuteExample.csv/file")),partitioner); 51 | 52 | while(recordReader.hasNext()){ 53 | outputData.add(recordReader.next()); 54 | } 55 | List> transformedOutput=LocalTransformExecutor.execute(outputData,transformProcess); 56 | recordWriter.writeBatch(transformedOutput); 57 | recordWriter.close(); 58 | } catch (IllegalArgumentException e) { 59 | System.out.println("Please provide proper file paths for titanic.csv & fle in place of: Path/to/titanic.csv-file && /Path/To/LocalExecuteExample.csv"); 60 | System.out.println("You need to create an empty CSV file and mention the file path in place of /Path/To/LocalExecuteExample.csv"); 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /02_Data_Extraction_Transform_and_Loading/sourceCode/cookbook-app/src/main/java/com/javadeeplearningcookbook/app/recordreaderexamples/CSVRecordReaderExample.java: -------------------------------------------------------------------------------- 1 | package com.javadeeplearningcookbook.app.recordreaderexamples; 2 | 3 | import org.datavec.api.records.reader.RecordReader; 4 | import org.datavec.api.records.reader.impl.csv.CSVRecordReader; 5 | import org.datavec.api.records.reader.impl.transform.TransformProcessRecordReader; 6 | import org.datavec.api.split.FileSplit; 7 | import org.datavec.api.transform.TransformProcess; 8 | import org.datavec.api.transform.schema.Schema; 9 | import org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator; 10 | import org.nd4j.linalg.dataset.api.iterator.DataSetIterator; 11 | 12 | import java.io.File; 13 | import java.io.IOException; 14 | import java.util.Arrays; 15 | 16 | /** 17 | * @Author Rahul Raj 18 | * DataVec Example 19 | * 20 | */ 21 | public class CSVRecordReaderExample 22 | { 23 | public static void main( String[] args ){ 24 | 25 | try { 26 | int numClasses = 2; 27 | int batchSize = 8; 28 | 29 | File file = new File("Path/to/titanic.csv-file"); 30 | RecordReader recordReader = new CSVRecordReader(1,','); 31 | recordReader.initialize(new FileSplit(file)); 32 | // WritableConverter writableConverter = new SelfWritableConverter(); 33 | 34 | Schema schema = new Schema.Builder() 35 | .addColumnInteger("Survived") 36 | .addColumnCategorical("Pclass", Arrays.asList("1","2","3")) 37 | .addColumnString("Name") 38 | .addColumnCategorical("Sex", Arrays.asList("male","female")) 39 | .addColumnsInteger("Age","Siblings/Spouses Aboard","Parents/Children Aboard") 40 | .addColumnDouble("Fare") 41 | .build(); 42 | TransformProcess transformProcess = new TransformProcess.Builder(schema) 43 | .removeColumns("Name","Fare") 44 | .categoricalToInteger("Sex") 45 | .categoricalToOneHot("Pclass") 46 | .removeColumns("Pclass[1]") 47 | .build(); 48 | 49 | RecordReader transformProcessRecordReader = new TransformProcessRecordReader(recordReader,transformProcess); 50 | //DataSetIterator dataSetIterator = new RecordReaderDataSetIterator(transformProcessRecordReader,writableConverter,8,1,7,2,-1,true); 51 | DataSetIterator dataSetIterator = new RecordReaderDataSetIterator.Builder(transformProcessRecordReader,batchSize) 52 | .classification(0,numClasses) 53 | .build(); 54 | System.out.println("Total number of possible labels = [" + dataSetIterator.totalOutcomes()+ "]"); 55 | } catch(IllegalArgumentException e){ 56 | System.out.println("Please provide proper file path for titanic.csv fle in place of: Path/to/titanic.csv-file"); 57 | }catch (IOException e) { 58 | e.printStackTrace(); 59 | } catch (InterruptedException e) { 60 | e.printStackTrace(); 61 | } 62 | 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /02_Data_Extraction_Transform_and_Loading/sourceCode/cookbook-app/src/main/java/com/javadeeplearningcookbook/app/recordreaderexamples/CodecReaderExample.java: -------------------------------------------------------------------------------- 1 | package com.javadeeplearningcookbook.app.recordreaderexamples; 2 | 3 | import org.datavec.api.conf.Configuration; 4 | import org.datavec.api.records.reader.SequenceRecordReader; 5 | import org.datavec.api.split.FileSplit; 6 | import org.datavec.api.writable.Writable; 7 | import org.datavec.codec.reader.CodecRecordReader; 8 | import org.datavec.codec.reader.NativeCodecRecordReader; 9 | 10 | import java.io.File; 11 | import java.io.IOException; 12 | import java.util.List; 13 | 14 | public class CodecReaderExample { 15 | public static void main(String[] args){ 16 | try { 17 | SequenceRecordReader codecRecordReader = new NativeCodecRecordReader(); 18 | Configuration conf = new Configuration(); 19 | conf.set(CodecRecordReader.RAVEL, "true"); 20 | conf.set(CodecRecordReader.START_FRAME, "2"); 21 | conf.set(CodecRecordReader.TOTAL_FRAMES, "10"); 22 | conf.set(CodecRecordReader.ROWS, "80"); 23 | conf.set(CodecRecordReader.COLUMNS, "46"); 24 | conf.set(CodecRecordReader.VIDEO_DURATION,"30"); 25 | //Replace with the video file path in your local drive 26 | codecRecordReader.initialize(new FileSplit(new File("/Path/to/video-file"))); 27 | codecRecordReader.setConf(conf); 28 | List> list = codecRecordReader.sequenceRecord(); 29 | list.listIterator().forEachRemaining(el->System.out.println(el.size())); 30 | } catch(IllegalArgumentException e) { 31 | System.out.println("Please provide proper video file path in place of: /Path/to/video-file "); 32 | } catch (IOException e) { 33 | e.printStackTrace(); 34 | } catch (InterruptedException e) { 35 | e.printStackTrace(); 36 | } 37 | 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /02_Data_Extraction_Transform_and_Loading/sourceCode/cookbook-app/src/main/java/com/javadeeplearningcookbook/app/recordreaderexamples/ImageRecordReaderExample.java: -------------------------------------------------------------------------------- 1 | package com.javadeeplearningcookbook.app.recordreaderexamples; 2 | 3 | import org.datavec.api.io.filters.BalancedPathFilter; 4 | import org.datavec.api.io.labels.ParentPathLabelGenerator; 5 | import org.datavec.api.split.FileSplit; 6 | import org.datavec.api.split.InputSplit; 7 | import org.datavec.image.loader.NativeImageLoader; 8 | import org.datavec.image.recordreader.ImageRecordReader; 9 | import org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator; 10 | import org.nd4j.linalg.dataset.api.DataSet; 11 | import org.nd4j.linalg.dataset.api.iterator.DataSetIterator; 12 | 13 | import java.io.File; 14 | import java.io.IOException; 15 | import java.util.ArrayList; 16 | import java.util.List; 17 | import java.util.Random; 18 | 19 | public class ImageRecordReaderExample { 20 | public static void main(String[] args){ 21 | 22 | /* 23 | * Note: 24 | * Download the image datasets from imagenet for 'n' different labels, 25 | * create 'n' different sub directories and place it under application root. 26 | * This outputs the number of possible outcomes of the model, which is nothing but the existing number of labels. 27 | * This is a simple example to check whether your data is properly extracted from source. 28 | * 29 | * */ 30 | try { 31 | FileSplit fileSplit = new FileSplit(new File("Path/to/image-files"),NativeImageLoader.ALLOWED_FORMATS,new Random(123)); 32 | int numLabels = fileSplit.getRootDir().listFiles(File::isDirectory).length; 33 | 34 | ParentPathLabelGenerator parentPathLabelGenerator = new ParentPathLabelGenerator(); 35 | BalancedPathFilter balancedPathFilter = new BalancedPathFilter( 36 | new Random(123), 37 | NativeImageLoader.ALLOWED_FORMATS, 38 | parentPathLabelGenerator 39 | ); 40 | 41 | InputSplit[] inputSplits = fileSplit.sample(balancedPathFilter,85,15); 42 | InputSplit trainData = inputSplits[0]; 43 | //InputSplit testData = inputSplits[1]; 44 | 45 | ImageRecordReader imageRecordReader = new ImageRecordReader(30,30,3, 46 | parentPathLabelGenerator 47 | ); 48 | imageRecordReader.initialize(trainData,null); 49 | 50 | DataSetIterator dataSetIterator = new RecordReaderDataSetIterator(imageRecordReader,4,1,numLabels); 51 | System.out.println(dataSetIterator.totalOutcomes()); 52 | } catch(IllegalArgumentException e){ 53 | System.out.println("Please provide proper image directory path in place of: Path/to/image-files "); 54 | System.out.println("For more details, please refer to the instructions listed in comment section"); 55 | } catch (IOException e) { 56 | e.printStackTrace(); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /02_Data_Extraction_Transform_and_Loading/sourceCode/cookbook-app/src/main/java/com/javadeeplearningcookbook/app/recordreaderexamples/JacksonLineRecordReaderExample.java: -------------------------------------------------------------------------------- 1 | package com.javadeeplearningcookbook.app.recordreaderexamples; 2 | 3 | import org.datavec.api.conf.Configuration; 4 | import org.datavec.api.records.reader.impl.jackson.FieldSelection; 5 | import org.datavec.api.records.reader.impl.jackson.JacksonLineRecordReader; 6 | import org.datavec.api.records.reader.impl.transform.TransformProcessRecordReader; 7 | import org.datavec.api.split.FileSplit; 8 | import org.datavec.api.transform.TransformProcess; 9 | import org.datavec.api.transform.schema.Schema; 10 | import org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator; 11 | import org.nd4j.linalg.dataset.api.iterator.DataSetIterator; 12 | import org.nd4j.shade.jackson.core.JsonFactory; 13 | import org.nd4j.shade.jackson.databind.ObjectMapper; 14 | 15 | import java.io.File; 16 | import java.io.IOException; 17 | import java.util.Arrays; 18 | 19 | public class JacksonLineRecordReaderExample { 20 | public static void main(String[] args){ 21 | 22 | try { 23 | Schema schema = new Schema.Builder() 24 | .addColumnsDouble("sepalLength","sepalWidth","petalLength","petalWidth") 25 | .addColumnCategorical("species", Arrays.asList("setosa","versicolor","virginica")) 26 | .build(); 27 | TransformProcess transformProcess = new TransformProcess.Builder(schema) 28 | .categoricalToInteger("species") 29 | .build(); 30 | 31 | FieldSelection fieldSelection = new FieldSelection.Builder() 32 | .addField("sepalLength") 33 | .addField("sepalWidth") 34 | .addField("petalLength") 35 | .addField("petalWidth") 36 | .addField("species") 37 | .build(); 38 | 39 | JacksonLineRecordReader jacksonLineRecordReader = new JacksonLineRecordReader(fieldSelection,new ObjectMapper(new JsonFactory())); 40 | Configuration configuration = new Configuration(); 41 | configuration.set(JacksonLineRecordReader.LABELS,"species"); 42 | jacksonLineRecordReader.initialize(new FileSplit(new File("Path/to/Iris-data"))); 43 | TransformProcessRecordReader recordReader = new TransformProcessRecordReader(jacksonLineRecordReader,transformProcess); 44 | System.out.println(jacksonLineRecordReader.next()); 45 | DataSetIterator dataSetIterator = new RecordReaderDataSetIterator(recordReader,5,-1,3); 46 | System.out.println(dataSetIterator.totalOutcomes()); 47 | } catch(IllegalArgumentException e){ 48 | System.out.println("Please provide proper file path for the IRIS data in place of: Path/to/Iris-data "); 49 | }catch (IOException e) { 50 | e.printStackTrace(); 51 | } catch (InterruptedException e) { 52 | e.printStackTrace(); 53 | } 54 | 55 | 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /02_Data_Extraction_Transform_and_Loading/sourceCode/cookbook-app/src/main/java/com/javadeeplearningcookbook/app/recordreaderexamples/RegexSequenceRecordReaderExample.java: -------------------------------------------------------------------------------- 1 | package com.javadeeplearningcookbook.app.recordreaderexamples; 2 | 3 | import org.datavec.api.records.reader.SequenceRecordReader; 4 | import org.datavec.api.records.reader.impl.regex.RegexSequenceRecordReader; 5 | import org.datavec.api.split.NumberedFileInputSplit; 6 | 7 | import java.io.IOException; 8 | 9 | public class RegexSequenceRecordReaderExample { 10 | public static void main(String[] args){ 11 | try { 12 | NumberedFileInputSplit fileSplit = new NumberedFileInputSplit("Path/to/logdata", 13 | 1, 14 | 20); 15 | String regex = "(\\d{2}/\\d{2}/\\d{2}) (\\d{2}:\\d{2}:\\d{2}) ([A-Z]) (.*)"; 16 | 17 | SequenceRecordReader recordReader = new RegexSequenceRecordReader(regex,0); 18 | recordReader.initialize(fileSplit); 19 | //There are 10 sequences of files. We are printing one of the sample sequence here 20 | System.out.println(recordReader.next().get(0).toString()); 21 | } catch(IllegalArgumentException e){ 22 | System.out.println("Please provide proper directory path to logdata in place of: Path/to/logdata"); 23 | } catch (IOException e) { 24 | e.printStackTrace(); 25 | } catch (InterruptedException e) { 26 | e.printStackTrace(); 27 | } 28 | } 29 | 30 | 31 | } 32 | 33 | -------------------------------------------------------------------------------- /02_Data_Extraction_Transform_and_Loading/sourceCode/cookbook-app/src/main/java/com/javadeeplearningcookbook/app/recordreaderexamples/SequenceRecordReaderExample.java: -------------------------------------------------------------------------------- 1 | package com.javadeeplearningcookbook.app.recordreaderexamples; 2 | 3 | import org.datavec.api.records.reader.SequenceRecordReader; 4 | import org.datavec.api.records.reader.impl.csv.CSVSequenceRecordReader; 5 | import org.datavec.api.records.reader.impl.transform.TransformProcessSequenceRecordReader; 6 | import org.datavec.api.split.NumberedFileInputSplit; 7 | import org.datavec.api.transform.TransformProcess; 8 | import org.datavec.api.transform.schema.Schema; 9 | import org.deeplearning4j.datasets.datavec.SequenceRecordReaderDataSetIterator; 10 | import org.nd4j.linalg.dataset.api.iterator.DataSetIterator; 11 | 12 | import java.io.File; 13 | import java.io.IOException; 14 | import java.util.Arrays; 15 | 16 | public class SequenceRecordReaderExample { 17 | 18 | public static void main(String[] args){ 19 | try { 20 | SequenceRecordReader trainFeatures = new CSVSequenceRecordReader(1); 21 | trainFeatures.initialize(new NumberedFileInputSplit(new File("{PATH-TO-FEATURES}/%d.csv").getPath(),1,4)); 22 | SequenceRecordReader trainLabels = new CSVSequenceRecordReader(1); 23 | trainLabels.initialize(new NumberedFileInputSplit(new File("{PATH-TO-LABELS}/%d.csv").getPath(),1,4)); 24 | 25 | Schema featureSchema = new Schema.Builder() 26 | .addColumnCategorical("Pclass", Arrays.asList("1","2","3")) 27 | .addColumnString("Name") 28 | .addColumnCategorical("Sex", Arrays.asList("male","female")) 29 | .addColumnsInteger("Age","Siblings/Spouses Aboard","Parents/Children Aboard") 30 | .addColumnDouble("Fare") 31 | .build(); 32 | TransformProcess featureTransformProcess = new TransformProcess.Builder(featureSchema) 33 | .removeColumns("Name","Fare") 34 | .categoricalToInteger("Sex") 35 | .categoricalToOneHot("Pclass") 36 | .removeColumns("Pclass[1]") 37 | .build(); 38 | TransformProcessSequenceRecordReader featureRecordReader = new TransformProcessSequenceRecordReader(trainFeatures,featureTransformProcess); 39 | 40 | 41 | Schema labelSchema = new Schema.Builder() 42 | .addColumnInteger("Survived") 43 | .build(); 44 | TransformProcess labelTransformProcess = new TransformProcess.Builder(labelSchema) 45 | .build(); 46 | TransformProcessSequenceRecordReader labelRecordReader = new TransformProcessSequenceRecordReader(trainLabels,labelTransformProcess); 47 | 48 | DataSetIterator trainIterator = new SequenceRecordReaderDataSetIterator(featureRecordReader,labelRecordReader,10,2,false, SequenceRecordReaderDataSetIterator.AlignmentMode.ALIGN_END); 49 | System.out.println(trainIterator.inputColumns()); 50 | } catch(IllegalArgumentException e){ 51 | System.out.println("Please provide proper file path for the dataset in place of: PATH-TO-FEATURES & PATH-TO-LABELS "); 52 | }catch (IOException e) { 53 | e.printStackTrace(); 54 | } catch (InterruptedException e) { 55 | e.printStackTrace(); 56 | } 57 | 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /02_Data_Extraction_Transform_and_Loading/sourceCode/cookbook-app/src/main/java/com/javadeeplearningcookbook/app/recordreaderexamples/TransformProcessRecordReaderExample.java: -------------------------------------------------------------------------------- 1 | package com.javadeeplearningcookbook.app.recordreaderexamples; 2 | 3 | import org.datavec.api.records.reader.RecordReader; 4 | import org.datavec.api.records.reader.impl.csv.CSVRecordReader; 5 | import org.datavec.api.records.reader.impl.transform.TransformProcessRecordReader; 6 | import org.datavec.api.split.FileSplit; 7 | import org.datavec.api.transform.TransformProcess; 8 | import org.datavec.api.transform.schema.Schema; 9 | import org.datavec.api.transform.transform.doubletransform.ConvertToDouble; 10 | 11 | import java.io.File; 12 | import java.io.IOException; 13 | import java.util.Arrays; 14 | 15 | public class TransformProcessRecordReaderExample { 16 | public static void main(String[] args) throws IOException, InterruptedException { 17 | try { 18 | Schema schema = new Schema.Builder() 19 | .addColumnsString("Name", "Subject") 20 | .addColumnInteger("Score") 21 | .addColumnCategorical("Grade", Arrays.asList("A","B","C","D")) 22 | .addColumnInteger("Passed").build(); 23 | 24 | TransformProcess transformProcess = new TransformProcess.Builder(schema) 25 | .removeColumns("Name") 26 | .transform(new ConvertToDouble("Score")) 27 | .categoricalToInteger("Grade").build(); 28 | RecordReader recordReader = new CSVRecordReader(1, ','); 29 | recordReader.initialize(new FileSplit(new File("Path/to/transform-data.csv"))); 30 | RecordReader transformRecordReader = new TransformProcessRecordReader(recordReader,transformProcess); 31 | System.out.println(transformRecordReader.next().get(0).toString()); 32 | } catch(IllegalArgumentException e){ 33 | System.out.println("Please provide proper directory path to transform-data.csv in place of: Path/to/transform-data.csv"); 34 | } catch (IOException e) { 35 | e.printStackTrace(); 36 | } catch (InterruptedException e) { 37 | e.printStackTrace(); 38 | } 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /02_Data_Extraction_Transform_and_Loading/sourceCode/cookbook-app/temp/n02131653_124.JPEG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahul-raj/Java-Deep-Learning-Cookbook/ba8dbb3a174cfe7e02a46ef5f222cb22254df199/02_Data_Extraction_Transform_and_Loading/sourceCode/cookbook-app/temp/n02131653_124.JPEG -------------------------------------------------------------------------------- /02_Data_Extraction_Transform_and_Loading/sourceCode/cookbook-app/temp/n02131653_170.JPEG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahul-raj/Java-Deep-Learning-Cookbook/ba8dbb3a174cfe7e02a46ef5f222cb22254df199/02_Data_Extraction_Transform_and_Loading/sourceCode/cookbook-app/temp/n02131653_170.JPEG -------------------------------------------------------------------------------- /02_Data_Extraction_Transform_and_Loading/sourceCode/cookbook-app/temp/n02131653_175.JPEG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahul-raj/Java-Deep-Learning-Cookbook/ba8dbb3a174cfe7e02a46ef5f222cb22254df199/02_Data_Extraction_Transform_and_Loading/sourceCode/cookbook-app/temp/n02131653_175.JPEG -------------------------------------------------------------------------------- /02_Data_Extraction_Transform_and_Loading/sourceCode/cookbook-app/temp/n02131653_176.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahul-raj/Java-Deep-Learning-Cookbook/ba8dbb3a174cfe7e02a46ef5f222cb22254df199/02_Data_Extraction_Transform_and_Loading/sourceCode/cookbook-app/temp/n02131653_176.png -------------------------------------------------------------------------------- /02_Data_Extraction_Transform_and_Loading/sourceCode/cookbook-app/temp/n02131653_82.JPEG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahul-raj/Java-Deep-Learning-Cookbook/ba8dbb3a174cfe7e02a46ef5f222cb22254df199/02_Data_Extraction_Transform_and_Loading/sourceCode/cookbook-app/temp/n02131653_82.JPEG -------------------------------------------------------------------------------- /02_Data_Extraction_Transform_and_Loading/sourceCode/cookbook-app/temp/n02131653_86.JPEG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahul-raj/Java-Deep-Learning-Cookbook/ba8dbb3a174cfe7e02a46ef5f222cb22254df199/02_Data_Extraction_Transform_and_Loading/sourceCode/cookbook-app/temp/n02131653_86.JPEG -------------------------------------------------------------------------------- /02_Data_Extraction_Transform_and_Loading/sourceCode/cookbook-app/temp/n02131653_87.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahul-raj/Java-Deep-Learning-Cookbook/ba8dbb3a174cfe7e02a46ef5f222cb22254df199/02_Data_Extraction_Transform_and_Loading/sourceCode/cookbook-app/temp/n02131653_87.png -------------------------------------------------------------------------------- /02_Data_Extraction_Transform_and_Loading/transform-data.csv: -------------------------------------------------------------------------------- 1 | Name,Subject,Score,Grade,Passed 2 | Alex,Maths,90,A,1 3 | Susan,Physics,88,A,1 4 | Graeme,Chemistry,40,D,0 5 | Jamie,English,69,B,1 6 | Smith,Botany,78,B,1 7 | Joan,Zoology,56,C,1 8 | Matt,Physics,25,D,0 9 | Robert,Maths,77,B,1 10 | Lucy,English,82,A,1 11 | -------------------------------------------------------------------------------- /03_Building_Deep_Neural_Networks_for_Binary_classification/README.md: -------------------------------------------------------------------------------- 1 | 2 | ### Chapter 3 : Building Deep Neural Networks for Binary classification 3 | 4 | In this chapter, you will be creating a deep neural network to perform binary classification on customer churn dataset. 5 |   6 |   7 |   8 | #### Instructions 9 | Navigate to **sourceCode** directory and you will see two directories: 10 | 11 | - cookbookapp 12 | - spring-dl4j 13 | 14 | For complete implementation on customer churn prediction, import the maven project: **cookbookapp**. For DL4J Spring integration example, import the maven project: 15 | **spring-dl4j** 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /03_Building_Deep_Neural_Networks_for_Binary_classification/sourceCode/cookbookapp/.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | target 3 | cookbook-app.iml 4 | cookbook-app.iws 5 | cookbook-app.ipr 6 | cookbookapp.iml 7 | dependency-reduced-pom.xml 8 | model.zip 9 | LocalExecuteExample.csv -------------------------------------------------------------------------------- /03_Building_Deep_Neural_Networks_for_Binary_classification/sourceCode/cookbookapp/src/main/java/com/javadeeplearningcookbook/api/CustomerRetentionPredictionApi.java: -------------------------------------------------------------------------------- 1 | package com.javadeeplearningcookbook.api; 2 | 3 | import org.datavec.api.records.reader.RecordReader; 4 | import org.datavec.api.records.reader.impl.csv.CSVRecordReader; 5 | import org.datavec.api.records.reader.impl.transform.TransformProcessRecordReader; 6 | import org.datavec.api.split.FileSplit; 7 | import org.datavec.api.transform.TransformProcess; 8 | import org.datavec.api.transform.schema.Schema; 9 | import org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator; 10 | import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; 11 | import org.deeplearning4j.util.ModelSerializer; 12 | import org.nd4j.linalg.api.ndarray.INDArray; 13 | import org.nd4j.linalg.dataset.api.iterator.DataSetIterator; 14 | import org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize; 15 | import org.nd4j.linalg.io.ClassPathResource; 16 | import org.slf4j.Logger; 17 | import org.slf4j.LoggerFactory; 18 | 19 | import java.io.File; 20 | import java.io.IOException; 21 | import java.util.Arrays; 22 | 23 | public class CustomerRetentionPredictionApi { 24 | 25 | private static final Logger log = LoggerFactory.getLogger("com.javadeeplearningcookbook.examples.CustomerLossPrediction.class"); 26 | 27 | private static Schema generateSchema(){ 28 | final Schema schema = new Schema.Builder() 29 | .addColumnString("RowNumber") 30 | .addColumnInteger("CustomerId") 31 | .addColumnString("Surname") 32 | .addColumnInteger("CreditScore") 33 | .addColumnCategorical("Geography", Arrays.asList("France","Germany","Spain")) 34 | .addColumnCategorical("Gender", Arrays.asList("Male","Female")) 35 | .addColumnsInteger("Age", "Tenure") 36 | .addColumnDouble("Balance") 37 | .addColumnsInteger("NumOfProducts","HasCrCard","IsActiveMember") 38 | .addColumnDouble("EstimatedSalary") 39 | .build(); 40 | return schema; 41 | 42 | } 43 | 44 | private static RecordReader applyTransform(RecordReader recordReader, Schema schema){ 45 | final TransformProcess transformProcess = new TransformProcess.Builder(schema) 46 | .removeColumns("RowNumber","CustomerId","Surname") 47 | .categoricalToInteger("Gender") 48 | .categoricalToOneHot("Geography") 49 | .removeColumns("Geography[France]") 50 | .build(); 51 | final TransformProcessRecordReader transformProcessRecordReader = new TransformProcessRecordReader(recordReader,transformProcess); 52 | return transformProcessRecordReader; 53 | 54 | } 55 | 56 | private static RecordReader generateReader(File file) throws IOException, InterruptedException { 57 | final RecordReader recordReader = new CSVRecordReader(1,','); 58 | recordReader.initialize(new FileSplit(file)); 59 | final RecordReader transformProcessRecordReader=applyTransform(recordReader,generateSchema()); 60 | return transformProcessRecordReader; 61 | } 62 | 63 | public static INDArray generateOutput(File inputFile, String modelFilePath) throws IOException, InterruptedException { 64 | final File modelFile = new File(modelFilePath); 65 | final MultiLayerNetwork network = ModelSerializer.restoreMultiLayerNetwork(modelFile); 66 | final RecordReader recordReader = generateReader(inputFile); 67 | //final INDArray array = RecordConverter.toArray(recordReader.next()); 68 | final NormalizerStandardize normalizerStandardize = ModelSerializer.restoreNormalizerFromFile(modelFile); 69 | //normalizerStandardize.transform(array); 70 | final DataSetIterator dataSetIterator = new RecordReaderDataSetIterator.Builder(recordReader,1).build(); 71 | normalizerStandardize.fit(dataSetIterator); 72 | dataSetIterator.setPreProcessor(normalizerStandardize); 73 | return network.output(dataSetIterator); 74 | 75 | } 76 | 77 | public static void main(String[] args) throws IOException, InterruptedException { 78 | 79 | INDArray indArray = CustomerRetentionPredictionApi.generateOutput(new ClassPathResource("test.csv").getFile(),"model.zip"); 80 | String message=""; 81 | for(int i=0; iindArray.getDouble(i,1)){ 83 | message+="Customer "+(i+1)+"-> Happy Customer\n"; 84 | } 85 | else{ 86 | message+="Customer "+(i+1)+"-> Unhappy Customer\n"; 87 | } 88 | } 89 | System.out.println(message); 90 | 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /03_Building_Deep_Neural_Networks_for_Binary_classification/sourceCode/cookbookapp/src/main/resources/test.csv: -------------------------------------------------------------------------------- 1 | RowNumber,CustomerId,Surname,CreditScore,Geography,Gender,Age,Tenure,Balance,NumOfProducts,HasCrCard,IsActiveMember,EstimatedSalary 2 | 3,15619304,Onio,502,France,Female,42,8,159660.8,3,1,0,113931.57 3 | 13,15632264,Kay,476,France,Female,34,10,0,2,1,0,26260.98 4 | 14,15691483,Chin,549,France,Female,25,5,0,2,0,0,190857.79 5 | 15,15600882,Scott,635,Spain,Female,35,7,0,2,1,1,65951.65 6 | -------------------------------------------------------------------------------- /03_Building_Deep_Neural_Networks_for_Binary_classification/sourceCode/spring-dl4j/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | /target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | 5 | ### STS ### 6 | .apt_generated 7 | .classpath 8 | .factorypath 9 | .project 10 | .settings 11 | .springBeans 12 | .sts4-cache 13 | 14 | ### IntelliJ IDEA ### 15 | .idea 16 | *.iws 17 | *.iml 18 | *.ipr 19 | 20 | ### NetBeans ### 21 | /nbproject/private/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ 26 | /build/ 27 | 28 | ### VS Code ### 29 | .vscode/ 30 | -------------------------------------------------------------------------------- /03_Building_Deep_Neural_Networks_for_Binary_classification/sourceCode/spring-dl4j/.mvn/wrapper/MavenWrapperDownloader.java: -------------------------------------------------------------------------------- 1 | /* 2 | Licensed to the Apache Software Foundation (ASF) under one 3 | or more contributor license agreements. See the NOTICE file 4 | distributed with this work for additional information 5 | regarding copyright ownership. The ASF licenses this file 6 | to you under the Apache License, Version 2.0 (the 7 | "License"); you may not use this file except in compliance 8 | with the License. You may obtain a copy of the License at 9 | 10 | https://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, 13 | software distributed under the License is distributed on an 14 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | KIND, either express or implied. See the License for the 16 | specific language governing permissions and limitations 17 | under the License. 18 | */ 19 | 20 | import java.io.File; 21 | import java.io.FileInputStream; 22 | import java.io.FileOutputStream; 23 | import java.io.IOException; 24 | import java.net.URL; 25 | import java.nio.channels.Channels; 26 | import java.nio.channels.ReadableByteChannel; 27 | import java.util.Properties; 28 | 29 | public class MavenWrapperDownloader { 30 | 31 | /** 32 | * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. 33 | */ 34 | private static final String DEFAULT_DOWNLOAD_URL = 35 | "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar"; 36 | 37 | /** 38 | * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to 39 | * use instead of the default one. 40 | */ 41 | private static final String MAVEN_WRAPPER_PROPERTIES_PATH = 42 | ".mvn/wrapper/maven-wrapper.properties"; 43 | 44 | /** 45 | * Path where the maven-wrapper.jar will be saved to. 46 | */ 47 | private static final String MAVEN_WRAPPER_JAR_PATH = 48 | ".mvn/wrapper/maven-wrapper.jar"; 49 | 50 | /** 51 | * Name of the property which should be used to override the default download url for the wrapper. 52 | */ 53 | private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; 54 | 55 | public static void main(String args[]) { 56 | System.out.println("- Downloader started"); 57 | File baseDirectory = new File(args[0]); 58 | System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); 59 | 60 | // If the maven-wrapper.properties exists, read it and check if it contains a custom 61 | // wrapperUrl parameter. 62 | File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); 63 | String url = DEFAULT_DOWNLOAD_URL; 64 | if(mavenWrapperPropertyFile.exists()) { 65 | FileInputStream mavenWrapperPropertyFileInputStream = null; 66 | try { 67 | mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); 68 | Properties mavenWrapperProperties = new Properties(); 69 | mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); 70 | url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); 71 | } catch (IOException e) { 72 | System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); 73 | } finally { 74 | try { 75 | if(mavenWrapperPropertyFileInputStream != null) { 76 | mavenWrapperPropertyFileInputStream.close(); 77 | } 78 | } catch (IOException e) { 79 | // Ignore ... 80 | } 81 | } 82 | } 83 | System.out.println("- Downloading from: : " + url); 84 | 85 | File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); 86 | if(!outputFile.getParentFile().exists()) { 87 | if(!outputFile.getParentFile().mkdirs()) { 88 | System.out.println( 89 | "- ERROR creating output direcrory '" + outputFile.getParentFile().getAbsolutePath() + "'"); 90 | } 91 | } 92 | System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); 93 | try { 94 | downloadFileFromURL(url, outputFile); 95 | System.out.println("Done"); 96 | System.exit(0); 97 | } catch (Throwable e) { 98 | System.out.println("- Error downloading"); 99 | e.printStackTrace(); 100 | System.exit(1); 101 | } 102 | } 103 | 104 | private static void downloadFileFromURL(String urlString, File destination) throws Exception { 105 | URL website = new URL(urlString); 106 | ReadableByteChannel rbc; 107 | rbc = Channels.newChannel(website.openStream()); 108 | FileOutputStream fos = new FileOutputStream(destination); 109 | fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); 110 | fos.close(); 111 | rbc.close(); 112 | } 113 | 114 | } 115 | -------------------------------------------------------------------------------- /03_Building_Deep_Neural_Networks_for_Binary_classification/sourceCode/spring-dl4j/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahul-raj/Java-Deep-Learning-Cookbook/ba8dbb3a174cfe7e02a46ef5f222cb22254df199/03_Building_Deep_Neural_Networks_for_Binary_classification/sourceCode/spring-dl4j/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /03_Building_Deep_Neural_Networks_for_Binary_classification/sourceCode/spring-dl4j/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /03_Building_Deep_Neural_Networks_for_Binary_classification/sourceCode/spring-dl4j/model.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahul-raj/Java-Deep-Learning-Cookbook/ba8dbb3a174cfe7e02a46ef5f222cb22254df199/03_Building_Deep_Neural_Networks_for_Binary_classification/sourceCode/spring-dl4j/model.zip -------------------------------------------------------------------------------- /03_Building_Deep_Neural_Networks_for_Binary_classification/sourceCode/spring-dl4j/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.8.RELEASE 9 | 10 | 11 | com.springdl4j 12 | spring-dl4j 13 | 0.0.1-SNAPSHOT 14 | spring-dl4j 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-thymeleaf 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-web 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-starter-test 37 | test 38 | 39 | 40 | com.javadeeplearningcookbook.app 41 | cookbookapp 42 | 1.0-SNAPSHOT 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | org.springframework.boot 51 | spring-boot-maven-plugin 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /03_Building_Deep_Neural_Networks_for_Binary_classification/sourceCode/spring-dl4j/src/main/java/com/springdl4j/springdl4j/SpringDl4jApplication.java: -------------------------------------------------------------------------------- 1 | package com.springdl4j.springdl4j; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringDl4jApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringDl4jApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /03_Building_Deep_Neural_Networks_for_Binary_classification/sourceCode/spring-dl4j/src/main/java/com/springdl4j/springdl4j/controller/CookBookController.java: -------------------------------------------------------------------------------- 1 | package com.springdl4j.springdl4j.controller; 2 | 3 | import com.springdl4j.springdl4j.service.CookBookService; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.beans.factory.annotation.Value; 6 | import org.springframework.stereotype.Controller; 7 | import org.springframework.ui.Model; 8 | import org.springframework.web.bind.annotation.GetMapping; 9 | import org.springframework.web.bind.annotation.PostMapping; 10 | import org.springframework.web.bind.annotation.RequestParam; 11 | import org.springframework.web.multipart.MultipartFile; 12 | 13 | import java.io.IOException; 14 | import java.util.List; 15 | 16 | @Controller 17 | public class CookBookController { 18 | 19 | @Autowired 20 | CookBookService cookBookService; 21 | 22 | @Value("${modelFilePath}") 23 | private String modelFilePath; 24 | 25 | @GetMapping("/") 26 | public String main(final Model model){ 27 | model.addAttribute("message", "Welcome to Java deep learning!"); 28 | return "welcome"; 29 | } 30 | 31 | @PostMapping("/") 32 | public String fileUpload(final Model model, final @RequestParam("uploadFile")MultipartFile multipartFile) throws IOException, InterruptedException { 33 | final List results = cookBookService.generateStringOutput(multipartFile,modelFilePath); 34 | model.addAttribute("message", "Welcome to Java deep learning!"); 35 | model.addAttribute("results",results); 36 | return "welcome"; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /03_Building_Deep_Neural_Networks_for_Binary_classification/sourceCode/spring-dl4j/src/main/java/com/springdl4j/springdl4j/service/CookBookService.java: -------------------------------------------------------------------------------- 1 | package com.springdl4j.springdl4j.service; 2 | 3 | import org.springframework.web.multipart.MultipartFile; 4 | 5 | import java.io.IOException; 6 | import java.util.List; 7 | 8 | 9 | public interface CookBookService { 10 | List generateStringOutput(MultipartFile multipartFile, String modelFilePath) throws IOException, InterruptedException; 11 | } 12 | -------------------------------------------------------------------------------- /03_Building_Deep_Neural_Networks_for_Binary_classification/sourceCode/spring-dl4j/src/main/java/com/springdl4j/springdl4j/service/CookBookServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.springdl4j.springdl4j.service; 2 | 3 | import com.javadeeplearningcookbook.api.CustomerRetentionPredictionApi; 4 | import org.nd4j.linalg.api.ndarray.INDArray; 5 | import org.springframework.stereotype.Service; 6 | import org.springframework.web.multipart.MultipartFile; 7 | 8 | import java.io.File; 9 | import java.io.IOException; 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | 13 | @Service 14 | public class CookBookServiceImpl implements CookBookService { 15 | 16 | @Override 17 | public List generateStringOutput(MultipartFile multipartFile, String modelFilePah) throws IOException, InterruptedException { 18 | final List results = new ArrayList<>(); 19 | File convFile = File.createTempFile(multipartFile.getOriginalFilename(),null, new File(System.getProperty("user.dir")+"/")); 20 | multipartFile.transferTo(convFile); 21 | INDArray indArray = CustomerRetentionPredictionApi.generateOutput(convFile, modelFilePah); 22 | for(int i=0; iindArray.getDouble(i,1)){ 24 | results.add("Customer "+(i+1)+"-> Happy Customer \n"); 25 | } 26 | else{ 27 | results.add("Customer "+(i+1)+"-> Unhappy Customer \n"); 28 | } 29 | } 30 | convFile.deleteOnExit(); 31 | 32 | return results; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /03_Building_Deep_Neural_Networks_for_Binary_classification/sourceCode/spring-dl4j/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | # Enable multipart uploads 2 | spring.servlet.multipart.enabled=true 3 | 4 | # Threshold after which files are written to disk. 5 | spring.servlet.multipart.file-size-threshold=2KB 6 | 7 | # Max file size. 8 | spring.servlet.multipart.max-file-size=200MB 9 | 10 | # Max Request Size 11 | spring.servlet.multipart.max-request-size=215MB 12 | 13 | 14 | -------------------------------------------------------------------------------- /03_Building_Deep_Neural_Networks_for_Binary_classification/sourceCode/spring-dl4j/src/main/resources/static/main.css: -------------------------------------------------------------------------------- 1 | body { 2 | padding-top: 5rem; 3 | } 4 | .starter-template { 5 | padding: 3rem 1.5rem; 6 | text-align: center; 7 | } 8 | 9 | h1{ 10 | color:#0000FF; 11 | } 12 | 13 | h2{ 14 | color:#FF0000; 15 | } -------------------------------------------------------------------------------- /03_Building_Deep_Neural_Networks_for_Binary_classification/sourceCode/spring-dl4j/src/main/resources/templates/welcome.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 17 | 18 |
19 | 20 |
21 |

Spring Boot Deeplearning4J integration example

22 |

23 | 24 |

25 |


26 | 27 |
28 |
29 |
30 |
31 |

Upload your input as CSV for customer rentention prediction

32 |
33 |
34 | 35 | 36 |
37 | 38 | 39 |
40 |
41 |
42 |
43 |

Result

44 |
    45 |
  • 46 |
47 |
48 |
49 |
50 |
51 |
52 | 53 |
54 | 55 | 56 | -------------------------------------------------------------------------------- /03_Building_Deep_Neural_Networks_for_Binary_classification/sourceCode/spring-dl4j/src/test/java/com/springdl4j/springdl4j/SpringDl4jApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.springdl4j.springdl4j; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringDl4jApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /04_Building_Convolutional_Neural_Networks/README.md: -------------------------------------------------------------------------------- 1 | 2 | ### Chapter 4 : Building Convolutional Neural Networks 3 | 4 | In this chapter, you will be creating a convolutional neural network to perform basic imagenet classification. 5 | 6 |   7 |   8 |   9 | 10 | #### Instructions 11 | Navigate to **sourceCode** directory and you will see two directories: 12 | 13 | - cookbookapp 14 | - spring-dl4j 15 | 16 |   17 | 18 | For complete implementation on customer churn prediction, import the maven project: **cookbookapp**. For DL4J Spring integration example, import the maven project: 19 | **spring-dl4j** 20 | 21 |   22 |   23 | 24 | You may use the dataset (*dataset.zip*) included in this directory or use on your own by following the instructions in cookbook. 25 | 26 | 27 | -------------------------------------------------------------------------------- /04_Building_Convolutional_Neural_Networks/dataset.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahul-raj/Java-Deep-Learning-Cookbook/ba8dbb3a174cfe7e02a46ef5f222cb22254df199/04_Building_Convolutional_Neural_Networks/dataset.zip -------------------------------------------------------------------------------- /04_Building_Convolutional_Neural_Networks/sourceCode/cookbookapp/.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | target 3 | cookbook-app.iml 4 | cookbook-app.iws 5 | cookbook-app.ipr 6 | cookbookapp.iml 7 | cookbookapp-cnn.iml 8 | dependency-reduced-pom.xml 9 | model.zip 10 | LocalExecuteExample.csv -------------------------------------------------------------------------------- /04_Building_Convolutional_Neural_Networks/sourceCode/cookbookapp/cnntrainedmodel.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahul-raj/Java-Deep-Learning-Cookbook/ba8dbb3a174cfe7e02a46ef5f222cb22254df199/04_Building_Convolutional_Neural_Networks/sourceCode/cookbookapp/cnntrainedmodel.zip -------------------------------------------------------------------------------- /04_Building_Convolutional_Neural_Networks/sourceCode/cookbookapp/src/main/java/com/javadeeplearningcookbook/api/ImageClassifierAPI.java: -------------------------------------------------------------------------------- 1 | package com.javadeeplearningcookbook.api; 2 | 3 | import org.datavec.api.records.reader.RecordReader; 4 | import org.datavec.api.split.FileSplit; 5 | import org.datavec.api.split.InputSplit; 6 | import org.datavec.image.recordreader.ImageRecordReader; 7 | import org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator; 8 | import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; 9 | import org.deeplearning4j.util.ModelSerializer; 10 | import org.nd4j.linalg.api.ndarray.INDArray; 11 | import org.nd4j.linalg.dataset.api.iterator.DataSetIterator; 12 | import org.nd4j.linalg.dataset.api.preprocessor.ImagePreProcessingScaler; 13 | import org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize; 14 | import org.nd4j.linalg.io.ClassPathResource; 15 | 16 | import java.io.File; 17 | import java.io.IOException; 18 | 19 | public class ImageClassifierAPI { 20 | public static INDArray generateOutput(File inputFile, String modelFileLocation) throws IOException, InterruptedException { 21 | //retrieve the saved model 22 | final File modelFile = new File(modelFileLocation); 23 | final MultiLayerNetwork model = ModelSerializer.restoreMultiLayerNetwork(modelFile); 24 | final RecordReader imageRecordReader = generateReader(inputFile); 25 | final ImagePreProcessingScaler normalizerStandardize = ModelSerializer.restoreNormalizerFromFile(modelFile); 26 | final DataSetIterator dataSetIterator = new RecordReaderDataSetIterator.Builder(imageRecordReader,1).build(); 27 | normalizerStandardize.fit(dataSetIterator); 28 | dataSetIterator.setPreProcessor(normalizerStandardize); 29 | return model.output(dataSetIterator); 30 | } 31 | 32 | private static RecordReader generateReader(File file) throws IOException, InterruptedException { 33 | final RecordReader recordReader = new ImageRecordReader(30,30,3); 34 | final InputSplit inputSplit = new FileSplit(file); 35 | recordReader.initialize(inputSplit); 36 | return recordReader; 37 | } 38 | 39 | public static void main(String[] args) throws IOException, InterruptedException { 40 | final File file = new File("D:\\dataset\\Beagle\\beagle_7.jpg"); 41 | final INDArray results = generateOutput(file,"cnntrainedmodel.zip"); 42 | System.out.println(results); 43 | } 44 | } 45 | 46 | 47 | -------------------------------------------------------------------------------- /04_Building_Convolutional_Neural_Networks/sourceCode/cookbookapp/src/main/resources/cnntrainedmodel.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahul-raj/Java-Deep-Learning-Cookbook/ba8dbb3a174cfe7e02a46ef5f222cb22254df199/04_Building_Convolutional_Neural_Networks/sourceCode/cookbookapp/src/main/resources/cnntrainedmodel.zip -------------------------------------------------------------------------------- /04_Building_Convolutional_Neural_Networks/sourceCode/spring-dl4j/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | /target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | 5 | ### STS ### 6 | .apt_generated 7 | .classpath 8 | .factorypath 9 | .project 10 | .settings 11 | .springBeans 12 | .sts4-cache 13 | 14 | ### IntelliJ IDEA ### 15 | .idea 16 | *.iws 17 | *.iml 18 | *.ipr 19 | 20 | ### NetBeans ### 21 | /nbproject/private/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ 26 | /build/ 27 | 28 | ### VS Code ### 29 | .vscode/ 30 | -------------------------------------------------------------------------------- /04_Building_Convolutional_Neural_Networks/sourceCode/spring-dl4j/.mvn/wrapper/MavenWrapperDownloader.java: -------------------------------------------------------------------------------- 1 | /* 2 | Licensed to the Apache Software Foundation (ASF) under one 3 | or more contributor license agreements. See the NOTICE file 4 | distributed with this work for additional information 5 | regarding copyright ownership. The ASF licenses this file 6 | to you under the Apache License, Version 2.0 (the 7 | "License"); you may not use this file except in compliance 8 | with the License. You may obtain a copy of the License at 9 | 10 | https://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, 13 | software distributed under the License is distributed on an 14 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | KIND, either express or implied. See the License for the 16 | specific language governing permissions and limitations 17 | under the License. 18 | */ 19 | 20 | import java.io.File; 21 | import java.io.FileInputStream; 22 | import java.io.FileOutputStream; 23 | import java.io.IOException; 24 | import java.net.URL; 25 | import java.nio.channels.Channels; 26 | import java.nio.channels.ReadableByteChannel; 27 | import java.util.Properties; 28 | 29 | public class MavenWrapperDownloader { 30 | 31 | /** 32 | * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. 33 | */ 34 | private static final String DEFAULT_DOWNLOAD_URL = 35 | "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar"; 36 | 37 | /** 38 | * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to 39 | * use instead of the default one. 40 | */ 41 | private static final String MAVEN_WRAPPER_PROPERTIES_PATH = 42 | ".mvn/wrapper/maven-wrapper.properties"; 43 | 44 | /** 45 | * Path where the maven-wrapper.jar will be saved to. 46 | */ 47 | private static final String MAVEN_WRAPPER_JAR_PATH = 48 | ".mvn/wrapper/maven-wrapper.jar"; 49 | 50 | /** 51 | * Name of the property which should be used to override the default download url for the wrapper. 52 | */ 53 | private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; 54 | 55 | public static void main(String args[]) { 56 | System.out.println("- Downloader started"); 57 | File baseDirectory = new File(args[0]); 58 | System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); 59 | 60 | // If the maven-wrapper.properties exists, read it and check if it contains a custom 61 | // wrapperUrl parameter. 62 | File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); 63 | String url = DEFAULT_DOWNLOAD_URL; 64 | if(mavenWrapperPropertyFile.exists()) { 65 | FileInputStream mavenWrapperPropertyFileInputStream = null; 66 | try { 67 | mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); 68 | Properties mavenWrapperProperties = new Properties(); 69 | mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); 70 | url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); 71 | } catch (IOException e) { 72 | System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); 73 | } finally { 74 | try { 75 | if(mavenWrapperPropertyFileInputStream != null) { 76 | mavenWrapperPropertyFileInputStream.close(); 77 | } 78 | } catch (IOException e) { 79 | // Ignore ... 80 | } 81 | } 82 | } 83 | System.out.println("- Downloading from: : " + url); 84 | 85 | File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); 86 | if(!outputFile.getParentFile().exists()) { 87 | if(!outputFile.getParentFile().mkdirs()) { 88 | System.out.println( 89 | "- ERROR creating output direcrory '" + outputFile.getParentFile().getAbsolutePath() + "'"); 90 | } 91 | } 92 | System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); 93 | try { 94 | downloadFileFromURL(url, outputFile); 95 | System.out.println("Done"); 96 | System.exit(0); 97 | } catch (Throwable e) { 98 | System.out.println("- Error downloading"); 99 | e.printStackTrace(); 100 | System.exit(1); 101 | } 102 | } 103 | 104 | private static void downloadFileFromURL(String urlString, File destination) throws Exception { 105 | URL website = new URL(urlString); 106 | ReadableByteChannel rbc; 107 | rbc = Channels.newChannel(website.openStream()); 108 | FileOutputStream fos = new FileOutputStream(destination); 109 | fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); 110 | fos.close(); 111 | rbc.close(); 112 | } 113 | 114 | } 115 | -------------------------------------------------------------------------------- /04_Building_Convolutional_Neural_Networks/sourceCode/spring-dl4j/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahul-raj/Java-Deep-Learning-Cookbook/ba8dbb3a174cfe7e02a46ef5f222cb22254df199/04_Building_Convolutional_Neural_Networks/sourceCode/spring-dl4j/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /04_Building_Convolutional_Neural_Networks/sourceCode/spring-dl4j/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /04_Building_Convolutional_Neural_Networks/sourceCode/spring-dl4j/model.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahul-raj/Java-Deep-Learning-Cookbook/ba8dbb3a174cfe7e02a46ef5f222cb22254df199/04_Building_Convolutional_Neural_Networks/sourceCode/spring-dl4j/model.zip -------------------------------------------------------------------------------- /04_Building_Convolutional_Neural_Networks/sourceCode/spring-dl4j/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.3.RELEASE 9 | 10 | 11 | com.springdl4j 12 | spring-dl4j 13 | 0.0.1-SNAPSHOT 14 | spring-dl4j 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-thymeleaf 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | com.javadeeplearningcookbook.app 32 | cookbookapp-cnn 33 | 1.0-SNAPSHOT 34 | 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-test 39 | test 40 | 41 | 42 | 43 | 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-maven-plugin 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /04_Building_Convolutional_Neural_Networks/sourceCode/spring-dl4j/src/.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | *.tmp 3 | target 4 | cookbook-app.iml 5 | cookbook-app.iws 6 | cookbook-app.ipr 7 | cookbookapp.iml 8 | cookbookapp-cnn.iml 9 | dependency-reduced-pom.xml 10 | model.zip 11 | LocalExecuteExample.csv -------------------------------------------------------------------------------- /04_Building_Convolutional_Neural_Networks/sourceCode/spring-dl4j/src/main/java/com/springdl4j/springdl4j/SpringDl4jApplication.java: -------------------------------------------------------------------------------- 1 | package com.springdl4j.springdl4j; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | //Run this only after building project 'cookbookapp-cnn' and jar file is created. 7 | @SpringBootApplication 8 | public class SpringDl4jApplication { 9 | 10 | public static void main(String[] args) { 11 | SpringApplication.run(SpringDl4jApplication.class, args); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /04_Building_Convolutional_Neural_Networks/sourceCode/spring-dl4j/src/main/java/com/springdl4j/springdl4j/controller/CookBookController.java: -------------------------------------------------------------------------------- 1 | package com.springdl4j.springdl4j.controller; 2 | 3 | import com.springdl4j.springdl4j.service.CookBookService; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.beans.factory.annotation.Value; 6 | import org.springframework.stereotype.Controller; 7 | import org.springframework.ui.Model; 8 | import org.springframework.web.bind.annotation.GetMapping; 9 | import org.springframework.web.bind.annotation.PostMapping; 10 | import org.springframework.web.bind.annotation.RequestParam; 11 | import org.springframework.web.multipart.MultipartFile; 12 | 13 | import java.io.IOException; 14 | import java.util.List; 15 | 16 | @Controller 17 | public class CookBookController { 18 | 19 | @Autowired 20 | CookBookService cookBookService; 21 | 22 | @Value("${modelFilePath}") 23 | private String modelFilePath; 24 | 25 | @GetMapping("/") 26 | public String main(final Model model){ 27 | model.addAttribute("message", "Welcome to Java deep learning!"); 28 | return "welcome"; 29 | } 30 | 31 | @PostMapping("/") 32 | public String fileUpload(final Model model, final @RequestParam("uploadFile")MultipartFile multipartFile) throws IOException, InterruptedException { 33 | final List results = cookBookService.generateStringOutput(multipartFile, modelFilePath); 34 | model.addAttribute("message", "Welcome to Java deep learning!"); 35 | model.addAttribute("results",results); 36 | return "welcome"; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /04_Building_Convolutional_Neural_Networks/sourceCode/spring-dl4j/src/main/java/com/springdl4j/springdl4j/service/CookBookService.java: -------------------------------------------------------------------------------- 1 | package com.springdl4j.springdl4j.service; 2 | 3 | import org.springframework.web.multipart.MultipartFile; 4 | 5 | import java.io.IOException; 6 | import java.util.List; 7 | 8 | 9 | public interface CookBookService { 10 | List generateStringOutput(MultipartFile multipartFile, String modelFilePath) throws IOException, InterruptedException; 11 | } 12 | -------------------------------------------------------------------------------- /04_Building_Convolutional_Neural_Networks/sourceCode/spring-dl4j/src/main/java/com/springdl4j/springdl4j/service/CookBookServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.springdl4j.springdl4j.service; 2 | 3 | import com.javadeeplearningcookbook.api.ImageClassifierAPI; 4 | import org.nd4j.linalg.api.ndarray.INDArray; 5 | import org.springframework.stereotype.Service; 6 | import org.springframework.web.multipart.MultipartFile; 7 | 8 | import java.io.File; 9 | import java.io.IOException; 10 | import java.text.DecimalFormat; 11 | import java.util.ArrayList; 12 | import java.util.List; 13 | 14 | @Service 15 | public class CookBookServiceImpl implements CookBookService { 16 | 17 | @Override 18 | public List generateStringOutput(MultipartFile multipartFile, String modelFilePath) throws IOException, InterruptedException { 19 | final List results = new ArrayList<>(); 20 | File convFile = File.createTempFile(multipartFile.getOriginalFilename(),null, new File(System.getProperty("user.dir")+"/")); 21 | multipartFile.transferTo(convFile); 22 | INDArray indArray = ImageClassifierAPI.generateOutput(convFile,modelFilePath); 23 | DecimalFormat df2 = new DecimalFormat("#.####"); 24 | for(int i=0; i>>>>"; 26 | for(int j=0;j 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 17 | 18 |
19 | 20 |
21 |

Spring Boot Deeplearning4J integration example

22 |

23 | 24 |

25 |


26 | 27 |
28 |
29 |
30 |
31 |

Upload your input as CSV for customer rentention prediction

32 |
33 |
34 | 35 | 36 |
37 | 38 | 39 |
40 |
41 |
42 |
43 |

Result

44 |
    45 |
  • 46 |
47 |
48 |
49 |
50 |
51 |
52 | 53 |
54 | 55 | 56 | -------------------------------------------------------------------------------- /04_Building_Convolutional_Neural_Networks/sourceCode/spring-dl4j/src/test/java/com/springdl4j/springdl4j/SpringDl4jApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.springdl4j.springdl4j; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringDl4jApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /05_Implementing_NLP/README.md: -------------------------------------------------------------------------------- 1 | ### Chapter 5 : Implementing NLP 2 | 3 | In this chapter, you will learn how to develop NLP applications using DL4J. 4 |   5 |   6 | 7 | #### Instructions 8 | 9 | - Navigate to **sourceCode/cookbookapp** directory and import the project using *pom.xml* file. 10 | - All the examples for chapter 5 are included in **cookbookapp** project. 11 | - Datasets can be located in *resources* directory apart from the ones that need to be downloaded. For more clarifications, refer to the cookbook instructions. 12 | -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | target 3 | cookbook-app.iml 4 | cookbook-app.iws 5 | cookbook-app.ipr 6 | cookbookapp.iml 7 | cookbookapp-nlp.iml 8 | cookbookapp-cnn.iml 9 | dependency-reduced-pom.xml 10 | model.zip 11 | LocalExecuteExample.csv 12 | words.txt -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/java/com/javadeeplearningcookbook/examples/BasicLineIteratorExample.java: -------------------------------------------------------------------------------- 1 | package com.javadeeplearningcookbook.examples; 2 | 3 | import org.deeplearning4j.models.word2vec.Word2Vec; 4 | import org.deeplearning4j.text.sentenceiterator.BasicLineIterator; 5 | import org.deeplearning4j.text.sentenceiterator.SentenceIterator; 6 | import org.deeplearning4j.text.tokenization.tokenizerfactory.NGramTokenizerFactory; 7 | import org.nd4j.linalg.io.ClassPathResource; 8 | 9 | import java.io.IOException; 10 | 11 | public class BasicLineIteratorExample { 12 | public static void main(String[] args) throws IOException { 13 | SentenceIterator iterator = new BasicLineIterator(new ClassPathResource("raw_sentences.txt").getFile()); 14 | int count=0; 15 | while(iterator.hasNext()){ 16 | iterator.nextSentence(); 17 | count++; 18 | } 19 | System.out.println("count = "+count); 20 | iterator.reset(); 21 | SentenceDataPreProcessor.setPreprocessor(iterator); 22 | while(iterator.hasNext()){ 23 | System.out.println(iterator.nextSentence()); 24 | } 25 | 26 | 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/java/com/javadeeplearningcookbook/examples/CollectionSentenceIteratorExample.java: -------------------------------------------------------------------------------- 1 | package com.javadeeplearningcookbook.examples; 2 | 3 | import org.deeplearning4j.text.sentenceiterator.CollectionSentenceIterator; 4 | import org.deeplearning4j.text.sentenceiterator.SentenceIterator; 5 | 6 | import java.io.IOException; 7 | import java.util.Arrays; 8 | import java.util.List; 9 | 10 | public class CollectionSentenceIteratorExample { 11 | public static void main(String[] args) throws IOException { 12 | List sentences = Arrays.asList( 13 | "No , he says now .", 14 | "And what did he do ?", 15 | "The money 's there .", 16 | "That was less than a year ago .", 17 | "But he made only the first .", 18 | "There 's still time for them to do it .", 19 | "But he should nt have .", 20 | " They have to come down to the people .", 21 | "I do nt know where that is .", 22 | "No , I would nt .", 23 | "Who Will It Be ?", 24 | "And no , I was not the one ." 25 | ); 26 | SentenceIterator iterator = new CollectionSentenceIterator(sentences); 27 | int count=0; 28 | while(iterator.hasNext()){ 29 | iterator.nextSentence(); 30 | count++; 31 | } 32 | System.out.println("count = "+count); 33 | iterator.reset(); 34 | SentenceDataPreProcessor.setPreprocessor(iterator); 35 | while(iterator.hasNext()){ 36 | System.out.println(iterator.nextSentence()); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/java/com/javadeeplearningcookbook/examples/FileSentenceIteratorExample.java: -------------------------------------------------------------------------------- 1 | package com.javadeeplearningcookbook.examples; 2 | 3 | import org.deeplearning4j.text.sentenceiterator.FileSentenceIterator; 4 | import org.deeplearning4j.text.sentenceiterator.SentenceIterator; 5 | import org.nd4j.linalg.io.ClassPathResource; 6 | 7 | import java.io.IOException; 8 | 9 | public class FileSentenceIteratorExample { 10 | public static void main(String[] args) throws IOException { 11 | SentenceIterator iterator = new FileSentenceIterator(new ClassPathResource("files/").getFile()); 12 | int count=0; 13 | while(iterator.hasNext()){ 14 | iterator.nextSentence(); 15 | count++; 16 | } 17 | System.out.println("count = "+count); 18 | iterator.reset(); 19 | SentenceDataPreProcessor.setPreprocessor(iterator); 20 | while(iterator.hasNext()){ 21 | System.out.println(iterator.nextSentence()); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/java/com/javadeeplearningcookbook/examples/GoogleNewsVectorExample.java: -------------------------------------------------------------------------------- 1 | package com.javadeeplearningcookbook.examples; 2 | 3 | import org.deeplearning4j.models.embeddings.loader.WordVectorSerializer; 4 | import org.deeplearning4j.models.word2vec.Word2Vec; 5 | import org.nd4j.linalg.exception.ND4JIllegalStateException; 6 | 7 | import java.io.File; 8 | import java.util.Arrays; 9 | 10 | public class GoogleNewsVectorExample { 11 | 12 | public static void main(String[] args) { 13 | try{ 14 | File file = new File("{PATH-TO-GOOGLE-WORD-VECTOR}"); 15 | Word2Vec model = WordVectorSerializer.readWord2VecModel(file); 16 | System.out.println(Arrays.asList(model.wordsNearest("season",10))); 17 | } catch(ND4JIllegalStateException e){ 18 | System.out.println("Please provide proper directory path in place of: PATH-TO-GOOGLE-WORD-VECTOR"); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/java/com/javadeeplearningcookbook/examples/LineSentenceIteratorExample.java: -------------------------------------------------------------------------------- 1 | package com.javadeeplearningcookbook.examples; 2 | 3 | import org.deeplearning4j.text.sentenceiterator.LineSentenceIterator; 4 | import org.deeplearning4j.text.sentenceiterator.SentenceIterator; 5 | import org.nd4j.linalg.io.ClassPathResource; 6 | 7 | import java.io.IOException; 8 | 9 | public class LineSentenceIteratorExample { 10 | public static void main(String[] args) throws IOException { 11 | SentenceIterator iterator = new LineSentenceIterator(new ClassPathResource("raw_sentences.txt").getFile()); 12 | int count=0; 13 | while(iterator.hasNext()){ 14 | iterator.nextSentence(); 15 | count++; 16 | } 17 | System.out.println("count = "+count); 18 | iterator.reset(); 19 | SentenceDataPreProcessor.setPreprocessor(iterator); 20 | while(iterator.hasNext()){ 21 | System.out.println(iterator.nextSentence()); 22 | } 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/java/com/javadeeplearningcookbook/examples/ParagraphVectorExample.java: -------------------------------------------------------------------------------- 1 | package com.javadeeplearningcookbook.examples; 2 | 3 | import org.deeplearning4j.models.embeddings.inmemory.InMemoryLookupTable; 4 | import org.deeplearning4j.models.paragraphvectors.ParagraphVectors; 5 | import org.deeplearning4j.models.word2vec.VocabWord; 6 | import org.deeplearning4j.models.word2vec.wordstore.VocabCache; 7 | import org.deeplearning4j.text.documentiterator.FileLabelAwareIterator; 8 | import org.deeplearning4j.text.documentiterator.LabelAwareIterator; 9 | import org.deeplearning4j.text.documentiterator.LabelledDocument; 10 | import org.deeplearning4j.text.tokenization.tokenizer.preprocessor.CommonPreprocessor; 11 | import org.deeplearning4j.text.tokenization.tokenizerfactory.DefaultTokenizerFactory; 12 | import org.deeplearning4j.text.tokenization.tokenizerfactory.TokenizerFactory; 13 | import org.nd4j.linalg.api.ndarray.INDArray; 14 | import org.nd4j.linalg.factory.Nd4j; 15 | import org.nd4j.linalg.io.ClassPathResource; 16 | import org.nd4j.linalg.ops.transforms.Transforms; 17 | import org.nd4j.linalg.primitives.Pair; 18 | import org.slf4j.Logger; 19 | import org.slf4j.LoggerFactory; 20 | 21 | import java.io.IOException; 22 | import java.util.ArrayList; 23 | import java.util.List; 24 | import java.util.concurrent.atomic.AtomicInteger; 25 | 26 | public class ParagraphVectorExample { 27 | private static Logger log = LoggerFactory.getLogger(ParagraphVectorExample.class); 28 | public static void main(String[] args) throws IOException { 29 | 30 | LabelAwareIterator labelAwareIterator = new FileLabelAwareIterator.Builder() 31 | .addSourceFolder(new ClassPathResource("label").getFile()) 32 | .build(); 33 | TokenizerFactory tokenizerFactory = new DefaultTokenizerFactory(); 34 | tokenizerFactory.setTokenPreProcessor(new CommonPreprocessor()); 35 | 36 | ParagraphVectors paragraphVectors = new ParagraphVectors.Builder() 37 | .learningRate(0.025) 38 | .minLearningRate(0.005) 39 | .batchSize(1000) 40 | .epochs(5) 41 | .iterate(labelAwareIterator) 42 | .trainWordVectors(true) 43 | .tokenizerFactory(tokenizerFactory) 44 | .build(); 45 | paragraphVectors.fit(); 46 | 47 | ClassPathResource unClassifiedResource = new ClassPathResource("unlabeled"); 48 | FileLabelAwareIterator unClassifiedIterator = new FileLabelAwareIterator.Builder() 49 | .addSourceFolder(unClassifiedResource.getFile()) 50 | .build(); 51 | InMemoryLookupTable lookupTable = (InMemoryLookupTable)paragraphVectors.getLookupTable(); 52 | 53 | 54 | while (unClassifiedIterator.hasNextDocument()) { 55 | LabelledDocument labelledDocument = unClassifiedIterator.nextDocument(); 56 | List documentAsTokens = tokenizerFactory.create(labelledDocument.getContent()).getTokens(); 57 | VocabCache vocabCache = lookupTable.getVocab(); 58 | AtomicInteger cnt = new AtomicInteger(0); 59 | for (String word: documentAsTokens) { 60 | if (vocabCache.containsWord(word)){ 61 | cnt.incrementAndGet(); 62 | } 63 | } 64 | INDArray allWords = Nd4j.create(cnt.get(), lookupTable.layerSize()); 65 | cnt.set(0); 66 | for (String word: documentAsTokens) { 67 | if (vocabCache.containsWord(word)) 68 | allWords.putRow(cnt.getAndIncrement(), lookupTable.vector(word)); 69 | } 70 | INDArray documentVector = allWords.mean(0); 71 | 72 | List labels = labelAwareIterator.getLabelsSource().getLabels(); 73 | 74 | List> result = new ArrayList<>(); 75 | for (String label: labels) { 76 | INDArray vecLabel = lookupTable.vector(label); 77 | if (vecLabel == null){ 78 | throw new IllegalStateException("Label '"+ label+"' has no known vector!"); 79 | } 80 | double sim = Transforms.cosineSim(documentVector, vecLabel); 81 | result.add(new Pair(label, sim)); 82 | } 83 | 84 | for (Pair score: result) { 85 | log.info(" " + score.getFirst() + ": " + score.getSecond()); 86 | } 87 | 88 | } 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/java/com/javadeeplearningcookbook/examples/SentenceDataPreProcessor.java: -------------------------------------------------------------------------------- 1 | package com.javadeeplearningcookbook.examples; 2 | 3 | import org.deeplearning4j.text.sentenceiterator.SentenceIterator; 4 | 5 | public class SentenceDataPreProcessor { 6 | public static void setPreprocessor(SentenceIterator iterator){ 7 | iterator.setPreProcessor(((String sentence) -> sentence.toLowerCase())); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/java/com/javadeeplearningcookbook/examples/TSNEVisualizationExample.java: -------------------------------------------------------------------------------- 1 | package com.javadeeplearningcookbook.examples; 2 | 3 | import org.deeplearning4j.models.embeddings.inmemory.InMemoryLookupTable; 4 | import org.deeplearning4j.models.embeddings.loader.WordVectorSerializer; 5 | import org.deeplearning4j.models.word2vec.wordstore.VocabCache; 6 | import org.deeplearning4j.plot.BarnesHutTsne; 7 | import org.nd4j.linalg.api.buffer.DataBuffer; 8 | import org.nd4j.linalg.api.ndarray.INDArray; 9 | import org.nd4j.linalg.factory.Nd4j; 10 | import org.nd4j.linalg.primitives.Pair; 11 | 12 | import java.io.File; 13 | import java.io.IOException; 14 | import java.util.ArrayList; 15 | import java.util.List; 16 | 17 | public class TSNEVisualizationExample { 18 | public static void main(String[] args) throws IOException { 19 | Nd4j.setDataType(DataBuffer.Type.DOUBLE); 20 | List cacheList = new ArrayList<>(); 21 | File file = new File("words.txt"); 22 | String outputFile = "tsne-standard-coords.csv"; 23 | Pair vectors = WordVectorSerializer.loadTxt(file); 24 | VocabCache cache = vectors.getSecond(); 25 | INDArray weights = vectors.getFirst().getSyn0(); 26 | 27 | for(int i=0;i words = model.wordsNearest("season",10); 39 | for(final String word: words){ 40 | System.out.println(word+ " "); 41 | } 42 | final double cosSimilarity = model.similarity("season","program"); 43 | System.out.println(cosSimilarity); 44 | 45 | BarnesHutTsne tsne = new BarnesHutTsne.Builder() 46 | .setMaxIter(100) 47 | .theta(0.5) 48 | .normalize(false) 49 | .learningRate(500) 50 | .useAdaGrad(false) 51 | .build(); 52 | 53 | 54 | //save word vectors for tSNE visualization. 55 | WordVectorSerializer.writeWordVectors(model.lookupTable(),new File("words.txt")); 56 | WordVectorSerializer.writeWord2VecModel(model, "model.zip"); 57 | 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/files/sentences1.txt: -------------------------------------------------------------------------------- 1 | You could do a Where are they now ? 2 | There 's no place like it that I know of . 3 | Be here now , and so on . 4 | It 's not you or him , it 's both of you . 5 | So it 's not going to get in my way . 6 | When it 's time to go , it 's time to go . 7 | No one 's going to do any of it for us . -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/files/sentences2.txt: -------------------------------------------------------------------------------- 1 | Well , I want more . 2 | Will they make it ? 3 | Who to take into school or not take into school ? 4 | But it 's about to get one just the same . 5 | We all have it . 6 | So we will be . 7 | You can put that out there . 8 | But only a part . 9 | That 's the only way I know , he said . -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/files/sentences3.txt: -------------------------------------------------------------------------------- 1 | But that was a week ago . 2 | It 's not going to be the last time . 3 | I would not want to work for her . 4 | They had to take it where they could get it . 5 | I said that was nt the case , he said . 6 | This has been a good time in our country . -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/label/finance/f01.txt: -------------------------------------------------------------------------------- 1 | Issuer services help companies from around the world to join the London equity market in order to gain access to capital. 2 | The LSE allows companies to raise money, increase their profile and obtain a market valuation through a variety of routes, thus following the firms throughout the whole IPO process. 3 | The London Stock Exchange runs several markets for listing, giving an opportunity for different sized companies to list. 4 | International companies can list a number of products in London including shares, depositary receipts and debt, offering different and cost-effective ways to raise capital. 5 | In 2004 the Exchange opened a Hong Kong office and has attracted more than 200 companies from the Asia-Pacific region. 6 | For the biggest companies exists the Premium Listed Main Market. 7 | This operates a Super Equivalence method where conditions of both the UK Listing Authority as well as London Stock Exchange’s own criteria have to be met. 8 | The largest IPO on the Exchange was completed in May 2011 by Glencore International plc. 9 | The company raised $10 billion at admission, making it one of the largest IPOs ever. -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/label/finance/f02.txt: -------------------------------------------------------------------------------- 1 | In December 2005, the London Stock Exchange rejected a £1.6 billion takeover offer from Macquarie Bank. 2 | The London Stock Exchange described the offer as "derisory", a sentiment echoed by shareholders in the Exchange. 3 | Shortly after Macquarie withdrew its offer, the LSE received an unsolicited approach from NASDAQ valuing the company at £2.4 billion. 4 | This too it rejected. 5 | NASDAQ later pulled its bid, and less than two weeks later on 11 April 2006, struck a deal with LSE's largest shareholder, Ameriprise Financial's Threadneedle Asset Management unit, to acquire all of that firm's stake, consisting of 35.4 million shares, at £11.75 per share. 6 | NASDAQ also purchased 2.69 million additional shares, resulting in a total stake of 15%. While the seller of those shares was undisclosed, it occurred simultaneously with a sale by Scottish Widows of 2.69 million shares. 7 | The move was seen as an effort to force LSE to the negotiating table, as well as to limit the Exchange's strategic flexibility. 8 | Subsequent purchases increased NASDAQ's stake to 25.1%, holding off competing bids for several months. 9 | United Kingdom financial rules required that NASDAQ wait for a period of time before renewing its effort. 10 | On 20 November 2006, within a month or two of the expiration of this period, NASDAQ increased its stake to 28.75% and launched a hostile offer at the minimum permitted bid of £12.43 per share, which was the highest NASDAQ had paid on the open market for its existing shares. 11 | The LSE immediately rejected this bid, stating that it "substantially undervalues" the company. -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/label/finance/f03.txt: -------------------------------------------------------------------------------- 1 | NASDAQ revised its offer (characterized as an "unsolicited" bid, rather than a "hostile takeover attempt") on 12 December 2006, indicating that it would be able to complete the deal with 50% (plus one share) of LSE's stock, rather than the 90% it had been seeking. 2 | The U.S. exchange did not, however, raise its bid. 3 | Many hedge funds had accumulated large positions within the LSE, and many managers of those funds, as well as Furse, indicated that the bid was still not satisfactory. 4 | NASDAQ's bid was made more difficult because it had described its offer as "final", which, under British bidding rules, restricted their ability to raise its offer except under certain circumstances. 5 | In the end, NASDAQ's offer was roundly rejected by LSE shareholders. 6 | Having received acceptances of only 0.41% of rest of the register by the deadline on 10 February 2007, Nasdaq's offer duly lapsed. 7 | Responding to the news, Chris Gibson-Smith, the LSE's chairman, said: "The Exchange’s strategy has produced outstanding results for shareholders by facilitating a structural shift in volume growth in an increasingly international market at the centre of the world’s equity flows. 8 | The Exchange intends to build on its exceptionally valuable brand by progressing various competitive, collaborative and strategic opportunities, thereby reinforcing its uniquely powerful position in a fast evolving global sector." 9 | On 20 August 2007, NASDAQ announced that it was abandoning its plan to take over the LSE and subsequently look for options to divest its 31% (61.3 million shares) shareholding in the company in light of its failed takeover attempt. 10 | In September 2007, NASDAQ agreed to sell the majority of its shares to Borse Dubai, leaving the United Arab Emirates-based exchange with 28% of the LSE. -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/label/finance/f04.txt: -------------------------------------------------------------------------------- 1 | In February 2011, in the wake of an announced merger of NYSE Euronext with Deutsche Borse, speculation developed that ICE and Nasdaq could mount a counter-bid of their own for NYSE Euronext. 2 | ICE was thought to be looking to acquire the American exchange's derivatives business, Nasdaq its cash equities business. 3 | As of the time of the speculation, "NYSE Euronext’s market value was $9.75 billion. 4 | Nasdaq was valued at $5.78 billion, while ICE was valued at $9.45 billion. 5 | "Late in the month, Nasdaq was reported to be considering asking either ICE or the Chicago Merc (CME) to join in what would be probably be an $11–12 billion counterbid for NYSE. 6 | On April 1, ICE and Nasdaq made an $11.3 billion offer which was rejected April 10 by NYSE. 7 | Another week later, ICE and Nasdaq sweetened their offer, including a $.17 increase per share to $42.67 and a $350 million breakup fee if the deal were to encounter regulatory trouble. 8 | The two said the offer was a $2 billion (21%) premium over the Deutsche offer and that they had fully committed financing of $3.8 billion from lenders to finance the deal. 9 | The Justice Department, also in April, "initiated an antitrust review of the proposal, which would have brought nearly all U.S. stock listings under a merged Nasdaq-NYSE." 10 | In May, saying it "became clear that we would not be successful in securing regulatory approval," the Nasdaq and ICE withdrew their bid. 11 | The European Commission then blocked the Deutsche merger on 1 February 2012, citing the fact that the merged company would have a near monopoly. 12 | In December 2012, ICE announced it would buy NYSE Euronext for $8.2 billion, pending regulatory approval. 13 | Jeffrey Sprecher will retain his position as Chairman and CEO. 14 | The boards of directors of both ICE and NYSE Euronext approved the acquisition. -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/label/finance/f05.txt: -------------------------------------------------------------------------------- 1 | In a response to US financial crisis in 2008, Sprecher formed ICE US Trust based in New York, now called ICE Clear Credit LLC, to serve as a limited-purpose bank, a clearing house for credit default swaps. 2 | Sprecher worked closely with the Federal Reserve to serve as its over-the-counter (OTC) derivatives clearing house. 3 | "US regulators were keen on the kind of clearing house for opaque over-the-counter (OTC) derivatives as a risk management device. 4 | In the absence of a central counterparty - which would guarantee pay-outs should a trading party be unable to do so - there was a high risk of massive market disruption(Weitzman 2008)" 5 | The principal backers for ICE US Trust were the same financial institutions most affected by the crisis, the top ten of the world's largest banks (Goldman Sachs, Bank of America, Citi, Credit Suisse, Deutsche Bank, JPMorgan, Merrill Lynch, Morgan Stanley and UBS). Sprecher's clearing house cleared their global credit default swaps (CDS) in exchange for sharing profits with these banks.(Weitzman 2008)(Terhune 2010). 6 | By 30 September 2008 the Financial Post warned that the "$54000bn credit derivatives market faced its biggest test in October 2008 as billions of dollars worth of contracts on now-defaulted derivatives would be auctioned by the International Swaps and Derivatives Association. 7 | In his article in the Financial Post, (Weitzman 2008) described ICE as a "US-based electronic futures exchange" which raised the stakes on October 30, 2008 in its effort to expand in the $54000 bn credit derivatives market. -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/label/finance/f06.txt: -------------------------------------------------------------------------------- 1 | For years, the NYMEX traders had done a large business trading futures of Maine's potato crop. 2 | According to Leah McGrath Goodman's 2011 book The Asylum, manipulation in this market was commonplace, performed by various parties including potato inspectors and NYMEX traders. 3 | The worst incident was the 1970s potato bust, when Idaho potato magnate J. R. Simplot allegedly went short in huge numbers, leaving a large amount of contracts unsettled at the expiration date, resulting in a large number of defaulted delivery contracts. 4 | A public outcry followed, and the newly created Commodity Futures Trading Commission held hearings. 5 | NYMEX was barred from trading not only potatoes futures, but from entering new areas it hadn't traded in before. 6 | NYMEX's reputation was severely damaged, because, as future chairman Michel Marks told Goodman in his book, "The essence of an exchange is the sanctity of its contract." 7 | When the potato ban came into effect, NYMEX's platinum, palladium and heating oil markets were not significantly affected. 8 | However, NYMEX's reputation suffered in Washington, D.C., especially with the regulations in the Commodity Futures Trading Commission (CFTC), the President of the Exchange, Richard Leone brought in John Elting Treat, White House energy adviser to Presidents Carter and Reagan to help restore the credibility of NYMEX and to help the Exchange explore the possibility of entering the petroleum market recognizing the great potential for moving well beyond the limited size of the New York Heating Oil market. 9 | When Leone left NYMEX in 1981 as a result of a strong disagreement with the NYMEX Board, John Elting Treat was asked to replace him as President. 10 | The launching of the WTI crude oil contract was championed by Treat, who, with difficulty, convinced the Board and the two Marks family members, veteran and highly respected floor trader Francis Marks and his son, Michael, who had just come Chairman of the Board, to take a chance on trading crude oil. Arnold Safir was one of the members of an advisory committee formed by Treat to help design the new contract. 11 | Treat, with Board Chairman Marks and the support of the rest of the NYMEX Board, eventually chose West Texas Intermediate (WTI) as the traded product and Cushing, Oklahoma, as the delivery point. 12 | Robin Woodhead, who later became the first Chairman of the International Petroleum Exchange (IPE) in London started an active dialogue with Treat about whether they could start a Brent Crude oil contracts. 13 | Treat was very supportive and gave Woodhead strong support and a lot of advice. Shortly thereafter, after substantial conversations, The IPE was formally launched and started trading Brent. -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/label/finance/f07.txt: -------------------------------------------------------------------------------- 1 | NYMEX held a virtual monopoly on "open market" oil futures trading (as opposed to the dark market or "over the counter" market). 2 | However, in the early 2000s the electronically based exchanges started taking away the business of the open outcry markets like NYMEX. 3 | Enron's online energy trading system was part of this trend. 4 | Jeff Sprecher's Intercontinental Exchange, or ICE, was another example. 5 | ICE eventually began trading oil contracts that were extremely similar to NYMEX's, taking away market share almost immediately. 6 | The open outcry NYMEX pit traders had always been against electronic trading because it threatened their income and their lifestyle. 7 | The executives at NYMEX felt that electronic trading was the only way to keep the exchange competitive. 8 | NYMEX teamed up with the Chicago Mercantile Exchange to use Globex in 2006. 9 | The trading pits emptied out as many traders quit. Banks, hedge funds, and huge oil companies stopped making telephone calls to the pits and started trading directly for themselves over screens. 10 | In this period the NYMEX also worked on founding the Dubai Mercantile Exchange in the United Arab Emirates. 11 | This was chronicled by Ben Mezrich in his New York Times Best Selling book Rigged which has been optioned for film adaptation by Summit Entertainment. -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/label/finance/f08.txt: -------------------------------------------------------------------------------- 1 | The final executive management of NYMEX decided to sell it off in pieces, take golden parachute buyouts, and leave. 2 | In 2006 NYMEX underwent an initial public offering (IPO) and was listed on the New York Stock Exchange. 3 | The executives and exchange members owning seats on the exchange saw their net worth increase by millions of dollars in a few hours - many of the pit traders, who leased their seats instead of owning, did not. 4 | Other parts of NYMEX were sold to private equity investors and the Chicago Mercantile Exchange. 5 | The CME got ownership of the physical facilities and began scrubbing the NYMEX logo and name off of various artifacts and closed the NYMEX museum. 6 | NYMEX eventually became little more than a brand name used by CME. 7 | By 2011, NYMEX open outcry trading was relegated for the most part to a small number of people trading options. 8 | In 2009 it was reported that holders of COMEX gold futures contracts experienced problems taking delivery of their metal. 9 | Along with chronic delivery delays, some investors received delivery of bars not matching their contract in serial number and weight. 10 | The delays could not be easily explained by slow warehouse movements, as the daily reports of these movements showed little activity. 11 | Because of these problems, there were concerns that COMEX did not have the gold inventory to back its existing warehouse receipts. -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/label/finance/f09.txt: -------------------------------------------------------------------------------- 1 | After launching the original crude oil futures contract, Treat began an aggressive marketing campaign to first bring in the large US and British oil companies and then moved on to pull in the large Middle East producers. 2 | It took almost a year to get the first US "majors" to start trading, but several "majors" did not start for almost 5 years. 3 | The initial resistance from the OPEC producers was almost impossible to break through, although some finally gave in, among the first being Venezuela. 4 | The rumors on the floor at that time were the Arab producers would trade gold futures as a proxy for oil prices (since the Arabs were major purchasers of gold and would buy more when their pockets were filled by rising oil prices, and conversely sell when oil revenues fell and reduced their ability to buy gold). 5 | After the potato ban, NYMEX's existence was in doubt. 6 | Most of the trading volume had been in potatoes. 7 | Without a good commodity, the traders had trouble making money. 8 | Eventually, the new chairman, Michel Marks – the son of commodities icon Francis Q. Marks– along with economist Arnold Safer, figured out that NYMEX could revamp an old heating oil futures contract. 9 | When the government deregulated heating oil, the contract had a chance of becoming a good object of trade on the floor. 10 | A new futures contract was carefully drawn up and trading began on a tiny scale in 1978. 11 | Some of the first users of NYMEX heating oil deliveries were small scale suppliers of people in the Northern United States. 12 | NYMEX's business threatened some entrenched interests like big oil and government groups like OPEC that had traditionally controlled oil prices. 13 | NYMEX provided an "open market" and thus transparent pricing for heating oil, and, eventually, crude oil, gasoline, and natural gas. 14 | NYMEX's oil futures contracts were standardized for the delivery of West Texas Intermediate light, sweet crude oil to Cushing 15 | The energy trading business took off, and NYMEX boomed. 16 | The open outcry floor became a cacophony of shouting traders and pit cards. 17 | The pits became a place where many people without much education or ability to fit into Wall Street could have a chance at being rich. 18 | Many traders and executives became millionaires. 19 | They threw lavish parties and went on exotic vacations. 20 | Many of these people also became heavily involved in drugs and prostitution, with drugs being traded right on the floor of the exchange. 21 | Goodman's book tells the stories of many of the personalities that built the exchange in this era. 22 | COMEX (Commodity Exchange, Inc), one of the exchanges that shared 4 World Trade Center with NYMEX, had traditionally looked down on NYMEX for being smaller and for having the toxic reputation from the potato bust. 23 | With NYMEX's energy trading boom, it became much larger and wealtheir than COMEX. 24 | On August 3, 1994, the NYMEX and COMEX finally merged under the NYMEX name. 25 | By the late 1990s, there were more people working on the NYMEX floor than there was space for them. 26 | In 1997, the NYMEX moved to a new building on the Southwestern portion of Manhattan, part of a complex called the World Financial Center. -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/label/finance/f10.txt: -------------------------------------------------------------------------------- 1 | The NYBOT's original headquarters and trading floor were destroyed in the September 11, 2001 terrorist attacks on the World Trade Center. 2 | Several NYMEX people were lost in the tragedy, even though the new NYMEX building itself was mostly undamaged. 3 | Despite the area's devastation, the exchange's management and staff had the exchange back up and running in very short order. 4 | On February 26, 2003, the New York Board of Trade (NYBOT) signed a lease agreement with the NYMEX to move into its World Financial Center headquarters and trading facility. 5 | NYMEX maintained a close relationship with many organizations and people at World Trade Center, and After the attacks, the NYMEX built a $12 million trading floor backup facility outside of New York City with 700 traders' booths, 2,000 telephones, and a backup computer system. 6 | This backup was in case of another terrorist attack or natural disaster in Lower Manhattan. -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/label/health/f01.txt: -------------------------------------------------------------------------------- 1 | Despite popular belief, a reliance on a single food which composes the majority of a diet is indicative of poor eating habits. 2 | An individual on such a diet may be prone to deficiency and most certainly will not be fulfilling the Recommended Nutrient Intake. 3 | While plants, vegetables, and fruits are known to help reduce the incidence of chronic disease, the benefits on health posed by plant-based foods, as well as the percentage on which a diet needs to be plant-based in order to have health benefits, is unknown. 4 | Nevertheless, plant-based food diets in society and between nutritionist circles are linked to health and longevity, as well as contributing to lowering cholesterol, weight loss, and, in some cases, stress reduction. 5 | Although a number of preconceptions of a healthy diet center around plant-based foods, the majority of assumptions about foods which are usually thought of as "bad" foods are usually correct, apart from the assumption that there are "bad" foods; many people associate dishes such as Full English cooked Breakfast and Bacon Sandwiches as foods which, if eaten regularly, can contribute to cholesterol, fat, and heart problems. -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/label/health/f02.txt: -------------------------------------------------------------------------------- 1 | A healthy diet is usually defined as a diet in which nutrient intake is maintained, and cholesterol, salt, sugar, and fat are reduced. 2 | The idea of a healthy diet is something used by a government to ensure that people are well "protected" against common illnesses and conditions which stem from poor diet. 3 | This could include headaches, lessened sexual drive, heart disease, alcohol poisoning, or obesity. 4 | A healthy diet is a way of eating that that reduces risk for complications such as heart disease and stroke. 5 | Healthy eating includes eating a wide variety of foods including: vegetables, whole grains, fruits, non-fat dairy products, beans, lean meats, poultry, fish 6 | The definition of a healthy diet is sometimes also thought of as a diet which will combat or prevent illness. 7 | Although the majority of people would support this definition, few know why, other than because "bad" foods are not consumed. 8 | People with healthy diets are less likely to succumb to common minor illnesses, such as lesser forms of Influenza, mainly because consumption of a healthy diet would provide ample nutrients and energy for the body, so as to help stave off such illnesses. 9 | Similarly, the healthy diet can also be used this way to aid the body during illness. 10 | The myth of "feed a cold, starve a fever" is a common misconception among the public, particularly in the United Kingdom. 11 | This is a myth in every sense of the word because providing the body with nutrients during illness is actually beneficial - nutrient and energy stores would be replenished, allowing for more energy to be used by the body to combat illness. -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/label/health/f03.txt: -------------------------------------------------------------------------------- 1 | Cholesterol is a steroid, a lipid, and an alcohol, found in the cell membranes of all body tissues, and transported in the blood plasma of all animals. 2 | Most cholesterol is not dietary in origin, it is synthesized internally. 3 | Cholesterol is present in higher concentrations in tissues which either produce more or have more densely-packed membranes, for example, the liver, spinal cord, brain and atheroma. 4 | Cholesterol plays a central role in many biochemical processes, but is best known for the association of cardiovascular disease with various lipoprotein cholesterol transport patterns in the blood. 5 | The name originates from the Greek chole- (bile) and stereos (solid), as researchers first identified cholesterol (C27H45OH) in solid form in gallstones. -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/label/health/f04.txt: -------------------------------------------------------------------------------- 1 | The selective toxicity of vitamin C for cancer cells has been demonstrated repeatedly in cell culture studies. 2 | The Proceedings of the National Academy of Sciences recently published a paper demonstrating vitamin C killing cancer cells. 3 | As of 2005, some physicians have called for a more careful reassessment of vitamin C, especially intravenous vitamin C, in cancer treatment. 4 | With two colleagues, Pauling founded the Institute of Orthomolecular Medicine in Menlo Park, California, in 1973, which was soon renamed the Linus Pauling Institute of Science and Medicine. 5 | Pauling directed research on vitamin C, but also continued his theoretical work in chemistry and physics until his death in 1994. 6 | In his last years, he became especially interested in the possible role of vitamin C in preventing atherosclerosis and published three case reports on the use of lysine and vitamin C to relieve angina pectoris. 7 | In 1996, the Linus Pauling Institute moved from Palo Alto, California, to Corvallis, Oregon, to become part of Oregon State University, where it continues to conduct research on micronutrients, phytochemicals (chemicals from plants), and other constituents of the diet in preventing and treating disease. -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/label/health/f05.txt: -------------------------------------------------------------------------------- 1 | Stress management refers to the wide spectrum of techniques and psychotherapies aimed at controlling a person's levels of stress, especially chronic stress, usually for the purpose of improving everyday functioning. 2 | In this context, the term 'stress' refers only to a stress with significant negative consequences, or distress in the terminology advocated by Hans Selye, rather than what he calls eustress, a stress whose consequences are helpful or otherwise positive. 3 | Stress produces numerous physical and mental symptoms which vary according to each individual's situational factors. 4 | These can include physical health decline as well as depression. 5 | The process of stress management is named as one of the keys to a happy and successful life in modern society. 6 | Although life provides numerous demands that can prove difficult to handle, stress management provides a number of ways to manage anxiety and maintain overall well-being. 7 | Despite stress often being thought of as a subjective experience, levels of stress are readily measurable, using various physiological tests, similar to those used in polygraphs. 8 | Many practical stress management techniques are available, some for use by health professionals and others, for self-help, which may help an individual reduce their levels of stress, provide positive feelings of control over one's life and promote general well-being. 9 | Evaluating the effectiveness of various stress management techniques can be difficult, as limited research currently exists. 10 | Consequently, the amount and quality of evidence for the various techniques varies widely. 11 | Some are accepted as effective treatments for use in psychotherapy, whilst others with less evidence favoring them are considered alternative therapies. 12 | Many professional organisations exist to promote and provide training in conventional or alternative therapies. 13 | There are several models of stress management, each with distinctive explanations of mechanisms for controlling stress. 14 | Much more research is necessary to provide a better understanding of which mechanisms actually operate and are effective in practice. -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/label/health/f06.txt: -------------------------------------------------------------------------------- 1 | Unintentional weight loss may result from loss of body fats, loss of body fluids, muscle atrophy, or even a combination of these. 2 | It is generally regarded as a medical problem when at least 10% of a person's body weight has been lost in six months or 5% in the last month. 3 | Another criterion used for assessing weight that is too low is the body mass index (BMI). 4 | However, even lesser amounts of weight loss can be a cause for serious concern in a frail elderly person. 5 | Unintentional weight loss can occur because of an inadequately nutritious diet relative to a person's energy needs (generally called malnutrition). 6 | Disease processes, changes in metabolism, hormonal changes, medications or other treatments, disease- or treatment-related dietary changes, or reduced appetite associated with a disease or treatment can also cause unintentional weight loss. 7 | Poor nutrient utilization can lead to weight loss, and can be caused by fistulae in the gastrointestinal tract, diarrhea, drug-nutrient interaction, enzyme depletion and muscle atrophy. 8 | Continuing weight loss may deteriorate into wasting, a vaguely defined condition called cachexia. 9 | Cachexia differs from starvation in part because it involves a systemic inflammatory response. 10 | It is associated with poorer outcomes. 11 | In the advanced stages of progressive disease, metabolism can change so that they lose weight even when they are getting what is normally regarded as adequate nutrition and the body cannot compensate. 12 | This leads to a condition called anorexia cachexia syndrome (ACS) and additional nutrition or supplementation is unlikely to help. 13 | Symptoms of weight loss from ACS include severe weight loss from muscle rather than body fat, loss of appetite and feeling full after eating small amounts, nausea, anemia, weakness and fatigue. 14 | Serious weight loss may reduce quality of life, impair treatment effectiveness or recovery, worsen disease processes and be a risk factor for high mortality rates. -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/label/health/f07.txt: -------------------------------------------------------------------------------- 1 | Medical treatment can directly or indirectly cause weight loss, impairing treatment effectiveness and recovery that can lead to further weight loss in a vicious cycle. 2 | Many patients will be in pain and have a loss of appetite after surgery. 3 | Part of the body's response to surgery is to direct energy to wound healing, which increases the body's overall energy requirements. 4 | Surgery affects nutritional status indirectly, particularly during the recovery period, as it can interfere with wound healing and other aspects of recovery. 5 | Surgery directly affects nutritional status if a procedure permanently alters the digestive system. 6 | Enteral nutrition (tube feeding) is often needed. 7 | However a policy of 'nil by mouth' for all gastrointestinal surgery has not been shown to benefit, with some suggestion it might hinder recovery. 8 | Early post-operative nutrition is a part of Enhanced Recovery After Surgery protocols. 9 | These protocols also include carbohydrate loading in the 24 hours before surgery, but earlier nutritional interventions have not been shown to have a significant impact. 10 | Some medications can cause weight loss, while others can cause weight gain. 11 | Social conditions such as poverty, social isolation and inability to get or prepare preferred foods can cause unintentional weight loss, and this may be particularly common in older people. 12 | Nutrient intake can also be affected by culture, family and belief systems. 13 | Ill-fitting dentures and other dental or oral health problems can also affect adequacy of nutrition. 14 | Loss of hope, status or social contact and spiritual distress can cause depression, which may be associated with reduced nutrition, as can fatigue. -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/label/health/f08.txt: -------------------------------------------------------------------------------- 1 | Intentional weight loss is the loss of total body mass as a result of efforts to improve fitness and health, or to change appearance through slimming. 2 | Weight loss in individuals who are overweight or obese can reduce health risks, increase fitness, and may delay the onset of diabetes. 3 | It could reduce pain and increase movement in people with osteoarthritis of the knee. 4 | Weight loss can lead to a reduction in hypertension (high blood pressure), however whether this reduces hypertension-related harm is unclear. 5 | Weight loss occurs when the body is expending more energy in work and metabolism than it is absorbing from food or other nutrients. 6 | It will then use stored reserves from fat or muscle, gradually leading to weight loss. 7 | For athletes seeking to improve performance or to meet required weight classification for participation in a sport, it is not uncommon to seek additional weight loss even if they are already at their ideal body weight. 8 | Others may be driven to lose weight to achieve an appearance they consider more attractive. 9 | Being underweight is associated with health risks such as difficulty fighting off infection, osteoporosis, decreased muscle strength, trouble regulating body temperature and even increased risk of death. 10 | Low-calorie diets are also referred to as balanced percentage diets. 11 | Due to their minimal detrimental effects, these types of diets are most commonly recommended by nutritionists. 12 | In addition to restricting calorie intake, a balanced diet also regulates macronutrient consumption. 13 | From the total number of allotted daily calories, it is recommended that 55% should come from carbohydrates, 15% from protein, and 30% from fats with no more than 10% of total fat coming from saturated forms. 14 | For instance, a recommended 1,200 calorie diet would supply about 660 calories from carbohydrates, 180 from protein, and 360 from fat. 15 | Some studies suggest that increased consumption of protein can help ease hunger pangs associated with reduced caloric intake by increasing the feeling of satiety. 16 | Calorie restriction in this way has many long-term benefits. After reaching the desired body weight, the calories consumed per day may be increased gradually, without exceeding 2,000 net (i.e. derived by subtracting calories burned by physical activity from calories consumed). 17 | Combined with increased physical activity, low-calorie diets are thought to be most effective long-term, unlike crash diets, which can achieve short-term results, at best. 18 | Physical activity could greatly enhance the efficiency of a diet. 19 | The healthiest weight loss regimen, therefore, is one that consists of a balanced diet and moderate physical activity. -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/label/health/f09.txt: -------------------------------------------------------------------------------- 1 | The least intrusive weight loss methods, and those most often recommended, are adjustments to eating patterns and increased physical activity, generally in the form of exercise. 2 | The World Health Organization recommended that people combine a reduction of processed foods high in saturated fats, sugar and salt and caloric content of the diet with an increase in physical activity. 3 | An increase in fiber intake is also recommended for regulating bowel movements. 4 | Other methods of weight loss include use of drugs and supplements that decrease appetite, block fat absorption, or reduce stomach volume. 5 | Bariatric surgery may be indicated in cases of severe obesity. 6 | Two common bariatric surgical procedures are gastric bypass and gastric banding. 7 | Both can be effective at limiting the intake of food energy by reducing the size of the stomach, but as with any surgical procedure both come with their own risks that should be considered in consultation with a physician. 8 | Dietary supplements, though widely used, are not considered a healthy option for weight loss. 9 | Many are available, but very few are effective in the long term. 10 | Virtual gastric band uses hypnosis to make the brain think the stomach is smaller than it really is and hence lower the amount of food ingested. 11 | This brings as a consequence weight reduction. 12 | This method is complemented with psychological treatment for anxiety management and with hypnopedia. 13 | Research has been conducted into the use of hypnosis as a weight management alternative. 14 | In 1996 a study found that cognitive-behavioral therapy (CBT) was more effective for weight reduction if reinforced with hypnosis. 15 | Acceptance and Commitment Therapy ACT, a mindfulness approach to weight loss, has also in the last few years been demonstrating its usefulness. -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/label/health/f10.txt: -------------------------------------------------------------------------------- 1 | Symptoms of parasites may not always be obvious. 2 | However, such symptoms may mimic anemia or a hormone deficiency. 3 | Some of the symptoms caused by several worm infestation can include itching affecting the anus or the vaginal area, abdominal pain, weight loss, increased appetite, bowel obstructions, diarrhea, and vomiting eventually leading to dehydration, sleeping problems, worms present in the vomit or stools, anemia, aching muscles or joints, general malaise, allergies, fatigue, nervousness. 4 | Symptoms may also be confused with pneumonia or food poisoning. 5 | The effects caused by parasitic diseases range from mild discomfort to death. 6 | The nematode parasites Necator americanus and Ancylostoma duodenale cause human hookworm infection, which leads to anaemia and protein malnutrition. 7 | This infection affects approximately 740 million people in the developing countries, including children and adults, of the tropics specifically in poor rural areas located in sub-Saharan Africa, Latin America, South-East Asia and China. 8 | Chronic hookworm in children leads to impaired physical and intellectual development, school performance and attendance are reduced. 9 | Pregnant women affected by a hookworm infection can also develop aneamia, which results in negative outcomes both for the mother and the infant. 10 | Some of them are: low birth weight, impaired milk production, as well as increased risk of death for the mother and the baby. -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/label/science/f01.txt: -------------------------------------------------------------------------------- 1 | Big data is a broad term for data sets so large or complex that traditional data processing applications are inadequate. 2 | Challenges include analysis, capture, data curation, search, sharing, storage, transfer, visualization, querying and information privacy. 3 | The term often refers simply to the use of predictive analytics or certain other advanced methods to extract value from data, and seldom to a particular size of data set. 4 | Accuracy in big data may lead to more confident decision making, and better decisions can result in greater operational efficiency, cost reduction and reduced risk. 5 | Analysis of data sets can find new correlations to "spot business trends, prevent diseases, combat crime and so on." 6 | Scientists, business executives, practitioners of medicine, advertising and governments alike regularly meet difficulties with large data sets in areas including Internet search, finance and business informatics. 7 | Scientists encounter limitations in e-Science work, including meteorology, genomics, connectomics, complex physics simulations, biology and environmental research. 8 | Data sets are growing rapidly in part because they are increasingly gathered by cheap and numerous information-sensing mobile devices, aerial (remote sensing), software logs, cameras, microphones, radio-frequency identification (RFID) readers and wireless sensor networks. 9 | The world's technological per-capita capacity to store information has roughly doubled every 40 months since the 1980s; as of 2012, every day 2.5 exabytes (2.5×1018) of data are created. 10 | One question for large enterprises is determining who should own big data initiatives that affect the entire organization. 11 | Relational database management systems and desktop statistics and visualization packages often have difficulty handling big data. 12 | The work instead requires "massively parallel software running on tens, hundreds, or even thousands of servers". 13 | What is considered "big data" varies depending on the capabilities of the users and their tools, and expanding capabilities make big data a moving target. 14 | "For some organizations, facing hundreds of gigabytes of data for the first time may trigger a need to reconsider data management options. 15 | For others, it may take tens or hundreds of terabytes before data size becomes a significant consideration." -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/label/science/f02.txt: -------------------------------------------------------------------------------- 1 | Big data usually includes data sets with sizes beyond the ability of commonly used software tools to capture, curate, manage, and process data within a tolerable elapsed time. 2 | Big data "size" is a constantly moving target, as of 2012 ranging from a few dozen terabytes to many petabytes of data. 3 | Big data requires a set of techniques and technologies with new forms of integration to reveal insights from datasets that are diverse, complex, and of a massive scale. 4 | In a 2001 research report and related lectures, META Group (now Gartner) analyst Doug Laney defined data growth challenges and opportunities as being three-dimensional, i.e. increasing volume (amount of data), velocity (speed of data in and out), and variety (range of data types and sources). 5 | Gartner, and now much of the industry, continue to use this "3Vs" model for describing big data. 6 | In 2012, Gartner updated its definition as follows: "Big data is high volume, high velocity, and/or high variety information assets that require new forms of processing to enable enhanced decision making, insight discovery and process optimization." 7 | Gartner's definition of the 3Vs is still widely used, and in agreement with a consensual definition that states that "Big Data represents the Information assets characterized by such a High Volume, Velocity and Variety to require specific Technology and Analytical Methods for its transformation into Value". 8 | Additionally, a new V "Veracity" is added by some organizations to describe it, revisionism challenged by some industry authorities. 9 | The 3Vs have been expanded to other complementary characteristics of big data: 10 | Volume: big data doesn't sample; it just observes and tracks what happens 11 | Velocity: big data is often available in real-time 12 | Variety: big data draws from text, images, audio, video; plus it completes missing pieces through data fusion 13 | Machine Learning: big data often doesn't ask why and simply detects patterns 14 | Digital footprint: big data is often a cost-free byproduct of digital interaction -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/label/science/f03.txt: -------------------------------------------------------------------------------- 1 | Big data requires exceptional technologies to efficiently process large quantities of data within tolerable elapsed times. 2 | A 2011 McKinsey report suggests suitable technologies include A/B testing, crowdsourcing, data fusion and integration, genetic algorithms, machine learning, natural language processing, signal processing, simulation, time series analysis and visualisation. 3 | Multidimensional big data can also be represented as tensors, which can be more efficiently handled by tensor-based computation, such as multilinear subspace learning. 4 | Additional technologies being applied to big data include massively parallel-processing (MPP) databases, search-based applications, data mining, distributed file systems, distributed databases, cloud-based infrastructure (applications, storage and computing resources) and the Internet. 5 | Some but not all MPP relational databases have the ability to store and manage petabytes of data. Implicit is the ability to load, monitor, back up, and optimize the use of the large data tables in the RDBMS. 6 | DARPA's Topological Data Analysis program seeks the fundamental structure of massive data sets and in 2008 the technology went public with the launch of a company called Ayasdi. 7 | The practitioners of big data analytics processes are generally hostile to slower shared storage, preferring direct-attached storage (DAS) in its various forms from solid state drive (Ssd) to high capacity SATA disk buried inside parallel processing nodes. 8 | The perception of shared storage architectures—Storage area network (SAN) and Network-attached storage (NAS) —is that they are relatively slow, complex, and expensive. 9 | These qualities are not consistent with big data analytics systems that thrive on system performance, commodity infrastructure, and low cost. 10 | Real or near-real time information delivery is one of the defining characteristics of big data analytics. 11 | Latency is therefore avoided whenever and wherever possible. 12 | Data in memory is good—data on spinning disk at the other end of a FC SAN connection is not. 13 | The cost of a SAN at the scale needed for analytics applications is very much higher than other storage techniques. 14 | There are advantages as well as disadvantages to shared storage in big data analytics, but big data analytics practitioners as of 2011 did not favour it.[50] -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/label/science/f04.txt: -------------------------------------------------------------------------------- 1 | Big data has increased the demand of information management specialists in that Software AG, Oracle Corporation, IBM, Microsoft, SAP, EMC, HP and Dell have spent more than $15 billion on software firms specializing in data management and analytics. 2 | In 2010, this industry was worth more than $100 billion and was growing at almost 10 percent a year: about twice as fast as the software business as a whole. 3 | Developed economies increasingly use data-intensive technologies. 4 | There are 4.6 billion mobile-phone subscriptions worldwide, and between 1 billion and 2 billion people accessing the internet. 5 | Between 1990 and 2005, more than 1 billion people worldwide entered the middle class, which means more people become more literate, which in turn leads to information growth. 6 | The world's effective capacity to exchange information through telecommunication networks was 281 petabytes in 1986, 471 petabytes in 1993, 2.2 exabytes in 2000, 65 exabytes in 2007 and predictions put the amount of internet traffic at 667 exabytes annually by 2014. 7 | According to one estimate, one third of the globally stored information is in the form of alphanumeric text and still image data, 8 | which is the format most useful for most big data applications. 9 | This also shows the potential of yet unused data (i.e. in the form of video and audio content). 10 | While many vendors offer off-the-shelf solutions for Big Data, experts recommend the development of in-house solutions custom-tailored to solve the company's problem at hand if the company has sufficient technical capabilities. -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/label/science/f05.txt: -------------------------------------------------------------------------------- 1 | Machine learning is a subfield of computer science that evolved from the study of pattern recognition and computational learning theory in artificial intelligence. 2 | Machine learning explores the study and construction of algorithms that can learn from and make predictions on data. 3 | Such algorithms operate by building a model from example inputs in order to make data-driven predictions or decisions, rather than following strictly static program instructions. 4 | Machine learning is closely related to computational statistics; a discipline that aims at the design of algorithms for implementing statistical methods on computers. 5 | It has strong ties to mathematical optimization, which delivers methods, theory and application domains to the field. 6 | Machine learning is employed in a range of computing tasks where designing and programming explicit algorithms is infeasible. 7 | Example applications include spam filtering, optical character recognition (OCR), search engines and computer vision. 8 | Machine learning is sometimes conflated with data mining, although that focuses more on exploratory data analysis. 9 | Machine learning and pattern recognition "can be viewed as two facets of the same field." 10 | When employed in industrial contexts, machine learning methods may be referred to as predictive analytics or predictive modelling. -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/label/science/f06.txt: -------------------------------------------------------------------------------- 1 | Machine learning tasks are typically classified into three broad categories, depending on the nature of the learning "signal" or "feedback" available to a learning system. 2 | Supervised learning: The computer is presented with example inputs and their desired outputs, given by a "teacher", and the goal is to learn a general rule that maps inputs to outputs. 3 | Unsupervised learning: No labels are given to the learning algorithm, leaving it on its own to find structure in its input. 4 | Unsupervised learning can be a goal in itself (discovering hidden patterns in data) or a means towards an end (feature learning). 5 | Reinforcement learning: A computer program interacts with a dynamic environment in which it must perform a certain goal (such as driving a vehicle), without a teacher explicitly telling it whether it has come close to its goal. 6 | Another example is learning to play a game by playing against an opponent. 7 | Between supervised and unsupervised learning is semi-supervised learning, where the teacher gives an incomplete training signal: a training set with some (often many) of the target outputs missing. 8 | Transduction is a special case of this principle where the entire set of problem instances is known at learning time, except that part of the targets are missing. 9 | A support vector machine is a classifier that divides its input space into two regions, separated by a linear boundary. 10 | Here, it has learned to distinguish black and white circles. 11 | Among other categories of machine learning problems, learning to learn learns its own inductive bias based on previous experience. 12 | Developmental learning, elaborated for robot learning, generates its own sequences (also called curriculum) of learning situations to cumulatively acquire repertoires of novel skills through autonomous self-exploration and social interaction with human teachers, and using guidance mechanisms such as active learning, maturation, motor synergies, and imitation. -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/label/science/f07.txt: -------------------------------------------------------------------------------- 1 | Another categorization of machine learning tasks arises when one considers the desired output of a machine-learned system: 2 | In classification, inputs are divided into two or more classes, and the learner must produce a model that assigns unseen inputs to one (or multi-label classification) or more of these classes. 3 | This is typically tackled in a supervised way. 4 | Spam filtering is an example of classification, where the inputs are email (or other) messages and the classes are "spam" and "not spam". 5 | In regression, also a supervised problem, the outputs are continuous rather than discrete. 6 | In clustering, a set of inputs is to be divided into groups. 7 | Unlike in classification, the groups are not known beforehand, making this typically an unsupervised task. 8 | Density estimation finds the distribution of inputs in some space. 9 | Dimensionality reduction simplifies inputs by mapping them into a lower-dimensional space. 10 | Topic modeling is a related problem, where a program is given a list of human language documents and is tasked to find out which documents cover similar topics. -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/label/science/f08.txt: -------------------------------------------------------------------------------- 1 | A core objective of a learner is to generalize from its experience. 2 | Generalization in this context is the ability of a learning machine to perform accurately on new, unseen examples/tasks after having experienced a learning data set. 3 | The training examples come from some generally unknown probability distribution (considered representative of the space of occurrences) and the learner has to build a general model about this space that enables it to produce sufficiently accurate predictions in new cases. 4 | The computational analysis of machine learning algorithms and their performance is a branch of theoretical computer science known as computational learning theory. 5 | Because training sets are finite and the future is uncertain, learning theory usually does not yield guarantees of the performance of algorithms. 6 | Instead, probabilistic bounds on the performance are quite common. The bias–variance decomposition is one way to quantify generalization error. 7 | In addition to performance bounds, computational learning theorists study the time complexity and feasibility of learning. 8 | In computational learning theory, a computation is considered feasible if it can be done in polynomial time. 9 | There are two kinds of time complexity results. 10 | Positive results show that a certain class of functions can be learned in polynomial time. 11 | Negative results show that certain classes cannot be learned in polynomial time. 12 | There are many similarities between machine learning theory and statistical inference, although they use different terms. -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/label/science/f09.txt: -------------------------------------------------------------------------------- 1 | An artificial neural network (ANN) learning algorithm, usually called "neural network" (NN), is a learning algorithm that is inspired by the structure and functional aspects of biological neural networks. 2 | Computations are structured in terms of an interconnected group of artificial neurons, processing information using a connectionist approach to computation. 3 | Modern neural networks are non-linear statistical data modeling tools. 4 | They are usually used to model complex relationships between inputs and outputs, to find patterns in data, or to capture the statistical structure in an unknown joint probability distribution between observed variables. -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/label/science/f10.txt: -------------------------------------------------------------------------------- 1 | Several learning algorithms, mostly unsupervised learning algorithms, aim at discovering better representations of the inputs provided during training. 2 | Classical examples include principal components analysis and cluster analysis. 3 | Representation learning algorithms often attempt to preserve the information in their input but transform it in a way that makes it useful, often as a pre-processing step before performing classification or predictions, allowing to reconstruct the inputs coming from the unknown data generating distribution, while not being necessarily faithful for configurations that are implausible under that distribution. 4 | Manifold learning algorithms attempt to do so under the constraint that the learned representation is low-dimensional. 5 | Sparse coding algorithms attempt to do so under the constraint that the learned representation is sparse (has many zeros). 6 | Multilinear subspace learning algorithms aim to learn low-dimensional representations directly from tensor representations for multidimensional data, without reshaping them into (high-dimensional) vectors. 7 | Deep learning algorithms discover multiple levels of representation, or a hierarchy of features, with higher-level, more abstract features defined in terms of (or generating) lower-level features. 8 | It has been argued that an intelligent machine is one that learns a representation that disentangles the underlying factors of variation that explain the observed data. -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/raw_sentences.txt: -------------------------------------------------------------------------------- 1 | No , he says now . 2 | And what did he do ? 3 | The money 's there . 4 | That was less than a year ago . 5 | But he made only the first . 6 | There 's still time for them to do it . 7 | But he should nt have . 8 | They have to come down to the people . 9 | I do nt know where that is . 10 | No , I would nt . 11 | Who Will It Be ? 12 | And no , I was not the one . -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/unlabeled/document1/f01.txt: -------------------------------------------------------------------------------- 1 | The Russian Trading System (RTS) was a stock market established in 1995 in Moscow, consolidating various regional trading floors into one exchange. 2 | Originally RTS was modeled on NASDAQ's trading and settlement software; in 1998 the exchange went on line with its own in-house system. 3 | Initially created as a non-profit organisation, it was transformed into a joint-stock company. 4 | In 2011 MICEX merged with RTS creating Moscow Exchange. 5 | The RTS Index, RTSI, the official Moscow Exchange indicator, was first calculated on September 1, 1995. 6 | The RTS is a capitalization-weighted composite index calculated based on prices of the 50 most liquid Russian stocks listed on Moscow Exchange. 7 | The Index is calculated in real time and denominated in US dollars. -------------------------------------------------------------------------------- /05_Implementing_NLP/sourceCode/cookbookapp/src/main/resources/unlabeled/document2/f02.txt: -------------------------------------------------------------------------------- 1 | Human immunodeficiency virus infection and acquired immune deficiency syndrome (HIV/AIDS) is a spectrum of conditions caused by infection with the human immunodeficiency virus (HIV). 2 | It may also be referred to as HIV disease or HIV infection. 3 | Following initial infection, a person may experience a brief period of influenza-like illness. 4 | This is typically followed by a prolonged period without symptoms. 5 | As the infection progresses, it interferes more and more with the immune system, making the person much more susceptible to common infections, like tuberculosis, as well as opportunistic infections and tumors that do not usually affect people who have working immune systems. 6 | The late symptoms of the infection are referred to as AIDS. 7 | This stage is often complicated by an infection of the lung known as pneumocystis pneumonia, severe weight loss, skin lesions caused by Kaposi's sarcoma, or other AIDS-defining conditions. 8 | HIV is transmitted primarily via unprotected sexual intercourse (including anal and oral sex), contaminated blood transfusions, hypodermic needles, and from mother to child during pregnancy, delivery, or breastfeeding. 9 | Some bodily fluids, such as saliva and tears, do not transmit HIV. 10 | Common methods of HIV/AIDS prevention include encouraging and practicing safe sex, needle-exchange programs, and treating those who are infected. 11 | There is no cure or vaccine; however, antiretroviral treatment can slow the course of the disease and may lead to a near-normal life expectancy. 12 | While antiretroviral treatment reduces the risk of death and complications from the disease, these medications are expensive and have side effects. 13 | Treatment is recommended as soon as the diagnosis is made. 14 | Without treatment, the average survival time after infection with HIV is estimated to be 9 to 11 years, depending on the HIV subtype. -------------------------------------------------------------------------------- /06_Constructing_LSTM_Network_for_time_series/README.md: -------------------------------------------------------------------------------- 1 | 2 | ### Chapter 6 : **Constructing LSTM Network for time series** 3 | 4 | In this chapter, you will learn how to develop LSTM neural network for time series application with single-class output using DL4J. 5 |   6 |   7 | 8 | #### Instructions 9 | 10 | - Navigate to **sourceCode/cookbookapp-lstm-time-series** directory and import the project using *pom.xml* file. 11 | - We use *PhysioNet* Dataset for the time series application. 12 | -------------------------------------------------------------------------------- /06_Constructing_LSTM_Network_for_time_series/sourceCode/cookbookapp-lstm-time-series/.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | target 3 | cookbook-app.iml 4 | cookbook-app.iws 5 | cookbook-app.ipr 6 | cookbookapp.iml 7 | cookbookapp-nlp.iml 8 | cookbookapp-cnn.iml 9 | dependency-reduced-pom.xml 10 | model.zip 11 | LocalExecuteExample.csv 12 | words.txt -------------------------------------------------------------------------------- /06_Constructing_LSTM_Network_for_time_series/sourceCode/cookbookapp-lstm-time-series/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.javadeeplearningcookbook.app 8 | cookbookapp-lstm-time-series 9 | 1.0-SNAPSHOT 10 | 11 | 12 | junit 13 | junit 14 | 4.11 15 | test 16 | 17 | 18 | org.deeplearning4j 19 | deeplearning4j-core 20 | 1.0.0-beta3 21 | 22 | 23 | org.nd4j 24 | nd4j-native-platform 25 | 1.0.0-beta3 26 | 27 | 28 | org.datavec 29 | datavec-api 30 | 1.0.0-beta3 31 | 32 | 33 | 34 | org.datavec 35 | datavec-data-codec 36 | 1.0.0-beta3 37 | 38 | 39 | org.bytedeco.javacpp-presets 40 | opencv 41 | 4.0.1-1.4.4 42 | 43 | 44 | org.bytedeco.javacpp-presets 45 | opencv-platform 46 | 4.0.1-1.4.4 47 | 48 | 49 | org.bytedeco 50 | javacv-platform 51 | 1.4.4 52 | 53 | 54 | org.bytedeco 55 | javacpp 56 | 1.4.4 57 | 58 | 59 | org.slf4j 60 | slf4j-simple 61 | 1.8.0-beta4 62 | 63 | 64 | org.slf4j 65 | slf4j-api 66 | 1.8.0-beta4 67 | 68 | 69 | 70 | org.datavec 71 | datavec-local 72 | 1.0.0-beta3 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /07_Constructing_LSTM_Neural_network_for_sequence_classification/README.md: -------------------------------------------------------------------------------- 1 | 2 | ### Chapter 7 : ****Constructing LSTM Neural network for sequence classification**** 3 | 4 | In this chapter, you will learn how to develop LSTM neural network for time series application on a UCI synthetic control dataset with *multi-class* output using DL4J. 5 |   6 | 7 | #### Instructions 8 | 9 | - Navigate to **sourceCode/cookbookapp** directory and import the project using *pom.xml* file. 10 | - We use UCI synthetic control Dataset for the time series application. 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /07_Constructing_LSTM_Neural_network_for_sequence_classification/sourceCode/cookbookapp/.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | target 3 | cookbook-app.iml 4 | cookbook-app.iws 5 | cookbook-app.ipr 6 | cookbookapp.iml 7 | cookbookapp-nlp.iml 8 | cookbookapp-cnn.iml 9 | dependency-reduced-pom.xml 10 | model.zip 11 | LocalExecuteExample.csv 12 | words.txt -------------------------------------------------------------------------------- /07_Constructing_LSTM_Neural_network_for_sequence_classification/sourceCode/cookbookapp/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.javadeeplearningcookbook.app 8 | cookbook-app 9 | 1.0-SNAPSHOT 10 | 11 | 12 | junit 13 | junit 14 | 4.11 15 | test 16 | 17 | 18 | org.deeplearning4j 19 | deeplearning4j-core 20 | 1.0.0-beta4 21 | 22 | 23 | org.nd4j 24 | nd4j-native-platform 25 | 1.0.0-beta4 26 | 27 | 28 | org.datavec 29 | datavec-api 30 | 1.0.0-beta4 31 | 32 | 33 | 34 | org.datavec 35 | datavec-data-codec 36 | 1.0.0-beta4 37 | 38 | 39 | org.bytedeco.javacpp-presets 40 | opencv 41 | 4.0.1-1.4.4 42 | 43 | 44 | org.bytedeco.javacpp-presets 45 | opencv-platform 46 | 4.0.1-1.4.4 47 | 48 | 49 | org.bytedeco 50 | javacv-platform 51 | 1.4.4 52 | 53 | 54 | org.bytedeco 55 | javacpp 56 | 1.4.4 57 | 58 | 59 | org.slf4j 60 | slf4j-simple 61 | 1.8.0-beta4 62 | 63 | 64 | org.slf4j 65 | slf4j-api 66 | 1.8.0-beta4 67 | 68 | 69 | 70 | org.datavec 71 | datavec-local 72 | 1.0.0-beta4 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /08_Performing_Anomaly_detection_on_unsupervised_data/README.md: -------------------------------------------------------------------------------- 1 | 2 | ### Chapter 8 : **Performing Anomaly detection on unsupervised data** 3 | 4 | In this chapter, you will learn how to develop an unsupervised anomaly detection application using DL4J. 5 |   6 | 7 | #### Instructions 8 | 9 | - Navigate to **sourceCode/cookbook-app** directory and import the project using *pom.xml* file. 10 | - We use *MNIST* Dataset for the unsupervised anomaly detection example. 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /08_Performing_Anomaly_detection_on_unsupervised_data/sourceCode/cookbook-app/.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | target 3 | cookbook-app.iml 4 | cookbook-app.iws 5 | cookbook-app.ipr 6 | cookbookapp.iml 7 | sourceCode.iml 8 | dependency-reduced-pom.xml 9 | model.zip 10 | LocalExecuteExample.csv 11 | words.txt -------------------------------------------------------------------------------- /08_Performing_Anomaly_detection_on_unsupervised_data/sourceCode/cookbook-app/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.javadeeplearningcookbook.app 8 | cookbook-app 9 | 1.0-SNAPSHOT 10 | 11 | 12 | junit 13 | junit 14 | 4.11 15 | test 16 | 17 | 18 | org.deeplearning4j 19 | deeplearning4j-core 20 | 1.0.0-beta4 21 | 22 | 23 | org.nd4j 24 | nd4j-native-platform 25 | 1.0.0-beta4 26 | 27 | 28 | org.datavec 29 | datavec-api 30 | 1.0.0-beta4 31 | 32 | 33 | 34 | org.datavec 35 | datavec-data-codec 36 | 1.0.0-beta4 37 | 38 | 39 | org.bytedeco.javacpp-presets 40 | opencv 41 | 4.0.1-1.4.4 42 | 43 | 44 | org.bytedeco.javacpp-presets 45 | opencv-platform 46 | 4.0.1-1.4.4 47 | 48 | 49 | org.bytedeco 50 | javacv-platform 51 | 1.4.4 52 | 53 | 54 | org.bytedeco 55 | javacpp 56 | 1.4.4 57 | 58 | 59 | org.slf4j 60 | slf4j-simple 61 | 1.8.0-beta4 62 | 63 | 64 | org.slf4j 65 | slf4j-api 66 | 1.8.0-beta4 67 | 68 | 69 | 70 | org.datavec 71 | datavec-local 72 | 1.0.0-beta4 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /09_Using_RL4J_for_Reinforcement_learning/README.md: -------------------------------------------------------------------------------- 1 | 2 | ### Chapter 9 : **Using RL4J for Reinforcement learning** 3 | 4 | In this chapter, you will learn how to develop a reinforcement learning agent that can learn to play the Malmo game using RL4J. 5 |   6 | 7 | #### Instructions 8 | 9 | - Navigate to **sourceCode/cookbookapp** directory and import the project using *pom.xml* file. 10 | - As a pre-requisite, Project Malmo needs to be installed and configured in your system. Follow the detailed instructions mentioned in the cookbook. 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /09_Using_RL4J_for_Reinforcement_learning/sourceCode/cookbookapp/.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | target 3 | */target/** 4 | cookbook-app.iml 5 | cookbook-app.iws 6 | cookbook-app.ipr 7 | cookbookapp.iml 8 | dependency-reduced-pom.xml 9 | model.zip 10 | LocalExecuteExample.csv 11 | cliffwalk_pixel.policy -------------------------------------------------------------------------------- /09_Using_RL4J_for_Reinforcement_learning/sourceCode/cookbookapp/src/main/resources/cliff_walking_rl4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | Cliff walking mission based on Sutton and Barto. 10 | 11 | 12 | 13 | 1 14 | 15 | 16 | 17 | 18 | 22 | clear 23 | false 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | Sherlock 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 320 56 | 240 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /10_Developing_applications_in_distributed_environment/README.md: -------------------------------------------------------------------------------- 1 | 2 | ### Chapter 10 : **Developing applications in distributed environment** 3 | 4 | In this chapter, you will learn how to develop distributed deep learning applications using DL4J. 5 |   6 | 7 | #### Instructions 8 | 9 | - Navigate to **sourceCode/cookbookapp** directory and import the project using *pom.xml* file. 10 | - *TinyImageNet* dataset is used in distributed deep learning example discussed in this chapter. 11 | -------------------------------------------------------------------------------- /10_Developing_applications_in_distributed_environment/sourceCode/cookbookapp/.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | target 3 | cookbook-app.iml 4 | cookbookapp.iml 5 | cookbook-app.iws 6 | cookbook-app.ipr 7 | cookbookapp.iml 8 | workspace.xml 9 | dependency-reduced-pom.xml 10 | model.zip 11 | LocalExecuteExample.csv -------------------------------------------------------------------------------- /10_Developing_applications_in_distributed_environment/sourceCode/cookbookapp/src/main/java/com/javacookbook/app/PreProcessLocal.java: -------------------------------------------------------------------------------- 1 | package com.javacookbook.app; 2 | 3 | import com.beust.jcommander.Parameter; 4 | import org.datavec.image.loader.NativeImageLoader; 5 | import org.deeplearning4j.common.resources.DL4JResources; 6 | import org.deeplearning4j.common.resources.ResourceType; 7 | import org.deeplearning4j.datasets.fetchers.TinyImageNetFetcher; 8 | import org.deeplearning4j.spark.util.SparkDataUtils; 9 | 10 | import java.io.File; 11 | 12 | /* 13 | Use this source code to pre-process the data and save the batch files to your local disk. 14 | You would need to manually transfer them to HDFS. 15 | */ 16 | public class PreProcessLocal { 17 | //The directory in which you would like to save your batches. 18 | private String localSaveDir = "{PATH-TO-SAVE-PREPROCESSED-DATASET}"; 19 | 20 | @Parameter(names = {"--batchSize"}, description = "Batch size for saving the data", required = false) 21 | private int batchSize = 32; 22 | 23 | public static void main(String[] args) throws Exception { 24 | new PreProcessLocal().entryPoint(args); 25 | } 26 | 27 | protected void entryPoint(String[] args) throws Exception { 28 | 29 | if(localSaveDir.equals("{PATH-TO-SAVE-PREPROCESSED-DATASET}")){ 30 | System.out.println("Please specify the directory in which pre-processed dataset needs to be stored"); 31 | System.exit(0); 32 | } 33 | 34 | TinyImageNetFetcher f = new TinyImageNetFetcher(); 35 | f.downloadAndExtract(); 36 | 37 | //Preprocess the training set 38 | File baseDirTrain = DL4JResources.getDirectory(ResourceType.DATASET, f.localCacheName() + "/train"); 39 | File saveDirTrain = new File(localSaveDir, "train"); 40 | if(!saveDirTrain.exists()) 41 | saveDirTrain.mkdirs(); 42 | SparkDataUtils.createFileBatchesLocal(baseDirTrain, NativeImageLoader.ALLOWED_FORMATS, true, saveDirTrain, batchSize); 43 | 44 | //Preprocess the test set 45 | File baseDirTest = DL4JResources.getDirectory(ResourceType.DATASET, f.localCacheName() + "/test"); 46 | File saveDirTest = new File(localSaveDir, "test"); 47 | if(!saveDirTest.exists()) 48 | saveDirTest.mkdirs(); 49 | SparkDataUtils.createFileBatchesLocal(baseDirTest, NativeImageLoader.ALLOWED_FORMATS, true, saveDirTest, batchSize); 50 | 51 | System.out.println("----- Data Preprocessing Complete -----"); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /10_Developing_applications_in_distributed_environment/sourceCode/cookbookapp/src/main/java/com/javacookbook/app/PreprocessSpark.java: -------------------------------------------------------------------------------- 1 | package com.javacookbook.app; 2 | 3 | import com.beust.jcommander.JCommander; 4 | import com.beust.jcommander.Parameter; 5 | import org.apache.spark.SparkConf; 6 | import org.apache.spark.api.java.JavaRDD; 7 | import org.apache.spark.api.java.JavaSparkContext; 8 | import org.datavec.image.loader.NativeImageLoader; 9 | import org.deeplearning4j.spark.util.SparkDataUtils; 10 | import org.deeplearning4j.spark.util.SparkUtils; 11 | 12 | /** 13 | * This file is for preparing the training data for the tiny imagenet CNN example. 14 | * Either this class OR PreprocessLocal (but not both) must be run before training can be run on a cluster via TrainSpark. 15 | * 16 | * PreprocessSpark requires that the tiny imagenet source image files (.jpg format) available on network storage that 17 | * Spark can access, such as HDFS, Azure blob storage, S3 etc. 18 | * 19 | * To get these image files, you have two options: 20 | * 21 | * Option 1: Direct download (We followed this approach in this source.) 22 | * Step 1: Download https://deeplearning4jblob.blob.core.windows.net/datasets/tinyimagenet_200_dl4j.v1.zip or http://cs231n.stanford.edu/tiny-imagenet-200.zip 23 | * Step 2: Extract files locally 24 | * Step 3: Copy contents (in their existing train/test subdirectories) to remote storage (for example, using Hadoop FS utils or similar) 25 | * 26 | * Option 2: Use TinyImageNetFetcher to download 27 | * Step 1: Run {@code new TinyImageNetFetcher().downloadAndExtract()} to download the files 28 | * Step 2: Copy the contents of the following directory to remote storage (for example, using Hadoop FS utils or similar) 29 | * Windows: C:\Users\\.deeplearning4j\data\TINYIMAGENET_200 30 | * Linux: ~/.deeplearning4j/data/TINYIMAGENET_200 31 | * 32 | * After completing the steps of option 1 or option 2, then run this script to preprocess the data. 33 | * 34 | * @author Alex Black 35 | */ 36 | public class PreprocessSpark { 37 | 38 | /* 39 | Sample data to be passed: --sourceDir="hdfs://localhost:9000/user/hadoop/tiny-imagenet-200/" ; 40 | */ 41 | @Parameter(names = {"--sourceDir"}, description = "Directory to get source image files", required = true) 42 | public String sourceDir=null; 43 | 44 | /* 45 | Sample data to be passed: --saveDir="hdfs://localhost:9000/user/hadoop/batches/" ; 46 | */ 47 | @Parameter(names = {"--saveDir"}, description = "Directory to save the preprocessed data files on remote storage (for example, HDFS)", required = true) 48 | private String saveDir=null; 49 | 50 | @Parameter(names = {"--batchSize"}, description = "Batch size for saving the data", required = false) 51 | private int batchSize = 32; 52 | 53 | public static void main(String[] args) throws Exception { 54 | new PreprocessSpark().entryPoint(args); 55 | } 56 | 57 | protected void entryPoint(String[] args) throws Exception { 58 | JCommander jcmdr = new JCommander(this); 59 | jcmdr.parse(args); 60 | //JCommanderUtils.parseArgs(this, args); 61 | SparkConf conf = new SparkConf(); 62 | conf.setMaster("local[*]"); 63 | conf.setAppName("DL4JTinyImageNetSparkPreproc"); 64 | JavaSparkContext sc = new JavaSparkContext(conf); 65 | 66 | //Create training set 67 | JavaRDD filePathsTrain = SparkUtils.listPaths(sc, sourceDir + "/train", true, NativeImageLoader.ALLOWED_FORMATS); 68 | SparkDataUtils.createFileBatchesSpark(filePathsTrain, saveDir, batchSize, sc); 69 | 70 | //Create test set 71 | JavaRDD filePathsTest = SparkUtils.listPaths(sc, sourceDir + "/test", true, NativeImageLoader.ALLOWED_FORMATS); 72 | SparkDataUtils.createFileBatchesSpark(filePathsTest, saveDir, batchSize, sc); 73 | 74 | 75 | System.out.println("----- Data Preprocessing Complete -----"); 76 | } 77 | 78 | } -------------------------------------------------------------------------------- /11_Applying_Transfer_Learning_to_network_models/README.md: -------------------------------------------------------------------------------- 1 | 2 | ### Chapter 11 : **Applying Transfer Learning to network models** 3 | 4 | In this chapter, you will learn how to apply transfer learning to DL4J applications. 5 |   6 | 7 | #### Instructions 8 | 9 | - Navigate to **sourceCode/cookbookapp** directory and import the project using *pom.xml* file. 10 | - Customer churn dataset is required for the example "**TransferLearnChurnExample**". It is already included in *resources* directory. 11 | -------------------------------------------------------------------------------- /11_Applying_Transfer_Learning_to_network_models/sourceCode/cookbookapp/.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | target 3 | cookbook-app.iml 4 | cookbookapp.iml 5 | cookbook-app.iws 6 | cookbook-app.ipr 7 | cookbookapp.iml 8 | workspace.xml 9 | dependency-reduced-pom.xml 10 | model.zip 11 | LocalExecuteExample.csv -------------------------------------------------------------------------------- /11_Applying_Transfer_Learning_to_network_models/sourceCode/cookbookapp/src/main/java/DataSetIteratorHelper.java: -------------------------------------------------------------------------------- 1 | import org.datavec.api.records.reader.RecordReader; 2 | import org.datavec.api.records.reader.impl.csv.CSVRecordReader; 3 | import org.datavec.api.records.reader.impl.transform.TransformProcessRecordReader; 4 | import org.datavec.api.split.FileSplit; 5 | import org.datavec.api.transform.TransformProcess; 6 | import org.datavec.api.transform.schema.Schema; 7 | import org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator; 8 | import org.deeplearning4j.datasets.iterator.AsyncDataSetIterator; 9 | import org.deeplearning4j.datasets.iterator.DataSetIteratorSplitter; 10 | import org.nd4j.linalg.dataset.ExistingMiniBatchDataSetIterator; 11 | import org.nd4j.linalg.dataset.api.iterator.DataSetIterator; 12 | import org.nd4j.linalg.dataset.api.preprocessor.DataNormalization; 13 | import org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize; 14 | import org.nd4j.linalg.io.ClassPathResource; 15 | 16 | import java.io.File; 17 | import java.io.IOException; 18 | import java.util.Arrays; 19 | 20 | public class DataSetIteratorHelper { 21 | 22 | private final static int labelIndex=11; 23 | private final static int batchSize=8; 24 | private final static int numClasses=2; 25 | private static final int featurizeExtractionLayer = 1; 26 | public static DataSetIterator trainIterator() throws IOException, InterruptedException { 27 | return createDataSetSplitter().getTrainIterator(); 28 | } 29 | public static DataSetIterator testIterator() throws IOException, InterruptedException { 30 | return createDataSetSplitter().getTestIterator(); 31 | } 32 | 33 | private static DataSetIteratorSplitter createDataSetSplitter() throws IOException, InterruptedException { 34 | final RecordReader recordReader = DataSetIteratorHelper.generateReader(new ClassPathResource("Churn_Modelling.csv").getFile()); 35 | final DataSetIterator dataSetIterator = new RecordReaderDataSetIterator.Builder(recordReader,batchSize) 36 | .classification(labelIndex,numClasses) 37 | .build(); 38 | final DataNormalization dataNormalization = new NormalizerStandardize(); 39 | dataNormalization.fit(dataSetIterator); 40 | dataSetIterator.setPreProcessor(dataNormalization); 41 | final DataSetIteratorSplitter dataSetIteratorSplitter = new DataSetIteratorSplitter(dataSetIterator,1250,0.8); 42 | return dataSetIteratorSplitter; 43 | } 44 | private static Schema generateSchema(){ 45 | final Schema schema; 46 | schema = new Schema.Builder() 47 | .addColumnString("RowNumber") 48 | .addColumnInteger("CustomerId") 49 | .addColumnString("Surname") 50 | .addColumnInteger("CreditScore") 51 | .addColumnCategorical("Geography", Arrays.asList("France","Germany","Spain")) 52 | .addColumnCategorical("Gender", Arrays.asList("Male","Female")) 53 | .addColumnsInteger("Age", "Tenure") 54 | .addColumnDouble("Balance") 55 | .addColumnsInteger("NumOfProducts","HasCrCard","IsActiveMember") 56 | .addColumnDouble("EstimatedSalary") 57 | .addColumnInteger("Exited") 58 | .build(); 59 | return schema; 60 | 61 | } 62 | private static RecordReader applyTransform(RecordReader recordReader, Schema schema){ 63 | final TransformProcess transformProcess = new TransformProcess.Builder(schema) 64 | .removeColumns("RowNumber","CustomerId","Surname") 65 | .categoricalToInteger("Gender") 66 | .categoricalToOneHot("Geography") 67 | .removeColumns("Geography[France]") 68 | .build(); 69 | final TransformProcessRecordReader transformProcessRecordReader = new TransformProcessRecordReader(recordReader,transformProcess); 70 | return transformProcessRecordReader; 71 | 72 | } 73 | public static RecordReader generateReader(File file) throws IOException, InterruptedException { 74 | final RecordReader recordReader = new CSVRecordReader(1,','); 75 | recordReader.initialize(new FileSplit(file)); 76 | final RecordReader transformProcessRecordReader=applyTransform(recordReader,generateSchema()); 77 | return transformProcessRecordReader; 78 | } 79 | 80 | public static DataSetIterator trainIteratorFeaturized(){ 81 | DataSetIterator trainIter = new ExistingMiniBatchDataSetIterator(new File("{PATH-TO-SAVE-TRAIN-SAMPLES}"),"churn-"+featurizeExtractionLayer+"-train-%d.bin"); 82 | return new AsyncDataSetIterator(trainIter); 83 | 84 | } 85 | public static DataSetIterator testIteratorFeaturized(){ 86 | DataSetIterator testIter = new ExistingMiniBatchDataSetIterator(new File("{PATH-TO-SAVE-TEST-SAMPLES}"),"churn-"+featurizeExtractionLayer+"-test-%d.bin"); 87 | return new AsyncDataSetIterator(testIter); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /11_Applying_Transfer_Learning_to_network_models/sourceCode/cookbookapp/src/main/java/SaveFeaturizedDataExample.java: -------------------------------------------------------------------------------- 1 | import org.deeplearning4j.nn.api.OptimizationAlgorithm; 2 | import org.deeplearning4j.nn.conf.GradientNormalization; 3 | import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; 4 | import org.deeplearning4j.nn.transferlearning.FineTuneConfiguration; 5 | import org.deeplearning4j.nn.transferlearning.TransferLearning; 6 | import org.deeplearning4j.nn.transferlearning.TransferLearningHelper; 7 | import org.deeplearning4j.nn.weights.WeightInit; 8 | import org.nd4j.linalg.activations.Activation; 9 | import org.nd4j.linalg.dataset.DataSet; 10 | import org.nd4j.linalg.dataset.api.iterator.DataSetIterator; 11 | import org.nd4j.linalg.learning.config.Nesterovs; 12 | import org.slf4j.Logger; 13 | 14 | import java.io.File; 15 | import java.io.FileNotFoundException; 16 | import java.io.IOException; 17 | 18 | public class SaveFeaturizedDataExample { 19 | private static final Logger log = org.slf4j.LoggerFactory.getLogger(SaveFeaturizedDataExample.class); 20 | private static final int featurizeExtractionLayer = 1; 21 | private static final long seed = 12345; 22 | 23 | public static void main(String[] args) throws IOException, InterruptedException { 24 | //Use the model file saved from chapter 3 example. 25 | File savedLocation = new File("{PATH-TO-MODEL-FILE}"); //Where to save the network. Note: the file is in .zip format - can be opened externally 26 | boolean saveUpdater = true; 27 | 28 | try{ 29 | MultiLayerNetwork oldModel = MultiLayerNetwork.load(savedLocation, saveUpdater); 30 | 31 | FineTuneConfiguration fineTuneConf = new FineTuneConfiguration.Builder() 32 | .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT) 33 | .updater(new Nesterovs(5e-5)) 34 | .biasInit(0.001) 35 | .gradientNormalization(GradientNormalization.RenormalizeL2PerLayer) 36 | .l2(0.0001) 37 | .weightInit(WeightInit.DISTRIBUTION) 38 | .seed(seed) 39 | .build(); 40 | 41 | MultiLayerNetwork newModel = new TransferLearning.Builder(oldModel) 42 | .fineTuneConfiguration(fineTuneConf) 43 | .setFeatureExtractor(featurizeExtractionLayer) 44 | .build(); 45 | 46 | TransferLearningHelper transferLearningHelper = new TransferLearningHelper(oldModel); 47 | DataSetIterator trainIter = DataSetIteratorHelper.trainIterator(); 48 | DataSetIterator testIter = DataSetIteratorHelper.testIterator(); 49 | 50 | int trainDataSaved = 0; 51 | while(trainIter.hasNext()) { 52 | DataSet currentFeaturized = transferLearningHelper.featurize(trainIter.next()); 53 | saveToDisk(currentFeaturized,trainDataSaved,true); 54 | trainDataSaved++; 55 | } 56 | 57 | int testDataSaved = 0; 58 | while(testIter.hasNext()) { 59 | DataSet currentFeaturized = transferLearningHelper.featurize(testIter.next()); 60 | saveToDisk(currentFeaturized,testDataSaved,false); 61 | testDataSaved++; 62 | } 63 | } 64 | catch(FileNotFoundException e){ 65 | System.out.println("Please provide file path in place of: PATH-TO-MODEL-FILE, PATH-TO-SAVE-TRAIN-SAMPLES & PATH-TO-SAVE-TEST-SAMPLES"); 66 | 67 | } 68 | } 69 | 70 | private static void saveToDisk(DataSet currentFeaturized, int iterNum, boolean isTrain){ 71 | File fileFolder = isTrain ? new File("{PATH-TO-SAVE-TRAIN-SAMPLES}"): new File("{PATH-TO-SAVE-TEST-SAMPLES}"); 72 | if (iterNum == 0) { 73 | fileFolder.mkdirs(); 74 | } 75 | String fileName = "churn-" + featurizeExtractionLayer + "-"; 76 | fileName += isTrain ? "train-" : "test-"; 77 | fileName += iterNum + ".bin"; 78 | currentFeaturized.save(new File(fileFolder,fileName)); 79 | log.info("Saved " + (isTrain?"train ":"test ") + "dataset #"+ iterNum); 80 | } 81 | 82 | } 83 | 84 | -------------------------------------------------------------------------------- /11_Applying_Transfer_Learning_to_network_models/sourceCode/cookbookapp/src/main/java/TransferLearnChurnExample.java: -------------------------------------------------------------------------------- 1 | import org.deeplearning4j.nn.api.OptimizationAlgorithm; 2 | import org.deeplearning4j.nn.conf.GradientNormalization; 3 | import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; 4 | import org.deeplearning4j.nn.transferlearning.FineTuneConfiguration; 5 | import org.deeplearning4j.nn.transferlearning.TransferLearning; 6 | import org.deeplearning4j.nn.transferlearning.TransferLearningHelper; 7 | import org.deeplearning4j.nn.weights.WeightInit; 8 | import org.nd4j.evaluation.classification.Evaluation; 9 | import org.nd4j.linalg.dataset.api.iterator.DataSetIterator; 10 | import org.nd4j.linalg.learning.config.Nesterovs; 11 | import org.slf4j.Logger; 12 | 13 | import java.io.File; 14 | import java.io.FileNotFoundException; 15 | import java.io.IOException; 16 | 17 | public class TransferLearnChurnExample { 18 | private static final Logger log = org.slf4j.LoggerFactory.getLogger(TransferLearnChurnExample.class); 19 | private static final long seed = 12345; 20 | private static final int featurizeExtractionLayer = 1; 21 | private static final int nEpochs = 100; 22 | public static void main(String[] args) throws IOException{ 23 | 24 | //Use the model file saved from chapter 3 example. 25 | File savedLocation = new File("{PATH-TO-MODEL-FILE}"); //Where to save the network. Note: the file is in .zip format - can be opened externally 26 | boolean saveUpdater = true; 27 | try{ 28 | MultiLayerNetwork oldModel = MultiLayerNetwork.load(savedLocation, saveUpdater); 29 | //System.out.println(restored.getLayerWiseConfigurations().toJson()); 30 | 31 | FineTuneConfiguration fineTuneConf = new FineTuneConfiguration.Builder() 32 | .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT) 33 | .updater(new Nesterovs(5e-5)) 34 | .biasInit(0.001) 35 | .gradientNormalization(GradientNormalization.RenormalizeL2PerLayer) 36 | .l2(0.0001) 37 | .weightInit(WeightInit.DISTRIBUTION) 38 | .seed(seed) 39 | .build(); 40 | 41 | MultiLayerNetwork newModel = new TransferLearning.Builder(oldModel) 42 | .fineTuneConfiguration(fineTuneConf) 43 | .setFeatureExtractor(featurizeExtractionLayer) 44 | .build(); 45 | 46 | TransferLearningHelper transferLearningHelper = new TransferLearningHelper(newModel); 47 | 48 | DataSetIterator trainIterFeaturized = DataSetIteratorHelper.trainIteratorFeaturized(); 49 | DataSetIterator testIterFeaturized = DataSetIteratorHelper.testIteratorFeaturized(); 50 | 51 | for(int i=0;i