File tree Expand file tree Collapse file tree 6 files changed +61
-10
lines changed Expand file tree Collapse file tree 6 files changed +61
-10
lines changed Original file line number Diff line number Diff line change @@ -33,12 +33,12 @@ public class DenseArgs : LayerArgs
33
33
/// <summary>
34
34
/// Regularizer function applied to the `kernel` weights matrix.
35
35
/// </summary>
36
- public IInitializer KernelRegularizer { get ; set ; }
36
+ public IRegularizer KernelRegularizer { get ; set ; }
37
37
38
38
/// <summary>
39
39
/// Regularizer function applied to the bias vector.
40
40
/// </summary>
41
- public IInitializer BiasRegularizer { get ; set ; }
41
+ public IRegularizer BiasRegularizer { get ; set ; }
42
42
43
43
/// <summary>
44
44
/// Constraint function applied to the `kernel` weights matrix.
Original file line number Diff line number Diff line change 2
2
{
3
3
public class RegularizerArgs
4
4
{
5
+ public Tensor X { get ; set ; }
6
+
7
+
8
+ public RegularizerArgs ( Tensor x )
9
+ {
10
+ X = x ;
11
+ }
5
12
}
6
13
}
Original file line number Diff line number Diff line change @@ -206,7 +206,7 @@ protected virtual void build(Tensors inputs)
206
206
207
207
protected virtual void add_loss ( Func < Tensor > losses )
208
208
{
209
-
209
+
210
210
}
211
211
212
212
/// <summary>
@@ -217,10 +217,13 @@ protected virtual void add_loss(Func<Tensor> losses)
217
217
/// <param name="regularizer"></param>
218
218
void _handle_weight_regularization ( string name , IVariableV1 variable , IRegularizer regularizer )
219
219
{
220
- add_loss ( ( ) => regularizer . Apply ( new RegularizerArgs
221
- {
222
220
223
- } ) ) ;
221
+ add_loss ( ( ) => tf_with ( ops . name_scope ( name + "/Regularizer" ) , scope =>
222
+ regularizer . Apply ( new RegularizerArgs ( variable . AsTensor ( ) )
223
+ {
224
+
225
+ } )
226
+ ) ) ;
224
227
}
225
228
226
229
/*protected virtual void add_update(Tensor[] updates, bool inputs = false)
Original file line number Diff line number Diff line change
1
+ using System ;
2
+
3
+ namespace Tensorflow . Keras
4
+ {
5
+ public class L1 : IRegularizer
6
+ {
7
+ float l1 ;
8
+
9
+ public L1 ( float l1 = 0.01f )
10
+ {
11
+ this . l1 = l1 ;
12
+ }
13
+
14
+ public Tensor Apply ( RegularizerArgs args )
15
+ {
16
+ return l1 * math_ops . reduce_sum ( math_ops . abs ( args . X ) ) ;
17
+ }
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ using System ;
2
+ using static Tensorflow . Binding ;
3
+ namespace Tensorflow . Keras
4
+ {
5
+ public class L1L2 : IRegularizer
6
+ {
7
+ float l1 ;
8
+ float l2 ;
9
+
10
+ public L1L2 ( float l1 = 0.0f , float l2 = 0.0f )
11
+ {
12
+ this . l1 = l1 ;
13
+ this . l2 = l2 ;
14
+
15
+ }
16
+ public Tensor Apply ( RegularizerArgs args )
17
+ {
18
+ Tensor regularization = tf . constant ( 0.0 , args . X . dtype ) ;
19
+ regularization += l1 * math_ops . reduce_sum ( math_ops . abs ( args . X ) ) ;
20
+ regularization += l2 * math_ops . reduce_sum ( math_ops . square ( args . X ) ) ;
21
+ return regularization ;
22
+ }
23
+ }
24
+ }
Original file line number Diff line number Diff line change 1
- using System ;
2
-
3
- namespace Tensorflow . Keras
1
+ namespace Tensorflow . Keras
4
2
{
5
3
public class L2 : IRegularizer
6
4
{
@@ -13,7 +11,7 @@ public L2(float l2 = 0.01f)
13
11
14
12
public Tensor Apply ( RegularizerArgs args )
15
13
{
16
- throw new NotImplementedException ( ) ;
14
+ return l2 * math_ops . reduce_sum ( math_ops . square ( args . X ) ) ;
17
15
}
18
16
}
19
17
}
You can’t perform that action at this time.
0 commit comments