# Conflicts: # README.md # src/TensorFlowNET.Core/Sessions/_FetchMapper.cstags/v0.9
| @@ -1,6 +1,7 @@ | |||||
| # TensorFlow.NET | # TensorFlow.NET | ||||
| TensorFlow.NET provides a .NET Standard binding for [TensorFlow](https://www.tensorflow.org/). It aims to implement the complete Tensorflow API in CSharp which allows .NET developers to develop, train and deploy Machine Learning models with the cross-platform .NET Standard framework. | TensorFlow.NET provides a .NET Standard binding for [TensorFlow](https://www.tensorflow.org/). It aims to implement the complete Tensorflow API in CSharp which allows .NET developers to develop, train and deploy Machine Learning models with the cross-platform .NET Standard framework. | ||||
| Here is a simple test | |||||
| TensorFlow.NET (TF.NET) provides a .NET Standard binding for [TensorFlow](https://www.tensorflow.org/). It aims to implement the complete Tensorflow API in CSharp which allows .NET developers to develop, train and deploy Machine Learning models with the cross-platform .NET Standard framework. | |||||
| [](https://gitter.im/sci-sharp/community) | [](https://gitter.im/sci-sharp/community) | ||||
| [](https://ci.appveyor.com/project/Haiping-Chen/tensorflow-net) | [](https://ci.appveyor.com/project/Haiping-Chen/tensorflow-net) | ||||
| [](https://codecov.io/gh/SciSharp/NumSharp) | [](https://codecov.io/gh/SciSharp/NumSharp) | ||||
| @@ -8,7 +9,7 @@ Here is a simple test | |||||
| [](https://tensorflownet.readthedocs.io/en/latest/?badge=latest) | [](https://tensorflownet.readthedocs.io/en/latest/?badge=latest) | ||||
| [](https://996.icu/#/en_US) | [](https://996.icu/#/en_US) | ||||
| TensorFlow.NET is a member project of [SciSharp STACK](https://github.com/SciSharp). | |||||
| TF.NET is a member project of [SciSharp STACK](https://github.com/SciSharp). | |||||
|  |  | ||||
| @@ -24,14 +25,14 @@ In comparison to other projects, like for instance TensorFlowSharp which only pr | |||||
| ### How to use | ### How to use | ||||
| Install TensorFlow.NET through NuGet. | |||||
| Install TF.NET through NuGet. | |||||
| ```sh | ```sh | ||||
| PM> Install-Package TensorFlow.NET | PM> Install-Package TensorFlow.NET | ||||
| ``` | ``` | ||||
| If you are using Linux or Mac OS, please download the pre-compiled dll [here](tensorflowlib) and place it in the working folder. This is only need for Linux and Mac OS, and already packed into NuGet for Windows. | If you are using Linux or Mac OS, please download the pre-compiled dll [here](tensorflowlib) and place it in the working folder. This is only need for Linux and Mac OS, and already packed into NuGet for Windows. | ||||
| Import tensorflow.net. | |||||
| Import TF.NET. | |||||
| ```cs | ```cs | ||||
| using Tensorflow; | using Tensorflow; | ||||
| @@ -17,10 +17,10 @@ namespace Tensorflow | |||||
| if (fetch.GetType().IsArray) | if (fetch.GetType().IsArray) | ||||
| return new _ListFetchMapper(fetches); | return new _ListFetchMapper(fetches); | ||||
| else | else | ||||
| return new _ElementFetchMapper(fetches, (List<object> fetched_vals) => fetched_vals[0]); | |||||
| return new _ElementFetchMapper(fetches, (List<NDArray> fetched_vals) => fetched_vals[0]); | |||||
| } | } | ||||
| public virtual NDArray build_results(List<object> values) | |||||
| public virtual NDArray build_results(List<NDArray> values) | |||||
| { | { | ||||
| var type = values[0].GetType(); | var type = values[0].GetType(); | ||||
| var nd = new NDArray(type, values.Count); | var nd = new NDArray(type, values.Count); | ||||
| @@ -31,16 +31,12 @@ namespace Tensorflow | |||||
| nd.SetData(values.Select(x => (float)x).ToArray()); | nd.SetData(values.Select(x => (float)x).ToArray()); | ||||
| break; | break; | ||||
| case "NDArray": | case "NDArray": | ||||
| // nd.SetData<NDArray>(values.ToArray()); | |||||
| //NDArray[] arr = new NDArray[values.Count]; | |||||
| //for (int i=0; i<values.Count; i++) | |||||
| NDArray[] arr = values.Select(x => (NDArray)x).ToArray(); | NDArray[] arr = values.Select(x => (NDArray)x).ToArray(); | ||||
| nd = new NDArray(arr); | nd = new NDArray(arr); | ||||
| break; | break; | ||||
| default: | default: | ||||
| break; | break; | ||||
| } | } | ||||
| return nd; | return nd; | ||||
| } | } | ||||
| @@ -33,19 +33,54 @@ namespace TensorFlowNET.Examples | |||||
| public bool Run() | public bool Run() | ||||
| { | { | ||||
| PrepareData(); | |||||
| var graph = tf.Graph().as_default(); | |||||
| tf.train.import_meta_graph("kmeans.meta"); | tf.train.import_meta_graph("kmeans.meta"); | ||||
| // Input images | // Input images | ||||
| var X = tf.placeholder(tf.float32, shape: new TensorShape(-1, num_features)); | |||||
| var X = graph.get_operation_by_name("Placeholder").output; // tf.placeholder(tf.float32, shape: new TensorShape(-1, num_features)); | |||||
| // Labels (for assigning a label to a centroid and testing) | // Labels (for assigning a label to a centroid and testing) | ||||
| var Y = tf.placeholder(tf.float32, shape: new TensorShape(-1, num_classes)); | |||||
| var Y = graph.get_operation_by_name("Placeholder_1").output; // tf.placeholder(tf.float32, shape: new TensorShape(-1, num_classes)); | |||||
| // K-Means Parameters | // K-Means Parameters | ||||
| var kmeans = new KMeans(X, k, distance_metric: KMeans.COSINE_DISTANCE, use_mini_batch: true); | |||||
| //var kmeans = new KMeans(X, k, distance_metric: KMeans.COSINE_DISTANCE, use_mini_batch: true); | |||||
| // Build KMeans graph | // Build KMeans graph | ||||
| var training_graph = kmeans.training_graph(); | |||||
| //var training_graph = kmeans.training_graph(); | |||||
| var init_vars = tf.global_variables_initializer(); | |||||
| Tensor init_op = graph.get_operation_by_name("cond/Merge"); | |||||
| var train_op = graph.get_operation_by_name("group_deps"); | |||||
| Tensor avg_distance = graph.get_operation_by_name("Mean"); | |||||
| Tensor cluster_idx = graph.get_operation_by_name("Squeeze_1"); | |||||
| with(tf.Session(graph), sess => | |||||
| { | |||||
| sess.run(init_vars, new FeedItem(X, full_data_x)); | |||||
| sess.run(init_op, new FeedItem(X, full_data_x)); | |||||
| // Training | |||||
| NDArray result = null; | |||||
| foreach(var i in range(1, num_steps + 1)) | |||||
| { | |||||
| result = sess.run(new ITensorOrOperation[] { train_op, avg_distance, cluster_idx }, new FeedItem(X, full_data_x)); | |||||
| if (i % 2 == 0 || i == 1) | |||||
| print($"Step {i}, Avg Distance: {result[1]}"); | |||||
| } | |||||
| var idx = result[2]; | |||||
| // Assign a label to each centroid | |||||
| // Count total number of labels per centroid, using the label of each training | |||||
| // sample to their closest centroid (given by 'idx') | |||||
| var counts = np.zeros(k, num_classes); | |||||
| foreach (var i in range(idx.len)) | |||||
| counts[idx[i]] += mnist.train.labels[i]; | |||||
| }); | |||||
| return false; | return false; | ||||
| } | } | ||||
| @@ -50,13 +50,9 @@ namespace TensorFlowNET.Examples | |||||
| with(tf.Session(graph), sess => | with(tf.Session(graph), sess => | ||||
| { | { | ||||
| var results = sess.run(outTensorArr, new FeedItem(imgTensor, imgArr)); | var results = sess.run(outTensorArr, new FeedItem(imgTensor, imgArr)); | ||||
| //NDArray scores = results.Array.GetValue(2) as NDArray; | |||||
| //floatscores.Data<float>(); | |||||
| NDArray[] resultArr = results.Data<NDArray>(); | NDArray[] resultArr = results.Data<NDArray>(); | ||||
| //float[] scores = resultArr[2].Data<float>(); | |||||
| buildOutputImage(resultArr); | buildOutputImage(resultArr); | ||||
| }); | }); | ||||
| @@ -1,6 +1,7 @@ | |||||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | using Microsoft.VisualStudio.TestTools.UnitTesting; | ||||
| using System; | using System; | ||||
| using System.Collections.Generic; | using System.Collections.Generic; | ||||
| using System.Linq; | |||||
| using System.Text; | using System.Text; | ||||
| using Tensorflow; | using Tensorflow; | ||||
| using Buffer = Tensorflow.Buffer; | using Buffer = Tensorflow.Buffer; | ||||
| @@ -20,6 +21,13 @@ namespace TensorFlowNET.UnitTest | |||||
| var handle = c_api.TF_GetAllOpList(); | var handle = c_api.TF_GetAllOpList(); | ||||
| var buffer = new Buffer(handle); | var buffer = new Buffer(handle); | ||||
| var op_list = OpList.Parser.ParseFrom(buffer); | var op_list = OpList.Parser.ParseFrom(buffer); | ||||
| var _registered_ops = new Dictionary<string, OpDef>(); | |||||
| foreach (var op_def in op_list.Op) | |||||
| _registered_ops[op_def.Name] = op_def; | |||||
| // r1.14 added NN op | |||||
| var op = _registered_ops.FirstOrDefault(x => x.Key == "NearestNeighbors"); | |||||
| Assert.IsTrue(op_list.Op.Count > 1000); | Assert.IsTrue(op_list.Op.Count > 1000); | ||||
| } | } | ||||