Esempio n. 1
0
        public override NdArray SingleInputForward(NdArray val)
        {
            NdArray result = val.Clone();

            result.ParentFunc = this;
            result.Reshape(this.Shape);

            return(result);
        }
Esempio n. 2
0
        public NdArray <T> ReshapeForward(NdArray <T> val)
        {
            NdArray <T> result = val.Clone();

            result.ParentFunc = this;
            result.Reshape(this.Shape);

            return(result);
        }
Esempio n. 3
0
        NdArray ForwardCpu(NdArray val)
        {
            NdArray result = val.Clone();

            result.ParentFunc = this;
            result.Reshape(this.Shape);

            return(result);
        }
Esempio n. 4
0
        public static NdArray <Real> SingleInputForward(NdArray <Real> x, IFunction <Real> upward, IFunction <Real> lateral, List <Real[][]> paramList, List <NdArray <Real> > hPrevParams, ref NdArray <Real> hParam, ref Real[] lcPrev, int outputCount, IFunction <Real> lstm)
        {
            int outputDataSize = x.BatchCount * outputCount;

            NdArray <Real> lstmIn = upward.Forward(x)[0];

            if (hParam == null)
            {
                lcPrev = new Real[outputDataSize];
            }
            else
            {
                NdArray <Real> hPrevParam = hParam.Clone();
                if (hPrevParam.Grad != null)
                {
                    hPrevParam.InitGrad();
                }
                lstmIn += lateral.Forward(hPrevParam)[0];
                hPrevParams.Add(hPrevParam);
            }

            //0:cPrev 1:a 2:i 3:f 4:o 5:c
            Real[][] param   = { lcPrev, new Real[outputDataSize], new Real[outputDataSize], new Real[outputDataSize], new Real[outputDataSize], new Real[outputDataSize] };
            Real[]   lhParam = new Real[outputDataSize];

            int index = 0;

            for (int outIndex = 0; outIndex < lhParam.Length; outIndex++)
            {
                param[1][outIndex] = Math.Tanh(lstmIn.Data[index++]);
                param[2][outIndex] = Sigmoid(lstmIn.Data[index++]);
                param[3][outIndex] = Sigmoid(lstmIn.Data[index++]);
                param[4][outIndex] = Sigmoid(lstmIn.Data[index++]);

                param[5][outIndex] = param[1][outIndex] * param[2][outIndex] + param[3][outIndex] * param[0][outIndex];

                lhParam[outIndex] = param[4][outIndex] * Math.Tanh(param[5][outIndex]);
            }

            paramList.Add(param);

            //Backwardで消えないように別で保管
            lcPrev = param[5];

            hParam = new NdArray <Real>(lhParam, new[] { outputCount }, x.BatchCount, lstm);
            return(hParam);
        }