Skip to content

fix: A new feature for NDArray.max #1091

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 5, 2023
Merged
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
11 changes: 10 additions & 1 deletion src/TensorFlowNET.Core/NumPy/Numpy.Math.cs
Original file line number Diff line number Diff line change
@@ -28,7 +28,16 @@ public partial class np
public static NDArray multiply(NDArray x1, NDArray x2) => new NDArray(tf.multiply(x1, x2));

[AutoNumPy]
public static NDArray maximum(NDArray x1, NDArray x2) => new NDArray(tf.maximum(x1, x2));
//public static NDArray maximum(NDArray x1, NDArray x2) => new NDArray(tf.maximum(x1, x2));
public static NDArray maximum(NDArray x1, NDArray x2, int? axis = null)
{
var maxValues = tf.maximum(x1, x2);
if (axis.HasValue)
{
maxValues = tf.reduce_max(maxValues, axis: axis.Value);
}
return new NDArray(maxValues);
}

[AutoNumPy]
public static NDArray minimum(NDArray x1, NDArray x2) => new NDArray(tf.minimum(x1, x2));
15 changes: 15 additions & 0 deletions test/TensorFlowNET.UnitTest/Numpy/Math.Test.cs
Original file line number Diff line number Diff line change
@@ -65,5 +65,20 @@ public void power()
var y = np.power(x, 3);
Assert.AreEqual(y, new[] { 0, 1, 8, 27, 64, 125 });
}
[TestMethod]
public void maximum()
{
var x1 = new NDArray(new[,] { { 1, 2, 3 }, { 4, 5.1, 6 } });
var x2 = new NDArray(new[,] { { 3, 2, 1 }, { 6, 5.1, 4 } });
var y0 = np.maximum(x1,x2);
var y1 = np.maximum(x1, x2, axis: 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution.
Can you add one more testing case

np.maximum(x1, x2, axis: 1)

var y2 = np.maximum(x1, x2, axis: 1);
var y3 = new NDArray(new[,] { { 3, 2, 3 }, { 6, 5.1, 6 } });
var y4 = new NDArray(new[] { 6, 5.1, 6 });
var y5 = new NDArray(new[] { 3.0, 6 });
Assert.AreEqual(y0, y3);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use the simplier syntax like: Assert.AreEqual(new[,] { { 3, 2, 3 }, { 6, 5.1, 6 } }, y0.numpy());

Assert.AreEqual(y1, y4);
Assert.AreEqual(y2, y5);
}
}
}