From 7d0d2715d735be335a7ed10ab9614d44b3a68f80 Mon Sep 17 00:00:00 2001 From: haiping008 Date: Wed, 20 Mar 2019 15:31:32 -0500 Subject: [PATCH] fix reduce_sum when axis is not null --- src/TensorFlowNET.Core/APIs/tf.math.cs | 8 ++++++-- src/TensorFlowNET.Core/Operations/gen_math_ops.cs | 7 +++++++ src/TensorFlowNET.Core/Operations/math_ops.py.cs | 8 +++++++- test/TensorFlowNET.Examples/LogisticRegression.cs | 9 ++++++--- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/TensorFlowNET.Core/APIs/tf.math.cs b/src/TensorFlowNET.Core/APIs/tf.math.cs index e01da00c..0039e0d2 100644 --- a/src/TensorFlowNET.Core/APIs/tf.math.cs +++ b/src/TensorFlowNET.Core/APIs/tf.math.cs @@ -36,8 +36,12 @@ namespace Tensorflow /// /// /// - public static Tensor reduce_sum(Tensor input, int[] axis = null, int? reduction_indices = null) - => math_ops.reduce_sum(input); + public static Tensor reduce_sum(Tensor input, int? axis = null, int? reduction_indices = null) + { + if(!axis.HasValue && reduction_indices.HasValue) + return math_ops.reduce_sum(input, reduction_indices.Value); + return math_ops.reduce_sum(input); + } 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/gen_math_ops.cs b/src/TensorFlowNET.Core/Operations/gen_math_ops.cs index 26513051..1ccb0e96 100644 --- a/src/TensorFlowNET.Core/Operations/gen_math_ops.cs +++ b/src/TensorFlowNET.Core/Operations/gen_math_ops.cs @@ -207,6 +207,13 @@ namespace Tensorflow return _op.outputs[0]; } + public static Tensor sum(Tensor input, int axis, bool keep_dims = false, string name = null) + { + var _op = _op_def_lib._apply_op_helper("Sum", name, args: new { input, reduction_indices = axis, keep_dims }); + + return _op.outputs[0]; + } + /// /// Creates a sequence of numbers. /// diff --git a/src/TensorFlowNET.Core/Operations/math_ops.py.cs b/src/TensorFlowNET.Core/Operations/math_ops.py.cs index fd2a0644..3c6ea594 100644 --- a/src/TensorFlowNET.Core/Operations/math_ops.py.cs +++ b/src/TensorFlowNET.Core/Operations/math_ops.py.cs @@ -209,6 +209,12 @@ namespace Tensorflow return _may_reduce_to_scalar(keepdims, m); } + public static Tensor reduce_sum(Tensor input_tensor, int axis, bool keepdims = false) + { + var m = gen_math_ops.sum(input_tensor, axis); + return _may_reduce_to_scalar(keepdims, m); + } + private static Tensor _may_reduce_to_scalar(bool keepdims, Tensor output) { output.shape = new long[0]; @@ -233,7 +239,7 @@ namespace Tensorflow return range(0, rank, 1); } } - + private static Tensor _ReductionDims(Tensor x, int[] axis) { if (axis != null) diff --git a/test/TensorFlowNET.Examples/LogisticRegression.cs b/test/TensorFlowNET.Examples/LogisticRegression.cs index 71760404..6cee6d8b 100644 --- a/test/TensorFlowNET.Examples/LogisticRegression.cs +++ b/test/TensorFlowNET.Examples/LogisticRegression.cs @@ -26,7 +26,7 @@ namespace TensorFlowNET.Examples private void PrepareData() { - var mnist = MnistDataSet.read_data_sets("logistic_regression", one_hot: true); + //var mnist = MnistDataSet.read_data_sets("logistic_regression", one_hot: true); // tf Graph Input var x = tf.placeholder(tf.float32, new TensorShape(-1, 784)); // mnist data image of shape 28*28=784 @@ -40,8 +40,11 @@ namespace TensorFlowNET.Examples var pred = tf.nn.softmax(tf.matmul(x, W) + b); // Softmax // Minimize error using cross entropy - var sum = -tf.reduce_sum(y * tf.log(pred), reduction_indices: 1); - var cost = tf.reduce_mean(sum); + var log = tf.log(pred); + var mul = y * log; + var sum = tf.reduce_sum(mul, reduction_indices: 1); + var neg = -sum; + var cost = tf.reduce_mean(neg); // Gradient Descent var optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost);