예제 #1
0
        public void SoftMaxGradient()
        {
            // input = [1,  0.1, 0.1, 0.1]
            var input = new Double.Volume(new[] { 1.0, 0.1, 0.1, 0.1 }, new Shape(1, 1, -1, 1));

            // output  = softmax(input)
            var output = input.SoftMax();

            // groundTruth = [0, 1, 0 , 0]
            var groundTruth = new Double.Volume(new[] { 0.0, 1.0, 0.0, 0.0 }, new Shape(1, 1, -1, 1));

            // output gradient = 1 - groundTruth ./ output
            var outputGradient = new Double.Volume(new double[4], new Shape(1, 1, -1, 1));

            groundTruth.Storage.Map((p, q) => 1 - p / q, output.Storage, outputGradient.Storage);

            // inputGradient = softmax_gradient(output, outputGradient)
            var inputGradient = output.SoftMaxGradient(outputGradient);

            // theorical result = output-groundTruth
            var result = output - groundTruth;

            Assert.AreEqual(result.Get(0, 0, 0, 0), inputGradient.Get(0, 0, 0, 0), 1e-4);
            Assert.AreEqual(result.Get(0, 0, 1, 0), inputGradient.Get(0, 0, 1, 0), 1e-4);
            Assert.AreEqual(result.Get(0, 0, 2, 0), inputGradient.Get(0, 0, 2, 0), 1e-4);
            Assert.AreEqual(result.Get(0, 0, 3, 0), inputGradient.Get(0, 0, 3, 0), 1e-4);
        }
예제 #2
0
        public void SoftMax()
        {
            var volume1  = new Double.Volume(new[] { 0.0, 0.0, 0.0, 10000.0 }, new Shape(1, 1, -1, 1));
            var softmax1 = volume1.SoftMax();

            Assert.AreEqual(0.0, softmax1.Get(0, 0, 0, 0));
            Assert.AreEqual(0.0, softmax1.Get(0, 0, 1, 0));
            Assert.AreEqual(0.0, softmax1.Get(0, 0, 1, 0));
            Assert.AreEqual(1.0, softmax1.Get(0, 0, 3, 0));

            var volume2  = new Double.Volume(new[] { 10000.0, 0.0, 0.0, 10000.0 }, new Shape(1, 1, -1, 1));
            var softmax2 = volume2.SoftMax();

            Assert.AreEqual(0.5, softmax2.Get(0, 0, 0, 0));
            Assert.AreEqual(0.5, softmax2.Get(0, 0, 3, 0));
        }
예제 #3
0
        public void SoftMax()
        {
            var input1 = new Double.Volume(new[] { 0.0, 0.0, 0.0, 10000.0 }, new Shape(1, 1, -1, 1),
                                           GpuContext.Default);
            var softmax1 = input1.SoftMax();

            Assert.AreEqual(0.0, softmax1.Get(0, 0, 0, 0));
            Assert.AreEqual(0.0, softmax1.Get(0, 0, 1, 0));
            Assert.AreEqual(0.0, softmax1.Get(0, 0, 2, 0));
            Assert.AreEqual(1.0, softmax1.Get(0, 0, 3, 0));

            var input2 = new Double.Volume(new[] { 10000.0, 0.0, 0.0, 10000.0 }, new Shape(1, 1, -1, 1),
                                           GpuContext.Default);
            var softmax2 = input2.SoftMax();

            Assert.AreEqual(0.5, softmax2.Get(0, 0, 0, 0));
            Assert.AreEqual(0.5, softmax2.Get(0, 0, 3, 0));
        }
예제 #4
0
        public void SoftMaxBatch()
        {
            var volume1 = new Double.Volume(new[]
            {
                0.0, 0.0, 0.0, 10000.0,
                0.0, 0.0, 10000.0, 0.0
            }, new Shape(1, 1, -1, 2), GpuContext.Default);
            var softmax1 = volume1.SoftMax();

            Assert.AreEqual(0.0, softmax1.Get(0, 0, 0, 0));
            Assert.AreEqual(0.0, softmax1.Get(0, 0, 1, 0));
            Assert.AreEqual(0.0, softmax1.Get(0, 0, 2, 0));
            Assert.AreEqual(1.0, softmax1.Get(0, 0, 3, 0));

            Assert.AreEqual(0.0, softmax1.Get(0, 0, 0, 1));
            Assert.AreEqual(0.0, softmax1.Get(0, 0, 1, 1));
            Assert.AreEqual(1.0, softmax1.Get(0, 0, 2, 1));
            Assert.AreEqual(0.0, softmax1.Get(0, 0, 3, 1));
        }