Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="OutputSentence"></param>
        /// <param name="g"></param>
        /// <param name="cost"></param>
        /// <param name="encoded"></param>
        /// <returns></returns>
        private double DecodeOutput(List <string> OutputSentence, ComputeGraph g, double cost, List <WeightMatrix> encoded)
        {
            int ix_input = 1;

            for (int i = 0; i < OutputSentence.Count + 1; i++)
            {
                int ix_target = 0;
                if (i == OutputSentence.Count)
                {
                    ix_target = 0;
                }
                else
                {
                    ix_target = wordToIndex[OutputSentence[i]].i;
                }

                var x       = g.PeekRow(Embedding, ix_input);
                var eOutput = decoder.Decode(x, encoded, g);
                if (UseDropout)
                {
                    eOutput = g.Dropout(eOutput, 0.2);
                }
                var o = g.add(
                    g.mul(eOutput, this.Whd), this.bd);
                if (UseDropout)
                {
                    o = g.Dropout(o, 0.2);
                }

                var probs = g.SoftmaxWithCrossEntropy(o);
                cost += -Math.Log(probs.Weight[ix_target]);

                o.Gradient             = probs.Weight;
                o.Gradient[ix_target] -= 1;
                ix_input = ix_target;
            }
            return(cost);
        }