Skip to content

Commit 3acfc1d

Browse files
committed
tf.math.reduce_euclidean_norm
1 parent 991c6b6 commit 3acfc1d

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

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

+11
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ public Tensor multiply(Tensor x, Tensor y, string name = null)
4646
public Tensor divide_no_nan(Tensor a, Tensor b, string name = null)
4747
=> math_ops.div_no_nan(a, b);
4848

49+
/// <summary>
50+
/// Computes the Euclidean norm of elements across dimensions of a tensor.
51+
/// </summary>
52+
/// <param name="input_tensor">The tensor to reduce. Should have numeric type.</param>
53+
/// <param name="axis">The dimensions to reduce. If `None` (the default), reduces all dimensions.Must be in the range `[-rank(input_tensor), rank(input_tensor))`</param>
54+
/// <param name="keepdims">If true, retains reduced dimensions with length 1.</param>
55+
/// <param name="name">A name for the operation (optional).</param>
56+
/// <returns>The reduced tensor, of the same dtype as the input_tensor.</returns>
57+
public Tensor reduce_euclidean_norm(Tensor input_tensor, Axis? axis = null, bool keepdims = false, string name = null)
58+
=> math_ops.reduce_euclidean_norm(input_tensor, axis: axis, keepdims: keepdims, name);
59+
4960
public Tensor square(Tensor x, string name = null)
5061
=> math_ops.square(x, name: name);
5162

src/TensorFlowNET.Core/Operations/math_ops.cs

+11
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,17 @@ public static Tensor reduce_any(Tensor input_tensor, Axis axis = null, bool keep
587587
return _may_reduce_to_scalar(keepdims, axis, max);
588588
}
589589

590+
public static Tensor reduce_euclidean_norm(Tensor input_tensor, Axis axis = null, bool keepdims = false, string name = null)
591+
{
592+
var r = _ReductionDims(input_tensor, axis);
593+
var distance = tf.Context.ExecuteOp("EuclideanNorm", name,
594+
new ExecuteOpArgs(input_tensor, r).SetAttributes(new
595+
{
596+
keep_dims = keepdims
597+
}));
598+
return _may_reduce_to_scalar(keepdims, axis, distance);
599+
}
600+
590601
public static Tensor reduce_max(Tensor input_tensor, Axis axis = null, bool keepdims = false, string name = null)
591602
{
592603
var r = _ReductionDims(input_tensor, axis);

test/TensorFlowNET.UnitTest/ManagedAPI/MathApiTest.cs

+23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System;
23
using System.Linq;
34
using Tensorflow;
5+
using Tensorflow.NumPy;
46
using static Tensorflow.Binding;
57

68
namespace TensorFlowNET.UnitTest.ManagedAPI
@@ -57,5 +59,26 @@ public void Erf()
5759
var actual = erf.ToArray<float>();
5860
Assert.IsTrue(Equal(expected, actual));
5961
}
62+
63+
[TestMethod]
64+
public void ReduceEuclideanNorm()
65+
{
66+
var x = tf.constant(new[,] { { 1, 2, 3 }, { 1, 1, 1 } });
67+
Assert.AreEqual(tf.math.reduce_euclidean_norm(x).numpy(), 4);
68+
69+
var y = tf.constant(new[,] { { 1, 2, 3 }, { 1, 1, 1 } }, dtype: tf.float32);
70+
Assert.IsTrue(Equal(tf.math.reduce_euclidean_norm(y).numpy(), 4.1231055f));
71+
72+
Assert.IsTrue(Equal(tf.math.reduce_euclidean_norm(y, 0).ToArray<float>(),
73+
new float[] { np.sqrt(2f), np.sqrt(5f), np.sqrt(10f) }));
74+
75+
Assert.IsTrue(Equal(tf.math.reduce_euclidean_norm(y, 1).ToArray<float>(),
76+
new float[] { np.sqrt(14f), np.sqrt(3f) }));
77+
78+
Assert.IsTrue(Equal(tf.math.reduce_euclidean_norm(y, 1, keepdims: true).ToArray<float>(),
79+
new float[] { np.sqrt(14f), np.sqrt(3f) }));
80+
81+
Assert.AreEqual(tf.math.reduce_euclidean_norm(y, (0, 1)).numpy(), np.sqrt(17f));
82+
}
6083
}
6184
}

0 commit comments

Comments
 (0)