Here's an example of a simple neural network in Java that uses backpropagation to train itself using the XOR truth table:
import java.util.Arrays;
public class NeuralNetwork {
// Hyperparameters
private static final double LEARNING_RATE = 0.1;
private static final int NUM_EPOCHS = 10000;
// Neural network architecture
private static final int NUM_INPUTS = 2;
private static final int NUM_HIDDEN = 2;
private static final int NUM_OUTPUTS = 1;
private double[][] inputHiddenWeights;
private double[][] hiddenOutputWeights;
public NeuralNetwork() {
// Initialize weights with random values
inputHiddenWeights = new double[NUM_INPUTS][NUM_HIDDEN];
hiddenOutputWeights = new double[NUM_HIDDEN][NUM_OUTPUTS];
for (int i = 0; i < NUM_INPUTS; i++) {
for (int j = 0; j < NUM_HIDDEN; j++) {
inputHiddenWeights[i][j] = Math.random() - 0.5;
}
}
for (int i = 0; i < NUM_HIDDEN; i++) {
for (int j = 0; j < NUM_OUTPUTS; j++) {
hiddenOutputWeights[i][j] = Math.random() - 0.5;
}
}
}
public double sigmoid(double x) {
return 1 / (1 + Math.exp(-x));
}
public double[] forwardPropagation(double[] inputs) {
double[] hiddenLayerOutput = new double[NUM_HIDDEN];
double[] outputLayerOutput = new double[NUM_OUTPUTS];
// Calculate hidden layer output
for (int i = 0; i < NUM_HIDDEN; i++) {
double weightedSum = 0;
for (int j = 0; j < NUM_INPUTS; j++) {
weightedSum += inputs[j] * inputHiddenWeights[j][i];
}
hiddenLayerOutput[i] = sigmoid(weightedSum);
}
// Calculate output layer output
for (int i = 0; i < NUM_OUTPUTS; i++) {
double weightedSum = 0;
for (int j = 0; j < NUM_HIDDEN; j++) {
weightedSum += hiddenLayerOutput[j] * hiddenOutputWeights[j][i];
}
outputLayerOutput[i] = sigmoid(weightedSum);
}
return outputLayerOutput;
}
public void backwardPropagation(double[] inputs, double[] expectedOutputs) {
// Perform forward propagation to get outputs
double[] hiddenLayerOutput = new double[NUM_HIDDEN];
double[] outputLayerOutput = new double[NUM_OUTPUTS];
// Calculate hidden layer output
for (int i = 0; i < NUM_HIDDEN; i++) {
double weightedSum = 0;
for (int j = 0; j < NUM_INPUTS; j++) {
weightedSum += inputs[j] * inputHiddenWeights[j][i];
}
hiddenLayerOutput[i] = sigmoid(weightedSum);
}
// Calculate output layer output
for (int i = 0; i < NUM_OUTPUTS; i++) {
double weightedSum = 0;
for (int j = 0; j < NUM_HIDDEN; j++) {
weightedSum += hiddenLayerOutput[j] * hiddenOutputWeights[j][i];
}
outputLayerOutput[i] = sigmoid(weightedSum);
}
// Calculate output layer errors
double[] outputLayerErrors = new double[NUM_OUTPUTS];
for (int i = 0; i < NUM_OUTPUTS; i++) {
double error = expectedOutputs[i] - outputLayerOutput[i];
outputLayerErrors[i] = error * outputLayerOutput[i] * (1 - outputLayerOutput[i]);
}
// Calculate hidden layer errors
double[] hiddenLayerErrors = new double[NUM_HIDDEN];
for (int i = 0; i < NUM_HIDDEN; i++) {
double error = 0;
for (int j = 0; j < NUM_OUTPUTS; j++) {
error += outputLayerErrors[j] * hiddenOutputWeights[i][j];
}
hiddenLayerErrors[i] = error * hiddenLayerOutput[i] * (1 - hiddenLayerOutput[i]);
}
// Update input-hidden weights
for (int i = 0; i < NUM_INPUTS; i++) {
for (int j = 0; j < NUM_HIDDEN; j++) {
inputHiddenWeights[i][j] += LEARNING_RATE * hiddenLayerErrors[j] * inputs[i];
}
}
// Update hidden-output weights
for (int i = 0; i < NUM_HIDDEN; i++) {
for (int j = 0; j < NUM_OUTPUTS; j++) {
hiddenOutputWeights[i][j] += LEARNING_RATE * outputLayerErrors[j] * hiddenLayerOutput[i];
}
}
}
public void train(double[][] inputs, double[][] expectedOutputs) {
for (int epoch = 1; epoch <= NUM_EPOCHS; epoch++) {
for (int i = 0; i < inputs.length; i++) {
backwardPropagation(inputs[i], expectedOutputs[i]);
}
}
}
public static void main(String[] args) {
NeuralNetwork neuralNetwork = new NeuralNetwork();
// XOR truth table
double[][] inputs = {
{0, 0},
{0, 1},
{1, 0},
{1, 1}
};
double[][] expectedOutputs = {
{0},
{1},
{1},
{0}
};
neuralNetwork.train(inputs, expectedOutputs);
// Test the neural network using trained weights
for (int i = 0; i < inputs.length; i++) {
double[] output = neuralNetwork.forwardPropagation(inputs[i]);
System.out.println("Input: " + Arrays.toString(inputs[i]) + " Output: " + Arrays.toString(output));
}
}
}
In this example, we define a NeuralNetwork class that consists of methods for initializing and training the neural network. The forwardPropagation method performs forward propagation to calculate the output of the neural network for a given input. The backwardPropagation method performs backpropagation to update the weights based on the calculated errors. The train method trains the neural network using the provided dataset.
Finally, in the main method, we create an instance of NeuralNetwork, define the XOR truth table as input and expected output, and train the network using the train method. After that, we test the network using the trained weights by using the forwardPropagation method and print the inputs and corresponding outputs.