public void ComputeTwiceGradientShouldYieldTheSameResult()
        {
            const int inputWidth  = 10;
            const int inputHeight = 10;
            const int inputDepth  = 2;

            const int filterWidth  = 3;
            const int filterHeight = 3;
            const int filterCount  = 2;

            // Create layer
            var layer = new ConvLayer <double>(filterWidth, filterHeight, filterCount)
            {
                Stride = 2, BiasPref = 0.1
            };

            layer.Init(inputWidth, inputHeight, inputDepth);

            // Forward pass
            var input = BuilderInstance <double> .Volume.Random(new Shape(inputWidth, inputHeight, inputDepth));

            var output = layer.DoForward(input, true);

            // Set output gradients to 1
            var outputGradient = BuilderInstance <double> .Volume.From(new double[output.Shape.TotalLength].Populate(1.0), output.Shape);

            // Backward pass to retrieve gradients
            layer.Backward(outputGradient);
            var step1 = ((Volume <double>)layer.InputActivationGradients.Clone()).ToArray();

            layer.Backward(outputGradient);
            var step2 = ((Volume <double>)layer.InputActivationGradients.Clone()).ToArray();

            Assert.IsTrue(step1.SequenceEqual(step2));
        }
コード例 #2
0
        public void ConvLayerSerialization()
        {
            var layer = new ConvLayer(5, 5, 8)
            {
                Stride = 1, Pad = 2, BiasPref = 0.5
            };

            layer.Init(28, 24, 1);

            var data = layer.GetData();

            Assert.AreEqual(28, data["InputWidth"]);
            Assert.AreEqual(24, data["InputHeight"]);
            Assert.AreEqual(1, data["InputDepth"]);

            var deserialized = LayerBase <double> .FromData(data) as ConvLayer;

            Assert.IsNotNull(deserialized);
            Assert.AreEqual(28, deserialized.InputWidth);
            Assert.AreEqual(24, deserialized.InputHeight);
            Assert.AreEqual(1, deserialized.InputDepth);

            Assert.AreEqual(layer.Width, deserialized.Width);
            Assert.AreEqual(layer.Height, deserialized.Height);
            Assert.AreEqual(layer.Pad, deserialized.Pad);
            Assert.AreEqual(layer.Stride, deserialized.Stride);
            Assert.AreEqual(layer.FilterCount, deserialized.FilterCount);

            Assert.AreEqual(layer.Filters.Shape, deserialized.Filters.Shape);
            Assert.IsTrue(layer.Filters.ToArray().SequenceEqual(deserialized.Filters.ToArray()));

            Assert.AreEqual(layer.Bias.Shape, deserialized.Bias.Shape);
            Assert.IsTrue(layer.Bias.ToArray().SequenceEqual(deserialized.Bias.ToArray()));

            Assert.AreEqual(layer.BiasPref, deserialized.BiasPref);
        }
コード例 #3
0
        public void SerializationTest()
        {
            // Create a ConvLayer
            var layer = new ConvLayer(5, 5, 2)
            {
                BiasPref = 0.1,
                Pad      = 1,
                Stride   = 2
            };

            layer.Init(10, 10, 3);

            foreach (var filter in layer.Filters)
            {
                for (int i = 0; i < filter.Length; i++)
                {
                    filter.Set(i, i);
                }
            }

            for (int i = 0; i < layer.Biases.Length; i++)
            {
                layer.Biases.Set(i, i);
            }

            ConvLayer deserialized;

            using (var ms = new MemoryStream())
            {
                // Serialize
                IFormatter formatter = new BinaryFormatter();
                formatter.Serialize(ms, layer);

                // Deserialize
                ms.Position  = 0;
                deserialized = formatter.Deserialize(ms) as ConvLayer;
            }


            Assert.AreEqual(layer.BiasPref, deserialized.BiasPref);
            Assert.AreEqual(layer.Stride, deserialized.Stride);
            Assert.AreEqual(layer.Pad, deserialized.Pad);
            Assert.AreEqual(layer.Filters.Count, deserialized.Filters.Count);
            Assert.AreEqual(layer.InputDepth, deserialized.InputDepth);
            Assert.AreEqual(layer.InputHeight, deserialized.InputHeight);
            Assert.AreEqual(layer.InputWidth, deserialized.InputWidth);
            Assert.AreEqual(layer.L1DecayMul, deserialized.L1DecayMul);
            Assert.AreEqual(layer.L2DecayMul, deserialized.L2DecayMul);
            Assert.AreEqual(layer.OutputDepth, deserialized.OutputDepth);
            Assert.AreEqual(layer.OutputHeight, deserialized.OutputHeight);
            Assert.AreEqual(layer.OutputWidth, deserialized.OutputWidth);

            for (int j = 0; j < layer.Filters.Count; j++)
            {
                var filter             = layer.Filters[j];
                var deserializedFilter = deserialized.Filters[j];

                for (int i = 0; i < filter.Length; i++)
                {
                    Assert.AreEqual(filter.Get(i), deserializedFilter.Get(i));
                }
            }

            for (int i = 0; i < layer.Biases.Length; i++)
            {
                Assert.AreEqual(layer.Biases.Get(i), deserialized.Biases.Get(i));
            }
        }