예제 #1
0
        public void VectorRotate()
        {
            const int blockCount = 1;
            var       a          = _cpu.CreateVector(4, i => i + 1).AsIndexable();

            using (var gpuA = _cuda.CreateVector(a)) {
                a.RotateInPlace(blockCount);
                gpuA.RotateInPlace(blockCount);
                FloatingPointHelper.AssertEqual(gpuA.AsIndexable(), a.AsIndexable());
            }
        }
예제 #2
0
        public void VectorRotate2()
        {
            Load();
            const int blockCount = 2;
            var       a          = _cpu.CreateVector(8, i => i + 1).AsIndexable();

            using var gpuA = _cuda.CreateVector(a);
            a.RotateInPlace(blockCount);
            gpuA.RotateInPlace(blockCount);
            FloatingPointHelper.AssertEqual(gpuA.AsIndexable(), a.AsIndexable());
            Cleanup();
        }
예제 #3
0
        void _Transpose(int rows, int columns)
        {
            var a  = _cpu.CreateMatrix(rows, columns, (j, k) => k).AsIndexable();
            var aT = a.Transpose();
            IIndexableMatrix gpuResults;

            using (var gpuA = _cuda.CreateMatrix(a))
                using (var gpuAT = gpuA.Transpose())
                    gpuResults = gpuAT.AsIndexable();

            FloatingPointHelper.AssertEqual(gpuResults, aT.AsIndexable());
        }
예제 #4
0
        public void MatrixGetRowSegment()
        {
            var a = _cpu.CreateMatrix(12, 18, (x, y) => x * 2 + y).AsIndexable();
            var r = a.GetRowSegment(1, 2, 5).AsIndexable();

            IIndexableVector gpuResults;

            using (var gpuA = _cuda.CreateMatrix(a)) {
                gpuResults = gpuA.GetRowSegment(1, 2, 5).AsIndexable();
            }
            FloatingPointHelper.AssertEqual(r, gpuResults);
        }
예제 #5
0
        public void MatrixGetColumnSegment()
        {
            var a = _cpu.CreateMatrix(9, 8, (x, y) => (x + 1) * (y + 1)).AsIndexable();
            var r = a.GetColumnSegment(1, 2, 5).AsIndexable();

            IIndexableVector gpuResults;

            using (var gpuA = _cuda.CreateMatrix(a)) {
                gpuResults = gpuA.GetColumnSegment(1, 2, 5).AsIndexable();
            }
            FloatingPointHelper.AssertEqual(r, gpuResults);
        }
예제 #6
0
        public void VectorL1Norm()
        {
            Load();
            var   distribution = new Normal(0, 5);
            var   a            = _cpu.CreateVector(5000, i => Convert.ToSingle(distribution.Sample())).AsIndexable();
            var   v1           = a.L1Norm();
            float v2;

            using (var gpuA = _cuda.CreateVector(a))
                v2 = gpuA.L1Norm();
            FloatingPointHelper.AssertEqual(v1, v2);
            Cleanup();
        }
예제 #7
0
        public void VectorAverage()
        {
            Load();
            var   distribution = new Normal(0, 5);
            var   a            = _cpu.CreateVector(5000, i => Convert.ToSingle(distribution.Sample())).AsIndexable();
            var   average      = a.Average();
            float average2;

            using (var gpuA = _cuda.CreateVector(a))
                average2 = gpuA.Average();
            FloatingPointHelper.AssertEqual(average, average2, 7);
            Cleanup();
        }
예제 #8
0
        public void VectorReverse()
        {
            var distribution = new Normal(0, 5);
            var a            = _cpu.CreateVector(128, i => (float)distribution.Sample()).AsIndexable();
            var cpuResult    = a.Reverse().AsIndexable();
            IIndexableVector result;

            using (var gpuA = _cuda.CreateVector(a)) {
                var gpuResult = gpuA.Reverse();
                result = gpuResult.AsIndexable();
            }
            FloatingPointHelper.AssertEqual(result, cpuResult);
        }
예제 #9
0
        public void VectorStdDev()
        {
            Load();
            var   distribution = new Normal(0, 5);
            var   a            = _cpu.CreateVector(5000, i => Convert.ToSingle(distribution.Sample())).AsIndexable();
            var   stdDev       = a.StdDev(null);
            float stdDev2;

            using (var gpuA = _cuda.CreateVector(a))
                stdDev2 = gpuA.StdDev(null);
            FloatingPointHelper.AssertEqual(stdDev, stdDev2);
            Cleanup();
        }
예제 #10
0
        public void VectorSqrt()
        {
            var a = _cpu.CreateVector(10, i => i * 2).AsIndexable();
            var b = a.Sqrt().AsIndexable();

            IIndexableVector gpuResults;

            using (var gpuA = _cuda.CreateVector(a))
                using (var gpuB = gpuA.Sqrt()) {
                    gpuResults = gpuB.AsIndexable();
                }
            FloatingPointHelper.AssertEqual(b, gpuResults);
        }
예제 #11
0
        public void MatrixRowL2Norm()
        {
            var a = _cpu.CreateMatrix(6, 3, (x, y) => x * 2 + y).AsIndexable();
            var r = a.RowL2Norm().AsIndexable();

            IIndexableVector gpuResults;

            using (var gpuA = _cuda.CreateMatrix(a))
                using (var norm = gpuA.RowL2Norm()) {
                    gpuResults = norm.AsIndexable();
                }
            FloatingPointHelper.AssertEqual(r, gpuResults);
        }
예제 #12
0
        public void MatrixDiagonal()
        {
            var a = _cpu.CreateMatrix(6, 6, (x, y) => x * 2 + y).AsIndexable();
            var d = a.Diagonal().AsIndexable();

            IIndexableVector gpuResults;

            using (var gpuA = _cuda.CreateMatrix(a))
                using (var diagonal = gpuA.Diagonal()) {
                    gpuResults = diagonal.AsIndexable();
                }
            FloatingPointHelper.AssertEqual(d, gpuResults);
        }
예제 #13
0
        public void TestManhattanDistance()
        {
            var distribution = new Normal(0, 5);
            var vectors = Enumerable.Range(0, 10).Select(i => _cpu.CreateVector(100, j => Convert.ToSingle(distribution.Sample())).AsIndexable()).ToList();
            var compareTo = Enumerable.Range(0, 20).Select(i => _cpu.CreateVector(100, j => Convert.ToSingle(distribution.Sample())).AsIndexable()).ToList();
            var distances = _cpu.CalculateDistances(vectors, compareTo, DistanceMetric.Manhattan);

            var gpuVectors = vectors.Select(v => _cuda.CreateVector(v)).ToList();
            var gpuCompareTo = compareTo.Select(v => _cuda.CreateVector(v)).ToList();
            var gpuDistances = _cuda.CalculateDistances(gpuVectors, gpuCompareTo, DistanceMetric.Manhattan);

            FloatingPointHelper.AssertEqual(distances.AsIndexable(), gpuDistances.AsIndexable());
        }
예제 #14
0
        public void MatrixColumnSums()
        {
            var a       = _cpu.CreateMatrix(2, 5, (j, k) => k).AsIndexable();
            var colSums = a.ColumnSums().AsIndexable();

            IIndexableVector gpuResults;

            using (var gpuA = _cuda.CreateMatrix(a))
                using (var gpuColSums = gpuA.ColumnSums())
                    gpuResults = gpuColSums.AsIndexable();

            FloatingPointHelper.AssertEqual(gpuResults, colSums);
        }
예제 #15
0
        public void VectorColumnMatrix()
        {
            var a      = _cpu.CreateVector(5, i => i).AsIndexable();
            var matrix = a.ReshapeAsColumnMatrix().AsIndexable();

            IIndexableMatrix gpuResults;

            using (var gpuA = _cuda.CreateVector(a))
                using (var gpuB = gpuA.ReshapeAsColumnMatrix())
                    gpuResults = gpuB.AsIndexable();

            FloatingPointHelper.AssertEqual(matrix, gpuResults);
        }
예제 #16
0
        public void TensorIm2Col()
        {
            var normalDistribution = new Normal(0, 1);

            using (var cpuTensor = _cpu.Create3DTensor(Enumerable.Range(0, 3).Select(i => _cpu.CreateMatrix(4, 4, (j, k) => Convert.ToSingle(normalDistribution.Sample()))).ToList()))
                using (var gpuTensor = _cuda.Create3DTensor(cpuTensor.AsIndexable()))
                    using (var cpuMatrix = cpuTensor.Im2Col(2, 2, 2))
                        using (var gpuMatrix = gpuTensor.Im2Col(2, 2, 2)) {
                            var cpu = cpuMatrix.AsIndexable();
                            var gpu = gpuMatrix.AsIndexable();
                            FloatingPointHelper.AssertEqual(cpu, gpu);
                        }
        }
예제 #17
0
        public void MatrixPow()
        {
            var         a       = _cpu.CreateMatrix(6, 3, (x, y) => x * 2 + y).AsIndexable();
            const float OPERAND = 2.5f;
            var         r       = a.Pow(OPERAND).AsIndexable();

            IIndexableMatrix gpuResults;

            using (var gpuA = _cuda.CreateMatrix(a))
                using (var pow = gpuA.Pow(OPERAND)) {
                    gpuResults = pow.AsIndexable();
                }
            FloatingPointHelper.AssertEqual(r, gpuResults);
        }
예제 #18
0
        public void MatrixL1Regularisation()
        {
            var         a       = _cpu.CreateMatrix(6, 3, (x, y) => x * 2 + y).AsIndexable();
            const float OPERAND = 2f;

            IIndexableMatrix gpuResults;

            using (var gpuA = _cuda.CreateMatrix(a)) {
                gpuA.L1Regularisation(OPERAND);
                gpuResults = gpuA.AsIndexable();
            }
            a.L1Regularisation(OPERAND);
            FloatingPointHelper.AssertEqual(a, gpuResults);
        }
예제 #19
0
        public void MatrixClear()
        {
            var a = _cpu.CreateMatrix(15, 23, (j, k) => k + 1).AsIndexable();

            IIndexableMatrix gpuResults;

            using (var gpuA = _cuda.CreateMatrix(a)) {
                gpuA.Clear();
                gpuResults = gpuA.AsIndexable();
            }

            a.Clear();
            FloatingPointHelper.AssertEqual(gpuResults, a);
        }
예제 #20
0
        public void MatrixNewMatrixFromColumns()
        {
            var cols    = new[] { 1, 2, 9 };
            var a       = _cpu.CreateMatrix(12, 13, (j, k) => (k + 1) * (j + 1)).AsIndexable();
            var results = a.GetNewMatrixFromColumns(cols).AsIndexable();

            IIndexableMatrix gpuResults;

            using (var gpuA = _cuda.CreateMatrix(a))
                using (var m = gpuA.GetNewMatrixFromColumns(cols))
                    gpuResults = m.AsIndexable();

            FloatingPointHelper.AssertEqual(gpuResults, results);
        }
예제 #21
0
        public void MatrixLeakyRELUDerivative()
        {
            var normalDistribution = new Normal(0, 1);
            var a = _cpu.CreateMatrix(3, 7, (j, k) => Convert.ToSingle(normalDistribution.Sample())).AsIndexable();
            var b = a.LeakyReluDerivative().AsIndexable();

            IIndexableMatrix gpuResults;

            using (var gpuA = _cuda.CreateMatrix(a))
                using (var relu = gpuA.LeakyReluDerivative())
                    gpuResults = relu.AsIndexable();

            FloatingPointHelper.AssertEqual(gpuResults, b);
        }
예제 #22
0
        public void MatrixRow()
        {
            const int INDEX = 11;
            var       a     = _cpu.CreateMatrix(20, 50, (j, k) => k * j).AsIndexable();
            var       row   = a.Row(INDEX).AsIndexable();

            IIndexableVector gpuResults;

            using (var gpuA = _cuda.CreateMatrix(a))
                using (var gpuRow = gpuA.Row(INDEX))
                    gpuResults = gpuRow.AsIndexable();

            FloatingPointHelper.AssertEqual(gpuResults, row);
        }
예제 #23
0
        public void VectorSigmoid()
        {
            var a       = _cpu.CreateVector(1000, i => i).AsIndexable();
            var results = a.Sigmoid().AsIndexable();

            IIndexableVector gpuResults;

            using (var gpuA = _cuda.CreateVector(a))
                using (var gpuB = gpuA.Sigmoid()) {
                    gpuResults = gpuB.AsIndexable();
                }

            FloatingPointHelper.AssertEqual(results, gpuResults);
        }
예제 #24
0
        public void MatrixToVector()
        {
            var matrix  = _cpu.CreateMatrix(3, 4, (x, y) => (x + 1) * (y + 1)).AsIndexable();
            var vector  = matrix.ReshapeAsVector();
            var matrix2 = vector.ReshapeAsMatrix(3, 4);

            FloatingPointHelper.AssertEqual(matrix.AsIndexable(), matrix2.AsIndexable());

            using (var gpuMatrix = _cuda.CreateMatrix(matrix.AsIndexable()))
                using (var gpuVector = gpuMatrix.ReshapeAsVector())
                    using (var gpuMatrix2 = gpuVector.ReshapeAsMatrix(3, 4)) {
                        FloatingPointHelper.AssertEqual(gpuMatrix.AsIndexable(), gpuMatrix2.AsIndexable());
                    }
        }
예제 #25
0
        public void VectorAddScalar()
        {
            var a = _cpu.CreateVector(1000, i => i).AsIndexable();

            IIndexableVector gpuResults;

            using (var gpuA = _cuda.CreateVector(a)) {
                gpuA.Add(0.5f);
                gpuResults = gpuA.AsIndexable();
            }

            a.Add(0.5f);
            FloatingPointHelper.AssertEqual(a, gpuResults);
        }
예제 #26
0
        public void MatrixSoftmaxActivation()
        {
            var normalDistribution = new Normal(0, 1);
            var a = _cpu.CreateMatrix(3, 7, (j, k) => Convert.ToSingle(normalDistribution.Sample())).AsIndexable();
            var b = a.SoftmaxActivation().AsIndexable();

            IIndexableMatrix gpuResults;

            using (var gpuA = _cuda.CreateMatrix(a))
                using (var softmax = gpuA.SoftmaxActivation())
                    gpuResults = softmax.AsIndexable();

            FloatingPointHelper.AssertEqual(gpuResults, b);
        }
예제 #27
0
        public void MatrixTransposeAndMultiplication2()
        {
            var a          = _cpu.CreateMatrix(2, 6, (j, k) => k).AsIndexable();
            var b          = _cpu.CreateMatrix(2, 5, (j, k) => j).AsIndexable();
            var cpuResults = a.TransposeThisAndMultiply(b);
            IIndexableMatrix gpuResults;

            using (var gpuA = _cuda.CreateMatrix(a))
                using (var gpuB = _cuda.CreateMatrix(b))
                    using (var gpuC = gpuA.TransposeThisAndMultiply(gpuB))
                        gpuResults = gpuC.AsIndexable();

            FloatingPointHelper.AssertEqual(gpuResults, cpuResults.AsIndexable());
        }
예제 #28
0
        public void VectorMinMax()
        {
            Load();
            var distribution = new Normal(0, 5);
            var a            = _cpu.CreateVector(5000, i => Convert.ToSingle(distribution.Sample())).AsIndexable();
            var minMax       = a.GetMinMax();

            (float Min, float Max)minMax2;
            using (var gpuA = _cuda.CreateVector(a))
                minMax2 = gpuA.GetMinMax();
            FloatingPointHelper.AssertEqual(minMax.Min, minMax2.Min);
            FloatingPointHelper.AssertEqual(minMax.Max, minMax2.Max);
            Cleanup();
        }
예제 #29
0
        public void TensorCreateFromMatrix()
        {
            const int DEPTH = 3, ROWS = 4, COLUMNS = 4;
            var       cpuTensor  = _cpu.Create3DTensor(Enumerable.Range(0, DEPTH).Select(i => _cpu.CreateMatrix(ROWS, COLUMNS, (j, k) => (i + 1) * (j + 1) * (k + 1))).ToList());
            var       cpuMatrix  = cpuTensor.ConvertToMatrix();
            var       cpuTensor2 = cpuMatrix.ConvertTo3DTensor(ROWS, COLUMNS);

            FloatingPointHelper.AssertEqual(cpuTensor.AsIndexable(), cpuTensor2.AsIndexable());

            using (var gpuMatrix = _cuda.CreateMatrix(cpuMatrix.AsIndexable()))
                using (var gpuTensor2 = gpuMatrix.ConvertTo3DTensor(ROWS, COLUMNS)) {
                    FloatingPointHelper.AssertEqual(cpuTensor.AsIndexable(), gpuTensor2.AsIndexable());
                }
        }
예제 #30
0
        public void MatrixColumn()
        {
            const int INDEX = 7;
            var       a     = _cpu.CreateMatrix(13, 17, (j, k) => (j + 1) * (k + 1)).AsIndexable();
            var       row   = a.Column(INDEX).AsIndexable();

            IIndexableVector gpuResults;

            using (var gpuA = _cuda.CreateMatrix(a))
                using (var gpuCol = gpuA.Column(INDEX))
                    gpuResults = gpuCol.AsIndexable();

            FloatingPointHelper.AssertEqual(gpuResults, row);
        }