Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added getActivations method #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/main/java/basicneuralnetwork/NeuralNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import basicneuralnetwork.utilities.MatrixUtilities;
import org.ejml.simple.SimpleMatrix;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;

Expand All @@ -14,7 +15,7 @@
public class NeuralNetwork {

private ActivationFunctionFactory activationFunctionFactory = new ActivationFunctionFactory();

private Random random = new Random();

// Dimensions of the neural network
Expand All @@ -25,7 +26,9 @@ public class NeuralNetwork {

private SimpleMatrix[] weights;
private SimpleMatrix[] biases;


private ArrayList<SimpleMatrix> activations;

private double learningRate;

private String activationFunctionKey;
Expand Down Expand Up @@ -119,14 +122,31 @@ public double[] guess(double[] input) {
// Transform array to matrix
SimpleMatrix output = MatrixUtilities.arrayToMatrix(input);

//Stores an the activation matrix for each layer
activations = new ArrayList<SimpleMatrix>();
activations.add(output);

for (int i = 0; i < hiddenLayers + 1; i++) {
output = calculateLayer(weights[i], biases[i], output, activationFunction);
activations.add(output);
}

return MatrixUtilities.getColumnFromMatrixAsArray(output, 0);
}
}

//Return 2D array of the activation values for each neuron in each layer
//Neurons are updated every time guess() is called
public double[][] getActivations()
{
double[][] values = new double[hiddenLayers + 2][];
for (int m = 0; m < activations.size(); m++)
{
values[m] = MatrixUtilities.getColumnFromMatrixAsArray(activations.get(m), 0);
}
return values;
}

public void train(double[] inputArray, double[] targetArray) {
if (inputArray.length != inputNodes) {
throw new WrongDimensionException(inputArray.length, inputNodes, "Input");
Expand Down