Browse Source

Naive Bayes Classifier running but prob is not correct.

tags/v0.9
Bo Peng 6 years ago
parent
commit
3fabf104c1
9 changed files with 31 additions and 13 deletions
  1. +6
    -0
      TensorFlow.NET.sln
  2. +5
    -0
      src/TensorFlowNET.Core/APIs/tf.math.cs
  3. +1
    -1
      src/TensorFlowNET.Core/Operations/Distributions/normal.py.cs
  4. +8
    -5
      src/TensorFlowNET.Core/Operations/math_ops.cs
  5. +4
    -0
      src/TensorFlowNET.Core/TensorFlowNET.Core.csproj
  6. +1
    -1
      test/TensorFlowNET.Examples/ImageRecognition.cs
  7. +2
    -2
      test/TensorFlowNET.Examples/KMeansClustering.cs
  8. +3
    -3
      test/TensorFlowNET.Examples/NaiveBayesClassifier.cs
  9. +1
    -1
      test/TensorFlowNET.Examples/TextClassification/TextClassificationWithMovieReviews.cs

+ 6
- 0
TensorFlow.NET.sln View File

@@ -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


+ 5
- 0
src/TensorFlowNET.Core/APIs/tf.math.cs View File

@@ -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);



+ 1
- 1
src/TensorFlowNET.Core/Operations/Distributions/normal.py.cs View File

@@ -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)


+ 8
- 5
src/TensorFlowNET.Core/Operations/math_ops.cs View File

@@ -222,14 +222,14 @@ namespace Tensorflow
/// <returns> The reduced tensor.</returns>
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
{


+ 4
- 0
src/TensorFlowNET.Core/TensorFlowNET.Core.csproj View File

@@ -58,4 +58,8 @@ Bug memory leak issue when allocating Tensor.</PackageReleaseNotes>
<Folder Include="Keras\Initializers\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj" />
</ItemGroup>

</Project>

+ 1
- 1
test/TensorFlowNET.Examples/ImageRecognition.cs View File

@@ -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";



+ 2
- 2
test/TensorFlowNET.Examples/KMeansClustering.cs View File

@@ -15,8 +15,8 @@ namespace TensorFlowNET.Examples
/// </summary>
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;


+ 3
- 3
test/TensorFlowNET.Examples/NaiveBayesClassifier.cs View File

@@ -12,7 +12,7 @@ namespace TensorFlowNET.Examples
/// </summary>
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 <float>(xx.ravel(), yy.ravel());
var samples = np.hstack<float>(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);


+ 1
- 1
test/TensorFlowNET.Examples/TextClassification/TextClassificationWithMovieReviews.cs View File

@@ -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";



Loading…
Cancel
Save