public void ComputeTwiceGradientShouldYieldTheSameResult() { const int inputWidth = 20; const int inputHeight = 20; const int inputDepth = 2; var layer = new TanhLayer <double>(); 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.SameAs(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)); }
public void SerializationTest() { // Create a SigmoidLayer var layer = new TanhLayer(); layer.Init(10, 10, 3); TanhLayer deserialized; using (var ms = new MemoryStream()) { // Serialize IFormatter formatter = new BinaryFormatter(); formatter.Serialize(ms, layer); // Deserialize ms.Position = 0; deserialized = formatter.Deserialize(ms) as TanhLayer; } Assert.AreEqual(layer.InputDepth, deserialized.InputDepth); Assert.AreEqual(layer.InputHeight, deserialized.InputHeight); Assert.AreEqual(layer.InputWidth, deserialized.InputWidth); Assert.AreEqual(layer.OutputDepth, deserialized.OutputDepth); Assert.AreEqual(layer.OutputHeight, deserialized.OutputHeight); Assert.AreEqual(layer.OutputWidth, deserialized.OutputWidth); }
public void TanhLayerSerialization() { var layer = new TanhLayer(); 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 TanhLayer; Assert.IsNotNull(deserialized); Assert.AreEqual(28, deserialized.InputWidth); Assert.AreEqual(24, deserialized.InputHeight); Assert.AreEqual(1, deserialized.InputDepth); Assert.AreEqual(layer.OutputWidth, deserialized.OutputWidth); Assert.AreEqual(layer.OutputHeight, deserialized.OutputHeight); Assert.AreEqual(layer.OutputDepth, deserialized.OutputDepth); }
public void Forward() { const int inputWidth = 2; const int inputHeight = 2; const int inputDepth = 2; const int inputBatchSize = 2; var layer = new TanhLayer <double>(); layer.Init(inputWidth, inputHeight, inputDepth); var input = new Volume.Double.Volume(new[] { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0 }, new Shape(inputWidth, inputHeight, inputDepth, inputBatchSize)); layer.DoForward(input); for (int n = 0; n < 2; n++) { for (int c = 0; c < 2; c++) { for (int y = 0; y < 2; y++) { for (int x = 0; x < 2; x++) { Assert.AreEqual(Math.Tanh(input.Get(x, y, c, n)), layer.OutputActivation.Get(x, y, c, n)); } } } } }