示例#1
0
 private static double Score(SimpleMatrix features, IList <SimpleMatrix> weights)
 {
     for (int i = 0; i < weights.Count; i += 2)
     {
         features = weights[i].Mult(features).Plus(weights[i + 1]);
         if (weights[i].NumRows() > 1)
         {
             features = NeuralUtils.ElementwiseApplyReLU(features);
         }
     }
     return(features.ElementSum());
 }
示例#2
0
        /// <summary>Applies softmax to all of the elements of the matrix.</summary>
        /// <remarks>
        /// Applies softmax to all of the elements of the matrix.  The return
        /// matrix will have all of its elements sum to 1.  If your matrix is
        /// not already a vector, be sure this is what you actually want.
        /// </remarks>
        public static SimpleMatrix Softmax(SimpleMatrix input)
        {
            SimpleMatrix output = new SimpleMatrix(input);

            for (int i = 0; i < output.NumRows(); ++i)
            {
                for (int j = 0; j < output.NumCols(); ++j)
                {
                    output.Set(i, j, Math.Exp(output.Get(i, j)));
                }
            }
            double sum = output.ElementSum();

            // will be safe, since exp should never return 0
            return(output.Scale(1.0 / sum));
        }