You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ZeroFractionTest.cs 2.7 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using System.Linq;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using NumSharp;
  5. using Tensorflow;
  6. namespace TensorFlowNET.UnitTest.nn_test
  7. {
  8. [TestClass]
  9. public class ZeroFractionTest : PythonTest
  10. {
  11. protected double _ZeroFraction(NDArray x)
  12. {
  13. assert(x.shape);
  14. int total_elements = np.prod(x.shape);
  15. var eps = 1e-8;
  16. var nonzeros = x.Data<double>().Count(d=>Math.Abs(d)> eps);
  17. return 1.0 - nonzeros / (double)total_elements;
  18. }
  19. [Ignore("TODO implement nn_impl.zero_fraction")]
  20. [TestMethod]
  21. public void testZeroFraction()
  22. {
  23. var x_shape = new Shape(5, 17);
  24. var x_np = np.random.randint(0, 2, x_shape);
  25. //x_np.astype(np.float32);
  26. var y_np = this._ZeroFraction(x_np);
  27. var x_tf = constant_op.constant(x_np);
  28. x_tf.set_shape(x_shape);
  29. var y_tf = nn_impl.zero_fraction(x_tf);
  30. var y_tf_np = self.evaluate<NDArray>(y_tf);
  31. var eps = 1e-8;
  32. self.assertAllClose(y_tf_np, y_np, eps);
  33. }
  34. [Ignore("TODO implement nn_impl.zero_fraction")]
  35. [TestMethod]
  36. public void testZeroFractionEmpty()
  37. {
  38. var x = np.zeros(0);
  39. var y = self.evaluate<NDArray>(nn_impl.zero_fraction(new Tensor(x)));
  40. self.assertTrue(np.isnan(y));
  41. }
  42. [Ignore("TODO implement nn_impl.zero_fraction")]
  43. [TestMethod]
  44. public void testZeroFraction2_27Zeros()
  45. {
  46. var sparsity = nn_impl.zero_fraction(
  47. array_ops.zeros(new Shape((int) Math.Pow(2, 27 * 1.01)), dtypes.int8));
  48. self.assertAllClose(1.0, self.evaluate<NDArray>(sparsity));
  49. }
  50. [Ignore("TODO implement nn_impl.zero_fraction")]
  51. [TestMethod]
  52. public void testZeroFraction2_27Ones()
  53. {
  54. var sparsity = nn_impl.zero_fraction(
  55. array_ops.ones(new TensorShape((int)Math.Pow(2, 27 * 1.01)), dtypes.int8));
  56. self.assertAllClose(0.0, self.evaluate<NDArray>(sparsity));
  57. }
  58. [Ignore("TODO implement nn_impl.zero_fraction")]
  59. [TestMethod]
  60. public void testUnknownSize()
  61. {
  62. var value = array_ops.placeholder(dtype: dtypes.float32);
  63. var sparsity = nn_impl.zero_fraction(value);
  64. using (var sess = self.cached_session())
  65. {
  66. // TODO: make this compile
  67. //self.assertAllClose(
  68. // 0.25,
  69. // sess.run(sparsity, {value: [[0., 1.], [0.3, 2.]]}));
  70. }
  71. }
  72. }
  73. }