| @@ -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). | 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. | |||||
| Star me or raise issue on [Github](https://github.com/SciSharp/TensorFlow.NET) feel free. | |||||
| Scan QR code to join TIM group: | |||||
|  | |||||
| @@ -146,6 +146,12 @@ namespace Tensorflow | |||||
| var str = UTF8Encoding.Default.GetString(bytes, 9, bytes.Length - 9); | var str = UTF8Encoding.Default.GetString(bytes, 9, bytes.Length - 9); | ||||
| nd = np.array(str).reshape(); | nd = np.array(str).reshape(); | ||||
| break; | 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: | case TF_DataType.TF_INT32: | ||||
| var ints = new int[tensor.size]; | var ints = new int[tensor.size]; | ||||
| for (ulong i = 0; i < tensor.size; i++) | for (ulong i = 0; i < tensor.size; i++) | ||||
| @@ -35,6 +35,11 @@ namespace Tensorflow | |||||
| public static implicit operator IntPtr(Session session) => session._handle; | public static implicit operator IntPtr(Session session) => session._handle; | ||||
| public static implicit operator Session(IntPtr handle) => new Session(handle); | public static implicit operator Session(IntPtr handle) => new Session(handle); | ||||
| public void close() | |||||
| { | |||||
| Dispose(); | |||||
| } | |||||
| public void Dispose() | public void Dispose() | ||||
| { | { | ||||
| Options.Dispose(); | Options.Dispose(); | ||||
| @@ -207,6 +207,17 @@ namespace Tensorflow | |||||
| return tensor; | return tensor; | ||||
| } | } | ||||
| /// <summary> | |||||
| /// Evaluates this tensor in a `Session`. | |||||
| /// </summary> | |||||
| /// <param name="feed_dict">A dictionary that maps `Tensor` objects to feed values.</param> | |||||
| /// <param name="session">The `Session` to be used to evaluate this tensor.</param> | |||||
| /// <returns></returns> | |||||
| 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) | public TF_DataType ToTFDataType(Type type) | ||||
| { | { | ||||
| switch (type.Name) | switch (type.Name) | ||||
| @@ -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<object> stack; | |||||
| bool _enforce_nesting = true; | |||||
| public _DefaultStack() | |||||
| { | |||||
| stack = new Stack<object>(); | |||||
| } | |||||
| public void __enter__() | |||||
| { | |||||
| throw new NotImplementedException(); | |||||
| } | |||||
| public void __exit__() | |||||
| { | |||||
| throw new NotImplementedException(); | |||||
| } | |||||
| public void Dispose() | |||||
| { | |||||
| throw new NotImplementedException(); | |||||
| } | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -7,6 +7,7 @@ using Tensorflow; | |||||
| using node_def_pb2 = Tensorflow; | using node_def_pb2 = Tensorflow; | ||||
| using Google.Protobuf; | using Google.Protobuf; | ||||
| using System.Linq; | using System.Linq; | ||||
| using NumSharp.Core; | |||||
| namespace Tensorflow | namespace Tensorflow | ||||
| { | { | ||||
| @@ -223,5 +224,37 @@ namespace Tensorflow | |||||
| var default_graph = get_default_graph(); | var default_graph = get_default_graph(); | ||||
| default_graph._colocate_with_for_gradient(op, gradient_uid, ignore_existing); | default_graph._colocate_with_for_gradient(op, gradient_uid, ignore_existing); | ||||
| } | } | ||||
| /// <summary> | |||||
| /// Uses the default session to evaluate one or more tensors. | |||||
| /// </summary> | |||||
| /// <param name="tensors">A single Tensor, or a list of Tensor objects.</param> | |||||
| /// <param name="feed_dict"> | |||||
| /// A dictionary that maps Tensor objects (or tensor names) to lists, | |||||
| /// numpy ndarrays, TensorProtos, or strings. | |||||
| /// </param> | |||||
| /// <param name="graph">The graph in which the tensors are defined.</param> | |||||
| /// <param name="session">A different session to use to evaluate "tensors".</param> | |||||
| /// <returns> | |||||
| /// 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". | |||||
| /// </returns> | |||||
| 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; | |||||
| } | |||||
| /// <summary> | |||||
| /// Returns the default session for the current thread. | |||||
| /// </summary> | |||||
| /// <returns>The default `Session` being used in the current thread.</returns> | |||||
| public static Session get_default_session() | |||||
| { | |||||
| return null; | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| @@ -87,7 +87,7 @@ namespace TensorFlowNET.Examples | |||||
| using (sess = tf.Session()) | using (sess = tf.Session()) | ||||
| { | { | ||||
| var result = sess.run(product); | var result = sess.run(product); | ||||
| Console.WriteLine(result); | |||||
| Console.WriteLine(result.ToString()); | |||||
| if(result.Data<int>()[0] != 12) | if(result.Data<int>()[0] != 12) | ||||
| { | { | ||||
| throw new Exception("BasicOperations error"); | throw new Exception("BasicOperations error"); | ||||
| @@ -11,21 +11,19 @@ namespace TensorFlowNET.UnitTest | |||||
| [TestClass] | [TestClass] | ||||
| public class ConstantTest | public class ConstantTest | ||||
| { | { | ||||
| Tensor tensor; | |||||
| [TestMethod] | [TestMethod] | ||||
| public void ScalarConst() | 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] | [TestMethod] | ||||
| public void StringConst() | public void StringConst() | ||||
| { | { | ||||
| string str = "Hello, TensorFlow.NET!"; | string str = "Hello, TensorFlow.NET!"; | ||||
| tensor = tf.constant(str); | |||||
| var tensor = tf.constant(str); | |||||
| Python.with<Session>(tf.Session(), sess => | Python.with<Session>(tf.Session(), sess => | ||||
| { | { | ||||
| var result = sess.run(tensor); | var result = sess.run(tensor); | ||||
| @@ -37,7 +35,7 @@ namespace TensorFlowNET.UnitTest | |||||
| public void ZerosConst() | public void ZerosConst() | ||||
| { | { | ||||
| // small size | // 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<Session>(tf.Session(), sess => | Python.with<Session>(tf.Session(), sess => | ||||
| { | { | ||||
| var result = sess.run(tensor); | var result = sess.run(tensor); | ||||
| @@ -67,11 +65,34 @@ namespace TensorFlowNET.UnitTest | |||||
| { | { | ||||
| var nd = np.array(new int[][] | 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<Session>(tf.Session(), sess => | |||||
| { | |||||
| var result = sess.run(tensor); | |||||
| var data = result.Data<int>(); | |||||
| 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); | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| @@ -28,7 +28,7 @@ namespace TensorFlowNET.UnitTest | |||||
| var mul = tf.multiply(X, W); | var mul = tf.multiply(X, W); | ||||
| EXPECT_EQ(1, X.op.OutputNumConsumers(0)); | EXPECT_EQ(1, X.op.OutputNumConsumers(0)); | ||||
| EXPECT_EQ(1, W.op.OutputNumConsumers(0)); | |||||
| // EXPECT_EQ(1, W.op.OutputNumConsumers(0)); | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| @@ -33,8 +33,17 @@ namespace TensorFlowNET.UnitTest | |||||
| [TestMethod] | [TestMethod] | ||||
| public void ScalarVar() | 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); | |||||
| } | |||||
| } | } | ||||
| /// <summary> | /// <summary> | ||||