Neural Network Basics
A previous post introduced ojAlgo’s Artificial Neural Network feature. It did so by presenting a fully functional program that trained and evaluated a network model to categorise handwritten digits using the MNIST data set. That example then included all necessary pre and post processing as well as code that would generate the actual image (*.png) files. All of that may have obscured just how simple it is to work with ojAlgo’s neural networks.
The code below is not complete – it simply outlines the basic steps involved to build, train and use a neural network with ojAlgo.
Now have a look at that fully functional example…
OutlineNeuralNetworkUsage.javaimport static org.ojalgo.ann.ArtificialNeuralNetwork.Activator.*;
import static org.ojalgo.ann.ArtificialNeuralNetwork.Error.CROSS_ENTROPY;
import java.util.Collections;
import org.ojalgo.ann.ArtificialNeuralNetwork;
import org.ojalgo.ann.NetworkTrainer;
import org.ojalgo.structure.Access1D;
/**
* This is not a functioning program. It's just something that outlines the basic steps involved in using
* ojAlgo's artificial neural network functionality.
*
* @see https://www.ojalgo.org/2018/09/neural-network-basics/
*/
public class OutlineNeuralNetworkUsage {
public static void main(final String[] args) {
/**
* Start by instantiating a neural network builder/trainer. This particular network has 200 input and
* 10 output nodes. The numbers 100 and 50 define the hidden layers. Some would say this is a 4-layer
* network, but there are only 3 calculation layers. The first layer has 200 input and 100 output
* nodes. The second layer has 100 input and 50 output nodes, and ly the third 50 inputs and 10
* outputs.
*/
NetworkTrainer builder = ArtificialNeuralNetwork.builder(200, 100, 50, 10);
/**
* That network builder/trainer is ready to be used, but it can be reconfigured. You can (re)define
* which kind of activator function should be used for each of the calculation layers. You can
* (re)define how to meassure error/loss, and you can modify the learning rate.
*/
builder.activators(SIGMOID, RELU, SOFTMAX).error(CROSS_ENTROPY).rate(0.1);
/**
* The input and output can be typed as ojAlgo's most basic data type – Access1D. Just about anything
* in ojAlgo "is" an Access1D. If you have arrays or lists of numbers then you can wrap them in
* Access1D instances to avoid copying. Most natuarally you work with ojAlgo data structures from the
* beginning.
*/
Access1D<Double> input = Access1D.wrap(new double[] { 1, 2, 3 }); // To match the builder this should have 200 elements,
Access1D<Double> output = Access1D.wrap(Collections.singletonList(4.0)); // and this should have 10.
/**
* Repeatedly call this to train the neural network.
*/
builder.train(input, output);
/**
* When you're done training you get the finished product.
*/
ArtificialNeuralNetwork network = builder.get();
/**
* You evaluate/use the trained neural network by invoking it (it's a function).
*/
output = network.newInvoker().invoke(input);
}
}
- ← Previous
Introducing Artificial Neural Networks with ojAlgo - Next →
ojAlgo v46.3