| @@ -40,6 +40,12 @@ Troubleshooting of running example or installation, please refer [here](tensorf | |||||
| There are many examples reside at [TensorFlow.NET Examples](https://github.com/SciSharp/TensorFlow.NET-Examples) written in C# and F#. | There are many examples reside at [TensorFlow.NET Examples](https://github.com/SciSharp/TensorFlow.NET-Examples) written in C# and F#. | ||||
| #### TensorFlow.net Version | |||||
| ` tf.net 0.4x -> tf native 2.4` | |||||
| `tf.net 0.6x -> tf native 2.6` | |||||
| `tf.net 0.7x -> tf native 2.7` | |||||
| `...` | |||||
| #### C# Example | #### C# Example | ||||
| Install TF.NET and TensorFlow binary through NuGet. | Install TF.NET and TensorFlow binary through NuGet. | ||||
| @@ -33,12 +33,12 @@ namespace Tensorflow.Keras.ArgsDefinition | |||||
| /// <summary> | /// <summary> | ||||
| /// Regularizer function applied to the `kernel` weights matrix. | /// Regularizer function applied to the `kernel` weights matrix. | ||||
| /// </summary> | /// </summary> | ||||
| public IInitializer KernelRegularizer { get; set; } | |||||
| public IRegularizer KernelRegularizer { get; set; } | |||||
| /// <summary> | /// <summary> | ||||
| /// Regularizer function applied to the bias vector. | /// Regularizer function applied to the bias vector. | ||||
| /// </summary> | /// </summary> | ||||
| public IInitializer BiasRegularizer { get; set; } | |||||
| public IRegularizer BiasRegularizer { get; set; } | |||||
| /// <summary> | /// <summary> | ||||
| /// Constraint function applied to the `kernel` weights matrix. | /// Constraint function applied to the `kernel` weights matrix. | ||||
| @@ -2,5 +2,12 @@ | |||||
| { | { | ||||
| public class RegularizerArgs | public class RegularizerArgs | ||||
| { | { | ||||
| public Tensor X { get; set; } | |||||
| public RegularizerArgs(Tensor x) | |||||
| { | |||||
| X = x; | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| @@ -206,7 +206,7 @@ namespace Tensorflow.Keras.Engine | |||||
| protected virtual void add_loss(Func<Tensor> losses) | protected virtual void add_loss(Func<Tensor> losses) | ||||
| { | { | ||||
| } | } | ||||
| /// <summary> | /// <summary> | ||||
| @@ -217,10 +217,13 @@ namespace Tensorflow.Keras.Engine | |||||
| /// <param name="regularizer"></param> | /// <param name="regularizer"></param> | ||||
| void _handle_weight_regularization(string name, IVariableV1 variable, IRegularizer regularizer) | void _handle_weight_regularization(string name, IVariableV1 variable, IRegularizer regularizer) | ||||
| { | { | ||||
| add_loss(() => regularizer.Apply(new RegularizerArgs | |||||
| { | |||||
| })); | |||||
| add_loss(() => tf_with(ops.name_scope(name + "/Regularizer"), scope => | |||||
| regularizer.Apply(new RegularizerArgs(variable.AsTensor()) | |||||
| { | |||||
| }) | |||||
| )); | |||||
| } | } | ||||
| /*protected virtual void add_update(Tensor[] updates, bool inputs = false) | /*protected virtual void add_update(Tensor[] updates, bool inputs = false) | ||||
| @@ -0,0 +1,19 @@ | |||||
| using System; | |||||
| namespace Tensorflow.Keras | |||||
| { | |||||
| public class L1 : IRegularizer | |||||
| { | |||||
| float l1; | |||||
| public L1(float l1 = 0.01f) | |||||
| { | |||||
| this.l1 = l1; | |||||
| } | |||||
| public Tensor Apply(RegularizerArgs args) | |||||
| { | |||||
| return l1 * math_ops.reduce_sum(math_ops.abs(args.X)); | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,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; | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -1,6 +1,4 @@ | |||||
| using System; | |||||
| namespace Tensorflow.Keras | |||||
| namespace Tensorflow.Keras | |||||
| { | { | ||||
| public class L2 : IRegularizer | public class L2 : IRegularizer | ||||
| { | { | ||||
| @@ -13,7 +11,7 @@ namespace Tensorflow.Keras | |||||
| public Tensor Apply(RegularizerArgs args) | public Tensor Apply(RegularizerArgs args) | ||||
| { | { | ||||
| throw new NotImplementedException(); | |||||
| return l2 * math_ops.reduce_sum(math_ops.square(args.X)); | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||