| @@ -7,17 +7,37 @@ TensorFlow.NET is a member project of SciSharp stack. | |||||
|  |  | ||||
| ### How to use | ### How to use | ||||
| Download the pre-compiled dll [here](tensorflowlib) and place it in the bin folder. | |||||
| ```cs | |||||
| // import tensorflow.net | |||||
| using using Tensorflow; | |||||
| ``` | |||||
| ```cs | ```cs | ||||
| using TensorFlowNET.Core; | |||||
| // Create a Constant op | |||||
| var a = tf.constant(4.0f); | |||||
| var b = tf.constant(5.0f); | |||||
| var c = tf.add(a, b); | |||||
| namespace TensorFlowNET.Examples | |||||
| using (var sess = tf.Session()) | |||||
| { | { | ||||
| public class HelloWorld : IExample | |||||
| { | |||||
| public void Run() | |||||
| { | |||||
| var hello = tf.constant("Hello, TensorFlow!"); | |||||
| } | |||||
| } | |||||
| var o = sess.run(c); | |||||
| } | |||||
| ``` | |||||
| ```cs | |||||
| // Create a placeholder op | |||||
| var a = tf.placeholder(tf.float32); | |||||
| var b = tf.placeholder(tf.float32); | |||||
| var c = tf.add(a, b); | |||||
| using(var sess = tf.Session()) | |||||
| { | |||||
| var feed_dict = new Dictionary<Tensor, object>(); | |||||
| feed_dict.Add(a, 3.0f); | |||||
| feed_dict.Add(b, 2.0f); | |||||
| var o = sess.run(c, feed_dict); | |||||
| } | } | ||||
| ``` | ``` | ||||
| @@ -66,16 +66,16 @@ namespace Tensorflow | |||||
| var status = new Status(); | var status = new Status(); | ||||
| c_api.TF_SessionRun(_session, | c_api.TF_SessionRun(_session, | ||||
| run_options: null, | |||||
| run_options: IntPtr.Zero, | |||||
| inputs: new TF_Output[] { }, | inputs: new TF_Output[] { }, | ||||
| input_values: new IntPtr[] { }, | input_values: new IntPtr[] { }, | ||||
| ninputs: 1, | |||||
| ninputs: 0, | |||||
| outputs: new TF_Output[] { }, | outputs: new TF_Output[] { }, | ||||
| output_values: new IntPtr[] { }, | output_values: new IntPtr[] { }, | ||||
| noutputs: 1, | noutputs: 1, | ||||
| target_opers: new IntPtr[] { }, | target_opers: new IntPtr[] { }, | ||||
| ntargets: 1, | ntargets: 1, | ||||
| run_metadata: null, | |||||
| run_metadata: IntPtr.Zero, | |||||
| status: status.Handle); | status: status.Handle); | ||||
| return null; | return null; | ||||
| @@ -67,11 +67,11 @@ namespace Tensorflow | |||||
| public static extern unsafe void TF_SetAttrTensor(TF_OperationDescription desc, string attr_name, TF_Tensor value, TF_Status status); | public static extern unsafe void TF_SetAttrTensor(TF_OperationDescription desc, string attr_name, TF_Tensor value, TF_Status status); | ||||
| [DllImport(TensorFlowLibName)] | [DllImport(TensorFlowLibName)] | ||||
| public static extern unsafe void TF_SessionRun(TF_Session session, TF_Buffer* run_options, | |||||
| public static extern unsafe void TF_SessionRun(TF_Session session, IntPtr run_options, | |||||
| TF_Output[] inputs, TF_Tensor[] input_values, int ninputs, | TF_Output[] inputs, TF_Tensor[] input_values, int ninputs, | ||||
| TF_Output[] outputs, TF_Tensor[] output_values, int noutputs, | TF_Output[] outputs, TF_Tensor[] output_values, int noutputs, | ||||
| TF_Operation[] target_opers, int ntargets, | TF_Operation[] target_opers, int ntargets, | ||||
| TF_Buffer* run_metadata, | |||||
| IntPtr run_metadata, | |||||
| TF_Status status); | TF_Status status); | ||||
| [DllImport(TensorFlowLibName)] | [DllImport(TensorFlowLibName)] | ||||
| @@ -21,9 +21,12 @@ namespace Tensorflow | |||||
| var op_desc = c_api.TF_NewOperation(graph.Handle, node_def.Op, node_def.Name); | var op_desc = c_api.TF_NewOperation(graph.Handle, node_def.Op, node_def.Name); | ||||
| // Add inputs | // Add inputs | ||||
| foreach(var op_input in inputs) | |||||
| if(inputs != null) | |||||
| { | { | ||||
| c_api.TF_AddInput(op_desc, op_input._as_tf_output()); | |||||
| foreach (var op_input in inputs) | |||||
| { | |||||
| c_api.TF_AddInput(op_desc, op_input._as_tf_output()); | |||||
| } | |||||
| } | } | ||||
| var status = new Status(); | var status = new Status(); | ||||
| @@ -16,6 +16,15 @@ namespace Tensorflow | |||||
| switch (values) | switch (values) | ||||
| { | { | ||||
| case float val: | |||||
| nparray = np.array(new float[] { val }, np.float32); | |||||
| tensor_proto = new tensor_pb2.TensorProto | |||||
| { | |||||
| Dtype = DataType.DtFloat, | |||||
| TensorShape = tensor_shape.as_shape().as_proto() | |||||
| }; | |||||
| tensor_proto.FloatVal.Add(val); | |||||
| break; | |||||
| case double val: | case double val: | ||||
| nparray = np.array(new double[] { val }, np.float64); | nparray = np.array(new double[] { val }, np.float64); | ||||
| tensor_proto = new tensor_pb2.TensorProto | tensor_proto = new tensor_pb2.TensorProto | ||||
| @@ -25,7 +34,6 @@ namespace Tensorflow | |||||
| }; | }; | ||||
| tensor_proto.DoubleVal.Add(val); | tensor_proto.DoubleVal.Add(val); | ||||
| break; | break; | ||||
| case string val: | case string val: | ||||
| nparray = np.array(new string[] { val }, np.chars); | nparray = np.array(new string[] { val }, np.chars); | ||||
| tensor_proto = new tensor_pb2.TensorProto | tensor_proto = new tensor_pb2.TensorProto | ||||
| @@ -12,7 +12,13 @@ namespace TensorFlowNET.UnitTest | |||||
| [TestMethod] | [TestMethod] | ||||
| public void constant() | public void constant() | ||||
| { | { | ||||
| tf.constant(4.0); | |||||
| var a = tf.constant(4.0f); | |||||
| var b = tf.constant(5.0f); | |||||
| var c = tf.add(a, b); | |||||
| using (var sess = tf.Session()) | |||||
| { | |||||
| var o = sess.run(c); | |||||
| } | |||||
| } | } | ||||
| [TestMethod] | [TestMethod] | ||||
| @@ -31,10 +37,10 @@ namespace TensorFlowNET.UnitTest | |||||
| using(var sess = tf.Session()) | using(var sess = tf.Session()) | ||||
| { | { | ||||
| var feed_dict = new Dictionary<Tensor, object>(); | var feed_dict = new Dictionary<Tensor, object>(); | ||||
| feed_dict.Add(a, 3); | |||||
| feed_dict.Add(b, 2); | |||||
| feed_dict.Add(a, 3.0f); | |||||
| feed_dict.Add(b, 2.0f); | |||||
| sess.run(c, feed_dict); | |||||
| var o = sess.run(c, feed_dict); | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||