From 126ed2766f27c2513d6c26e59919de57979fcdbe Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Sun, 13 Jan 2019 21:39:15 -0600 Subject: [PATCH] reduce_sum in progress --- src/TensorFlowNET.Core/APIs/tf.math.cs | 15 ++++++++- .../Operations/gen_array_ops.cs | 10 ++++++ .../Operations/gen_math_ops.cs | 32 +++++++++++++++++-- 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/TensorFlowNET.Core/APIs/tf.math.cs b/src/TensorFlowNET.Core/APIs/tf.math.cs index dc70344a..5d74a4df 100644 --- a/src/TensorFlowNET.Core/APIs/tf.math.cs +++ b/src/TensorFlowNET.Core/APIs/tf.math.cs @@ -26,9 +26,22 @@ namespace Tensorflow return gen_math_ops.pow(x, y); } + /// + /// Computes the sum of elements across dimensions of a tensor. + /// + /// + /// + /// public static Tensor reduce_sum(Tensor input, int[] axis = null) { - return gen_math_ops.sum(input, axis); + Tensor rank; + using (var namescop = new ops.name_scope("", "Rank", new List { input })) + { + string name = namescop; + rank = gen_array_ops.rank(input, name); + } + var s = gen_math_ops.sum(input, rank); + return gen_math_ops.range(0, s); } } } diff --git a/src/TensorFlowNET.Core/Operations/gen_array_ops.cs b/src/TensorFlowNET.Core/Operations/gen_array_ops.cs index b08d5e1f..560f6173 100644 --- a/src/TensorFlowNET.Core/Operations/gen_array_ops.cs +++ b/src/TensorFlowNET.Core/Operations/gen_array_ops.cs @@ -41,5 +41,15 @@ namespace Tensorflow return _op.outputs[0]; } + + public static Tensor rank(Tensor input, string name = "") + { + var keywords = new Dictionary(); + keywords.Add("input", input); + + var _op = _op_def_lib._apply_op_helper("Rank", name: name, keywords: keywords); + + return _op.outputs[0]; + } } } diff --git a/src/TensorFlowNET.Core/Operations/gen_math_ops.cs b/src/TensorFlowNET.Core/Operations/gen_math_ops.cs index a7cfce43..91e7e629 100644 --- a/src/TensorFlowNET.Core/Operations/gen_math_ops.cs +++ b/src/TensorFlowNET.Core/Operations/gen_math_ops.cs @@ -78,17 +78,43 @@ namespace Tensorflow return new Tensor(_op, 0, _op.OutputType(0)); } - public static Tensor sum(Tensor input, int[] axis = null) + public static Tensor sum(Tensor input, Tensor axis = null) { - if(axis == null) axis = new int[0]; var keywords = new Dictionary(); keywords.Add("input", input); - keywords.Add("reduction_indices", constant_op.Constant(axis)); + keywords.Add("reduction_indices", axis); keywords.Add("keep_dims", false); var _op = _op_def_lib._apply_op_helper("Sum", keywords: keywords); return new Tensor(_op, 0, _op.OutputType(0)); } + + /// + /// Creates a sequence of numbers. + /// + /// + /// + /// + /// + /// + public static Tensor range(int start, Tensor limit, int delta = 1) + { + using (var namescope = new ops.name_scope("", "Range", new List { start, limit, delta })) + { + var start1 = ops.convert_to_tensor(start, "start"); + var limit1 = ops.convert_to_tensor(limit, "limit"); + var delta1 = ops.convert_to_tensor(delta, "delta"); + + var keywords = new Dictionary(); + keywords.Add("start", start1); + keywords.Add("limit", limit1); + keywords.Add("delta", delta1); + + var _op = _op_def_lib._apply_op_helper("Range", namescope, keywords); + + return _op.outputs[0]; + } + } } }