githubEdit

diagram-projectNeural Network Example

In this quickstart you will train your first neural network with Brain4J. The goal is to learn the XOR logical operator, a classic example that cannot be solved by a linear model and therefore requires a neural network with hidden layers.

The XOR operation

The XOR function behaves as follows:

x₁
x₂
XOR

0

0

0

0

1

1

1

0

1

1

1

0

Architecture

We start by defining the network architecture. For such a simple problem, even a very small network would be sufficient, but we will use a slightly over-parameterized model for clarity.

ModelSpecs specs = ModelSpecs.of(
    new InputLayer(2),
    new DenseLayer(16, Activations.RELU),
    new DenseLayer(16, Activations.RELU),
    new DenseLayer(1, Activations.SIGMOID)
);

The InputLayer defines the shape of the input (2 features), the hidden layers use ReLU activations and the output layer uses Sigmoid to map predictions to the range [0, 1].

circle-exclamation

Once the architecture is defined, we can compile the model:

Calling summary() prints the model structure and parameter count to the console.

Dataset Loading & Creation

Brain4J separates model definition from data handling. Here we build a small in-memory dataset representing the XOR truth table.

The dataset is shuffled and trained with a batch size of 1.

Training

We configure the training process using binary cross-entropy and the Adam optimizer.

To monitor training progress, we attach two monitors:

Monitors are used to keep track of the training process.

  • DefaultMonitor prints training progress.

  • EvalMonitor evaluates the model every 25 epochs.

We can now start training:

Evaluating

Once our model finished training, we can evaluate it's results:

We expect the results to be similar to this:

Last updated