-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add copy functionality: generate an exact and "independent" copy of a neural network calling the therefore added copy constructor by copy()-method - add merge functionality: merges the weights and biases of two neural networks either by a ratio of 50:50 or given probability, returns a new neural network - add mutate functionality: slightly changes the weights and biases of a neural network using gaussian ditribution by a given probability - change MatrixConverter-class to MatrixUtilities-class and add static method to merge two matrices with a given probability (this will be called when merging two neural networks) - add two new WrongDimensionException-constructors
- Loading branch information
1 parent
d99d221
commit 422cf46
Showing
5 changed files
with
162 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 0 additions & 39 deletions
39
src/main/java/basicneuralnetwork/utilities/MatrixConverter.java
This file was deleted.
Oops, something went wrong.
63 changes: 63 additions & 0 deletions
63
src/main/java/basicneuralnetwork/utilities/MatrixUtilities.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package basicneuralnetwork.utilities; | ||
|
||
import basicneuralnetwork.WrongDimensionException; | ||
import org.ejml.simple.SimpleMatrix; | ||
|
||
import java.util.Random; | ||
|
||
/** | ||
* Created by KimFeichtinger on 07.03.18. | ||
*/ | ||
public class MatrixUtilities { | ||
|
||
// Converts a 2D array into a SimpleMatrix | ||
public static SimpleMatrix arrayToMatrix(double[] i) { | ||
double[][] input = {i}; | ||
return new SimpleMatrix(input).transpose(); | ||
} | ||
|
||
// Converts a SimpleMatrix into a 2D array | ||
public static double[][] matrixTo2DArray(SimpleMatrix i) { | ||
double[][] result = new double[i.numRows()][i.numCols()]; | ||
|
||
for (int j = 0; j < result.length; j++) { | ||
for (int k = 0; k < result[0].length; k++) { | ||
result[j][k] = i.get(j, k); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
// Returns one specific column of a matrix as a 1D array | ||
public static double[] getColumnFromMatrixAsArray(SimpleMatrix data, int column) { | ||
double[] result = new double[data.numRows()]; | ||
|
||
for (int i = 0; i < result.length; i++) { | ||
result[i] = data.get(i, column); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
// Merge two matrices and return a new one | ||
public static SimpleMatrix mergeMatrices(SimpleMatrix matrixA, SimpleMatrix matrixB, double probability) { | ||
if (matrixA.numCols() != matrixB.numCols() || matrixA.numRows() != matrixB.numRows()) { | ||
throw new WrongDimensionException(); | ||
} else { | ||
Random random = new Random(); | ||
SimpleMatrix result = new SimpleMatrix(matrixA.numRows(), matrixA.numCols()); | ||
|
||
for (int i = 0; i < matrixA.getNumElements(); i++) { | ||
// %-chance of replacing this value with the one from the input nn | ||
if (random.nextDouble() > probability) { | ||
result.set(i, matrixA.get(i)); | ||
} else { | ||
result.set(i, matrixB.get(i)); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
|
||
} |