Example usage for org.deeplearning4j.nn.multilayer MultiLayerNetwork output

List of usage examples for org.deeplearning4j.nn.multilayer MultiLayerNetwork output

Introduction

In this page you can find the example usage for org.deeplearning4j.nn.multilayer MultiLayerNetwork output.

Prototype

public INDArray output(DataSetIterator iterator) 

Source Link

Document

Equivalent to #output(DataSetIterator,boolean) with train=false

Usage

From source file:com.example.android.displayingbitmaps.ui.ImageGridActivity.java

License:Apache License

public void trainMLP() throws Exception {
    Nd4j.ENFORCE_NUMERICAL_STABILITY = true;
    final int numRows = 28;
    final int numColumns = 28;
    int outputNum = 10;
    int numSamples = 10000;
    int batchSize = 500;
    int iterations = 10;
    int seed = 123;
    int listenerFreq = iterations / 5;
    int splitTrainNum = (int) (batchSize * .8);
    DataSet mnist;/*from   w  w  w .  j  a  v a  2 s.  c o m*/
    SplitTestAndTrain trainTest;
    DataSet trainInput;
    List<INDArray> testInput = new ArrayList<>();
    List<INDArray> testLabels = new ArrayList<>();

    log.info("Load data....");
    DataSetIterator mnistIter = new MnistDataSetIterator(batchSize, numSamples, true);

    log.info("Build model....");
    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().seed(seed)
            .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).iterations(iterations)
            .gradientNormalization(GradientNormalization.RenormalizeL2PerLayer).learningRate(1e-1f)
            .momentum(0.5).momentumAfter(Collections.singletonMap(3, 0.9)).useDropConnect(true).list(2)
            .layer(0,
                    new DenseLayer.Builder().nIn(numRows * numColumns).nOut(1000).activation("relu")
                            .weightInit(WeightInit.XAVIER).build())
            .layer(1, new OutputLayer.Builder(LossFunction.NEGATIVELOGLIKELIHOOD).nIn(1000).nOut(outputNum)
                    .activation("softmax").weightInit(WeightInit.XAVIER).build())
            .build();

    MultiLayerNetwork model = new MultiLayerNetwork(conf);
    model.init();
    model.setListeners(Arrays.asList((IterationListener) new ScoreIterationListener(listenerFreq)));

    log.info("Train model....");
    model.setListeners(Arrays.asList((IterationListener) new ScoreIterationListener(listenerFreq)));
    while (mnistIter.hasNext()) {
        mnist = mnistIter.next();
        trainTest = mnist.splitTestAndTrain(splitTrainNum, new Random(seed)); // train set that is the result
        trainInput = trainTest.getTrain(); // get feature matrix and labels for training
        testInput.add(trainTest.getTest().getFeatureMatrix());
        testLabels.add(trainTest.getTest().getLabels());
        model.fit(trainInput);
    }

    log.info("Evaluate model....");
    Evaluation eval = new Evaluation(outputNum);
    for (int i = 0; i < testInput.size(); i++) {
        INDArray output = model.output(testInput.get(i));
        eval.eval(testLabels.get(i), output);
    }

    log.info(eval.stats());
    log.info("****************Example finished********************");
}

From source file:com.heatonresearch.aifh.examples.ann.LearnXORBackprop.java

License:Apache License

/**
 * The main method.//from w w w  .j a  v a2  s .  c o m
 * @param args Not used.
 */
public static void main(String[] args) {
    int seed = 43;
    double learningRate = 0.4;
    int nEpochs = 100;

    int numInputs = XOR_INPUT[0].length;
    int numOutputs = XOR_IDEAL[0].length;
    int numHiddenNodes = 4;

    // Setup training data.
    INDArray xorInput = Nd4j.create(XOR_INPUT);
    INDArray xorIdeal = Nd4j.create(XOR_IDEAL);
    DataSet xorDataSet = new DataSet(xorInput, xorIdeal);

    // Create neural network.
    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().seed(seed).iterations(1)
            .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).learningRate(learningRate)
            .updater(Updater.NESTEROVS).momentum(0.9).list(2)
            .layer(0,
                    new DenseLayer.Builder().nIn(numInputs).nOut(numHiddenNodes).weightInit(WeightInit.XAVIER)
                            .activation("relu").build())
            .layer(1,
                    new OutputLayer.Builder(LossFunction.MSE).weightInit(WeightInit.XAVIER)
                            .activation("identity").nIn(numHiddenNodes).nOut(numOutputs).build())
            .pretrain(false).backprop(true).build();

    MultiLayerNetwork model = new MultiLayerNetwork(conf);
    model.init();
    model.setListeners(new ScoreIterationListener(1));

    // Train
    for (int n = 0; n < nEpochs; n++) {
        model.fit(xorDataSet);
    }

    // Evaluate
    System.out.println("Evaluating neural network.");
    for (int i = 0; i < 4; i++) {
        INDArray input = xorInput.getRow(i);
        INDArray output = model.output(input);
        System.out.println(input + " : " + output);
    }
}

From source file:Dl4j.Doc2VecWithAutoEncoder.java

public void saveModel(DataSetIterator iter, String outputFilename, MultiLayerNetwork model) throws IOException {
    FileWriter fw = new FileWriter(new File(outputFilename));
    BufferedWriter bw = new BufferedWriter(fw);
    while (iter.hasNext()) {
        DataSet v = iter.next();//from  w w w . j av  a 2 s.  c o  m
        INDArray st = model.output(v.getFeatures());
        bw.write(st.toString());
        bw.newLine();
    }
    bw.close();
}

From source file:org.audiveris.omr.classifier.ui.ValidationPanel.java

License:Open Source License

private void runValidation() {
    logger.info("Validating {} on {} set...", classifier.getName(), setName);

    // Empty the display
    positiveValue.setText("");
    accuracyValue.setText("");
    weakPositiveValue.setText("");
    falsePositiveValue.setText("");
    weakNegativeValue.setText("");

    weakPositiveAction.setEnabled(false);
    falsePositiveAction.setEnabled(false);
    weakNegativeAction.setEnabled(false);

    weakPositives.clear();/*from www  .j a  v a2s  .  c o m*/
    falsePositives.clear();
    weakNegatives.clear();

    int positives = 0;

    // Validation is performed on TRAIN or TEST set
    final List<Sample> samples = isTrainSet ? sampleSource.getTrainSamples() : sampleSource.getTestSamples();

    progressBar.setValue(0);
    progressBar.setMaximum(samples.size());

    int index = 0;

    for (Sample sample : samples) {
        Evaluation[] evals = classifier.evaluate(sample, sample.getInterline(), 1, 0, Classifier.NO_CONDITIONS);
        Evaluation eval = evals[0];

        if (eval.shape.getPhysicalShape() == sample.getShape().getPhysicalShape()) {
            if (eval.grade >= Grades.validationMinGrade) {
                positives++;
            } else {
                weakPositives.add(sample);
            }
        } else {
            if (eval.grade >= Grades.validationMinGrade) {
                falsePositives.add(sample);
            } else {
                weakNegatives.add(sample);
            }
        }

        progressBar.setValue(++index); // Update progress bar
    }

    int total = samples.size();
    int allPositives = positives + weakPositives.size();
    double accuracy = allPositives / (double) total;
    DecimalFormat df = new DecimalFormat("#.####");
    String accuStr = df.format(accuracy);
    logger.info("{} accuracy: {}  {}/{}", classifier.getName(), accuStr, allPositives, total);
    accuracyValue.setText(accuStr);
    positiveValue.setText(Integer.toString(positives));
    weakPositiveValue.setText(Integer.toString(weakPositives.size()));
    falsePositiveValue.setText(Integer.toString(falsePositives.size()));
    weakNegativeValue.setText(Integer.toString(weakNegatives.size()));

    // Additional evaluation
    if (classifier instanceof DeepClassifier) {
        final DeepClassifier deepClassifier = (DeepClassifier) classifier;
        final MultiLayerNetwork model = deepClassifier.getModel();
        DataSet dataSet = deepClassifier.getRawDataSet(samples);
        deepClassifier.normalize(dataSet.getFeatures());

        final List<String> names = Arrays.asList(ShapeSet.getPhysicalShapeNames());
        org.deeplearning4j.eval.Evaluation eval = new org.deeplearning4j.eval.Evaluation(names);
        INDArray guesses = model.output(dataSet.getFeatureMatrix());
        eval.eval(dataSet.getLabels(), guesses);
        System.out.println(eval.stats(true));

        logger.info(
                String.format("Accuracy: %s Precision: %s Recall: %s F1 Score: %s", df.format(eval.accuracy()),
                        df.format(eval.precision()), df.format(eval.recall()), df.format(eval.f1())));
    }
}

From source file:stratego.neural.net.NeuralNetTest.java

public static void main(String[] args) throws Exception {
    int numInput = 12; //Setting the number of input neurons
    int numHidden = 50; // SUBJECT TO CHANGE setting the number of hidden layer neurons
    int numOutput = 9; // setting the number of output neurons
    int rngSeed = 123; // setting the RNG seed 
    int batchSize = 150; // SUBJECT TO CHANGE setting the size of the mini-batch        
    int numEpochs = 150; // SUBJECT TO CHANGE setting the number of epochs to run the training for
    int iterations = 10; // SUBJECT TO CHANGE setting the number of iterations
    double learningRate = 0.05; // SUBJECT TO CHANGE the learning rate of the network

    // Reading in the data from a file
    //MIGHT NOT NEED THIS
    /*/*from  ww w  .  j  a  v a2s .c  o m*/
    int numLinesToSkip = 0; // SUBJECT TO CHANGE The amount of lines to be skipped (should be zero if we format our data well)
    String delimiter = ","; // what the data is going to be split on
            
    RecordReader recordReader = new CSVRecordReader(numLinesToSkip,delimiter);
    recordReader.initialize(new FileSplit(new ClassPathResource(data).getFile())); // NOTE UPDATE "data.txt" to where the actual file is, and it's name!
    */

    int labelIndex = 12; // SUBJECT TO CHANGE: The index of where the label will be (The label is what the outcome should be)
    String data = "src/Data/test_data_1.csv"; // SUBJECT TO CHANGE the location of our data

    DataSet allData = readCSVDataset(data, batchSize, labelIndex, numOutput);

    allData.shuffle();
    double ratio = 0.9; // SUBJECT TO CHANGE the percentage of data to be used for training (now set to 80%)

    SplitTestAndTrain testAndTrain = allData.splitTestAndTrain(ratio);

    DataSet trainingData = testAndTrain.getTrain();
    DataSet testData = testAndTrain.getTest();

    //Normalizing our data (giving us mean 0, unit variance):
    DataNormalization normalizer = new NormalizerStandardize();
    normalizer.fit(trainingData); // collect the statistics from the training data. This does not modify the input data
    normalizer.transform(trainingData); // Apply normalization to the training data
    normalizer.transform(testData); // Apply the normalization to the test data, using the statistics from the training set (which is bigger so should be the same or better)

    //Building the neural network  NOTE: the format of the network is different across examples, might want to try a bunch of them out or research what vague things do
    System.out.println("Build Model....");
    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().seed(rngSeed).iterations(iterations)
            .learningRate(learningRate).updater(Updater.NESTEROVS).momentum(0.9) // Not exactly sure what this does, might want to leave it out or properly research this
            .regularization(true).l2(1e-4) // applying L2 regularizations to work against overfitting (that's an "l" not a one)
            .list().layer(0, new DenseLayer.Builder().nIn(numInput).nOut(numHidden).activation("relu") // again, not entirely sure what relu is, but this is the activation fucntion (might need to research)
                    .weightInit(WeightInit.XAVIER).build())
            .layer(1,
                    new OutputLayer.Builder(LossFunction.NEGATIVELOGLIKELIHOOD).nIn(numHidden).nOut(numOutput)
                            .activation("softmax") // again, but vague, but I have some idea what a softmax function is (S function, 1/(1+e^-x))
                            .weightInit(WeightInit.XAVIER).build())
            .pretrain(false).backprop(true) // of course we're using backpropagation!
            .build();

    MultiLayerNetwork model = new MultiLayerNetwork(conf);
    model.init();
    model.setListeners(new ScoreIterationListener(1)); // Listens to the score every iteration (might want to raise this value if we start training on large datasets

    double[] trainAccuracies = new double[numEpochs];
    double[] testAccuracies = new double[numEpochs];
    Evaluation eval = new Evaluation(numOutput);

    // HERE BE BUGS!
    System.out.println("Train model....");

    model.fit(trainingData);

    for (int i = 0; i < numEpochs; i++) { // for the total amount of epochs
        System.out.println("=====================");
        System.out.println("     Epoch " + i);
        System.out.println("=====================");
        model.fit(trainingData);

        INDArray outputTraining = model.output(trainingData.getFeatureMatrix());
        INDArray outputTest = model.output(testData.getFeatureMatrix());

        //Here we want some of the data from the evaluation, so we can make nice plots regarding the accuracy so we can say something about overfitting

        //evaluating on the training data and storing the accuracy in the array
        eval.eval(trainingData.getLabels(), outputTraining);
        trainAccuracies[i] = eval.accuracy();

        //evaluating on the test data and storing the accuracy in the array
        eval.eval(testData.getLabels(), outputTest);
        testAccuracies[i] = eval.accuracy();
    }

    //creating a list for the two accuracy arrays so we can use them for plotting
    List<NamedDataSet> AccuracyData = new ArrayList<>();
    NamedDataSet trainAccurSet = new NamedDataSet("Training", trainAccuracies);
    NamedDataSet testAccurSet = new NamedDataSet("Test", testAccuracies);

    AccuracyData.add(trainAccurSet);
    AccuracyData.add(testAccurSet);

    plotDataSet(AccuracyData);

    //Evaluate the model on the test set

    System.out.println("Evaluate model....");

    INDArray outputTraining = model.output(trainingData.getFeatureMatrix());
    INDArray outputTest = model.output(testData.getFeatureMatrix());
    System.out.println("Scores on training data");
    eval.eval(trainingData.getLabels(), outputTraining);
    System.out.println(eval.stats());
    System.out.println("Scores on test data");
    eval.eval(testData.getLabels(), outputTest);
    System.out.println(eval.stats());

    //Degbug code

    /*
     System.out.println("Test accuracy");
    System.out.print("[");
    for(int i=0; i<testAccuracies.length;i++){
    System.out.print(testAccuracies[i]+" ");
    }
    System.out.print("]");
    System.out.println();
            
     System.out.println("Train accuracy");
    System.out.print("[");
    for(int i=0; i<trainAccuracies.length;i++){
    System.out.print(trainAccuracies[i]+" ");
    }
    System.out.print("]");
    System.out.println();
            
    */

    //This is the predicting bit!

    /*
    double[][] voorspellingData = new double[][]{{4,4,3,4,1,5,4,4,4,2,4,3},{4,4,4,1,4,5,4,4,4,3,2,3}};
    INDArray voorspeldata = Nd4j.create(voorspellingData);
            
    int[] resultaat = model.predict(voorspeldata);
            
    System.out.println("Testje voor het voorspellen");
    System.out.print("[");
    for(int i=0; i<resultaat.length;i++){
    System.out.print(resultaat[i]+" ");
    }
    System.out.print("]");
    System.out.println();
    */

}

From source file:vectorizer.Doc2Vec.java

void storeOutputLayer(DataSetIterator iter, MultiLayerNetwork model) throws Exception {
    iter.reset();/*w  w  w .  ja  v a2s  .  co  m*/

    String fileName = getOutputVecFileName();
    System.out.println("saving o/p layers in file: " + fileName);
    FileWriter fw = new FileWriter(fileName);
    StringBuffer buff;

    int docid = 0;

    while (iter.hasNext()) {
        String docName = luceneDocFetcher.getDocName(docid);

        DataSet v = iter.next();
        INDArray st = model.output(v.getFeatures());
        buff = new StringBuffer();
        buff.append(docName).append('\t').append(st.toString()).append("\n");

        fw.write(buff.toString());
        docid++;
    }

    fw.close();
}