@@ -470,5 +470,9 @@ namespace Tensorflow | |||||
throw new NotImplementedException("array_ops.stack"); | throw new NotImplementedException("array_ops.stack"); | ||||
} | } | ||||
public static Tensor placeholder(TF_DataType dtype) | |||||
{ | |||||
throw new NotImplementedException("array_ops.placeholder"); | |||||
} | |||||
} | } | ||||
} | } |
@@ -99,5 +99,10 @@ namespace Tensorflow | |||||
is_training, | is_training, | ||||
name); | name); | ||||
} | } | ||||
public static Tensor zero_fraction(Tensor t) | |||||
{ | |||||
throw new NotImplementedException(); | |||||
} | |||||
} | } | ||||
} | } |
@@ -88,6 +88,11 @@ namespace Tensorflow | |||||
return tensor_util.to_shape(shape); | return tensor_util.to_shape(shape); | ||||
} | } | ||||
public void setShape(Shape shape) | |||||
{ | |||||
this.shape = shape.Dimensions; | |||||
} | |||||
/// <summary> | /// <summary> | ||||
/// number of dimensions | /// number of dimensions | ||||
/// 0 Scalar (magnitude only) | /// 0 Scalar (magnitude only) | ||||
@@ -272,5 +277,6 @@ namespace Tensorflow | |||||
c_api.TF_DeleteTensor(_handle); | c_api.TF_DeleteTensor(_handle); | ||||
status.Dispose(); | status.Dispose(); | ||||
} | } | ||||
} | } | ||||
} | } |
@@ -6,6 +6,9 @@ namespace Tensorflow | |||||
{ | { | ||||
public static class dtypes | public static class dtypes | ||||
{ | { | ||||
public static TF_DataType int8 = TF_DataType.TF_INT8; | |||||
public static TF_DataType float32 = TF_DataType.TF_FLOAT; // is that float32? | |||||
public static Type as_numpy_datatype(this TF_DataType type) | public static Type as_numpy_datatype(this TF_DataType type) | ||||
{ | { | ||||
switch (type) | switch (type) | ||||
@@ -80,6 +80,13 @@ namespace TensorFlowNET.UnitTest | |||||
assertEqual(given, expected); | assertEqual(given, expected); | ||||
} | } | ||||
public void assert(object given) | |||||
{ | |||||
if (given is bool) | |||||
Assert.IsTrue((bool)given); | |||||
Assert.IsNotNull(given); | |||||
} | |||||
public void assertIsNotNone(object given) | public void assertIsNotNone(object given) | ||||
{ | { | ||||
Assert.IsNotNull(given); | Assert.IsNotNull(given); | ||||
@@ -95,6 +102,16 @@ namespace TensorFlowNET.UnitTest | |||||
Assert.IsTrue(cond); | Assert.IsTrue(cond); | ||||
} | } | ||||
public void assertAllClose(NDArray array1, NDArray array2, double eps = 1e-5) | |||||
{ | |||||
Assert.IsTrue(np.allclose(array1, array2, rtol: eps)); | |||||
} | |||||
public void assertAllClose(double value, NDArray array2, double eps = 1e-5) | |||||
{ | |||||
var array1 = np.ones_like(array2) * value; | |||||
Assert.IsTrue(np.allclose(array1, array2, rtol: eps)); | |||||
} | |||||
public void assertProtoEquals(object toProto, object o) | public void assertProtoEquals(object toProto, object o) | ||||
{ | { | ||||
@@ -172,7 +189,7 @@ namespace TensorFlowNET.UnitTest | |||||
} | } | ||||
protected Session cached_session() | |||||
public Session cached_session() | |||||
{ | { | ||||
throw new NotImplementedException(); | throw new NotImplementedException(); | ||||
} | } | ||||
@@ -20,7 +20,7 @@ namespace TensorFlowNET.UnitTest.control_flow_ops_test | |||||
var c = new Func<Tensor, Tensor>(x => gen_math_ops.less(x, 10, name: "c")); | var c = new Func<Tensor, Tensor>(x => gen_math_ops.less(x, 10, name: "c")); | ||||
var b = new Func<Tensor, Tensor>(x => gen_math_ops.add(x, 1, name: "c")); | var b = new Func<Tensor, Tensor>(x => gen_math_ops.add(x, 1, name: "c")); | ||||
control_flow_ops.while_loop( | control_flow_ops.while_loop( | ||||
c, b, new[] { i }, maximum_iterations = maximum_iterations); | |||||
c, b, new[] { i }, maximum_iterations); | |||||
foreach (Operation op in sess.graph.get_operations()) | foreach (Operation op in sess.graph.get_operations()) | ||||
{ | { | ||||
var control_flow_context = op._get_control_flow_context(); | var control_flow_context = op._get_control_flow_context(); | ||||
@@ -0,0 +1,87 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Text; | |||||
using Microsoft.VisualStudio.TestTools.UnitTesting; | |||||
using NumSharp; | |||||
using Tensorflow; | |||||
namespace TensorFlowNET.UnitTest.nn_test | |||||
{ | |||||
[TestClass] | |||||
public class ZeroFractionTest : PythonTest | |||||
{ | |||||
protected double _ZeroFraction(NDArray x) | |||||
{ | |||||
assert(x.shape); | |||||
int total_elements = np.prod(x.shape); | |||||
var eps = 1e-8; | |||||
var nonzeros = x.Data<double>().Count(d=>Math.Abs(d)> eps); | |||||
return 1.0 - nonzeros / (double)total_elements; | |||||
} | |||||
[Ignore("TODO implement nn_impl.zero_fraction")] | |||||
[TestMethod] | |||||
public void testZeroFraction() | |||||
{ | |||||
var x_shape = new Shape(5, 17); | |||||
var x_np = new NumPyRandom().randint(0, 2, x_shape); | |||||
x_np.astype(np.float32); | |||||
var y_np = this._ZeroFraction(x_np); | |||||
var x_tf = constant_op.constant(x_np); | |||||
x_tf.setShape(x_shape); | |||||
var y_tf = nn_impl.zero_fraction(x_tf); | |||||
var y_tf_np = self.evaluate<NDArray>(y_tf); | |||||
var eps = 1e-8; | |||||
self.assertAllClose(y_tf_np, y_np, eps); | |||||
} | |||||
[Ignore("TODO implement nn_impl.zero_fraction")] | |||||
[TestMethod] | |||||
public void testZeroFractionEmpty() | |||||
{ | |||||
var x = np.zeros(0); | |||||
var y = self.evaluate<NDArray>(nn_impl.zero_fraction(new Tensor(x))); | |||||
self.assertTrue(np.isnan(y)); | |||||
} | |||||
[Ignore("TODO implement nn_impl.zero_fraction")] | |||||
[TestMethod] | |||||
public void testZeroFraction2_27Zeros() | |||||
{ | |||||
var sparsity = nn_impl.zero_fraction( | |||||
array_ops.zeros(new Shape((int) Math.Pow(2, 27 * 1.01)), dtypes.int8)); | |||||
self.assertAllClose(1.0, self.evaluate<NDArray>(sparsity)); | |||||
} | |||||
[Ignore("TODO implement nn_impl.zero_fraction")] | |||||
[TestMethod] | |||||
public void testZeroFraction2_27Ones() | |||||
{ | |||||
var sparsity = nn_impl.zero_fraction( | |||||
array_ops.ones(new Shape((int)Math.Pow(2, 27 * 1.01)), dtypes.int8)); | |||||
self.assertAllClose(0.0, self.evaluate<NDArray>(sparsity)); | |||||
} | |||||
[Ignore("TODO implement nn_impl.zero_fraction")] | |||||
[TestMethod] | |||||
public void testUnknownSize() | |||||
{ | |||||
var value = array_ops.placeholder(dtype: dtypes.float32); | |||||
var sparsity = nn_impl.zero_fraction(value); | |||||
with<Session>(self.cached_session(), sess => { | |||||
// TODO: make this compile | |||||
//self.assertAllClose( | |||||
// 0.25, | |||||
// sess.run(sparsity, {value: [[0., 1.], [0.3, 2.]]})); | |||||
}); | |||||
} | |||||
} | |||||
} |