Skip to content

Commit aea1166

Browse files
committed
ported NN ZeroFraction tests
1 parent b66e258 commit aea1166

7 files changed

Lines changed: 124 additions & 2 deletions

File tree

‎src/TensorFlowNET.Core/Operations/array_ops.py.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,5 +470,9 @@ public static Tensor stack(object values, int axis = 0, string name = "stack")
470470
throw new NotImplementedException("array_ops.stack");
471471
}
472472

473+
public static Tensor placeholder(TF_DataType dtype)
474+
{
475+
throw new NotImplementedException("array_ops.placeholder");
476+
}
473477
}
474478
}

‎src/TensorFlowNET.Core/Operations/nn_impl.py.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,10 @@ public static Tensor[] fused_batch_norm(Tensor x,
9999
is_training,
100100
name);
101101
}
102+
103+
public static Tensor zero_fraction(Tensor t)
104+
{
105+
throw new NotImplementedException();
106+
}
102107
}
103108
}

‎src/TensorFlowNET.Core/Tensors/Tensor.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ public TensorShape getShape()
8888
return tensor_util.to_shape(shape);
8989
}
9090

91+
public void setShape(Shape shape)
92+
{
93+
this.shape = shape.Dimensions;
94+
}
95+
9196
/// <summary>
9297
/// number of dimensions
9398
/// 0 Scalar (magnitude only)
@@ -272,5 +277,6 @@ public void Dispose()
272277
c_api.TF_DeleteTensor(_handle);
273278
status.Dispose();
274279
}
280+
275281
}
276282
}

‎src/TensorFlowNET.Core/Tensors/dtypes.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ namespace Tensorflow
66
{
77
public static class dtypes
88
{
9+
public static TF_DataType int8 = TF_DataType.TF_INT8;
10+
public static TF_DataType float32 = TF_DataType.TF_FLOAT; // is that float32?
11+
912
public static Type as_numpy_datatype(this TF_DataType type)
1013
{
1114
switch (type)

‎test/TensorFlowNET.UnitTest/PythonTest.cs‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ public void assertEquals(object given, object expected)
8080
assertEqual(given, expected);
8181
}
8282

83+
public void assert(object given)
84+
{
85+
if (given is bool)
86+
Assert.IsTrue((bool)given);
87+
Assert.IsNotNull(given);
88+
}
89+
8390
public void assertIsNotNone(object given)
8491
{
8592
Assert.IsNotNull(given);
@@ -95,6 +102,16 @@ public void assertTrue(bool cond)
95102
Assert.IsTrue(cond);
96103
}
97104

105+
public void assertAllClose(NDArray array1, NDArray array2, double eps = 1e-5)
106+
{
107+
Assert.IsTrue(np.allclose(array1, array2, rtol: eps));
108+
}
109+
110+
public void assertAllClose(double value, NDArray array2, double eps = 1e-5)
111+
{
112+
var array1 = np.ones_like(array2) * value;
113+
Assert.IsTrue(np.allclose(array1, array2, rtol: eps));
114+
}
98115

99116
public void assertProtoEquals(object toProto, object o)
100117
{
@@ -172,7 +189,7 @@ public T evaluate<T>(Tensor tensor)
172189
}
173190

174191

175-
protected Session cached_session()
192+
public Session cached_session()
176193
{
177194
throw new NotImplementedException();
178195
}

‎test/TensorFlowNET.UnitTest/control_flow_ops_test/WhileContextTestCase.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ private void _testWhileContextHelper(int? maximum_iterations = null)
2020
var c = new Func<Tensor, Tensor>(x => gen_math_ops.less(x, 10, name: "c"));
2121
var b = new Func<Tensor, Tensor>(x => gen_math_ops.add(x, 1, name: "c"));
2222
control_flow_ops.while_loop(
23-
c, b, new[] { i }, maximum_iterations = maximum_iterations);
23+
c, b, new[] { i }, maximum_iterations);
2424
foreach (Operation op in sess.graph.get_operations())
2525
{
2626
var control_flow_context = op._get_control_flow_context();
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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

Comments
 (0)