How to do it...

  1. Load the word vector model using WordVectorSerializer:
WordVectors wordVectors = WordVectorSerializer.loadStaticModel(new File(WORD_VECTORS_PATH));
  1. Create a sentence provider using FileLabeledSentenceProvider:
 Map<String,List<File>> reviewFilesMap = new HashMap<>();
reviewFilesMap.put("Positive", Arrays.asList(filePositive.listFiles()));
reviewFilesMap.put("Negative", Arrays.asList(fileNegative.listFiles()));
LabeledSentenceProvider sentenceProvider = new FileLabeledSentenceProvider(reviewFilesMap, rndSeed);
  1. Create train iterators or test iterators using CnnSentenceDataSetIterator to load the IMDB review data:
CnnSentenceDataSetIterator iterator = new CnnSentenceDataSetIterator.Builder(CnnSentenceDataSetIterator.Format.CNN2D)
.sentenceProvider(sentenceProvider)
.wordVectors(wordVectors) //we mention word vectors here
.minibatchSize(minibatchSize)
.maxSentenceLength(maxSentenceLength) //words with length greater than this will be ignored.
.useNormalizedWordVectors(false)
.build();
  1. Create a ComputationGraph configuration by adding default hyperparameters:
ComputationGraphConfiguration.GraphBuilder builder = new NeuralNetConfiguration.Builder()
.weightInit(WeightInit.RELU)
.activation(Activation.LEAKYRELU)
.updater(new Adam(0.01))
.convolutionMode(ConvolutionMode.Same) //This is important so we can 'stack' the results later
.l2(0.0001).graphBuilder();

  1. Configure layers for ComputationGraph using the addLayer() method:
builder.addLayer("cnn3", new ConvolutionLayer.Builder()
.kernelSize(3,vectorSize) //vectorSize=300 for google vectors
.stride(1,vectorSize)
.nOut(100)
.build(), "input");
builder.addLayer("cnn4", new ConvolutionLayer.Builder()
.kernelSize(4,vectorSize)
.stride(1,vectorSize)
.nOut(100)
.build(), "input");
builder.addLayer("cnn5", new ConvolutionLayer.Builder()
.kernelSize(5,vectorSize)
.stride(1,vectorSize)
.nOut(100)
.build(), "input");
  1. Set the convolution mode to stack the results later:
builder.addVertex("merge", new MergeVertex(), "cnn3", "cnn4", "cnn5")
  1. Create a ComputationGraph model and initialize it:
ComputationGraphConfiguration config = builder.build();
ComputationGraph net = new ComputationGraph(config);
net.init();
  1. Perform the training using the fit() method:
for (int i = 0; i < numEpochs; i++) {
net.fit(trainIterator);
}
  1. Evaluate the results:
Evaluation evaluation = net.evaluate(testIter);
System.out.println(evaluation.stats());
  1. Retrieve predictions for the IMDB reviews data:
INDArray features = ((CnnSentenceDataSetIterator)testIterator).loadSingleSentence(contents);
INDArray predictions = net.outputSingle(features);
List<String> labels = testIterator.getLabels();
System.out.println(" Predictions for first negative review:");
for( int i=0; i<labels.size(); i++ ){
System.out.println("P(" + labels.get(i) + ") = " + predictions.getDouble(i));
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset