Example #1
0
            public void CategoricalCrossEntropyTest()
            {
                var actual   = new Matrix(4, 1);
                var expected = new Matrix(4, 1);

                actual.InRandomize();
                expected.InRandomize();

                var autoErr = new CategoricalCrossEntropy().Evaluate(actual, expected);
                var error   = 0.0;

                for (var i = 0; i < actual.Rows; i++)
                {
                    for (var j = 0; j < actual.Columns; j++)
                    {
                        error += -expected[i, j] * Math.Log(actual[i, j] + double.Epsilon);
                    }
                }
                error /= actual.Rows * actual.Columns;
                Assert.IsTrue(Math.Abs(error - autoErr) < 0.01, new CategoricalCrossEntropy().Type().ToString() + " Forward!");

                var autoDErr = new CategoricalCrossEntropy().Backward(actual, expected);
                var dErr     = (expected * -1).HadamardDivision(actual + actual.Fill(double.Epsilon));

                Assert.IsTrue(Math.Abs(dErr.FrobeniusNorm() - autoDErr.FrobeniusNorm()) < 0.01, new CategoricalCrossEntropy().Type().ToString() + " Backward!");
            }
        public void ShouldCompile()
        {
            var graph   = new TFGraph();
            var context = new ModelCompilationContext(graph);

            var predictions = graph.Placeholder(TFDataType.Double, new TFShape(-1, 10));
            var actuals     = graph.Placeholder(TFDataType.Double, new TFShape(-1, 10));

            var loss = new CategoricalCrossEntropy().Compile(context, predictions, actuals);

            loss.Should().NotBeNull();
        }
Example #3
0
        public void BaseCost()
        {
            var actual   = new Matrix(4, 1);
            var expected = new Matrix(4, 1);

            actual.InRandomize();
            expected.InRandomize();

            var b = new CategoricalCrossEntropy();

            b.Evaluate(actual, expected);
            b.Backward(actual, expected);
            b.ResetCost();

            Assert.IsTrue(Math.Abs(b.BatchCost) < 0.01, "Base Cost Reset!");
        }
        public void ShouldBeOptimizable()
        {
            var graph   = new TFGraph();
            var context = new ModelCompilationContext(graph);

            var input  = new Input(new long[] { 10 }, name: "Input0");
            var output = new Dense(2, input, name: "Dense0");

            var compiledInput  = input.Compile(context);
            var compiledOutput = output.Compile(context);

            var loss = new CategoricalCrossEntropy();

            var compiledLoss = loss.Compile(context, compiledOutput,
                                            context.Graph.Placeholder(TFDataType.Double, new TFShape(-1, 2)));

            var gradients = graph.AddGradients(
                new [] { compiledLoss },
                context.Parameters.ToArray());
        }