Example #1
0
        static double Evaluate(FunctionStack model, int[] dataset)
        {
            FunctionStack predictModel = (FunctionStack)model.Clone();

            predictModel.ResetState();

            Real totalLoss      = 0;
            long totalLossCount = 0;

            for (int i = 0; i < dataset.Length - 1; i++)
            {
                NdArray x = new NdArray(new[] { 1 }, BATCH_SIZE);
                NdArray t = new NdArray(new[] { 1 }, BATCH_SIZE);

                for (int j = 0; j < BATCH_SIZE; j++)
                {
                    x.Data[j] = dataset[j + i];
                    t.Data[j] = dataset[j + i + 1];
                }

                Real sumLoss = new SoftmaxCrossEntropy().Evaluate(predictModel.Forward(x), t);
                totalLoss += sumLoss;
                totalLossCount++;
            }

            //calc perplexity
            return(Math.Exp(totalLoss / (totalLossCount - 1)));
        }