Skip to content

Commit c9e58d8

Browse files
authored
Merge pull request SciSharp#201 from PppBr/master
NB classifier running
2 parents 3593978 + c15a1ea commit c9e58d8

9 files changed

Lines changed: 31 additions & 13 deletions

File tree

TensorFlow.NET.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Core", "src\T
1111
EndProject
1212
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Visualization", "src\TensorFlowNET.Visualization\TensorFlowNET.Visualization.csproj", "{0254BFF9-453C-4FE0-9609-3644559A79CE}"
1313
EndProject
14+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NumSharp.Core", "..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj", "{647E7590-9B50-48D9-B9E2-47743E9EAA56}"
15+
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1618
Debug|Any CPU = Debug|Any CPU
@@ -33,6 +35,10 @@ Global
3335
{0254BFF9-453C-4FE0-9609-3644559A79CE}.Debug|Any CPU.Build.0 = Debug|Any CPU
3436
{0254BFF9-453C-4FE0-9609-3644559A79CE}.Release|Any CPU.ActiveCfg = Release|Any CPU
3537
{0254BFF9-453C-4FE0-9609-3644559A79CE}.Release|Any CPU.Build.0 = Release|Any CPU
38+
{647E7590-9B50-48D9-B9E2-47743E9EAA56}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39+
{647E7590-9B50-48D9-B9E2-47743E9EAA56}.Debug|Any CPU.Build.0 = Debug|Any CPU
40+
{647E7590-9B50-48D9-B9E2-47743E9EAA56}.Release|Any CPU.ActiveCfg = Release|Any CPU
41+
{647E7590-9B50-48D9-B9E2-47743E9EAA56}.Release|Any CPU.Build.0 = Release|Any CPU
3642
EndGlobalSection
3743
GlobalSection(SolutionProperties) = preSolution
3844
HideSolutionNode = FALSE

src/TensorFlowNET.Core/APIs/tf.math.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,11 @@ public static Tensor reduce_sum(Tensor input, int? axis = null, int? reduction_i
252252
return math_ops.reduce_sum(input);
253253
}
254254

255+
public static Tensor reduce_sum(Tensor input, int axis, int? reduction_indices = null)
256+
{
257+
return math_ops.reduce_sum(input, axis);
258+
}
259+
255260
public static Tensor reduce_mean(Tensor input_tensor, int[] axis = null, bool keepdims = false, string name = null, int? reduction_indices = null)
256261
=> math_ops.reduce_mean(input_tensor, axis: axis, keepdims: keepdims, name: name, reduction_indices: reduction_indices);
257262

src/TensorFlowNET.Core/Operations/Distributions/normal.py.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ protected override Tensor _log_prob(Tensor x)
8383
{
8484
var log_prob = _log_unnormalized_prob(_z(x));
8585
var log_norm = _log_normalization();
86-
return log_prob - log_norm;
86+
return tf.sub(log_prob, log_norm);
8787
}
8888

8989
private Tensor _log_unnormalized_prob (Tensor x)

src/TensorFlowNET.Core/Operations/math_ops.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,14 @@ public static Tensor reduce_all(Tensor input_tensor, int[] axis = null, bool kee
211211
/// <returns> The reduced tensor.</returns>
212212
public static Tensor reduce_logsumexp(Tensor input_tensor, int[] axis = null, bool keepdims = false, string name = null)
213213
{
214-
with(ops.name_scope(name, "ReduceLogSumExp", new { input_tensor }), scope =>
214+
return with(ops.name_scope(name, "ReduceLogSumExp", new { input_tensor }), scope =>
215215
{
216216
var raw_max = reduce_max(input_tensor, axis, true);
217217
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)));
218218
var result = gen_math_ops.log(
219219
reduce_sum(
220220
gen_math_ops.exp(gen_math_ops.sub(input_tensor, my_max)),
221-
new Tensor(axis),
221+
axis[0],
222222
keepdims));
223223
if (!keepdims)
224224
{
@@ -227,7 +227,6 @@ public static Tensor reduce_logsumexp(Tensor input_tensor, int[] axis = null, bo
227227
result = gen_math_ops.add(result, my_max);
228228
return _may_reduce_to_scalar(keepdims, axis, result);
229229
});
230-
return null;
231230
}
232231

233232
public static Tensor reduce_max(Tensor input_tensor, int[] axis = null, bool keepdims = false, string name = null)
@@ -284,13 +283,17 @@ private static Tensor _may_reduce_to_scalar(bool keepdims, Tensor axis, Tensor o
284283
if (!common_shapes.has_fully_defined_shape(output) &&
285284
!keepdims &&
286285
axis == null)
286+
// We want set_shape to be reflected in the C API graph for when we run it.
287287
output.shape = new long[0];
288288
return output;
289289
}
290290

291291
private static Tensor _may_reduce_to_scalar(bool keepdims, int[] axis, Tensor output)
292292
{
293-
output.shape = new long[0];
293+
if (!common_shapes.has_fully_defined_shape(output) &&
294+
!keepdims &&
295+
axis == null)
296+
output.shape = new long[0];
294297
return output;
295298
}
296299

@@ -312,7 +315,7 @@ private static Tensor _ReductionDims(Tensor x, int[] axis)
312315
if (axis != null)
313316
{
314317
// should return axis. or check before.
315-
return null;
318+
return ops.convert_to_tensor(axis, TF_DataType.TF_INT32);
316319
}
317320
else
318321
{

src/TensorFlowNET.Core/TensorFlowNET.Core.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,8 @@ Bug memory leak issue when allocating Tensor.</PackageReleaseNotes>
5858
<Folder Include="Keras\Initializers\" />
5959
</ItemGroup>
6060

61+
<ItemGroup>
62+
<ProjectReference Include="..\..\..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj" />
63+
</ItemGroup>
64+
6165
</Project>

test/TensorFlowNET.Examples/ImageRecognition.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace TensorFlowNET.Examples
1212
{
1313
public class ImageRecognition : Python, IExample
1414
{
15-
public int Priority => 6;
15+
public int Priority => 7;
1616
public bool Enabled => true;
1717
public string Name => "Image Recognition";
1818

test/TensorFlowNET.Examples/KMeansClustering.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ namespace TensorFlowNET.Examples
1515
/// </summary>
1616
public class KMeansClustering : Python, IExample
1717
{
18-
public int Priority => 7;
19-
public bool Enabled => true;
18+
public int Priority => 8;
19+
public bool Enabled => false;
2020
public string Name => "K-means Clustering";
2121

2222
Datasets mnist;

test/TensorFlowNET.Examples/NaiveBayesClassifier.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace TensorFlowNET.Examples
1212
/// </summary>
1313
public class NaiveBayesClassifier : Python, IExample
1414
{
15-
public int Priority => 100;
15+
public int Priority => 6;
1616
public bool Enabled => true;
1717
public string Name => "Naive Bayes Classifier";
1818

@@ -88,7 +88,7 @@ public bool Run()
8888
var s = tf.Session();
8989
if (xx.dtype == typeof(float))
9090
{
91-
var samples = np.vstack <float>(xx.ravel(), yy.ravel());
91+
var samples = np.hstack<float>(xx.ravel().reshape(-1,1), yy.ravel().reshape(-1,1));
9292
var Z = s.run(predict(samples));
9393
}
9494

@@ -179,7 +179,7 @@ public Tensor predict (NDArray X)
179179
var joint_likelihood = tf.add(ops.convert_to_tensor(priors, TF_DataType.TF_FLOAT), cond_probs);
180180
// normalize to get (log)-probabilities
181181

182-
var norm_factor = tf.reduce_logsumexp(joint_likelihood, new int[] { 1 }, true);
182+
var norm_factor = tf.reduce_logsumexp(joint_likelihood, new int[] { 1 }, keepdims: true);
183183
var log_prob = joint_likelihood - norm_factor;
184184
// exp to get the actual probabilities
185185
return tf.exp(log_prob);

test/TensorFlowNET.Examples/TextClassification/TextClassificationWithMovieReviews.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace TensorFlowNET.Examples
1111
{
1212
public class TextClassificationWithMovieReviews : Python, IExample
1313
{
14-
public int Priority => 7;
14+
public int Priority => 9;
1515
public bool Enabled => false;
1616
public string Name => "Movie Reviews";
1717

0 commit comments

Comments
 (0)