Skip to content

Commit f891213

Browse files
acifonelliOceania2018
authored andcommitted
Adding Cumsum operation (SciSharp#322)
Unit testing the operation too.
1 parent 49c8262 commit f891213

4 files changed

Lines changed: 62 additions & 0 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,9 @@ public static Tensor round(Tensor x, string name = null)
348348
public static Tensor cast(Tensor x, TF_DataType dtype = TF_DataType.DtInvalid, string name = null)
349349
=> math_ops.cast(x, dtype, name);
350350

351+
public static Tensor cumsum(Tensor x, int axis = 0, bool exclusive = false, bool reverse = false, string name = null)
352+
=> math_ops.cumsum(x, axis: axis, exclusive: exclusive, reverse: reverse, name: name);
353+
351354
public static Tensor argmax(Tensor input, int axis = -1, string name = null, int? dimension = null, TF_DataType output_type = TF_DataType.TF_INT64)
352355
=> gen_math_ops.arg_max(input, axis, name: name, output_type: output_type);
353356

src/TensorFlowNET.Core/Operations/gen_math_ops.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,13 @@ public static Tensor cosh(Tensor x, string name = null)
238238
return _op.outputs[0];
239239
}
240240

241+
public static Tensor cumsum(Tensor x, int axis = 0, bool exclusive = false, bool reverse = false, string name = null)
242+
{
243+
var _op = _op_def_lib._apply_op_helper("Cumsum", name, args: new { x, axis, exclusive, reverse });
244+
245+
return _op.outputs[0];
246+
}
247+
241248
/// <summary>
242249
/// Computes the sum along segments of a tensor.
243250
/// </summary>

src/TensorFlowNET.Core/Operations/math_ops.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ public static Tensor cast(Tensor x, TF_DataType dtype = TF_DataType.DtInvalid, s
8080
});
8181
}
8282

83+
public static Tensor cumsum(Tensor x, int axis = 0, bool exclusive = false, bool reverse = false, string name = null)
84+
{
85+
return with(ops.name_scope(name, "Cumsum", new {x}), scope =>
86+
{
87+
name = scope;
88+
x = ops.convert_to_tensor(x, name: "x");
89+
90+
return gen_math_ops.cumsum(x, axis: axis, exclusive: exclusive, reverse: reverse, name: name);
91+
});
92+
}
93+
8394
/// <summary>
8495
/// Computes Psi, the derivative of Lgamma (the log of the absolute value of
8596
/// `Gamma(x)`), element-wise.

test/TensorFlowNET.UnitTest/OperationsTest.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,47 @@ public void isNan()
8989
}
9090
}
9191

92+
[TestMethod]
93+
public void cumSumTest()
94+
{
95+
var a = tf.constant(new[] { 1, 1, 2, 3, 4, 5 });
96+
var b = tf.cumsum(a);
97+
var check = np.array(1, 2, 4, 7, 11, 16);
98+
99+
using (var sess = tf.Session())
100+
{
101+
var o = sess.run(b);
102+
Assert.IsTrue(o.array_equal(check));
103+
}
104+
105+
b = tf.cumsum(a, exclusive: true);
106+
check = np.array(0, 1, 2, 4, 7, 11);
107+
108+
using (var sess = tf.Session())
109+
{
110+
var o = sess.run(b);
111+
Assert.IsTrue(o.array_equal(check));
112+
}
113+
114+
b = tf.cumsum(a, reverse: true);
115+
check = np.array(16, 15, 14, 12, 9, 5);
116+
117+
using (var sess = tf.Session())
118+
{
119+
var o = sess.run(b);
120+
Assert.IsTrue(o.array_equal(check));
121+
}
122+
123+
b = tf.cumsum(a, exclusive:true, reverse: true);
124+
check = np.array(15, 14, 12, 9, 5, 0);
125+
126+
using (var sess = tf.Session())
127+
{
128+
var o = sess.run(b);
129+
Assert.IsTrue(o.array_equal(check));
130+
}
131+
}
132+
92133
[TestMethod]
93134
public void addOpTests()
94135
{

0 commit comments

Comments
 (0)