PredictionIO deeplearning4j RNTN engine template.
Switch branches/tags
Nothing to show
Clone or download
Latest commit 4bc3859 May 12, 2015
Type Name Latest commit message Commit time
Failed to load latest commit information.
data Added script for automated check of predictions. Apr 21, 2015
project Included pio-build sbt plugin. May 12, 2015
src/main/scala Using new event store api. May 12, 2015
.gitignore PredictionIO 0.9.2 May 9, 2015 Added May 9, 2015
build.sbt Included pio-build sbt plugin. May 12, 2015
engine.json Using new event store api. May 12, 2015

Template description.

This template is based on deeplearning4j RNTN example. It's goal is to show how to integrate deeplearning4j library with PredictionIO.

Recursive Neural Tensor Network algorithm is supervised learning algorithm used to predict sentiment of sentences.

As of today, deeplearning4j RNTN algorithm implementation does not work properly (eg. training does not finnish). Corresponding issue in deeplearning4j library has been added.


Follow installation guide for PredictionIO.

After installation start all PredictionIO vendors and check pio status:

pio status

This template depends on deeplearning4j In order to install it run:

git clone
cd deeplearning4j
chmod a+x

Copy this template to your local directory with:

pio template get ts335793/template-scala-parallel-dl4j-rntn <TemplateName>

Build, train, deploy.

You might build template, train it and deploy by typing:

pio build
pio train -- --executor-memory=4GB --driver-memory=4GB
pio deploy -- --executor-memory=4GB --driver-memory=4GB

Those pio train options are used to avoid problems with java garbage collector. In case they appear increase executor memory and driver memory.


  • pio train command won't stop as deeplearning4j RNTN fit function does not work properly

Importing training data.

You can import example training data from kaggle. It is collection of the Rotten Tomatoes movie reviews with sentiment labels.

In order to use this data, create new app:

pio app new <ApplicationName> # prints out ApplicationAccessKey and ApplicationId

set appId in engine.json to ApplicationId and import data with:

python data/ --access_key <ApplicationAccessKey> --file train.tsv

You can always remind your application id and key with:

pio app list

Sending requests to server.

In order to send a query run in template directory:

python data/

and type phrase you want sentiment to be predicted. The result will be a list of predicted sentiments for all sentences in phrase.

Algorithm overview.

At first Word2Vec is trained (it creates mapping from words to vectors).

val (vocabCache, weightLookupTable) = {
  val result = new SparkWord2Vec().train(data.phrases)
  (result.getFirst, result.getSecond)
val word2vec = new Word2Vec.Builder()

It is passed to RNTN builder.

val rntn = new RNTN.Builder()
  .setRng(new DefaultRandom())

Each phrase from training set is converted to tree with TreeVectorizer.

val listsOfTrees = data.labeledPhrases.mapPartitions(labeledPhrases => {
  val treeVectorizer = new TreeVectorizer() // it is so slow
    x => treeVectorizer.getTreesWithLabels(x.phrase, x.sentiment.toString, data.labels))
val listOfTrees = listsOfTrees.reduce(_ ++ _)

RNTN is fitted to those trees.

Finally, model is saved.

new Model(
  rntn = rntn,
  labels = data.labels

Serving overview.

List of trees for sentences in query is created.

val trees = new TreeVectorizer().getTreesWithLabels(query.phrase, model.labels)

Sentiment for each sentence is being predicted.

val sentiment = model.rntn.predict(trees)

Result is returned.

PredictedResult(sentiment = sentiment.toList)