Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions TensorFlow.NET.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/TensorFlowNET.Core/APIs/tf.math.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,11 @@ public static Tensor reduce_sum(Tensor input, int? axis = null, int? reduction_i
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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected override Tensor _log_prob(Tensor x)
{
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)
Expand Down
13 changes: 8 additions & 5 deletions src/TensorFlowNET.Core/Operations/math_ops.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,14 @@ public static Tensor reduce_all(Tensor input_tensor, int[] axis = null, bool kee
/// <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)
{
Expand All @@ -238,7 +238,6 @@ public static Tensor reduce_logsumexp(Tensor input_tensor, int[] axis = null, bo
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)
Expand Down Expand Up @@ -295,13 +294,17 @@ private static Tensor _may_reduce_to_scalar(bool keepdims, Tensor axis, Tensor o
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;
}

Expand All @@ -323,7 +326,7 @@ private static Tensor _ReductionDims(Tensor x, int[] axis)
if (axis != null)
{
// should return axis. or check before.
return null;
return ops.convert_to_tensor(axis, TF_DataType.TF_INT32);
}
else
{
Expand Down
4 changes: 4 additions & 0 deletions src/TensorFlowNET.Core/TensorFlowNET.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
2 changes: 1 addition & 1 deletion test/TensorFlowNET.Examples/ImageRecognition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
4 changes: 2 additions & 2 deletions test/TensorFlowNET.Examples/KMeansClustering.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions test/TensorFlowNET.Examples/NaiveBayesClassifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -88,7 +88,7 @@ public bool Run()
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));
}

Expand Down Expand Up @@ -179,7 +179,7 @@ public Tensor predict (NDArray X)
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down