From 3fabf104c1586d0eb660f829ec2b5910283cb137 Mon Sep 17 00:00:00 2001 From: Bo Peng Date: Fri, 29 Mar 2019 15:32:44 -0500 Subject: [PATCH] Naive Bayes Classifier running but prob is not correct. --- TensorFlow.NET.sln | 6 ++++++ src/TensorFlowNET.Core/APIs/tf.math.cs | 5 +++++ .../Operations/Distributions/normal.py.cs | 2 +- src/TensorFlowNET.Core/Operations/math_ops.cs | 13 ++++++++----- src/TensorFlowNET.Core/TensorFlowNET.Core.csproj | 4 ++++ test/TensorFlowNET.Examples/ImageRecognition.cs | 2 +- test/TensorFlowNET.Examples/KMeansClustering.cs | 4 ++-- test/TensorFlowNET.Examples/NaiveBayesClassifier.cs | 6 +++--- .../TextClassificationWithMovieReviews.cs | 2 +- 9 files changed, 31 insertions(+), 13 deletions(-) diff --git a/TensorFlow.NET.sln b/TensorFlow.NET.sln index e50bb267..489e3ba3 100644 --- a/TensorFlow.NET.sln +++ b/TensorFlow.NET.sln @@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Core", "src\T EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Visualization", "src\TensorFlowNET.Visualization\TensorFlowNET.Visualization.csproj", "{0254BFF9-453C-4FE0-9609-3644559A79CE}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NumSharp.Core", "..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj", "{647E7590-9B50-48D9-B9E2-47743E9EAA56}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -33,6 +35,10 @@ Global {0254BFF9-453C-4FE0-9609-3644559A79CE}.Debug|Any CPU.Build.0 = Debug|Any CPU {0254BFF9-453C-4FE0-9609-3644559A79CE}.Release|Any CPU.ActiveCfg = Release|Any CPU {0254BFF9-453C-4FE0-9609-3644559A79CE}.Release|Any CPU.Build.0 = Release|Any CPU + {647E7590-9B50-48D9-B9E2-47743E9EAA56}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {647E7590-9B50-48D9-B9E2-47743E9EAA56}.Debug|Any CPU.Build.0 = Debug|Any CPU + {647E7590-9B50-48D9-B9E2-47743E9EAA56}.Release|Any CPU.ActiveCfg = Release|Any CPU + {647E7590-9B50-48D9-B9E2-47743E9EAA56}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/TensorFlowNET.Core/APIs/tf.math.cs b/src/TensorFlowNET.Core/APIs/tf.math.cs index 6ba7805a..6ee90749 100644 --- a/src/TensorFlowNET.Core/APIs/tf.math.cs +++ b/src/TensorFlowNET.Core/APIs/tf.math.cs @@ -252,6 +252,11 @@ namespace Tensorflow return math_ops.reduce_sum(input); } + public static Tensor reduce_sum(Tensor input, int axis, int? reduction_indices = null) + { + return math_ops.reduce_sum(input, axis); + } + public static Tensor reduce_mean(Tensor input_tensor, int[] axis = null, bool keepdims = false, string name = null, int? reduction_indices = null) => math_ops.reduce_mean(input_tensor, axis: axis, keepdims: keepdims, name: name, reduction_indices: reduction_indices); diff --git a/src/TensorFlowNET.Core/Operations/Distributions/normal.py.cs b/src/TensorFlowNET.Core/Operations/Distributions/normal.py.cs index e5243c17..773bdd6b 100644 --- a/src/TensorFlowNET.Core/Operations/Distributions/normal.py.cs +++ b/src/TensorFlowNET.Core/Operations/Distributions/normal.py.cs @@ -83,7 +83,7 @@ namespace Tensorflow { var log_prob = _log_unnormalized_prob(_z(x)); var log_norm = _log_normalization(); - return log_prob - log_norm; + return tf.sub(log_prob, log_norm); } private Tensor _log_unnormalized_prob (Tensor x) diff --git a/src/TensorFlowNET.Core/Operations/math_ops.cs b/src/TensorFlowNET.Core/Operations/math_ops.cs index 6d07376a..ab4b74b3 100644 --- a/src/TensorFlowNET.Core/Operations/math_ops.cs +++ b/src/TensorFlowNET.Core/Operations/math_ops.cs @@ -222,14 +222,14 @@ namespace Tensorflow /// The reduced tensor. public static Tensor reduce_logsumexp(Tensor input_tensor, int[] axis = null, bool keepdims = false, string name = null) { - with(ops.name_scope(name, "ReduceLogSumExp", new { input_tensor }), scope => + return with(ops.name_scope(name, "ReduceLogSumExp", new { input_tensor }), scope => { var raw_max = reduce_max(input_tensor, axis, true); var my_max = array_ops.stop_gradient(array_ops.where(gen_math_ops.is_finite(raw_max), raw_max, array_ops.zeros_like(raw_max))); var result = gen_math_ops.log( reduce_sum( gen_math_ops.exp(gen_math_ops.sub(input_tensor, my_max)), - new Tensor(axis), + axis[0], keepdims)); if (!keepdims) { @@ -238,7 +238,6 @@ namespace Tensorflow result = gen_math_ops.add(result, my_max); return _may_reduce_to_scalar(keepdims, axis, result); }); - return null; } public static Tensor reduce_max(Tensor input_tensor, int[] axis = null, bool keepdims = false, string name = null) @@ -295,13 +294,17 @@ namespace Tensorflow if (!common_shapes.has_fully_defined_shape(output) && !keepdims && axis == null) + // We want set_shape to be reflected in the C API graph for when we run it. output.shape = new long[0]; return output; } private static Tensor _may_reduce_to_scalar(bool keepdims, int[] axis, Tensor output) { - output.shape = new long[0]; + if (!common_shapes.has_fully_defined_shape(output) && + !keepdims && + axis == null) + output.shape = new long[0]; return output; } @@ -323,7 +326,7 @@ namespace Tensorflow if (axis != null) { // should return axis. or check before. - return null; + return ops.convert_to_tensor(axis, TF_DataType.TF_INT32); } else { diff --git a/src/TensorFlowNET.Core/TensorFlowNET.Core.csproj b/src/TensorFlowNET.Core/TensorFlowNET.Core.csproj index 4feaaf37..65b2ab6a 100644 --- a/src/TensorFlowNET.Core/TensorFlowNET.Core.csproj +++ b/src/TensorFlowNET.Core/TensorFlowNET.Core.csproj @@ -58,4 +58,8 @@ Bug memory leak issue when allocating Tensor. + + + + diff --git a/test/TensorFlowNET.Examples/ImageRecognition.cs b/test/TensorFlowNET.Examples/ImageRecognition.cs index 9a5805eb..50524826 100644 --- a/test/TensorFlowNET.Examples/ImageRecognition.cs +++ b/test/TensorFlowNET.Examples/ImageRecognition.cs @@ -12,7 +12,7 @@ namespace TensorFlowNET.Examples { public class ImageRecognition : Python, IExample { - public int Priority => 6; + public int Priority => 7; public bool Enabled => true; public string Name => "Image Recognition"; diff --git a/test/TensorFlowNET.Examples/KMeansClustering.cs b/test/TensorFlowNET.Examples/KMeansClustering.cs index fcec5efa..73d4e2d7 100644 --- a/test/TensorFlowNET.Examples/KMeansClustering.cs +++ b/test/TensorFlowNET.Examples/KMeansClustering.cs @@ -15,8 +15,8 @@ namespace TensorFlowNET.Examples /// public class KMeansClustering : Python, IExample { - public int Priority => 7; - public bool Enabled => true; + public int Priority => 8; + public bool Enabled => false; public string Name => "K-means Clustering"; Datasets mnist; diff --git a/test/TensorFlowNET.Examples/NaiveBayesClassifier.cs b/test/TensorFlowNET.Examples/NaiveBayesClassifier.cs index b9c4fe76..4dd77e07 100644 --- a/test/TensorFlowNET.Examples/NaiveBayesClassifier.cs +++ b/test/TensorFlowNET.Examples/NaiveBayesClassifier.cs @@ -12,7 +12,7 @@ namespace TensorFlowNET.Examples /// public class NaiveBayesClassifier : Python, IExample { - public int Priority => 100; + public int Priority => 6; public bool Enabled => true; public string Name => "Naive Bayes Classifier"; @@ -88,7 +88,7 @@ namespace TensorFlowNET.Examples var s = tf.Session(); if (xx.dtype == typeof(float)) { - var samples = np.vstack (xx.ravel(), yy.ravel()); + var samples = np.hstack(xx.ravel().reshape(-1,1), yy.ravel().reshape(-1,1)); var Z = s.run(predict(samples)); } @@ -179,7 +179,7 @@ namespace TensorFlowNET.Examples var joint_likelihood = tf.add(ops.convert_to_tensor(priors, TF_DataType.TF_FLOAT), cond_probs); // normalize to get (log)-probabilities - var norm_factor = tf.reduce_logsumexp(joint_likelihood, new int[] { 1 }, true); + var norm_factor = tf.reduce_logsumexp(joint_likelihood, new int[] { 1 }, keepdims: true); var log_prob = joint_likelihood - norm_factor; // exp to get the actual probabilities return tf.exp(log_prob); diff --git a/test/TensorFlowNET.Examples/TextClassification/TextClassificationWithMovieReviews.cs b/test/TensorFlowNET.Examples/TextClassification/TextClassificationWithMovieReviews.cs index a1e0fd74..0f7737cd 100644 --- a/test/TensorFlowNET.Examples/TextClassification/TextClassificationWithMovieReviews.cs +++ b/test/TensorFlowNET.Examples/TextClassification/TextClassificationWithMovieReviews.cs @@ -11,7 +11,7 @@ namespace TensorFlowNET.Examples { public class TextClassificationWithMovieReviews : Python, IExample { - public int Priority => 7; + public int Priority => 9; public bool Enabled => false; public string Name => "Movie Reviews";