From dc8a8cf0dd4ea383186e904fbf6713ad61ed4240 Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Sat, 27 Apr 2019 12:56:42 -0500 Subject: [PATCH] add tf.convert_to_tensor --- TensorFlow.NET.sln | 6 --- src/TensorFlowNET.Core/APIs/tf.tensor.cs | 13 +++++++ src/TensorFlowNET.Core/Python.cs | 2 +- .../TensorFlowNET.Core.csproj | 10 ++--- .../KMeansClustering.cs | 39 +++++++++++++++---- .../LogisticRegression.cs | 2 +- .../TensorFlowNET.Examples/NearestNeighbor.cs | 4 +- .../TensorFlowNET.Examples.csproj | 5 +-- .../TensorFlowNET.UnitTest.csproj | 5 +-- 9 files changed, 55 insertions(+), 31 deletions(-) create mode 100644 src/TensorFlowNET.Core/APIs/tf.tensor.cs diff --git a/TensorFlow.NET.sln b/TensorFlow.NET.sln index bef96f5e..b96f8203 100644 --- a/TensorFlow.NET.sln +++ b/TensorFlow.NET.sln @@ -9,8 +9,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Examples", "t EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Core", "src\TensorFlowNET.Core\TensorFlowNET.Core.csproj", "{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NumSharp.Core", "..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj", "{DE97EAD7-B92C-4112-9690-91C40A97179E}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -29,10 +27,6 @@ Global {FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Debug|Any CPU.Build.0 = Debug|Any CPU {FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Release|Any CPU.ActiveCfg = Release|Any CPU {FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Release|Any CPU.Build.0 = Release|Any CPU - {DE97EAD7-B92C-4112-9690-91C40A97179E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {DE97EAD7-B92C-4112-9690-91C40A97179E}.Debug|Any CPU.Build.0 = Debug|Any CPU - {DE97EAD7-B92C-4112-9690-91C40A97179E}.Release|Any CPU.ActiveCfg = Release|Any CPU - {DE97EAD7-B92C-4112-9690-91C40A97179E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/TensorFlowNET.Core/APIs/tf.tensor.cs b/src/TensorFlowNET.Core/APIs/tf.tensor.cs new file mode 100644 index 00000000..33d86e14 --- /dev/null +++ b/src/TensorFlowNET.Core/APIs/tf.tensor.cs @@ -0,0 +1,13 @@ +using NumSharp; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow +{ + public static partial class tf + { + public static Tensor convert_to_tensor(object value, + string name = null) => ops.convert_to_tensor(value, name: name); + } +} diff --git a/src/TensorFlowNET.Core/Python.cs b/src/TensorFlowNET.Core/Python.cs index e183ef43..84b534bc 100644 --- a/src/TensorFlowNET.Core/Python.cs +++ b/src/TensorFlowNET.Core/Python.cs @@ -29,7 +29,7 @@ namespace Tensorflow protected IEnumerable range(int start, int end) { - return Enumerable.Range(start, end); + return Enumerable.Range(start, end - start); } public static T New(object args) where T : IPyClass diff --git a/src/TensorFlowNET.Core/TensorFlowNET.Core.csproj b/src/TensorFlowNET.Core/TensorFlowNET.Core.csproj index cae47f15..16ffa373 100644 --- a/src/TensorFlowNET.Core/TensorFlowNET.Core.csproj +++ b/src/TensorFlowNET.Core/TensorFlowNET.Core.csproj @@ -4,10 +4,10 @@ netstandard2.0 TensorFlow.NET Tensorflow - 0.6.1-alpha + 0.6.1 Haiping Chen SciSharp STACK - true + false Apache 2.0 https://github.com/SciSharp/TensorFlow.NET git @@ -44,7 +44,7 @@ More math/ linalg APIs. - + @@ -57,8 +57,4 @@ More math/ linalg APIs. - - - - diff --git a/test/TensorFlowNET.Examples/KMeansClustering.cs b/test/TensorFlowNET.Examples/KMeansClustering.cs index d9e2de47..a02c3b2d 100644 --- a/test/TensorFlowNET.Examples/KMeansClustering.cs +++ b/test/TensorFlowNET.Examples/KMeansClustering.cs @@ -1,6 +1,8 @@ using NumSharp; using System; using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; using System.Text; using Tensorflow; using Tensorflow.Clustering; @@ -26,7 +28,7 @@ namespace TensorFlowNET.Examples Datasets mnist; NDArray full_data_x; - int num_steps = 50; // Total steps to train + int num_steps = 10; // Total steps to train int k = 25; // The number of clusters int num_classes = 10; // The 10 digits int num_features = 784; // Each image is 28x28 pixels @@ -63,22 +65,43 @@ namespace TensorFlowNET.Examples // Training NDArray result = null; - foreach(var i in range(1, num_steps + 1)) + var sw = new Stopwatch(); + + foreach (var i in range(1, num_steps + 1)) { + sw.Start(); 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]}"); + sw.Stop(); + + if (i % 5 == 0 || i == 1) + print($"Step {i}, Avg Distance: {result[1]} Elapse: {sw.ElapsedMilliseconds}ms"); + + sw.Reset(); } - var idx = result[2]; + var idx = result[2].Data(); // 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]; + var counts = np.zeros((k, num_classes), np.float32); + + sw.Start(); + foreach (var i in range(idx.Length)) + { + var x = mnist.train.labels[i]; + counts[idx[i]] += x; + } + + sw.Stop(); + print($"Assign a label to each centroid took {sw.ElapsedMilliseconds}ms"); + + // Assign the most frequent label to the centroid + var labels_map_array = np.argmax(counts, 1); + var labels_map = tf.convert_to_tensor(labels_map_array); + // Evaluation ops + // Lookup: centroid_id -> label }); return false; diff --git a/test/TensorFlowNET.Examples/LogisticRegression.cs b/test/TensorFlowNET.Examples/LogisticRegression.cs index 71238bf6..7f537778 100644 --- a/test/TensorFlowNET.Examples/LogisticRegression.cs +++ b/test/TensorFlowNET.Examples/LogisticRegression.cs @@ -88,7 +88,7 @@ namespace TensorFlowNET.Examples // Display logs per epoch step if ((epoch + 1) % display_step == 0) - print($"Epoch: {(epoch + 1).ToString("D4")} cost= {avg_cost.ToString("G9")} elapse= {sw.ElapsedMilliseconds}ms"); + print($"Epoch: {(epoch + 1).ToString("D4")} Cost: {avg_cost.ToString("G9")} Elapse: {sw.ElapsedMilliseconds}ms"); sw.Reset(); } diff --git a/test/TensorFlowNET.Examples/NearestNeighbor.cs b/test/TensorFlowNET.Examples/NearestNeighbor.cs index dd5624f1..f811b5da 100644 --- a/test/TensorFlowNET.Examples/NearestNeighbor.cs +++ b/test/TensorFlowNET.Examples/NearestNeighbor.cs @@ -51,9 +51,9 @@ namespace TensorFlowNET.Examples long nn_index = sess.run(pred, new FeedItem(xtr, Xtr), new FeedItem(xte, Xte[i])); // Get nearest neighbor class label and compare it to its true label int index = (int)nn_index; - print($"Test {i} Prediction: {np.argmax(Ytr[(NDArray)index])} True Class: {np.argmax(Yte[i] as NDArray)}"); + print($"Test {i} Prediction: {np.argmax(Ytr[index])} True Class: {np.argmax(Yte[i])}"); // Calculate accuracy - if (np.argmax(Ytr[(NDArray)index]) == np.argmax(Yte[i] as NDArray)) + if ((int)np.argmax(Ytr[index]) == (int)np.argmax(Yte[i])) accuracy += 1f/ Xte.shape[0]; } diff --git a/test/TensorFlowNET.Examples/TensorFlowNET.Examples.csproj b/test/TensorFlowNET.Examples/TensorFlowNET.Examples.csproj index c29034ca..f636bc14 100644 --- a/test/TensorFlowNET.Examples/TensorFlowNET.Examples.csproj +++ b/test/TensorFlowNET.Examples/TensorFlowNET.Examples.csproj @@ -2,19 +2,18 @@ Exe - netcoreapp2.1 + netcoreapp2.2 false - + - diff --git a/test/TensorFlowNET.UnitTest/TensorFlowNET.UnitTest.csproj b/test/TensorFlowNET.UnitTest/TensorFlowNET.UnitTest.csproj index dd6e6ff6..5e619165 100644 --- a/test/TensorFlowNET.UnitTest/TensorFlowNET.UnitTest.csproj +++ b/test/TensorFlowNET.UnitTest/TensorFlowNET.UnitTest.csproj @@ -1,7 +1,7 @@ - + - netcoreapp2.1 + netcoreapp2.2 false @@ -22,7 +22,6 @@ -