Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/TensorFlowNET.Core/APIs/tf.math.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,9 @@ public static Tensor round(Tensor x, string name = null)
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);

Expand Down
7 changes: 7 additions & 0 deletions src/TensorFlowNET.Core/Operations/gen_math_ops.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,13 @@ public static Tensor cosh(Tensor x, string name = null)
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];
}

/// <summary>
/// Computes the sum along segments of a tensor.
/// </summary>
Expand Down
11 changes: 11 additions & 0 deletions src/TensorFlowNET.Core/Operations/math_ops.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ public static Tensor cast(Tensor x, TF_DataType dtype = TF_DataType.DtInvalid, s
});
}

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);
});
}

/// <summary>
/// Computes Psi, the derivative of Lgamma (the log of the absolute value of
/// `Gamma(x)`), element-wise.
Expand Down
41 changes: 41 additions & 0 deletions test/TensorFlowNET.UnitTest/OperationsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,47 @@ public void isNan()
}
}

[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()
{
Expand Down