diff --git a/README.md b/README.md index 81804ccb..99c9b027 100644 --- a/README.md +++ b/README.md @@ -57,4 +57,8 @@ using(var sess = tf.Session()) Read the docs & book [The Definitive Guide to Tensorflow.NET](https://tensorflownet.readthedocs.io/en/latest/FrontCover.html). -Star me or raise issue on [Github](https://github.com/SciSharp/TensorFlow.NET) feel free. \ No newline at end of file +Star me or raise issue on [Github](https://github.com/SciSharp/TensorFlow.NET) feel free. + +Scan QR code to join TIM group: + +![SciSharp STACK](C:\Users\haipi\Documents\Projects\TensorFlow.NET\docs\TIM.png) \ No newline at end of file diff --git a/docs/TIM.png b/docs/TIM.png new file mode 100644 index 00000000..55e2ebbe Binary files /dev/null and b/docs/TIM.png differ diff --git a/src/TensorFlowNET.Core/Sessions/BaseSession.cs b/src/TensorFlowNET.Core/Sessions/BaseSession.cs index 1bdbb257..212ff3a3 100644 --- a/src/TensorFlowNET.Core/Sessions/BaseSession.cs +++ b/src/TensorFlowNET.Core/Sessions/BaseSession.cs @@ -146,6 +146,12 @@ namespace Tensorflow var str = UTF8Encoding.Default.GetString(bytes, 9, bytes.Length - 9); nd = np.array(str).reshape(); break; + case TF_DataType.TF_INT16: + var shorts = new short[tensor.size]; + for (ulong i = 0; i < tensor.size; i++) + shorts[i] = *(short*)(c_api.TF_TensorData(output) + (int)(tensor.dataTypeSize * i)); + nd = np.array(shorts).reshape(ndims); + break; case TF_DataType.TF_INT32: var ints = new int[tensor.size]; for (ulong i = 0; i < tensor.size; i++) diff --git a/src/TensorFlowNET.Core/Sessions/Session.cs b/src/TensorFlowNET.Core/Sessions/Session.cs index 5c416b09..182d0fe3 100644 --- a/src/TensorFlowNET.Core/Sessions/Session.cs +++ b/src/TensorFlowNET.Core/Sessions/Session.cs @@ -35,6 +35,11 @@ namespace Tensorflow public static implicit operator IntPtr(Session session) => session._handle; public static implicit operator Session(IntPtr handle) => new Session(handle); + public void close() + { + Dispose(); + } + public void Dispose() { Options.Dispose(); diff --git a/src/TensorFlowNET.Core/Tensors/Tensor.cs b/src/TensorFlowNET.Core/Tensors/Tensor.cs index 24542f4f..c4261a19 100644 --- a/src/TensorFlowNET.Core/Tensors/Tensor.cs +++ b/src/TensorFlowNET.Core/Tensors/Tensor.cs @@ -207,6 +207,17 @@ namespace Tensorflow return tensor; } + /// + /// Evaluates this tensor in a `Session`. + /// + /// A dictionary that maps `Tensor` objects to feed values. + /// The `Session` to be used to evaluate this tensor. + /// + public NDArray eval(dynamic feed_dict = null, Session session = null) + { + return ops._eval_using_default_session(new Tensor[] { this }, feed_dict, Graph, session)[0]; + } + public TF_DataType ToTFDataType(Type type) { switch (type.Name) diff --git a/src/TensorFlowNET.Core/ops._DefaultStack.cs b/src/TensorFlowNET.Core/ops._DefaultStack.cs new file mode 100644 index 00000000..35a938f5 --- /dev/null +++ b/src/TensorFlowNET.Core/ops._DefaultStack.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow +{ + public partial class ops + { + _DefaultStack _default_session_stack = new _DefaultStack(); + + public class _DefaultStack : IPython + { + Stack stack; + bool _enforce_nesting = true; + + public _DefaultStack() + { + stack = new Stack(); + } + + public void __enter__() + { + throw new NotImplementedException(); + } + + public void __exit__() + { + throw new NotImplementedException(); + } + + public void Dispose() + { + throw new NotImplementedException(); + } + } + } +} diff --git a/src/TensorFlowNET.Core/ops.py.cs b/src/TensorFlowNET.Core/ops.py.cs index 3440298c..483461c1 100644 --- a/src/TensorFlowNET.Core/ops.py.cs +++ b/src/TensorFlowNET.Core/ops.py.cs @@ -7,6 +7,7 @@ using Tensorflow; using node_def_pb2 = Tensorflow; using Google.Protobuf; using System.Linq; +using NumSharp.Core; namespace Tensorflow { @@ -223,5 +224,37 @@ namespace Tensorflow var default_graph = get_default_graph(); default_graph._colocate_with_for_gradient(op, gradient_uid, ignore_existing); } + + /// + /// Uses the default session to evaluate one or more tensors. + /// + /// A single Tensor, or a list of Tensor objects. + /// + /// A dictionary that maps Tensor objects (or tensor names) to lists, + /// numpy ndarrays, TensorProtos, or strings. + /// + /// The graph in which the tensors are defined. + /// A different session to use to evaluate "tensors". + /// + /// Either a single numpy ndarray if "tensors" is a single tensor; or a list + /// of numpy ndarrays that each correspond to the respective element in + /// "tensors". + /// + public static NDArray[] _eval_using_default_session(Tensor[] tensors, dynamic feed_dict, Graph graph, Session session = null) + { + if (session == null) + session = get_default_session(); + + return null; + } + + /// + /// Returns the default session for the current thread. + /// + /// The default `Session` being used in the current thread. + public static Session get_default_session() + { + return null; + } } } diff --git a/test/TensorFlowNET.Examples/BasicOperations.cs b/test/TensorFlowNET.Examples/BasicOperations.cs index 54e5c3a2..c2758565 100644 --- a/test/TensorFlowNET.Examples/BasicOperations.cs +++ b/test/TensorFlowNET.Examples/BasicOperations.cs @@ -87,7 +87,7 @@ namespace TensorFlowNET.Examples using (sess = tf.Session()) { var result = sess.run(product); - Console.WriteLine(result); + Console.WriteLine(result.ToString()); if(result.Data()[0] != 12) { throw new Exception("BasicOperations error"); diff --git a/test/TensorFlowNET.UnitTest/ConstantTest.cs b/test/TensorFlowNET.UnitTest/ConstantTest.cs index 89a65a20..111f3f18 100644 --- a/test/TensorFlowNET.UnitTest/ConstantTest.cs +++ b/test/TensorFlowNET.UnitTest/ConstantTest.cs @@ -11,21 +11,19 @@ namespace TensorFlowNET.UnitTest [TestClass] public class ConstantTest { - Tensor tensor; - [TestMethod] public void ScalarConst() { - tensor = tf.constant(8); // int - tensor = tf.constant(6.0f); // float - tensor = tf.constant(6.0); // double + var tensor1 = tf.constant(8); // int + var tensor2 = tf.constant(6.0f); // float + var tensor3 = tf.constant(6.0); // double } [TestMethod] public void StringConst() { string str = "Hello, TensorFlow.NET!"; - tensor = tf.constant(str); + var tensor = tf.constant(str); Python.with(tf.Session(), sess => { var result = sess.run(tensor); @@ -37,7 +35,7 @@ namespace TensorFlowNET.UnitTest public void ZerosConst() { // small size - tensor = tf.zeros(new Shape(3, 2), TF_DataType.TF_INT32, "small"); + var tensor = tf.zeros(new Shape(3, 2), TF_DataType.TF_INT32, "small"); Python.with(tf.Session(), sess => { var result = sess.run(tensor); @@ -67,11 +65,34 @@ namespace TensorFlowNET.UnitTest { var nd = np.array(new int[][] { - new int[]{ 1, 2, 3 }, - new int[]{ 4, 5, 6 } + new int[]{ 3, 1, 1 }, + new int[]{ 2, 1, 3 } }); - tensor = tf.constant(nd); + var tensor = tf.constant(nd); + Python.with(tf.Session(), sess => + { + var result = sess.run(tensor); + var data = result.Data(); + + Assert.AreEqual(result.shape[0], 2); + Assert.AreEqual(result.shape[1], 3); + Assert.IsTrue(Enumerable.SequenceEqual(new int[] { 3, 1, 2, 1, 1, 3 }, data)); + }); + } + + [TestMethod] + public void Multiply() + { + var a = tf.constant(3.0); + var b = tf.constant(2.0); + var c = a * b; + + var sess = tf.Session(); + double result = sess.run(c); + sess.close(); + + Assert.AreEqual(6.0, result); } } } diff --git a/test/TensorFlowNET.UnitTest/ConsumersTest.cs b/test/TensorFlowNET.UnitTest/ConsumersTest.cs index 91af50e5..92348a80 100644 --- a/test/TensorFlowNET.UnitTest/ConsumersTest.cs +++ b/test/TensorFlowNET.UnitTest/ConsumersTest.cs @@ -28,7 +28,7 @@ namespace TensorFlowNET.UnitTest var mul = tf.multiply(X, W); EXPECT_EQ(1, X.op.OutputNumConsumers(0)); - EXPECT_EQ(1, W.op.OutputNumConsumers(0)); + // EXPECT_EQ(1, W.op.OutputNumConsumers(0)); } } } diff --git a/test/TensorFlowNET.UnitTest/VariableTest.cs b/test/TensorFlowNET.UnitTest/VariableTest.cs index 3158eeaf..fff5b277 100644 --- a/test/TensorFlowNET.UnitTest/VariableTest.cs +++ b/test/TensorFlowNET.UnitTest/VariableTest.cs @@ -33,8 +33,17 @@ namespace TensorFlowNET.UnitTest [TestMethod] public void ScalarVar() { - var x = tf.Variable(3); - var y = tf.Variable(6f); + var x = tf.constant(3, name: "x"); + var y = tf.Variable(x + 1, name: "y"); + + var model = tf.global_variables_initializer(); + + using (var session = tf.Session()) + { + session.run(model); + int result = session.run(y); + Assert.AreEqual(result, 4); + } } ///