From d1f4c136e44f96eb9a38e0011d556edeca6a27e0 Mon Sep 17 00:00:00 2001 From: Sean Chen Date: Fri, 17 May 2024 12:40:39 -0400 Subject: [PATCH] UnitTests --- .../Keras/Regularizers/IRegularizer.cs | 1 + .../Operations/Regularizers/L1.cs | 2 +- src/TensorFlowNET.Keras/Regularizers.cs | 36 ++++++++++++++++--- .../Model/ModelSaveTest.cs | 9 ++++- 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/TensorFlowNET.Core/Keras/Regularizers/IRegularizer.cs b/src/TensorFlowNET.Core/Keras/Regularizers/IRegularizer.cs index 2be231c8..06dbb7c8 100644 --- a/src/TensorFlowNET.Core/Keras/Regularizers/IRegularizer.cs +++ b/src/TensorFlowNET.Core/Keras/Regularizers/IRegularizer.cs @@ -16,6 +16,7 @@ namespace Tensorflow.Keras public interface IRegularizerApi { + IRegularizer GetRegularizerFromName(string name); IRegularizer L1 { get; } IRegularizer L2 { get; } IRegularizer L1L2 { get; } diff --git a/src/TensorFlowNET.Core/Operations/Regularizers/L1.cs b/src/TensorFlowNET.Core/Operations/Regularizers/L1.cs index 8a5c6889..9e061945 100644 --- a/src/TensorFlowNET.Core/Operations/Regularizers/L1.cs +++ b/src/TensorFlowNET.Core/Operations/Regularizers/L1.cs @@ -9,7 +9,7 @@ namespace Tensorflow.Operations.Regularizers float _l1; private readonly Dictionary _config; - public string ClassName => "L2"; + public string ClassName => "L1"; public virtual IDictionary Config => _config; public L1(float l1 = 0.01f) diff --git a/src/TensorFlowNET.Keras/Regularizers.cs b/src/TensorFlowNET.Keras/Regularizers.cs index 402eedd5..73b72a05 100644 --- a/src/TensorFlowNET.Keras/Regularizers.cs +++ b/src/TensorFlowNET.Keras/Regularizers.cs @@ -1,23 +1,51 @@ -namespace Tensorflow.Keras +using Tensorflow.Operations.Regularizers; + +namespace Tensorflow.Keras { public class Regularizers: IRegularizerApi { + private static Dictionary _nameActivationMap; + public IRegularizer l1(float l1 = 0.01f) - => new Tensorflow.Operations.Regularizers.L1(l1); + => new L1(l1); public IRegularizer l2(float l2 = 0.01f) - => new Tensorflow.Operations.Regularizers.L2(l2); + => new L2(l2); //From TF source //# The default value for l1 and l2 are different from the value in l1_l2 //# for backward compatibility reason. Eg, L1L2(l2=0.1) will only have l2 //# and no l1 penalty. public IRegularizer l1l2(float l1 = 0.00f, float l2 = 0.00f) - => new Tensorflow.Operations.Regularizers.L1L2(l1, l2); + => new L1L2(l1, l2); + + static Regularizers() + { + _nameActivationMap = new Dictionary(); + _nameActivationMap["L1"] = new L1(); + _nameActivationMap["L1"] = new L2(); + _nameActivationMap["L1"] = new L1L2(); + } public IRegularizer L1 => l1(); public IRegularizer L2 => l2(); public IRegularizer L1L2 => l1l2(); + + public IRegularizer GetRegularizerFromName(string name) + { + if (name == null) + { + throw new Exception($"Regularizer name cannot be null"); + } + if (!_nameActivationMap.TryGetValue(name, out var res)) + { + throw new Exception($"Regularizer {name} not found"); + } + else + { + return res; + } + } } } diff --git a/test/TensorFlowNET.Keras.UnitTest/Model/ModelSaveTest.cs b/test/TensorFlowNET.Keras.UnitTest/Model/ModelSaveTest.cs index 0854a09d..bf27bb5f 100644 --- a/test/TensorFlowNET.Keras.UnitTest/Model/ModelSaveTest.cs +++ b/test/TensorFlowNET.Keras.UnitTest/Model/ModelSaveTest.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Diagnostics; using Tensorflow.Keras.Engine; +using Tensorflow.Keras.Layers; using Tensorflow.Keras.Models; using Tensorflow.Keras.Optimizers; using Tensorflow.Keras.Saving; @@ -108,7 +109,13 @@ namespace Tensorflow.Keras.UnitTest.Model tf.keras.layers.BatchNormalization(), tf.keras.layers.MaxPooling2D((3, 3), strides:(2, 2)), - tf.keras.layers.Conv2D(256, (5, 5), (1, 1), "same", activation: "relu"), + tf.keras.layers.Conv2D(256, (5, 5), (1, 1), "same", activation: keras.activations.Relu, bias_regularizer:keras.regularizers.L1L2), + tf.keras.layers.BatchNormalization(), + + tf.keras.layers.Conv2D(256, (5, 5), (1, 1), "same", activation: keras.activations.Relu, bias_regularizer:keras.regularizers.L2), + tf.keras.layers.BatchNormalization(), + + tf.keras.layers.Conv2D(256, (5, 5), (1, 1), "same", activation: keras.activations.Relu, bias_regularizer:keras.regularizers.L1), tf.keras.layers.BatchNormalization(), tf.keras.layers.MaxPooling2D((3, 3), (2, 2)),