Esempio n. 1
0
        internal void Update(Layer preLayer, Connection connection)
        {
            Blas2.gemv(BlasLayout.RowMajor, BlasTranspose.NoTrans,
                       connection.PostLayerSize, connection.PreLayerSize, 1.0f, connection.Weight, connection.PreLayerSize,
                       preLayer.Unit, 1, 0.0f, Input, 1);

            Blas1.copy(PureSize, Function(Input), 1, Unit, 1);
        }
Esempio n. 2
0
        public void CopyTest()
        {
            var xf = new[] { 1.0f, -1.0f, 0.0f };
            var yf = new float[xf.Length];
            var xd = new[] { 1.0, -1.0, 0.0 };
            var yd = new double[xd.Length];

            Blas1.copy(3, xf, 1, yf, 1);
            Blas1.copy(3, xd, 1, yd, 1);

            for (var i = 0; i < yf.Length; i++)
            {
                Assert.AreEqual(xf[i], yf[i]);
                Assert.AreEqual(xd[i], yd[i]);
            }
        }
Esempio n. 3
0
        internal override void Update(float[] weight, float[] delta)
        {
            if (_weightMemory == null)
            {
                Blas1.copy(weight.Length, weight, 1, out _weightMemory, 1);
            }
            var velocity = _velocity ?? new float[weight.Length];

            Blas1.scal(velocity.Length, MomentumValue, velocity, 1);
            Blas1.axpy(velocity.Length, -LearningRate, delta, 1, velocity, 1);
            _velocity = velocity;
            Blas1.axpy(_weightMemory.Length, 1.0f, velocity, 1, _weightMemory, 1);
            Blas1.copy(_velocity.Length, _velocity, 1, out var ahead, 1);
            Blas1.scal(ahead.Length, MomentumValue, ahead, 1);
            Blas1.copy(_weightMemory.Length, _weightMemory, 1, weight, 1);
            Blas1.axpy(weight.Length, 1.0f, ahead, 1, weight, 1);
        }
Esempio n. 4
0
        public void ScalTest()
        {
            const float  af = 2.0f;
            const double ad = -1.0;
            var          xf = new[] { 1.0f, 1.0f, 1.0f };

            Blas1.copy(xf.Length, xf, 1, out var mxf, 1);
            var xd = new[] { 1.0, 1.0, 1.0 };

            Blas1.copy(xd.Length, xd, 1, out var mxd, 1);

            Blas1.scal(mxf.Length, af, mxf, 1);
            Blas1.scal(mxd.Length, ad, mxd, 1);

            for (var i = 0; i < mxf.Length; i++)
            {
                Assert.AreEqual(af * xf[i], mxf[i]);
            }
            for (var i = 0; i < mxd.Length; i++)
            {
                Assert.AreEqual(ad * xd[i], mxd[i]);
            }
        }
Esempio n. 5
0
        private static void Main()
        {
            CompareTimeDot(10);
            CompareTimeDot(100);
            CompareTimeDot(100000);

            CompareTimeLU();

            void CompareTimeDot(int size)
            {
                var sw = new Stopwatch();

                (var x, var y) = GenerateVector();
                WriteLine($"Calc dot product by raw C# : size = {size}");
                sw.Reset();
                var res = 0.0;

                for (var i = 0; i < LoopDot; i++)
                {
                    sw.Start();
                    res = Dot(x, y);
                    sw.Stop();
                }
                WriteLine($"Result : {res}\tTime : {sw.Elapsed / (double) LoopDot}");

                WriteLine($"Calc dot product by BLAS : size = {size}");
                sw.Reset();
                for (var i = 0; i < LoopDot; i++)
                {
                    sw.Start();
                    res = Blas1.dot(size, x, 1, y, 1);
                    sw.Stop();
                }
                WriteLine($"Result : {res}\tTime : {sw.Elapsed / (double) LoopDot}\n");

                (double[] x, double[] y) GenerateVector()
                {
                    x = new double[size];
                    y = new double[size];
                    for (var i = 0; i < size; i++)
                    {
                        x[i] = 1.0;
                        y[i] = 1.0;
                    }
                    return(x, y);
                }
            }

            void CompareTimeLU()
            {
                const int    M     = 49;
                const int    N     = M * M;
                const double h     = 1.0 / (M + 1);
                const double Heat  = 4.0;
                var          aBase = new double[N * N];
                var          bBase = new double[N];
                var          ipiv  = new int[N];

                for (var i = 1; i <= M; i++)
                {
                    for (var j = 1; j <= M; j++)
                    {
                        var k = (j - 1) * M + i - 1;
                        aBase[k * N + k] = 4.0 / (h * h);
                        if (i > 1)
                        {
                            var kl = k - 1;
                            aBase[kl * N + k] = -1.0 / (h * h);
                        }
                        if (i < M)
                        {
                            var kr = k + 1;
                            aBase[kr * N + k] = -1.0 / (h * h);
                        }
                        if (j > 1)
                        {
                            var kd = k - M;
                            aBase[kd * N + k] = -1.0 / (h * h);
                        }
                        if (j < M)
                        {
                            var ku = k + M;
                            aBase[ku * N + k] = -1.0 / (h * h);
                        }
                        bBase[k] = Heat;
                    }
                }

                var sw = new Stopwatch();

                WriteLine("Calc Poisson eq by raw C#");
                sw.Reset();
                var res = new double[bBase.Length];

                for (var i = 0; i < LoopLU; i++)
                {
                    Blas1.copy(aBase.Length, aBase, 1, out var a, 1);
                    Blas1.copy(bBase.Length, bBase, 1, out var b, 1);
                    sw.Start();
                    Decomp(N, N, a, ipiv);
                    Solve(N, N, a, b, ipiv);
                    sw.Stop();
                    if (i == LoopLU - 1)
                    {
                        Blas1.copy(b.Length, b, 1, res, 1);
                    }
                }
                WriteLine($"Result : {res[((M + 1) / 2 - 1) * M + M + 1]}\tTime : {sw.Elapsed / (double) LoopLU}");

                WriteLine("Calc Poisson eq by LAPACK");
                sw.Reset();
                for (var i = 0; i < LoopLU; i++)
                {
                    Blas1.copy(aBase.Length, aBase, 1, out var a, 1);
                    Blas1.copy(bBase.Length, bBase, 1, out var b, 1);
                    sw.Start();
                    Lapack.getrf(LapackLayout.RowMajor, N, N, a, N, ipiv);
                    Lapack.getrs(LapackLayout.RowMajor, LapackTranspose.NoTrans, N, 1, a, N, ipiv, b, 1);
                    sw.Stop();
                    if (i == LoopLU - 1)
                    {
                        Blas1.copy(b.Length, b, 1, res, 1);
                    }
                }
                WriteLine($"Result : {res[((M + 1) / 2 - 1) * M + M + 1]}\tTime : {sw.Elapsed / (double) LoopLU}");
            }
        }