Browse Source

Fix Float64 equal compare in GradientTest

tags/v0.20
Will Haiping Chen 5 years ago
parent
commit
788cd5c20c
2 changed files with 25 additions and 10 deletions
  1. +21
    -0
      test/TensorFlowNET.UnitTest/PythonTest.cs
  2. +4
    -10
      test/TensorFlowNET.UnitTest/gradients_test/GradientsTest.cs

+ 21
- 0
test/TensorFlowNET.UnitTest/PythonTest.cs View File

@@ -52,6 +52,17 @@ namespace TensorFlowNET.UnitTest
assertItemsEqual(given, expected); assertItemsEqual(given, expected);
} }
public void assertFloat32Equal(float expected, float actual, string msg)
{
float eps = 1e-6f;
Assert.IsTrue(Math.Abs(expected - actual) < eps * Math.Max(1.0f, Math.Abs(expected)), $"{msg}: expected {expected} vs actual {actual}");
}
public void assertFloat64Equal(double expected, double actual, string msg)
{
double eps = 1e-16f;
Assert.IsTrue(Math.Abs(expected - actual) < eps * Math.Max(1.0f, Math.Abs(expected)), $"{msg}: expected {expected} vs actual {actual}");
}
public void assertEqual(object given, object expected) public void assertEqual(object given, object expected)
{ {
@@ -70,6 +81,16 @@ namespace TensorFlowNET.UnitTest
assertItemsEqual(given as ICollection, expected as ICollection); assertItemsEqual(given as ICollection, expected as ICollection);
return; return;
} }
if (given is float && expected is float)
{
assertFloat32Equal((float)expected, (float)given, "");
return;
}
if (given is double && expected is double)
{
assertFloat64Equal((double)expected, (double)given, "");
return;
}
Assert.AreEqual(expected, given); Assert.AreEqual(expected, given);
} }


+ 4
- 10
test/TensorFlowNET.UnitTest/gradients_test/GradientsTest.cs View File

@@ -110,12 +110,6 @@ namespace TensorFlowNET.UnitTest.gradients_test
} }
} }


void assertFloat32Equal(float expected, float actual, string msg)
{
float eps = 1e-6f;
Assert.IsTrue(Math.Abs(expected - actual) < eps * Math.Max(1.0f, Math.Abs(expected)), $"{msg}: expected {expected} vs actual {actual}");
}

void test(string name, Func<Tensor, Tensor> tfF, Func<double, (double, double)> targetF, double[] values) void test(string name, Func<Tensor, Tensor> tfF, Func<double, (double, double)> targetF, double[] values)
{ {
foreach (var x in values) foreach (var x in values)
@@ -124,14 +118,14 @@ namespace TensorFlowNET.UnitTest.gradients_test


{ {
var (actualY, actualDY) = evaluateDerivatives(tfF, x); var (actualY, actualDY) = evaluateDerivatives(tfF, x);
Assert.AreEqual(expectedY, actualY, $"value {name}/float64 at {x}");
Assert.AreEqual(expectedDY, actualDY, $"derivative {name}/float64 at {x}");
self.assertFloat64Equal(expectedY, actualY, $"value {name}/float64 at {x}");
self.assertFloat64Equal(expectedDY, actualDY, $"derivative {name}/float64 at {x}");
} }


{ {
var (actualY, actualDY) = evaluateDerivatives(tfF, (float)x); var (actualY, actualDY) = evaluateDerivatives(tfF, (float)x);
assertFloat32Equal((float)expectedY, actualY, $"value {name}/float32 at {x}");
assertFloat32Equal((float)expectedDY, actualDY, $"derivative {name}/float32 at {x}");
self.assertFloat32Equal((float)expectedY, actualY, $"value {name}/float32 at {x}");
self.assertFloat32Equal((float)expectedDY, actualDY, $"derivative {name}/float32 at {x}");
} }
} }
} }


Loading…
Cancel
Save