Getting Started with Deep Learning for Java - Sentiment Example

Overview

 This beginner's guide to deep learning for Java uses the sentiment example from eclipse's deep learning examples.

This guide uses the Valohai platform to run the deep learning executions.

Objective

 The objective of this guide is to demonstrate how to setup a Valohai project that builds software to train a neural network and execute evaluations of the trained network.

The neural network in this project is a one that is trained to detect positive or negative sentiment in a text blurb (a paragraph or a few sentences).

Here is sample output of the completed project:


Prerequisites

Are you going to follow along at home? If yes, then be sure to have and/or understand the following items before continuing.

  • Super Minimal Deep Learning Understanding - Following this guide requires no prior understanding of deep learning. But it helps to understand the basics (duh right?) See the 'Deep Learning - Getting Started' link in the resources section below for links to get up to speed enough to understand what the code in this project is doing.
  • Internet - If you are reading this, then you very likely already have this prerequisite.
  • Java - Being a Java programming language expert is not required to complete this guide, but if you intend to try your own test data (text with sentiment) then basic understanding of Java is needed to update the code with your sample sentiments.
  • Valohai Platform - Create a 'free-tier' account with valohai.com and reference these Valohai platform tips if you are not familiar with it.
  • Git - Basic git understanding is needed to update the code to use different test data than the hard-coded/built-in test data.

Sentiment Deep Learning

 Below are the main components of this project.

  • Build the software to train and evaluate test data.
  • Train the model for the neural netork.
  • Execute evaluations of the trained network.
  • Update the code to evaluate other test data.

Build the Software

 At this point a free-tier account on valohai.com exists and the admins there have enabled network access from inside the Docker container.

  • Once logged in, click the 'Projects' drop down and select 'Create project'.
  • Give the project a reasonable name, and click the 'Create project' button.
  • Specify the Github repository by clicking the 'Settings' tab and then the 'Repository' tab. Populate the URL field with this value: 'https://github.com/onebeartoe/deeplearning4j-sentiment-example'. Click the 'Save' button.
  • On the next screen, make sure the 'Step' field shows 'build'. Click the 'Create execution' button.

Download the Training Dataset

 This step is stand alone in that you likely only need to run it once.

  • Click the 'Create execution' button. Make sure the 'Step' field shows 'download Google Word Vectors'.
  • Click on the 'Create execution' button and wait a bit while the 1GB+ dataset file downloads.

Train the Model

 At this point, the Java code to train and evaluate the neural network (model) is compiled and built into an executable binary. The training dataset has also been downloaded.

  • Click the 'Create execution' button. Make sure the 'Step' field shows 'train'.
  • Fill out the 'target' field in the 'Inputs' section by clicking the 'Add file' link. Then click the 'Data file' radio button. Then type/select 'dl4j-sentiment-example-1.0.0-jar-with-dependencies.jar' that was created in the previous ('Build the Software') step in this guide.
  • Fill out the 'dataset' field in the 'Inputs' section by clicking the 'Add file' link. Then click the 'Data file' radio button. Then type/select 'GoogleNews-vectors-negative300.bin.gz' that was created in the previous ('Download Dataset') step in this guide.
  • Click on the 'Create execution' button and wait a bit while the code trains the neural network with the word vectors data from Google.

Execute Evaluations

 At this point everything is in place to evaluate the hard-coded test data; 3 sample text blurbs.

  • Click the 'Create execution' button. Make sure the 'Step' field shows 'evaluate'.
  • Fill out the 'target' field in the 'Inputs' section by clicking the 'Add file' link. Then click the 'Data file' radio button. Then type/select 'dl4j-sentiment-example-1.0.0-jar-with-dependencies.jar' that was created in the previous ('Build the Software') step in this guide.
  • Fill out the 'dataset' field in the 'Inputs' section by clicking the 'Add file' link. Then click the 'Data file' radio button. Then type/select 'GoogleNews-vectors-negative300.bin.gz' that was created in the previous ('Download Dataset') step in this guide.
  • Fill out the 'trained-model' field in the 'Inputs' section by clicking the 'Add file' link. Then click the 'Data file' radio button. Then type/select 'trained-model.zip' that was created in the previous ('Train the Model') step in this guide.

  • Click on the 'Create execution' button. Wait a small bit while the code evaluates the hardcoded test data against the neural network.
  • Once the execution finishes, the output should match the results image at the very top of this page, in the 'Overview' section.

Update the Code to Evaluate Other Test Data

Fork this Github project and clone it locally.

The sample text (evaluation data) for this project is hard-coded into this file:

Word2VecSentimentRnnEvaluate.java

It looks like this:

// expected bad review
String shortNegativeReview = "Boy, did that movie suck. It was like a bad version of my least favorite cartoon.";        
deepLearner.evaluate(test, model, truncateReviewsToLength, shortNegativeReview);

// another expected bad review
String secondBadReview = "Homer - Yeah Moe that team sure did suck last night. They just plain sucked! I've seen teams suck before, but they were the suckiest bunch of sucks that ever sucked.";                
deepLearner.evaluate(test, model, truncateReviewsToLength, secondBadReview);

// a good review follows (hopefully)
String goodReview = "Boy, did I sure enjoy that movie.  It was great!";        
deepLearner.evaluate(test, model, truncateReviewsToLength, goodReview);
            

Update the *Review Strings to whatever you like. Commit and push the changes to the fork.

Go back to valohai.com and repeat the 'build', 'download' and 'train' steps from this guide, but this time using the URL of the fork that was just created.

Create an 'evaluate' execution (just like before) on the forked project and the output should reflect updated reviews.

Going Further

 

  • Update the evaluation logic so that test data is dynamically loaded (and not hard-coded).
  • There is still a 80Mb file that is downloaded each train execution. Move all downloads to the 'Downloads' step.
  • Try other examples from eclipse's deep learning examples.

Date Created

Sat Sep 14 07:28:12 CDT 2019


Last Updated

----





back    |    top