Table Of Contents


Built with 🛠 MkDocs - Theme 🖤 Github.

Creating a Model

Build a network whose input count matches the feature columns and whose output layer, activation, and loss match the prediction task.

Regression

Use a linear output for predicting a continuous numerical value:

import deepnetts.net.FeedForwardNetwork;
import deepnetts.net.layers.activation.ActivationType;
import deepnetts.net.loss.LossType;

FeedForwardNetwork neuralNet = FeedForwardNetwork.builder()
        .addInputLayer(NUM_INPUTS)
        .addOutputLayer(1, ActivationType.LINEAR)
        .lossFunction(LossType.MEAN_SQUARED_ERROR)
        .randomSeed(42)
        .build();

This is the smallest linear regression model. Add hidden layers only when the problem requires a nonlinear relationship.

Binary classification

Use one Sigmoid output to produce a value between zero and one:

FeedForwardNetwork neuralNet = FeedForwardNetwork.builder()
        .addInputLayer(NUM_INPUTS)
        .addFullyConnectedLayer(32)
        .addFullyConnectedLayer(16)
        .addOutputLayer(1, ActivationType.SIGMOID)
        .lossFunction(LossType.CROSS_ENTROPY)
        .randomSeed(42)
        .build();

Interpret the output as a class probability and apply an explicit threshold when choosing the class.

Multiclass classification

Use one Softmax output per class:

FeedForwardNetwork neuralNet = FeedForwardNetwork.builder()
        .addInputLayer(NUM_INPUTS)
        .addFullyConnectedLayer(16, ActivationType.TANH)
        .addOutputLayer(NUM_CLASSES, ActivationType.SOFTMAX)
        .lossFunction(LossType.CROSS_ENTROPY)
        .randomSeed(42)
        .build();

The order of the outputs must match the order of the one-hot target columns and the class-name mapping used during prediction.

Convolutional Neural Network

Use ConvolutionalNetwork when the input has spatial structure, such as an image. Convolutional layers learn local patterns, pooling reduces spatial dimensions, and fully connected layers combine the learned feature maps before classification.

CNN architecture from image input through convolution, pooling, dense layers, and a binary or multiclass classification output
A CNN preserves image structure while extracting features, then uses a task-specific output layer for the final prediction.

A compact binary image classifier can be built with the current Deep Netts API:

import deepnetts.net.ConvolutionalNetwork;
import deepnetts.net.layers.Filters;
import deepnetts.net.layers.activation.ActivationType;
import deepnetts.net.loss.LossType;

ConvolutionalNetwork network = ConvolutionalNetwork.builder()
        .addInputLayer(width, height, 3)
        .addConvolutionalLayer(6, Filters.ofSize(3))
        .addMaxPoolingLayer(2, 2)
        .addFullyConnectedLayer(16)
        .addOutputLayer(1, ActivationType.SIGMOID)
        .hiddenActivationFunction(ActivationType.LEAKY_RELU)
        .lossFunction(LossType.CROSS_ENTROPY)
        .randomSeed(42)
        .build();
Builder step Responsibility
addInputLayer(width, height, 3) Declares RGB image dimensions and channels
addConvolutionalLayer(6, Filters.ofSize(3)) Learns six local 3 × 3 filters
addMaxPoolingLayer(2, 2) Reduces feature-map width and height
addFullyConnectedLayer(16) Combines extracted spatial features
addOutputLayer(1, SIGMOID) Produces one binary-class probability

For grayscale input, use one channel. For multiclass classification, use one Softmax output per class:

.addInputLayer(width, height, 1)
// convolution, pooling, and fully connected layers
.addOutputLayer(NUM_CLASSES, ActivationType.SOFTMAX)
.lossFunction(LossType.CROSS_ENTROPY)

The output order must remain identical to the class-label mapping used by the dataset and prediction code. Add another convolution-and-pooling block only when the image size and problem complexity justify the additional capacity.

Check the dimensions

Use a fixed random seed when you want reproducible weight initialization while comparing changes.

See it in practice

Use Linear for regression, Sigmoid for binary classification, and Softmax for mutually exclusive multiclass classification.

The input size must match the number of prepared numerical features or the exact dimensions of the image tensor.

Was this helpful?