The inputSize of each layer does not need to be specified anymore
This commit is contained in:
parent
95501bf4b1
commit
1c66f1b72f
3 changed files with 60 additions and 41 deletions
|
@ -12,9 +12,9 @@ public class ExampleXOR {
|
||||||
new SimpleMatrix(new double[][]{{0}})};
|
new SimpleMatrix(new double[][]{{0}})};
|
||||||
|
|
||||||
Network network = new Network();
|
Network network = new Network();
|
||||||
network.addLayer(new FCLayer(2, 3));
|
network.addLayer(new FCLayer(3));
|
||||||
network.addLayer(new ActivationLayer(ActivationFunctions::tanh, ActivationFunctions::tanhPrime));
|
network.addLayer(new ActivationLayer(ActivationFunctions::tanh, ActivationFunctions::tanhPrime));
|
||||||
network.addLayer(new FCLayer(3, 1));
|
network.addLayer(new FCLayer(1));
|
||||||
network.addLayer(new ActivationLayer(ActivationFunctions::tanh, ActivationFunctions::tanhPrime));
|
network.addLayer(new ActivationLayer(ActivationFunctions::tanh, ActivationFunctions::tanhPrime));
|
||||||
|
|
||||||
network.use(LossFunctions::MSE, LossFunctions::MSEPrime);
|
network.use(LossFunctions::MSE, LossFunctions::MSEPrime);
|
||||||
|
|
|
@ -12,9 +12,9 @@ public class ExampleXORBlankLayers {
|
||||||
new SimpleMatrix(new double[][]{{0}})};
|
new SimpleMatrix(new double[][]{{0}})};
|
||||||
|
|
||||||
Network network = new Network();
|
Network network = new Network();
|
||||||
network.addLayer(new FCLayer(2, 1));
|
network.addLayer(new FCLayer(1));
|
||||||
network.addLayer(new ActivationLayer(ActivationFunctions::tanh, ActivationFunctions::tanhPrime));
|
network.addLayer(new ActivationLayer(ActivationFunctions::tanh, ActivationFunctions::tanhPrime));
|
||||||
network.addLayer(new FCLayer(1, 1));
|
network.addLayer(new FCLayer(1));
|
||||||
network.addLayer(new ActivationLayer(ActivationFunctions::tanh, ActivationFunctions::tanhPrime));
|
network.addLayer(new ActivationLayer(ActivationFunctions::tanh, ActivationFunctions::tanhPrime));
|
||||||
network.addNeuron(0, 2);
|
network.addNeuron(0, 2);
|
||||||
|
|
||||||
|
|
|
@ -3,15 +3,23 @@ import org.ejml.simple.SimpleMatrix;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
public class FCLayer extends Layer {
|
public class FCLayer extends Layer {
|
||||||
SimpleMatrix weights;
|
private SimpleMatrix weights;
|
||||||
SimpleMatrix biases;
|
private SimpleMatrix biases;
|
||||||
|
private int numNeurons;
|
||||||
|
private boolean isInitialized;
|
||||||
|
|
||||||
public FCLayer(int inputSize, int outputSize) {
|
public FCLayer(int numNeurons) {
|
||||||
|
this.numNeurons = numNeurons;
|
||||||
|
isInitialized = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initialize(int inputSize) {
|
||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
weights = new SimpleMatrix(inputSize, outputSize, true,
|
this.weights = new SimpleMatrix(inputSize, numNeurons, true,
|
||||||
random.doubles((long) inputSize*outputSize, -1, 1).toArray());
|
random.doubles((long) inputSize*numNeurons, -1, 1).toArray());
|
||||||
biases = new SimpleMatrix(1, outputSize, true,
|
this.biases = new SimpleMatrix(1, numNeurons, true,
|
||||||
random.doubles(outputSize, -1, 1).toArray());
|
random.doubles(numNeurons, -1, 1).toArray());
|
||||||
|
this.isInitialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -19,21 +27,23 @@ public class FCLayer extends Layer {
|
||||||
* @param n amount of new neurons in previous layer
|
* @param n amount of new neurons in previous layer
|
||||||
*/
|
*/
|
||||||
public void updateInputSize(int n) {
|
public void updateInputSize(int n) {
|
||||||
Random random = new Random();
|
if (isInitialized) {
|
||||||
|
Random random = new Random();
|
||||||
|
|
||||||
// add new weights
|
// add new weights
|
||||||
SimpleMatrix newWeights = new SimpleMatrix(this.weights.numRows() + n, this.weights.numCols());
|
SimpleMatrix newWeights = new SimpleMatrix(this.weights.numRows() + n, this.weights.numCols());
|
||||||
for (int i = 0; i < this.weights.numRows(); i++) {
|
for (int i = 0; i < this.weights.numRows(); i++) {
|
||||||
for (int j = 0; j < this.weights.numCols(); j++) {
|
for (int j = 0; j < this.weights.numCols(); j++) {
|
||||||
newWeights.set(i, j, this.weights.get(i, j));
|
newWeights.set(i, j, this.weights.get(i, j));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
for (int i = 0; i < newWeights.getNumElements(); i++) {
|
||||||
for (int i = 0; i < newWeights.getNumElements(); i++) {
|
if (newWeights.get(i) == 0) {
|
||||||
if (newWeights.get(i) == 0) {
|
newWeights.set(i, random.nextDouble(-1, 1));
|
||||||
newWeights.set(i, random.nextDouble(-1, 1));
|
}
|
||||||
}
|
}
|
||||||
|
this.weights = newWeights;
|
||||||
}
|
}
|
||||||
this.weights = newWeights;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -43,30 +53,39 @@ public class FCLayer extends Layer {
|
||||||
public void addNeuron(int n) {
|
public void addNeuron(int n) {
|
||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
|
|
||||||
// add new weights
|
// update neuron count
|
||||||
SimpleMatrix newWeights = new SimpleMatrix(this.weights.numRows(), this.weights.numCols() + n);
|
this.numNeurons += n;
|
||||||
for (int i = 0; i < this.weights.numRows(); i++) {
|
|
||||||
for (int j = 0; j < this.weights.numCols(); j++) {
|
|
||||||
newWeights.set(i, j, this.weights.get(i, j));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (int i = 0; i < newWeights.getNumElements(); i++) {
|
|
||||||
if (newWeights.get(i) == 0) {
|
|
||||||
newWeights.set(i, random.nextDouble(-1, 1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.weights = newWeights;
|
|
||||||
|
|
||||||
// add new biases
|
if (isInitialized) {
|
||||||
SimpleMatrix newBiases = new SimpleMatrix(1, this.biases.numCols() + n);
|
// add new weights
|
||||||
double[] newBiasValues = random.doubles(n, -1, 1).toArray();
|
SimpleMatrix newWeights = new SimpleMatrix(this.weights.numRows(), this.weights.numCols() + n);
|
||||||
System.arraycopy(this.biases.getDDRM().data, 0, newBiases.getDDRM().data, 0, this.biases.numCols());
|
for (int i = 0; i < this.weights.numRows(); i++) {
|
||||||
System.arraycopy(newBiasValues, 0, newBiases.getDDRM().data, this.biases.numCols(), n);
|
for (int j = 0; j < this.weights.numCols(); j++) {
|
||||||
this.biases = newBiases;
|
newWeights.set(i, j, this.weights.get(i, j));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = 0; i < newWeights.getNumElements(); i++) {
|
||||||
|
if (newWeights.get(i) == 0) {
|
||||||
|
newWeights.set(i, random.nextDouble(-1, 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.weights = newWeights;
|
||||||
|
|
||||||
|
// add new biases
|
||||||
|
SimpleMatrix newBiases = new SimpleMatrix(1, this.biases.numCols() + n);
|
||||||
|
double[] newBiasValues = random.doubles(n, -1, 1).toArray();
|
||||||
|
System.arraycopy(this.biases.getDDRM().data, 0, newBiases.getDDRM().data, 0, this.biases.numCols());
|
||||||
|
System.arraycopy(newBiasValues, 0, newBiases.getDDRM().data, this.biases.numCols(), n);
|
||||||
|
this.biases = newBiases;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SimpleMatrix forwardPropagation(SimpleMatrix inputs) {
|
public SimpleMatrix forwardPropagation(SimpleMatrix inputs) {
|
||||||
|
if (!isInitialized) {
|
||||||
|
initialize(inputs.numCols());
|
||||||
|
}
|
||||||
|
|
||||||
this.input = inputs;
|
this.input = inputs;
|
||||||
this.output = this.input.mult(this.weights).plus(this.biases);
|
this.output = this.input.mult(this.weights).plus(this.biases);
|
||||||
return this.output;
|
return this.output;
|
||||||
|
|
Loading…
Reference in a new issue