| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5f595eb commit cb96250
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,17 +7,37 @@ TensorFlow.NET is a member project of SciSharp stack. | |||
| 7 | 7 |  | |
| 8 | 8 | ||
| 9 | 9 | ### How to use | |
| 10 | + Download the pre-compiled dll [here](tensorflowlib) and place it in the bin folder. | ||
| 11 | + | ||
| 12 | + ```cs | ||
| 13 | + // import tensorflow.net | ||
| 14 | + using using Tensorflow; | ||
| 15 | + ``` | ||
| 16 | + | ||
| 10 | 17 | ```cs | |
| 11 | - using TensorFlowNET.Core; | ||
| 18 | + // Create a Constant op | ||
| 19 | + var a = tf.constant(4.0f); | ||
| 20 | + var b = tf.constant(5.0f); | ||
| 21 | + var c = tf.add(a, b); | ||
| 12 | 22 | ||
| 13 | - namespace TensorFlowNET.Examples | ||
| 23 | + using (var sess = tf.Session()) | ||
| 14 | 24 | { | |
| 15 | - public class HelloWorld : IExample | ||
| 16 | - { | ||
| 17 | - public void Run() | ||
| 18 | - { | ||
| 19 | - var hello = tf.constant("Hello, TensorFlow!"); | ||
| 20 | - } | ||
| 21 | - } | ||
| 25 | + var o = sess.run(c); | ||
| 26 | + } | ||
| 27 | + ``` | ||
| 28 | + | ||
| 29 | + ```cs | ||
| 30 | + // Create a placeholder op | ||
| 31 | + var a = tf.placeholder(tf.float32); | ||
| 32 | + var b = tf.placeholder(tf.float32); | ||
| 33 | + var c = tf.add(a, b); | ||
| 34 | + | ||
| 35 | + using(var sess = tf.Session()) | ||
| 36 | + { | ||
| 37 | + var feed_dict = new Dictionary<Tensor, object>(); | ||
| 38 | + feed_dict.Add(a, 3.0f); | ||
| 39 | + feed_dict.Add(b, 2.0f); | ||
| 40 | + | ||
| 41 | + var o = sess.run(c, feed_dict); | ||
| 22 | 42 | } | |
| 23 | 43 | ``` | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -66,16 +66,16 @@ private unsafe byte[] _run(Tensor fetches, Dictionary<Tensor, object> feed_dict | |||
| 66 | 66 | var status = new Status(); | |
| 67 | 67 | ||
| 68 | 68 | c_api.TF_SessionRun(_session, | |
| 69 | - run_options: null, | ||
| 69 | + run_options: IntPtr.Zero, | ||
| 70 | 70 | inputs: new TF_Output[] { }, | |
| 71 | 71 | input_values: new IntPtr[] { }, | |
| 72 | - ninputs: 1, | ||
| 72 | + ninputs: 0, | ||
| 73 | 73 | outputs: new TF_Output[] { }, | |
| 74 | 74 | output_values: new IntPtr[] { }, | |
| 75 | 75 | noutputs: 1, | |
| 76 | 76 | target_opers: new IntPtr[] { }, | |
| 77 | 77 | ntargets: 1, | |
| 78 | - run_metadata: null, | ||
| 78 | + run_metadata: IntPtr.Zero, | ||
| 79 | 79 | status: status.Handle); | |
| 80 | 80 | ||
| 81 | 81 | return null; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -67,11 +67,11 @@ public static class c_api | |||
| 67 | 67 | public static extern unsafe void TF_SetAttrTensor(TF_OperationDescription desc, string attr_name, TF_Tensor value, TF_Status status); | |
| 68 | 68 | ||
| 69 | 69 | [DllImport(TensorFlowLibName)] | |
| 70 | - public static extern unsafe void TF_SessionRun(TF_Session session, TF_Buffer* run_options, | ||
| 70 | + public static extern unsafe void TF_SessionRun(TF_Session session, IntPtr run_options, | ||
| 71 | 71 | TF_Output[] inputs, TF_Tensor[] input_values, int ninputs, | |
| 72 | 72 | TF_Output[] outputs, TF_Tensor[] output_values, int noutputs, | |
| 73 | 73 | TF_Operation[] target_opers, int ntargets, | |
| 74 | - TF_Buffer* run_metadata, | ||
| 74 | + IntPtr run_metadata, | ||
| 75 | 75 | TF_Status status); | |
| 76 | 76 | ||
| 77 | 77 | [DllImport(TensorFlowLibName)] | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,9 +21,12 @@ public static unsafe IntPtr _create_c_op(Graph graph, NodeDef node_def, List<Ten | |||
| 21 | 21 | var op_desc = c_api.TF_NewOperation(graph.Handle, node_def.Op, node_def.Name); | |
| 22 | 22 | ||
| 23 | 23 | // Add inputs | |
| 24 | - foreach(var op_input in inputs) | ||
| 24 | + if(inputs != null) | ||
| 25 | 25 | { | |
| 26 | - c_api.TF_AddInput(op_desc, op_input._as_tf_output()); | ||
| 26 | + foreach (var op_input in inputs) | ||
| 27 | + { | ||
| 28 | + c_api.TF_AddInput(op_desc, op_input._as_tf_output()); | ||
| 29 | + } | ||
| 27 | 30 | } | |
| 28 | 31 | ||
| 29 | 32 | var status = new Status(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,6 +16,15 @@ public static TensorProto make_tensor_proto(object values, Type dtype = null) | |||
| 16 | 16 | ||
| 17 | 17 | switch (values) | |
| 18 | 18 | { | |
| 19 | + case float val: | ||
| 20 | + nparray = np.array(new float[] { val }, np.float32); | ||
| 21 | + tensor_proto = new tensor_pb2.TensorProto | ||
| 22 | + { | ||
| 23 | + Dtype = DataType.DtFloat, | ||
| 24 | + TensorShape = tensor_shape.as_shape().as_proto() | ||
| 25 | + }; | ||
| 26 | + tensor_proto.FloatVal.Add(val); | ||
| 27 | + break; | ||
| 19 | 28 | case double val: | |
| 20 | 29 | nparray = np.array(new double[] { val }, np.float64); | |
| 21 | 30 | tensor_proto = new tensor_pb2.TensorProto | |
@@ -25,7 +34,6 @@ public static TensorProto make_tensor_proto(object values, Type dtype = null) | |||
| 25 | 34 | }; | |
| 26 | 35 | tensor_proto.DoubleVal.Add(val); | |
| 27 | 36 | break; | |
| 28 | - | ||
| 29 | 37 | case string val: | |
| 30 | 38 | nparray = np.array(new string[] { val }, np.chars); | |
| 31 | 39 | tensor_proto = new tensor_pb2.TensorProto | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,7 +12,13 @@ public class OperationsTest | |||
| 12 | 12 | [TestMethod] | |
| 13 | 13 | public void constant() | |
| 14 | 14 | { | |
| 15 | - tf.constant(4.0); | ||
| 15 | + var a = tf.constant(4.0f); | ||
| 16 | + var b = tf.constant(5.0f); | ||
| 17 | + var c = tf.add(a, b); | ||
| 18 | + using (var sess = tf.Session()) | ||
| 19 | + { | ||
| 20 | + var o = sess.run(c); | ||
| 21 | + } | ||
| 16 | 22 | } | |
| 17 | 23 | ||
| 18 | 24 | [TestMethod] | |
@@ -31,10 +37,10 @@ public void add() | |||
| 31 | 37 | using(var sess = tf.Session()) | |
| 32 | 38 | { | |
| 33 | 39 | var feed_dict = new Dictionary<Tensor, object>(); | |
| 34 | - feed_dict.Add(a, 3); | ||
| 35 | - feed_dict.Add(b, 2); | ||
| 40 | + feed_dict.Add(a, 3.0f); | ||
| 41 | + feed_dict.Add(b, 2.0f); | ||
| 36 | 42 | ||
| 37 | - sess.run(c, feed_dict); | ||
| 43 | + var o = sess.run(c, feed_dict); | ||
| 38 | 44 | } | |
| 39 | 45 | } | |
| 40 | 46 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments