Example #1
0
        public Vector Run(Vector input, LstmGatesForLayer gatesLayer)
        {
            Input = input;
            var lastOutput = input;

            for (var cellNumber = 0; cellNumber < Cells.Count; cellNumber++)
            {
                var cell         = Cells[cellNumber];
                var gatesForCell = gatesLayer[cellNumber];
                lastOutput = cell.CalculateAndGetOutput(lastOutput, gatesForCell);
            }
            Output = lastOutput;
            return(Output);
        }
Example #2
0
        public (Vector error, Vector[] diffsOutput, Vector[] diffsForget) Learn(Vector actual, Vector ideal,
                                                                                Vector[] diffsOutputFromNext, Vector[] diffsForgetFromNext, LstmGatesForLayer gatesLayer)
        {
            var diffsOutput = new Vector[Cells.Count];
            var diffsForget = new Vector[Cells.Count];
            var error       = ideal is null
                                ? null
                                : ((actual - ideal) ^ 2) * 0.5;
            var diffInputFromNextCell = ideal is null
                                ? new Vector(Cells.Last().Output.Length)
                                : actual - ideal;

            for (var i = Cells.Count - 1; i >= 0; i--)
            {
                var diffOutputFromNextLayer = diffsOutputFromNext[i];
                var diffForgetFromNextLayer = diffsForgetFromNext[i];
                var gatesForCell            = gatesLayer[i];
                (diffsOutput[i], diffsForget[i], diffInputFromNextCell) = Cells[i].Learn(diffInputFromNextCell, diffOutputFromNextLayer,
                                                                                         diffForgetFromNextLayer, gatesForCell);
            }
            return(error, diffsOutput, diffsForget);
        }