コード例 #1
0
        public List <Matrix> activate(List <Matrix> x, ForwdBackwdProp g)
        {
            // List<int> input = x.inputs;
            Matrix        final   = new Matrix(Global.hiddenDim, 1);
            List <Matrix> outputs = new List <Matrix>();
            Matrix        _h_tm1  = Matrix.newMatrix_0(_hiddenDim, 1);
            Matrix        _s_tm1  = Matrix.newMatrix_0(_hiddenDim, 1);

            for (int i = 0; i < x.Count; i++)
            {
                //input gate
                Matrix sum0      = g.Mul(_wix, x[i]);
                Matrix sum1      = g.Mul(_wih, _h_tm1);
                Matrix inputGate = g.sigNonlin(g.Add(g.Add(sum0, sum1), _iBias));

                //forget gate
                Matrix sum2       = g.Mul(_wfx, x[i]);
                Matrix sum3       = g.Mul(_wfh, _h_tm1);
                Matrix forgetGate = g.sigNonlin(g.Add(g.Add(sum2, sum3), _fBias));

                //output gate
                Matrix sum4       = g.Mul(_wox, x[i]);
                Matrix sum5       = g.Mul(_woh, _h_tm1);
                Matrix outputGate = g.sigNonlin(g.Add(g.Add(sum4, sum5), _Bias));

                //write operation on cells
                Matrix sum6      = g.Mul(_wcx, x[i]);
                Matrix sum7      = g.Mul(_wch, _h_tm1);
                Matrix cellInput = g.tanhNonlin(g.Add(g.Add(sum6, sum7), _cBias));

                //compute new cell activation
                Matrix retainCell = g.Elmul(forgetGate, _s_tm1);
                Matrix writeCell  = g.Elmul(inputGate, cellInput);
                Matrix cellAct    = g.Add(retainCell, writeCell);

                //compute hidden state as gated, saturated cell activations
                Matrix output = g.Elmul(outputGate, g.tanhNonlin(cellAct));
                //if (i == 0)
                //{
                //    final = output;
                //}
                //else
                //{
                //    final = g.ConcatVectors(final, output);
                //}
                //final = g.Add(final, output);

                outputs.Add(output);
                //rollover activations for next iteration
                _h_tm1 = output;
                _s_tm1 = cellAct;
                //_h = g.Add(output, _h);
            }

            return(outputs);
        }
コード例 #2
0
        public List <Matrix> activate(List <Matrix> input, ForwdBackwdProp g)
        {
            List <Matrix> temp = new List <Matrix>();

            for (int i = 1; i < input.Count; i++)
            {
                Matrix concanate = g.ConcatVectors(input[i - 1], input[i]);
                //Matrix rl = g.sigNonlin(g.Add(g.Mul(rl1w, input[i - 1]), g.Mul(rl2w, input[i])));
                Matrix rl = g.sigNonlin(g.Mul(_gl, concanate));
                Matrix rr = g.sigNonlin(g.Mul(_gr, concanate));
                //Matrix rr = g.sigNonlin(g.Add(g.Mul(rr1w, input[i - 1]), g.Mul(rr2w, input[i])));
                //Matrix hh = g.tanhNonlin(g.Add(g.Mul(wi, input[i - 1]), g.Mul(wx, input[i])));
                Matrix hh = g.tanhNonlin(g.Mul(hh1w, g.ConcatVectors(g.Elmul(rl, input[i - 1]), g.Elmul(rr, input[i]))));

                //Matrix hhh = g.ConcatVectors(g.ConcatVectors(hh, input[i - 1]), input[i]);
                //Matrix zh1 = g.sigNonlin(g.Mul(hh2w, hhh));
                //Matrix zh2 = g.sigNonlin(g.Mul(hh3w, hhh));
                //Matrix zh3 = g.tanhNonlin(g.Mul(hh4w, hhh));
                //Matrix z1=g.Exp()


                Matrix concanate1 = g.ConcatVectors(g.ConcatVectors(hh, input[i - 1]), input[i]);

                Matrix z1 = g.sigNonlin(g.Mul(_u1, concanate1));
                Matrix z2 = g.sigNonlin(g.Mul(_u2, concanate1));
                Matrix z3 = g.sigNonlin(g.Mul(_u3, concanate1));


                //Matrix zl = g.SumDivid1(zh1, zh2, zh3);
                // Matrix zm = g.SumDivid2(zh1, zh2, zh3);
                //Matrix zr = g.SumDivid3(zh1, zh2, zh3);

                Matrix output = g.Add(g.Add(g.Elmul(z1, hh), g.Elmul(z2, input[i - 1])), g.Elmul(z3, input[i]));

                temp.Add(output);
            }


            return(temp);
        }