Browse Source

Merge branch 'SciSharp:master' into master

pull/943/head
Andy GitHub 3 years ago
parent
commit
7bd93e0b92
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 67 additions and 10 deletions
  1. +6
    -0
      README.md
  2. +2
    -2
      src/TensorFlowNET.Core/Keras/ArgsDefinition/Core/DenseArgs.cs
  3. +7
    -0
      src/TensorFlowNET.Core/Keras/Regularizers/RegularizerArgs.cs
  4. +7
    -4
      src/TensorFlowNET.Keras/Engine/Layer.cs
  5. +19
    -0
      src/TensorFlowNET.Keras/Regularizers/L1.cs
  6. +24
    -0
      src/TensorFlowNET.Keras/Regularizers/L1L2.cs
  7. +2
    -4
      src/TensorFlowNET.Keras/Regularizers/L2.cs

+ 6
- 0
README.md View File

@@ -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#.

#### 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

Install TF.NET and TensorFlow binary through NuGet.


+ 2
- 2
src/TensorFlowNET.Core/Keras/ArgsDefinition/Core/DenseArgs.cs View File

@@ -33,12 +33,12 @@ namespace Tensorflow.Keras.ArgsDefinition
/// <summary>
/// Regularizer function applied to the `kernel` weights matrix.
/// </summary>
public IInitializer KernelRegularizer { get; set; }
public IRegularizer KernelRegularizer { get; set; }

/// <summary>
/// Regularizer function applied to the bias vector.
/// </summary>
public IInitializer BiasRegularizer { get; set; }
public IRegularizer BiasRegularizer { get; set; }

/// <summary>
/// Constraint function applied to the `kernel` weights matrix.


+ 7
- 0
src/TensorFlowNET.Core/Keras/Regularizers/RegularizerArgs.cs View File

@@ -2,5 +2,12 @@
{
public class RegularizerArgs
{
public Tensor X { get; set; }


public RegularizerArgs(Tensor x)
{
X = x;
}
}
}

+ 7
- 4
src/TensorFlowNET.Keras/Engine/Layer.cs View File

@@ -206,7 +206,7 @@ namespace Tensorflow.Keras.Engine

protected virtual void add_loss(Func<Tensor> losses)
{
}

/// <summary>
@@ -217,10 +217,13 @@ namespace Tensorflow.Keras.Engine
/// <param name="regularizer"></param>
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)


+ 19
- 0
src/TensorFlowNET.Keras/Regularizers/L1.cs View File

@@ -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));
}
}
}

+ 24
- 0
src/TensorFlowNET.Keras/Regularizers/L1L2.cs View File

@@ -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;
}
}
}

+ 2
- 4
src/TensorFlowNET.Keras/Regularizers/L2.cs View File

@@ -1,6 +1,4 @@
using System;

namespace Tensorflow.Keras
namespace Tensorflow.Keras
{
public class L2 : IRegularizer
{
@@ -13,7 +11,7 @@ namespace Tensorflow.Keras

public Tensor Apply(RegularizerArgs args)
{
throw new NotImplementedException();
return l2 * math_ops.reduce_sum(math_ops.square(args.X));
}
}
}

Loading…
Cancel
Save