Example #1
0
        public LSTM(int inSize, int outSize, Array lateralInit = null, Array upwardInit = null, Array biasInit = null, Array forgetBiasInit = null, string name = FUNCTION_NAME, string[] inputNames = null, string[] outputNames = null, bool gpuEnable = false) : base(name, inputNames, outputNames)
        {
            this.OutputCount = outSize;

            List <NdArray <T> > functionParameters = new List <NdArray <T> >();

            T[] lateralW = null;
            T[] upwardW  = null;
            T[] upwardb  = null;

            if (upwardInit != null)
            {
                upwardW = new T[inSize * outSize * 4];

                for (int i = 0; i < 4; i++)
                {
                    Buffer.BlockCopy(upwardInit, 0, upwardW, i * upwardInit.Length * Marshal.SizeOf <T>(), upwardInit.Length * Marshal.SizeOf <T>());
                }
            }

            if (lateralInit != null)
            {
                lateralW = new T[outSize * outSize * 4];

                for (int i = 0; i < 4; i++)
                {
                    Buffer.BlockCopy(lateralInit, 0, lateralW, i * lateralInit.Length * Marshal.SizeOf <T>(), lateralInit.Length * Marshal.SizeOf <T>());
                }
            }

            if (biasInit != null && forgetBiasInit != null)
            {
                upwardb = new T[outSize * 4];

                T[] tmpBiasInit = biasInit.FlattenEx <T>();

                for (int i = 0; i < biasInit.Length; i++)
                {
                    upwardb[i * 4 + 0] = tmpBiasInit[i];
                    upwardb[i * 4 + 1] = tmpBiasInit[i];
                    upwardb[i * 4 + 3] = tmpBiasInit[i];
                }

                T[] tmpforgetBiasInit = forgetBiasInit.FlattenEx <T>();

                for (int i = 0; i < tmpforgetBiasInit.Length; i++)
                {
                    upwardb[i * 4 + 2] = tmpforgetBiasInit[i];
                }
            }

            this.upward = new Linear <T>(inSize, outSize * 4, noBias: false, initialW: upwardW, initialb: upwardb, name: "upward");
            functionParameters.AddRange(this.upward.Parameters);

            //lateralはBiasは無し
            this.lateral = new Linear <T>(outSize, outSize * 4, noBias: true, initialW: lateralW, name: "lateral");
            functionParameters.AddRange(this.lateral.Parameters);

            this.Parameters = functionParameters.ToArray();

            InitFunc(new StreamingContext());
        }
Example #2
0
        public void RandomTest()
        {
            Python.Initialize();
            Chainer.Initialize();

            int inputCount  = 1 + Mother.Dice.Next() % 49;
            int outputCount = 1 + Mother.Dice.Next() % 49;

            Real[,] input = (Real[, ])Initializer.GetRealNdArray(new[] { 1, inputCount });

            Real[,] dummyGy = (Real[, ])Initializer.GetRealNdArray(new[] { 1, outputCount });
            Real[,] w       = (Real[, ])Initializer.GetRealNdArray(new[] { outputCount, inputCount });

            Real[] b = Initializer.GetRealArray(outputCount);

            //Chainer
            NChainer.Linear <Real> cLinear = new NChainer.Linear <Real>(inputCount, outputCount, false, Real.ToBaseNdArray(w), Real.ToBaseArray(b));

            Variable <Real> cX = new Variable <Real>(Real.ToBaseNdArray(input));

            Variable <Real> cY = cLinear.Forward(cX);

            cY.Grad = Real.ToBaseNdArray(dummyGy);

            cY.Backward();


            //KelpNet
            KelpNet.Linear linear = new KelpNet.Linear(inputCount, outputCount, false, w, b);

            NdArray x = new NdArray(input);

            NdArray y = linear.Forward(x)[0];

            y.Grad = Real.ToRealArray(dummyGy);

            y.Backward();


            Real[] cYdata = Real.ToRealArray((Real[, ])cY.Data);
            Real[] cXgrad = Real.ToRealArray((Real[, ])cX.Grad);

            Real[] cWgrad = Real.ToRealArray((Real[, ])cLinear.W.Grad);
            Real[] cbgrad = (Real[])cLinear.b.Grad;

            //許容範囲を算出
            double delta = 0.00001;

            //y
            Assert.AreEqual(cYdata.Length, y.Data.Length);
            for (int i = 0; i < y.Data.Length; i++)
            {
                Assert.AreEqual(cYdata[i], y.Data[i], delta);
            }

            //x.grad
            Assert.AreEqual(cXgrad.Length, x.Grad.Length);
            for (int i = 0; i < x.Grad.Length; i++)
            {
                Assert.AreEqual(cXgrad[i], x.Grad[i], delta);
            }

            //W.grad
            Assert.AreEqual(cWgrad.Length, linear.Weight.Grad.Length);
            for (int i = 0; i < linear.Weight.Grad.Length; i++)
            {
                Assert.AreEqual(cWgrad[i], linear.Weight.Grad[i], delta);
            }

            //b.grad
            Assert.AreEqual(cbgrad.Length, linear.Bias.Grad.Length);
            for (int i = 0; i < linear.Bias.Grad.Length; i++)
            {
                Assert.AreEqual(cbgrad[i], linear.Bias.Grad[i], delta);
            }
        }
Example #3
0
        public LSTM(int inSize, int outSize, Array lateralInit = null, Array upwardInit = null, Array biasInit = null, Array forgetBiasInit = null, string name = FUNCTION_NAME, string[] inputNames = null, string[] outputNames = null, bool gpuEnable = false) : base(name, inputNames, outputNames)
        {
            this.InputCount  = inSize;
            this.OutputCount = outSize;

            List <NdArray> functionParameters = new List <NdArray>();

            Real[] lateralW = null;
            Real[] upwardW  = null;
            Real[] upwardb  = null;

            if (upwardInit != null)
            {
                upwardW = new Real[inSize * outSize * 4];

                Real[] tmpUpwardInit = Real.ToRealArray(upwardInit);

                for (int i = 0; i < 4; i++)
                {
                    Array.Copy(tmpUpwardInit, 0, upwardW, i * tmpUpwardInit.Length, tmpUpwardInit.Length);
                }
            }

            if (lateralInit != null)
            {
                lateralW = new Real[outSize * outSize * 4];

                Real[] tmpLateralInit = Real.ToRealArray(lateralInit);

                for (int i = 0; i < 4; i++)
                {
                    Array.Copy(tmpLateralInit, 0, lateralW, i * tmpLateralInit.Length, tmpLateralInit.Length);
                }
            }

            if (biasInit != null && forgetBiasInit != null)
            {
                upwardb = new Real[outSize * 4];

                Real[] tmpBiasInit = Real.ToRealArray(biasInit);

                for (int i = 0; i < biasInit.Length; i++)
                {
                    upwardb[i * 4 + 0] = tmpBiasInit[i];
                    upwardb[i * 4 + 1] = tmpBiasInit[i];
                    upwardb[i * 4 + 3] = tmpBiasInit[i];
                }

                Real[] tmpforgetBiasInit = Real.ToRealArray(forgetBiasInit);

                for (int i = 0; i < tmpforgetBiasInit.Length; i++)
                {
                    upwardb[i * 4 + 2] = tmpforgetBiasInit[i];
                }
            }

            this.upward = new Linear(inSize, outSize * 4, noBias: false, initialW: upwardW, initialb: upwardb, name: "upward", gpuEnable: gpuEnable);
            functionParameters.AddRange(this.upward.Parameters);

            //lateralはBiasは無し
            this.lateral = new Linear(outSize, outSize * 4, noBias: true, initialW: lateralW, name: "lateral", gpuEnable: gpuEnable);
            functionParameters.AddRange(this.lateral.Parameters);

            this.Parameters = functionParameters.ToArray();

            SingleInputForward   = ForwardCpu;
            SingleOutputBackward = BackwardCpu;
        }