public void computeGaussianPerplexity_1() { double[][] points = { new double[] { 2, 3, 2 }, new double[] { 5, 4, 5 }, new double[] { 9, 6, 4 }, new double[] { 4, 7, 5 }, new double[] { 8, 1, 1 }, new double[] { 1, 2, 4 }, }; double perplexity = 0.5; int N = points.Length; int D = 3; var X = points.ToMatrix(); double[,] expected = new double[N, N]; TSNEWrapper.computeGaussianPerplexity(X, N, D, expected, perplexity); double[][] actual = Jagged.Zeros(N, N); TSNE.computeGaussianPerplexity(points, N, D, ref actual, perplexity); Assert.IsTrue(actual.IsEqual(expected, rtol: 1e-5)); }
public void computeGaussianPerplexity2_larger() { var points = yinyang.Submatrix(null, 0, 1).ToJagged(); double perplexity = 0.5; int N = points.Rows(); int D = points.Columns(); int K = (int)(3 * perplexity); double[,] X = points.ToMatrix(); double[][] x = X.ToJagged(); uint[] expected_row = Vector.Zeros <uint>(200); uint[] expected_col = Vector.Zeros <uint>(200); double[] expected_val = Vector.Zeros <double>(200); TSNEWrapper.computeGaussianPerplexity(X, N, D, expected_row, expected_col, expected_val, perplexity, K); int[] actual_row = null; int[] actual_col = null; double[] actual_val = null; TSNE.computeGaussianPerplexity(x, N, D, ref actual_row, ref actual_col, ref actual_val, perplexity, K); expected_row = expected_row.First(101); expected_col = expected_col.First(100); expected_val = expected_val.First(100); Assert.IsTrue(actual_row.IsEqual(expected_row)); Assert.IsTrue(actual_col.IsEqual(expected_col)); Assert.IsTrue(actual_val.IsEqual(expected_val, 1e-4)); }
public void compute_squared_distance_larger() { var points = yinyang.Submatrix(null, 0, 1).ToJagged(); var X = points.ToMatrix(); int N = X.Rows(); int D = X.Columns(); double[,] expected = new double[N, N]; TSNEWrapper.computeSquaredEuclideanDistance(X, expected); double[][] actual = Jagged.Zeros(N, N); TSNE.computeSquaredEuclideanDistance(points, N, D, actual); Assert.IsTrue(actual.IsEqual(expected)); }
public void computeGradient_1() { Accord.Math.Random.Generator.Seed = 0; double perplexity = 0.5; double theta = 0.5; int N = 100; int K = (int)(3 * perplexity); int D = 3; uint[] row_P = Vector.Create(N + 1, new uint[] { 0, 1, 2, 3, 4, 5, 6 }); uint[] col_P = Vector.Create(N * K, new uint[] { 5, 3, 1, 1, 2, 1 }); double[] val_P = Vector.Create(N * K, new double[] { 0.83901046609114708, 0.39701047304189827, 0.19501046869768451, 0.59401047304189827, 0.49301046869768484, 0.59901046869768451, }); double[,] P = Matrix.Random(N, N, new NormalDistribution()); double[][] p = P.ToJagged(); double[,] Y = Matrix.Random(N, D, new NormalDistribution()); double[][] y = Y.ToJagged(); uint[] expected_row = Vector.Create(row_P); uint[] expected_col = Vector.Create(col_P); double[] expected_val = Vector.Create(val_P); double[,] expected = Matrix.Zeros(N, D); TSNEWrapper.computeGradient(P, expected_row, expected_col, expected_val, Y, N, D, expected, theta); int[] actual_row = row_P.To <int[]>(); int[] actual_col = col_P.To <int[]>(); double[] actual_val = (double[])val_P.Clone(); double[][] actual = Jagged.Zeros(N, D); TSNE.computeGradient(p, actual_row, actual_col, actual_val, y, N, D, actual, theta); Assert.IsTrue(actual.IsEqual(expected)); Assert.IsTrue(actual_row.IsEqual(expected_row)); Assert.IsTrue(actual_col.IsEqual(expected_col)); Assert.IsTrue(actual_val.IsEqual(expected_val, 1e-4)); }
public void evaluateError_2() { int N = 6; int D = 2; double[,] P = Matrix.Random(6, 6, new NormalDistribution()); double[][] p = P.ToJagged(); double[,] Y = Matrix.Random(6, 2, new NormalDistribution()); double[][] y = Y.ToJagged(); double expected = TSNEWrapper.evaluateError(P, Y, N, D); double actual = TSNE.evaluateError(p, y, N, D); Assert.AreEqual(expected, actual); }
public void computeGaussianPerplexity_larger() { var points = yinyang.Submatrix(null, 0, 1).ToJagged(); double perplexity = 0.5; int N = points.Rows(); int D = points.Columns(); var X = points.ToMatrix(); double[,] expected = new double[N, N]; TSNEWrapper.computeGaussianPerplexity(X, N, D, expected, perplexity); double[][] actual = Jagged.Zeros(N, N); TSNE.computeGaussianPerplexity(points, N, D, ref actual, perplexity); Assert.IsTrue(actual.IsEqual(expected, rtol: 1e-5)); }
public void evaluateError_1() { double perplexity = 0.5; double theta = 0.5; int N = 6; int K = (int)(3 * perplexity); int D = 2; uint[] row_P = Vector.Create(N + 1, new uint[] { 0, 1, 2, 3, 4, 5, 6 }); uint[] col_P = Vector.Create(N * K, new uint[] { 5, 3, 1, 1, 2, 1 }); double[] val_P = Vector.Create(N * K, new double[] { 0.99901046609114708, 0.99901047304189827, 0.99901046869768451, 0.99901047304189827, 0.99901046869768484, 0.99901046869768451, }); double[,] Y = Matrix.Random(6, 2, new NormalDistribution()); double[][] y = Y.ToJagged(); uint[] expected_row = Vector.Create(row_P); uint[] expected_col = Vector.Create(col_P); double[] expected_val = Vector.Create(val_P); double expected = TSNEWrapper.evaluateError(expected_row, expected_col, expected_val, Y, N, D, theta); int[] actual_row = row_P.To <int[]>(); int[] actual_col = col_P.To <int[]>(); double[] actual_val = (double[])val_P.Clone(); double actual = TSNE.evaluateError(actual_row, actual_col, actual_val, y, N, D, theta); Assert.AreEqual(expected, actual); Assert.IsTrue(actual_col.IsEqual(expected_col)); Assert.IsTrue(actual_row.IsEqual(expected_row)); Assert.IsTrue(actual_val.IsEqual(expected_val)); }
public void computeGaussianPerplexity_2() { double[][] points = { new double[] { 2, 3, 20 }, new double[] { 5, 4, 5 }, new double[] { 9, 6, 500 }, new double[] { 4, 7, -100 }, new double[] { 8, 1, 67 }, new double[] { 1, 2, -888 }, }; double perplexity = 0.5; int N = points.Length; int D = 3; int K = (int)(3 * perplexity); double[,] X = points.ToMatrix(); double[][] x = X.ToJagged(); uint[] expected_row = Vector.Zeros <uint>(100); uint[] expected_col = Vector.Zeros <uint>(100); double[] expected_val = Vector.Zeros <double>(100); TSNEWrapper.computeGaussianPerplexity(X, N, D, expected_row, expected_col, expected_val, perplexity, K); int[] actual_row = null; int[] actual_col = null; double[] actual_val = null; TSNE.computeGaussianPerplexity(x, N, D, ref actual_row, ref actual_col, ref actual_val, perplexity, K); expected_row = expected_row.First(7); expected_col = expected_col.First(6); expected_val = expected_val.First(6); Assert.IsTrue(actual_row.IsEqual(expected_row)); Assert.IsTrue(actual_col.IsEqual(expected_col)); Assert.IsTrue(actual_val.IsEqual(expected_val, 1e-4)); }
public void FromDataTest2() { Accord.Math.Random.Generator.Seed = 0; int N = yinyang.GetLength(0); var init = Matrix.Random(N, 1, new NormalDistribution(0, 0.001)); var X = yinyang.Submatrix(null, 0, 1); var Y = (double[, ])init.Clone(); var x = X.ToJagged(); var y = Y.ToJagged(); double perplexity = 20; double theta = 0.5; TSNEWrapper.run(X, Y, perplexity, theta); var expected = Y.Flatten(); TSNE.run(x, y, perplexity, theta, true); var actual = y.Flatten(); Assert.IsTrue(actual.IsEqual(expected)); }
public void symmetrizeMatrix_1() { double perplexity = 0.5; int n = 6; int k = (int)(3 * perplexity); uint[] row_P = Vector.Create(n + 1, new uint[] { 0, 1, 2, 3, 4, 5, 6 }); uint[] col_P = Vector.Create(n * k, new uint[] { 5, 3, 1, 1, 2, 1 }); double[] val_P = Vector.Create(n * k, new double[] { 0.99901046609114708, 0.99901047304189827, 0.99901046869768451, 0.99901047304189827, 0.99901046869768484, 0.99901046869768451, }); uint[] expected_row = Vector.Create(100, row_P); uint[] expected_col = Vector.Create(100, col_P); double[] expected_val = Vector.Create(100, val_P); TSNEWrapper.symmetrizeMatrix(expected_row, expected_col, expected_val, n); int[] actual_row = row_P.To <int[]>(); int[] actual_col = col_P.To <int[]>(); double[] actual_val = (double[])val_P.Clone(); TSNE.symmetrizeMatrix(ref actual_row, ref actual_col, ref actual_val, n); expected_row = expected_row.First(7); expected_col = expected_col.First(10); expected_val = expected_val.First(10); Assert.IsTrue(actual_col.IsEqual(expected_col)); Assert.IsTrue(actual_row.IsEqual(expected_row)); Assert.IsTrue(actual_val.IsEqual(expected_val)); }
public void compute_squared_distance_1() { double[][] points = { new double[] { 2, 3, 2 }, new double[] { 5, 4, 5 }, new double[] { 9, 6, 4 }, new double[] { 4, 7, 5 }, new double[] { 8, 1, 1 }, new double[] { 1, 2, 4 }, }; var X = points.ToMatrix(); int N = X.Rows(); int D = X.Columns(); double[,] expected = new double[N, N]; TSNEWrapper.computeSquaredEuclideanDistance(X, expected); double[][] actual = Jagged.Zeros(N, N); TSNE.computeSquaredEuclideanDistance(points, N, D, actual); Assert.IsTrue(actual.IsEqual(expected)); }