USing Colt library as optional dependency

This commit is contained in:
ngb
2022-07-18 11:06:08 +02:00
parent 9a9a714050
commit 4c8e5c8939
8 changed files with 439 additions and 128 deletions

View File

@@ -6,22 +6,11 @@ import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.*;
class MatrixTest {
@Test
void initializeIdentity() {
Matrix m = new Matrix(4, 4);
m.initializeIdentity();
assertArrayEquals(new double[]{1.0, 0.0, 0.0, 0.0}, m.coefficients[0]);
assertArrayEquals(new double[]{0.0, 1.0, 0.0, 0.0}, m.coefficients[1]);
assertArrayEquals(new double[]{0.0, 0.0, 1.0, 0.0}, m.coefficients[2]);
assertArrayEquals(new double[]{0.0, 0.0, 0.0, 1.0}, m.coefficients[3]);
}
class DoubleMatrixTest {
@Test
void initializeOne() {
Matrix m = new Matrix(4, 4);
DoubleMatrix m = new DoubleMatrix(4, 4);
m.initializeOne();
double[] ones = new double[]{1.0, 1.0, 1.0, 1.0};
@@ -33,7 +22,7 @@ class MatrixTest {
@Test
void initializeZero() {
Matrix m = new Matrix(4, 4);
DoubleMatrix m = new DoubleMatrix(4, 4);
m.initializeZero();
double[] zeros = new double[]{0.0, 0.0, 0.0, 0.0};
@@ -45,7 +34,7 @@ class MatrixTest {
@Test
void initializeRandom() {
Matrix m = new Matrix(4, 4);
DoubleMatrix m = new DoubleMatrix(4, 4);
m.initializeRandom(-1, 1);
assertTrue(Arrays.stream(m.coefficients[0]).allMatch((d) -> -1.0 <= d && d < 1.0));

View File

@@ -3,6 +3,7 @@ package schule.ngb.zm.ml;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import schule.ngb.zm.util.Log;
import schule.ngb.zm.util.Timer;
import java.io.File;
import java.util.ArrayList;
@@ -18,7 +19,7 @@ class NeuralNetworkTest {
Log.enableGlobalDebugging();
}
@Test
/*@Test
void readWrite() {
// XOR Dataset
NeuralNetwork net = new NeuralNetwork(2, 4, 1);
@@ -53,7 +54,7 @@ class NeuralNetworkTest {
}
assertArrayEquals(net.predict(inputs), net2.predict(inputs));
}
}*/
@Test
void learnXor() {
@@ -78,14 +79,14 @@ class NeuralNetworkTest {
}
// calculate predictions
double[][] predictions = net.predict(inputs);
Matrix predictions = net.predict(inputs);
for( int i = 0; i < 4; i++ ) {
int parsed_pred = predictions[i][0] < 0.5 ? 0 : 1;
int parsed_pred = predictions.get(i, 0) < 0.5 ? 0 : 1;
System.out.printf(
"{%.0f, %.0f} = %.4f (%d) -> %s\n",
inputs[i][0], inputs[i][1],
predictions[i][0],
predictions.get(i, 0),
parsed_pred,
parsed_pred == outputs[i][0] ? "correct" : "miss"
);
@@ -112,9 +113,13 @@ class NeuralNetworkTest {
outputs[i][0] = trainingData.get(i).result;
}
Timer timer = new Timer();
System.out.println("Training the neural net to learn "+OPERATION+"...");
timer.start();
net.train(inputs, outputs, TRAINING_CYCLES);
System.out.println(" finished training");
timer.stop();
System.out.println(" finished training (" + timer.getMillis() + "ms)");
for( int i = 1; i <= net.getLayerCount(); i++ ) {
System.out.println("Layer " +i + " weights");
@@ -136,9 +141,9 @@ class NeuralNetworkTest {
System.out.printf(
"Prediction on data (%.2f, %.2f) was %.4f, expected %.2f (of by %.4f)\n",
data.a, data.b,
net.getOutput()[0][0],
net.getOutput().get(0, 0),
data.result,
net.getOutput()[0][0] - data.result
net.getOutput().get(0, 0) - data.result
);
}