forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL1L2.cs
More file actions
24 lines (22 loc) · 626 Bytes
/
L1L2.cs
File metadata and controls
24 lines (22 loc) · 626 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;
using static Tensorflow.Binding;
namespace Tensorflow.Keras
{
public class L1L2 : IRegularizer
{
float l1;
float l2;
public L1L2(float l1 = 0.0f, float l2 = 0.0f)
{
this.l1 = l1;
this.l2 = l2;
}
public Tensor Apply(RegularizerArgs args)
{
Tensor regularization = tf.constant(0.0, args.X.dtype);
regularization += l1 * math_ops.reduce_sum(math_ops.abs(args.X));
regularization += l2 * math_ops.reduce_sum(math_ops.square(args.X));
return regularization;
}
}
}