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