Table Of Contents


Built with 🛠 MkDocs - Theme 🖤 Github.

Making Predictions

Pass raw feature values to a trained network in exactly the same order used by the training dataset. When the fitted normalizer was attached before saving, the model keeps preprocessing with the prediction pipeline.

Regression

Regression returns a continuous value:

float[] hardware = {
    125,  // MYCT
    256,  // MMIN
    6000, // MMAX
    256,  // CACH
    16,   // CHMIN
    128   // CHMAX
};

float predictedPerformance = neuralNet.predict(hardware)[0];

Binary classification

A single Sigmoid output can be converted into a class using a threshold:

float[] email = {4, 55, 1, 0.35f, 1};

float spamProbability = neuralNet.predict(email)[0];
String predictedClass =
        spamProbability >= 0.5f ? "SPAM" : "NOT SPAM";

Choose the threshold based on the application's false-positive and false-negative costs rather than assuming 0.5 is always optimal.

Multiclass classification

Softmax returns one probability per class. Select the index with the largest value:

float[] flower = {5.1f, 3.5f, 1.4f, 0.2f};
float[] probabilities = neuralNet.predict(flower);

int predictedClass = 0;
for (int i = 1; i < probabilities.length; i++) {
    if (probabilities[i] > probabilities[predictedClass]) {
        predictedClass = i;
    }
}

String species = CLASS_NAMES[predictedClass];

CLASS_NAMES must use the same order as the target columns used during training.

Image classification with a CNN

Prepare an image with exactly the same dimensions, channels, and numerical transformation used during training. A held-out ExampleImage already contains the tensor expected by the network:

import deepnetts.data.ExampleImage;

ExampleImage image = testSet.get(0);
float probability = network.predict(image.getInput()).getValues()[0];

String predictedClass =
        probability >= 0.5f ? "positive" : "negative";

For a multiclass CNN, select the index of the largest Softmax probability and map it through the original class-name order. A new external image must go through the same resize, grayscale/RGB conversion, channel order, and value scaling as the training images before calling predict.

When the application has finished all predictions, release the network worker threads:

network.getThreadPool().shutdown();

Prediction checklist

See complete examples

Yes. Apply the scaler, normalizer, image resize, or channel conversion used during training.

Map the highest Softmax probability to its label, or apply the chosen threshold to a binary Sigmoid probability.

Was this helpful?