From f8912135a75406959bc27f28ffdbfa26163832c0 Mon Sep 17 00:00:00 2001 From: Antonio Date: Fri, 26 Jul 2019 20:59:05 +0200 Subject: [PATCH] Adding `Cumsum` operation (#322) Unit testing the operation too. --- src/TensorFlowNET.Core/APIs/tf.math.cs | 3 ++ .../Operations/gen_math_ops.cs | 7 ++++ src/TensorFlowNET.Core/Operations/math_ops.cs | 11 +++++ test/TensorFlowNET.UnitTest/OperationsTest.cs | 41 +++++++++++++++++++ 4 files changed, 62 insertions(+) diff --git a/src/TensorFlowNET.Core/APIs/tf.math.cs b/src/TensorFlowNET.Core/APIs/tf.math.cs index d3b24373..5586840c 100644 --- a/src/TensorFlowNET.Core/APIs/tf.math.cs +++ b/src/TensorFlowNET.Core/APIs/tf.math.cs @@ -348,6 +348,9 @@ namespace Tensorflow public static Tensor cast(Tensor x, TF_DataType dtype = TF_DataType.DtInvalid, string name = null) => math_ops.cast(x, dtype, name); + public static Tensor cumsum(Tensor x, int axis = 0, bool exclusive = false, bool reverse = false, string name = null) + => math_ops.cumsum(x, axis: axis, exclusive: exclusive, reverse: reverse, name: name); + public static Tensor argmax(Tensor input, int axis = -1, string name = null, int? dimension = null, TF_DataType output_type = TF_DataType.TF_INT64) => gen_math_ops.arg_max(input, axis, name: name, output_type: output_type); diff --git a/src/TensorFlowNET.Core/Operations/gen_math_ops.cs b/src/TensorFlowNET.Core/Operations/gen_math_ops.cs index 7ab6f858..8ec7e253 100644 --- a/src/TensorFlowNET.Core/Operations/gen_math_ops.cs +++ b/src/TensorFlowNET.Core/Operations/gen_math_ops.cs @@ -238,6 +238,13 @@ namespace Tensorflow return _op.outputs[0]; } + public static Tensor cumsum(Tensor x, int axis = 0, bool exclusive = false, bool reverse = false, string name = null) + { + var _op = _op_def_lib._apply_op_helper("Cumsum", name, args: new { x, axis, exclusive, reverse }); + + return _op.outputs[0]; + } + /// /// Computes the sum along segments of a tensor. /// diff --git a/src/TensorFlowNET.Core/Operations/math_ops.cs b/src/TensorFlowNET.Core/Operations/math_ops.cs index 486fea8d..fc8f08ac 100644 --- a/src/TensorFlowNET.Core/Operations/math_ops.cs +++ b/src/TensorFlowNET.Core/Operations/math_ops.cs @@ -80,6 +80,17 @@ namespace Tensorflow }); } + public static Tensor cumsum(Tensor x, int axis = 0, bool exclusive = false, bool reverse = false, string name = null) + { + return with(ops.name_scope(name, "Cumsum", new {x}), scope => + { + name = scope; + x = ops.convert_to_tensor(x, name: "x"); + + return gen_math_ops.cumsum(x, axis: axis, exclusive: exclusive, reverse: reverse, name: name); + }); + } + /// /// Computes Psi, the derivative of Lgamma (the log of the absolute value of /// `Gamma(x)`), element-wise. diff --git a/test/TensorFlowNET.UnitTest/OperationsTest.cs b/test/TensorFlowNET.UnitTest/OperationsTest.cs index 7f099fde..37f8d450 100644 --- a/test/TensorFlowNET.UnitTest/OperationsTest.cs +++ b/test/TensorFlowNET.UnitTest/OperationsTest.cs @@ -89,6 +89,47 @@ namespace TensorFlowNET.UnitTest } } + [TestMethod] + public void cumSumTest() + { + var a = tf.constant(new[] { 1, 1, 2, 3, 4, 5 }); + var b = tf.cumsum(a); + var check = np.array(1, 2, 4, 7, 11, 16); + + using (var sess = tf.Session()) + { + var o = sess.run(b); + Assert.IsTrue(o.array_equal(check)); + } + + b = tf.cumsum(a, exclusive: true); + check = np.array(0, 1, 2, 4, 7, 11); + + using (var sess = tf.Session()) + { + var o = sess.run(b); + Assert.IsTrue(o.array_equal(check)); + } + + b = tf.cumsum(a, reverse: true); + check = np.array(16, 15, 14, 12, 9, 5); + + using (var sess = tf.Session()) + { + var o = sess.run(b); + Assert.IsTrue(o.array_equal(check)); + } + + b = tf.cumsum(a, exclusive:true, reverse: true); + check = np.array(15, 14, 12, 9, 5, 0); + + using (var sess = tf.Session()) + { + var o = sess.run(b); + Assert.IsTrue(o.array_equal(check)); + } + } + [TestMethod] public void addOpTests() {