mirror of
https://github.com/jneug/zeichenmaschine.git
synced 2026-10-12 09:56:33 +02:00
Colt als optionale Abhängigkeit
DAs Anlernen des NN geht um den Faktor 20 schneller, wenn Colt benutzt wird.
This commit is contained in:
@@ -1,46 +0,0 @@
|
||||
package schule.ngb.zm.ml;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class DoubleMatrixTest {
|
||||
|
||||
@Test
|
||||
void initializeOne() {
|
||||
DoubleMatrix m = new DoubleMatrix(4, 4);
|
||||
m.initializeOne();
|
||||
|
||||
double[] ones = new double[]{1.0, 1.0, 1.0, 1.0};
|
||||
assertArrayEquals(ones, m.coefficients[0]);
|
||||
assertArrayEquals(ones, m.coefficients[1]);
|
||||
assertArrayEquals(ones, m.coefficients[2]);
|
||||
assertArrayEquals(ones, m.coefficients[3]);
|
||||
}
|
||||
|
||||
@Test
|
||||
void initializeZero() {
|
||||
DoubleMatrix m = new DoubleMatrix(4, 4);
|
||||
m.initializeZero();
|
||||
|
||||
double[] zeros = new double[]{0.0, 0.0, 0.0, 0.0};
|
||||
assertArrayEquals(zeros, m.coefficients[0]);
|
||||
assertArrayEquals(zeros, m.coefficients[1]);
|
||||
assertArrayEquals(zeros, m.coefficients[2]);
|
||||
assertArrayEquals(zeros, m.coefficients[3]);
|
||||
}
|
||||
|
||||
@Test
|
||||
void initializeRandom() {
|
||||
DoubleMatrix m = new DoubleMatrix(4, 4);
|
||||
m.initializeRandom(-1, 1);
|
||||
|
||||
assertTrue(Arrays.stream(m.coefficients[0]).allMatch((d) -> -1.0 <= d && d < 1.0));
|
||||
assertTrue(Arrays.stream(m.coefficients[1]).allMatch((d) -> -1.0 <= d && d < 1.0));
|
||||
assertTrue(Arrays.stream(m.coefficients[2]).allMatch((d) -> -1.0 <= d && d < 1.0));
|
||||
assertTrue(Arrays.stream(m.coefficients[3]).allMatch((d) -> -1.0 <= d && d < 1.0));
|
||||
}
|
||||
|
||||
}
|
||||
375
src/test/java/schule/ngb/zm/ml/MLMatrixTest.java
Normal file
375
src/test/java/schule/ngb/zm/ml/MLMatrixTest.java
Normal file
@@ -0,0 +1,375 @@
|
||||
package schule.ngb.zm.ml;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.TestInfo;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class MLMatrixTest {
|
||||
|
||||
private TestInfo info;
|
||||
|
||||
@BeforeEach
|
||||
void saveTestInfo( TestInfo info ) {
|
||||
this.info = info;
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource( classes = {DoubleMatrix.class, MatrixFactory.ColtMatrix.class} )
|
||||
void initializeOne( Class<? extends MLMatrix> mType ) {
|
||||
MatrixFactory.matrixType = mType;
|
||||
|
||||
MLMatrix m = MatrixFactory.create(4, 4);
|
||||
m.initializeOne();
|
||||
|
||||
assertEquals(mType, m.getClass());
|
||||
|
||||
for( int i = 0; i < m.rows(); i++ ) {
|
||||
for( int j = 0; j < m.columns(); j++ ) {
|
||||
assertEquals(1.0, m.get(i, j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource( classes = {DoubleMatrix.class, MatrixFactory.ColtMatrix.class} )
|
||||
void initializeZero( Class<? extends MLMatrix> mType ) {
|
||||
MatrixFactory.matrixType = mType;
|
||||
|
||||
MLMatrix m = MatrixFactory.create(4, 4);
|
||||
m.initializeZero();
|
||||
|
||||
assertEquals(mType, m.getClass());
|
||||
|
||||
for( int i = 0; i < m.rows(); i++ ) {
|
||||
for( int j = 0; j < m.columns(); j++ ) {
|
||||
assertEquals(0.0, m.get(i, j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource( classes = {DoubleMatrix.class, MatrixFactory.ColtMatrix.class} )
|
||||
void initializeRandom( Class<? extends MLMatrix> mType ) {
|
||||
MatrixFactory.matrixType = mType;
|
||||
|
||||
MLMatrix m = MatrixFactory.create(4, 4);
|
||||
m.initializeRandom();
|
||||
|
||||
assertEquals(mType, m.getClass());
|
||||
|
||||
for( int i = 0; i < m.rows(); i++ ) {
|
||||
for( int j = 0; j < m.columns(); j++ ) {
|
||||
double d = m.get(i, j);
|
||||
assertTrue(-1.0 <= d && d < 1.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource( classes = {DoubleMatrix.class, MatrixFactory.ColtMatrix.class} )
|
||||
void multiplyTransposed( Class<? extends MLMatrix> mType ) {
|
||||
MatrixFactory.matrixType = mType;
|
||||
|
||||
MLMatrix A = MatrixFactory.create(new double[][]{
|
||||
{1.0, 2.0, 3.0, 4.0},
|
||||
{1.0, 2.0, 3.0, 4.0},
|
||||
{1.0, 2.0, 3.0, 4.0}
|
||||
});
|
||||
MLMatrix B = MatrixFactory.create(new double[][]{
|
||||
{1, 3, 5, 7},
|
||||
{2, 4, 6, 8}
|
||||
});
|
||||
|
||||
MLMatrix C = A.multiplyTransposed(B);
|
||||
|
||||
assertEquals(mType, A.getClass());
|
||||
assertEquals(mType, B.getClass());
|
||||
assertEquals(mType, C.getClass());
|
||||
|
||||
assertEquals(3, C.rows());
|
||||
assertEquals(2, C.columns());
|
||||
for( int i = 0; i < C.rows(); i++ ) {
|
||||
assertEquals(50.0, C.get(i, 0));
|
||||
assertEquals(60.0, C.get(i, 1));
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource( classes = {DoubleMatrix.class, MatrixFactory.ColtMatrix.class} )
|
||||
void multiplyAddBias( Class<? extends MLMatrix> mType ) {
|
||||
MatrixFactory.matrixType = mType;
|
||||
|
||||
MLMatrix A = MatrixFactory.create(new double[][]{
|
||||
{1.0, 2.0, 3.0, 4.0},
|
||||
{1.0, 2.0, 3.0, 4.0},
|
||||
{1.0, 2.0, 3.0, 4.0}
|
||||
});
|
||||
MLMatrix B = MatrixFactory.create(new double[][]{
|
||||
{1.0, 2.0},
|
||||
{3.0, 4.0},
|
||||
{5.0, 6.0},
|
||||
{7.0, 8.0}
|
||||
});
|
||||
MLMatrix V = MatrixFactory.create(new double[][]{
|
||||
{1000.0, 2000.0}
|
||||
});
|
||||
|
||||
MLMatrix C = A.multiplyAddBias(B, V);
|
||||
|
||||
assertEquals(mType, A.getClass());
|
||||
assertEquals(mType, B.getClass());
|
||||
assertEquals(mType, C.getClass());
|
||||
assertEquals(mType, V.getClass());
|
||||
|
||||
assertEquals(3, C.rows());
|
||||
assertEquals(2, C.columns());
|
||||
for( int i = 0; i < C.rows(); i++ ) {
|
||||
assertEquals(1050.0, C.get(i, 0));
|
||||
assertEquals(2060.0, C.get(i, 1));
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource( classes = {DoubleMatrix.class, MatrixFactory.ColtMatrix.class} )
|
||||
void transposedMultiplyAndScale( Class<? extends MLMatrix> mType ) {
|
||||
MatrixFactory.matrixType = mType;
|
||||
|
||||
MLMatrix A = MatrixFactory.create(new double[][]{
|
||||
{1, 1, 1},
|
||||
{2, 2, 2},
|
||||
{3, 3, 3},
|
||||
{4, 4, 4}
|
||||
});
|
||||
MLMatrix B = MatrixFactory.create(new double[][]{
|
||||
{1.0, 2.0},
|
||||
{3.0, 4.0},
|
||||
{5.0, 6.0},
|
||||
{7.0, 8.0}
|
||||
});
|
||||
|
||||
MLMatrix C = A.transposedMultiplyAndScale(B, 2.0);
|
||||
|
||||
assertEquals(mType, A.getClass());
|
||||
assertEquals(mType, B.getClass());
|
||||
assertEquals(mType, C.getClass());
|
||||
|
||||
assertEquals(3, C.rows());
|
||||
assertEquals(2, C.columns());
|
||||
for( int i = 0; i < C.rows(); i++ ) {
|
||||
assertEquals(100.0, C.get(i, 0));
|
||||
assertEquals(120.0, C.get(i, 1));
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource( classes = {DoubleMatrix.class, MatrixFactory.ColtMatrix.class} )
|
||||
void apply( Class<? extends MLMatrix> mType ) {
|
||||
MatrixFactory.matrixType = mType;
|
||||
|
||||
MLMatrix M = MatrixFactory.create(new double[][]{
|
||||
{1, 1, 1},
|
||||
{2, 2, 2},
|
||||
{3, 3, 3},
|
||||
{4, 4, 4}
|
||||
});
|
||||
|
||||
MLMatrix R = M.apply(( d ) -> d * d);
|
||||
|
||||
assertEquals(mType, M.getClass());
|
||||
assertEquals(mType, R.getClass());
|
||||
assertNotSame(M, R);
|
||||
|
||||
for( int i = 0; i < M.rows(); i++ ) {
|
||||
for( int j = 0; j < M.columns(); j++ ) {
|
||||
assertEquals(
|
||||
(i + 1) * (i + 1), R.get(i, j),
|
||||
msg("(%d,%d)", "apply", i, j)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
MLMatrix M2 = M.applyInPlace(( d ) -> d * d * d);
|
||||
assertSame(M, M2);
|
||||
for( int i = 0; i < M.rows(); i++ ) {
|
||||
for( int j = 0; j < M.columns(); j++ ) {
|
||||
assertEquals(
|
||||
(i + 1) * (i + 1) * (i + 1), M.get(i, j),
|
||||
msg("(%d,%d)", "applyInPlace", i, j)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource( classes = {DoubleMatrix.class, MatrixFactory.ColtMatrix.class} )
|
||||
void add( Class<? extends MLMatrix> mType ) {
|
||||
MatrixFactory.matrixType = mType;
|
||||
|
||||
MLMatrix M = MatrixFactory.create(new double[][]{
|
||||
{1, 1, 1},
|
||||
{2, 2, 2},
|
||||
{3, 3, 3},
|
||||
{4, 4, 4}
|
||||
});
|
||||
|
||||
MLMatrix R = M.add(M);
|
||||
|
||||
assertEquals(mType, M.getClass());
|
||||
assertEquals(mType, R.getClass());
|
||||
assertNotSame(M, R);
|
||||
|
||||
for( int i = 0; i < M.rows(); i++ ) {
|
||||
for( int j = 0; j < M.columns(); j++ ) {
|
||||
assertEquals(
|
||||
(i + 1) + (i + 1), R.get(i, j),
|
||||
msg("(%d,%d)", "add", i, j)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
MLMatrix M2 = M.addInPlace(R);
|
||||
assertSame(M, M2);
|
||||
for( int i = 0; i < M.rows(); i++ ) {
|
||||
for( int j = 0; j < M.columns(); j++ ) {
|
||||
assertEquals(
|
||||
(i + 1) + (i + 1) + (i + 1), M.get(i, j),
|
||||
msg("(%d,%d)", "addInPlace", i, j)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource( classes = {DoubleMatrix.class, MatrixFactory.ColtMatrix.class} )
|
||||
void sub( Class<? extends MLMatrix> mType ) {
|
||||
MatrixFactory.matrixType = mType;
|
||||
|
||||
MLMatrix M = MatrixFactory.create(new double[][]{
|
||||
{1, 1, 1},
|
||||
{2, 2, 2},
|
||||
{3, 3, 3},
|
||||
{4, 4, 4}
|
||||
});
|
||||
|
||||
MLMatrix R = M.sub(M);
|
||||
|
||||
assertEquals(mType, M.getClass());
|
||||
assertEquals(mType, R.getClass());
|
||||
assertNotSame(M, R);
|
||||
|
||||
for( int i = 0; i < M.rows(); i++ ) {
|
||||
for( int j = 0; j < M.columns(); j++ ) {
|
||||
assertEquals(
|
||||
0.0, R.get(i, j),
|
||||
msg("(%d,%d)", "sub", i, j)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource( classes = {DoubleMatrix.class, MatrixFactory.ColtMatrix.class} )
|
||||
void colSums( Class<? extends MLMatrix> mType ) {
|
||||
MatrixFactory.matrixType = mType;
|
||||
|
||||
MLMatrix M = MatrixFactory.create(new double[][]{
|
||||
{1, 2, 3},
|
||||
{1, 2, 3},
|
||||
{1, 2, 3},
|
||||
{1, 2, 3}
|
||||
});
|
||||
|
||||
MLMatrix R = M.colSums();
|
||||
|
||||
assertEquals(mType, M.getClass());
|
||||
assertEquals(mType, R.getClass());
|
||||
assertNotSame(M, R);
|
||||
|
||||
assertEquals(1, R.rows());
|
||||
assertEquals(3, R.columns());
|
||||
for( int j = 0; j < M.columns(); j++ ) {
|
||||
assertEquals(
|
||||
(j+1)*4, R.get(0, j),
|
||||
msg("(%d,%d)", "colSums", 0, j)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource( classes = {DoubleMatrix.class, MatrixFactory.ColtMatrix.class} )
|
||||
void duplicate( Class<? extends MLMatrix> mType ) {
|
||||
MatrixFactory.matrixType = mType;
|
||||
|
||||
MLMatrix M = MatrixFactory.create(new double[][]{
|
||||
{1, 2, 3},
|
||||
{1, 2, 3},
|
||||
{1, 2, 3},
|
||||
{1, 2, 3}
|
||||
});
|
||||
|
||||
MLMatrix R = M.duplicate();
|
||||
|
||||
assertEquals(mType, M.getClass());
|
||||
assertEquals(mType, R.getClass());
|
||||
assertNotSame(M, R);
|
||||
|
||||
for( int i = 0; i < M.rows(); i++ ) {
|
||||
for( int j = 0; j < M.columns(); j++ ) {
|
||||
assertEquals(
|
||||
M.get(i, j), R.get(i, j),
|
||||
msg("(%d,%d)", "duplicate", i, j)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource( classes = {DoubleMatrix.class, MatrixFactory.ColtMatrix.class} )
|
||||
void scale( Class<? extends MLMatrix> mType ) {
|
||||
MatrixFactory.matrixType = mType;
|
||||
|
||||
MLMatrix M = MatrixFactory.create(new double[][]{
|
||||
{1, 1, 1},
|
||||
{2, 2, 2},
|
||||
{3, 3, 3},
|
||||
{4, 4, 4}
|
||||
});
|
||||
|
||||
MLMatrix M2 = M.scaleInPlace(2.0);
|
||||
|
||||
assertEquals(mType, M.getClass());
|
||||
assertEquals(mType, M2.getClass());
|
||||
assertSame(M, M2);
|
||||
|
||||
for( int i = 0; i < M.rows(); i++ ) {
|
||||
for( int j = 0; j < M.columns(); j++ ) {
|
||||
assertEquals(
|
||||
(i+1)*2.0, M2.get(i, j),
|
||||
msg("(%d,%d)", "scaleInPlace", i, j)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
MLMatrix M3 = M.scaleInPlace(M);
|
||||
assertSame(M, M3);
|
||||
for( int i = 0; i < M.rows(); i++ ) {
|
||||
for( int j = 0; j < M.columns(); j++ ) {
|
||||
assertEquals(
|
||||
((i+1)*2.0)*((i+1)*2.0), M.get(i, j),
|
||||
msg("(%d,%d)", "addInPlace", i, j)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String msg( String msg, String methodName, Object... args ) {
|
||||
String testName = this.info.getTestMethod().get().getName();
|
||||
String className = MatrixFactory.matrixType.getSimpleName();
|
||||
return String.format("[" + testName + "(" + className + ") " + methodName + "()] " + msg, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package schule.ngb.zm.ml;
|
||||
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import schule.ngb.zm.Constants;
|
||||
import schule.ngb.zm.util.Log;
|
||||
import schule.ngb.zm.util.Timer;
|
||||
|
||||
@@ -16,6 +17,13 @@ class NeuralNetworkTest {
|
||||
Log.enableGlobalDebugging();
|
||||
}
|
||||
|
||||
@BeforeAll
|
||||
static void setupMatrixLibrary() {
|
||||
Constants.setSeed(1001);
|
||||
MatrixFactory.matrixType = MatrixFactory.ColtMatrix.class;
|
||||
//MatrixFactory.matrixType = DoubleMatrix.class;
|
||||
}
|
||||
|
||||
/*@Test
|
||||
void readWrite() {
|
||||
// XOR Dataset
|
||||
@@ -107,7 +115,7 @@ class NeuralNetworkTest {
|
||||
for( int i = 0; i < trainingData.size(); i++ ) {
|
||||
inputs[i][0] = trainingData.get(i).a;
|
||||
inputs[i][1] = trainingData.get(i).b;
|
||||
outputs[i][0] = trainingData.get(i).result;
|
||||
outputs[i][0] = trainingData.get(i).getResult();
|
||||
}
|
||||
|
||||
Timer timer = new Timer();
|
||||
@@ -139,8 +147,8 @@ class NeuralNetworkTest {
|
||||
"Prediction on data (%.2f, %.2f) was %.4f, expected %.2f (of by %.4f)\n",
|
||||
data.a, data.b,
|
||||
net.getOutput().get(0, 0),
|
||||
data.result,
|
||||
net.getOutput().get(0, 0) - data.result
|
||||
data.getResult(),
|
||||
net.getOutput().get(0, 0) - data.getResult()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -183,7 +191,6 @@ class NeuralNetworkTest {
|
||||
|
||||
double a;
|
||||
double b;
|
||||
double result;
|
||||
CalcType type;
|
||||
|
||||
TestData( double a, double b ) {
|
||||
@@ -191,6 +198,8 @@ class NeuralNetworkTest {
|
||||
this.b = b;
|
||||
}
|
||||
|
||||
abstract double getResult();
|
||||
|
||||
}
|
||||
|
||||
private static final class AddData extends TestData {
|
||||
@@ -199,7 +208,9 @@ class NeuralNetworkTest {
|
||||
|
||||
public AddData( double a, double b ) {
|
||||
super(a, b);
|
||||
result = a + b;
|
||||
}
|
||||
double getResult() {
|
||||
return a+b;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -210,7 +221,9 @@ class NeuralNetworkTest {
|
||||
|
||||
public SubData( double a, double b ) {
|
||||
super(a, b);
|
||||
result = a - b;
|
||||
}
|
||||
double getResult() {
|
||||
return a-b;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -221,7 +234,9 @@ class NeuralNetworkTest {
|
||||
|
||||
public MulData( double a, double b ) {
|
||||
super(a, b);
|
||||
result = a * b;
|
||||
}
|
||||
double getResult() {
|
||||
return a*b;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -235,7 +250,9 @@ class NeuralNetworkTest {
|
||||
if( b == 0.0 ) {
|
||||
b = .1;
|
||||
}
|
||||
result = a / b;
|
||||
}
|
||||
double getResult() {
|
||||
return a/b;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -246,7 +263,12 @@ class NeuralNetworkTest {
|
||||
|
||||
public ModData( double b, double a ) {
|
||||
super(b, a);
|
||||
result = a % b;
|
||||
if( b == 0.0 ) {
|
||||
b = .1;
|
||||
}
|
||||
}
|
||||
double getResult() {
|
||||
return a%b;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user