public void SoftMaxLayer_Backward() { var batchSize = 1; var width = 28; var height = 28; var depth = 3; var numberOfClasses = 10; var random = new Random(232); var sut = new SoftMaxLayer(numberOfClasses); sut.Initialize(width, height, depth, batchSize, Initialization.GlorotUniform, random); var input = Matrix <float> .Build.Random(batchSize, numberOfClasses, random.Next()); sut.Forward(input); var delta = Matrix <float> .Build.Random(batchSize, numberOfClasses, random.Next()); var actual = sut.Backward(delta); Trace.WriteLine(string.Join(", ", actual.ToColumnMajorArray())); var expected = Matrix <float> .Build.Dense(batchSize, numberOfClasses, new float[] { -0.3891016f, -0.6150756f, 0.0618184f, -0.2334358f, 1.544145f, -1.01483f, 0.6160479f, 0.3225261f, -1.007966f, -0.1111263f }); MatrixAsserts.AreEqual(expected, actual); }
public void ForwardBackwardTest() { Shape shape = new Shape(new[] { 1, SoftMaxLayerTest.weights.Length }); SoftMaxLayer layer = new SoftMaxLayer(shape); Session session = new Session(); Tensor x = new Tensor(null, shape); x.Set(SoftMaxLayerTest.weights); Tensor y = layer.Forward(session, new[] { x })[0]; Helpers.AreArraysEqual(SoftMaxLayerTest.activations, y.Weights); // unroll the graph y.Gradient[0] = 1.0f; session.Unroll(); ////float[] expectedDx = SoftMaxLayerTest.activations.Select((w, i) => i == 0 ? w - 1.0f : w).ToArray(); Helpers.AreArraysEqual(new float[] { 1.0f, 0, 0, 0 }, x.Gradient); }
public void SoftMaxLayer_Forward() { var batchSize = 1; var width = 28; var height = 28; var depth = 3; var numberOfClasses = 10; var random = new Random(232); var sut = new SoftMaxLayer(numberOfClasses); sut.Initialize(width, height, depth, batchSize, Initialization.GlorotUniform, random); var input = Matrix <float> .Build.Random(batchSize, numberOfClasses, random.Next()); var actual = sut.Forward(input); Trace.WriteLine(string.Join(", ", actual.ToColumnMajorArray())); var expected = Matrix <float> .Build.Dense(batchSize, numberOfClasses, new float[] { 0.06976377f, 0.1327717f, 0.02337802f, 0.3784489f, 0.0777365f, 0.05847027f, 0.1072708f, 0.0503228f, 0.0624512f, 0.03938601f }); MatrixAsserts.AreEqual(expected, actual); }