Skip to content

Commit 7cc6319

Browse files
committed
add tf.nn.l2_loss
1 parent 2815724 commit 7cc6319

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ public Tensor bias_add(Tensor value, IVariableV1 bias, string data_format = null
157157
});
158158
}
159159

160+
public Tensor l2_loss(Tensor t, string name = null)
161+
=> nn_ops.l2_loss(t, name: name);
162+
160163
/// <summary>
161164
/// Local Response Normalization.
162165
/// </summary>

src/TensorFlowNET.Core/Operations/nn_ops.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ public static Tensor softmax(Tensor logits, int axis = -1, string name = null)
128128
return _softmax(logits, gen_nn_ops.softmax, axis, name);
129129
}
130130

131+
public static Tensor l2_loss(Tensor t, string name = null)
132+
=> tf.Context.ExecuteOp("L2Loss", name, new ExecuteOpArgs(t));
133+
131134
public static Tensor leaky_relu(Tensor features, float alpha = 0.2f, string name = null)
132135
{
133136
return tf_with(ops.name_scope(name, "LeakyRelu", new { features, alpha }), scope =>

test/TensorFlowNET.UnitTest/ManagedAPI/ActivationFunctionTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using Tensorflow;
33
using static Tensorflow.Binding;
44

5-
namespace TensorFlowNET.UnitTest.nn_test
5+
namespace TensorFlowNET.UnitTest.NenuralNetwork
66
{
77
[TestClass]
88
public class ActivationFunctionTest : EagerModeTestBase
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using static Tensorflow.Binding;
3+
using Tensorflow.NumPy;
4+
5+
namespace TensorFlowNET.UnitTest.NenuralNetwork
6+
{
7+
[TestClass]
8+
public class NeuralNetworkTest : EagerModeTestBase
9+
{
10+
[TestMethod]
11+
public void l2_loss()
12+
{
13+
var x = tf.Variable(np.array(new[,] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 } }), dtype: tf.float32);
14+
var l2 = tf.nn.l2_loss(x);
15+
Assert.AreEqual(l2.numpy(), 102f);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)