From 1c3529267e03391c72c6b4fce184b8a257de9561 Mon Sep 17 00:00:00 2001 From: Deepak Kumar Date: Sat, 11 Jan 2020 23:24:34 +1030 Subject: [PATCH 1/5] Optimizer and Regularizer classes skeleton added --- TensorFlow.NET.sln | 10 +++++ src/TensorFlowNET.Keras/Engine/CallContext.cs | 2 +- src/TensorFlowNET.Keras/Engine/Saving.cs | 10 ----- src/TensorFlowNET.Keras/KwArgs.cs | 43 +++++++++++++++++++ src/TensorFlowNET.Keras/Optimizer/Adadelta.cs | 25 +++++++++++ src/TensorFlowNET.Keras/Optimizer/Adagrad.cs | 25 +++++++++++ src/TensorFlowNET.Keras/Optimizer/Adam.cs | 25 +++++++++++ src/TensorFlowNET.Keras/Optimizer/Adamax.cs | 25 +++++++++++ src/TensorFlowNET.Keras/Optimizer/Nadam.cs | 25 +++++++++++ .../Optimizer/Optimizer.cs | 36 ++++++++++++++++ src/TensorFlowNET.Keras/Optimizer/RMSprop.cs | 25 +++++++++++ src/TensorFlowNET.Keras/Optimizer/SGD.cs | 25 +++++++++++ .../{BaseOptimizerV2.cs => OptimizerV2.cs} | 2 +- src/TensorFlowNET.Keras/Regularizers/L1L2.cs | 17 +++++++- .../Regularizers/Regularizer.cs | 32 +++++++++++++- .../OptimizerTest.cs | 14 ++++++ .../Tensorflow.Keras.UnitTest.csproj | 20 +++++++++ 17 files changed, 347 insertions(+), 14 deletions(-) delete mode 100644 src/TensorFlowNET.Keras/Engine/Saving.cs create mode 100644 src/TensorFlowNET.Keras/KwArgs.cs create mode 100644 src/TensorFlowNET.Keras/Optimizer/Adadelta.cs create mode 100644 src/TensorFlowNET.Keras/Optimizer/Adagrad.cs create mode 100644 src/TensorFlowNET.Keras/Optimizer/Adam.cs create mode 100644 src/TensorFlowNET.Keras/Optimizer/Adamax.cs create mode 100644 src/TensorFlowNET.Keras/Optimizer/Nadam.cs create mode 100644 src/TensorFlowNET.Keras/Optimizer/Optimizer.cs create mode 100644 src/TensorFlowNET.Keras/Optimizer/RMSprop.cs create mode 100644 src/TensorFlowNET.Keras/Optimizer/SGD.cs rename src/TensorFlowNET.Keras/OptimizersV2/{BaseOptimizerV2.cs => OptimizerV2.cs} (83%) create mode 100644 test/Tensorflow.Keras.UnitTest/OptimizerTest.cs create mode 100644 test/Tensorflow.Keras.UnitTest/Tensorflow.Keras.UnitTest.csproj diff --git a/TensorFlow.NET.sln b/TensorFlow.NET.sln index 5562853c..e599d8b9 100644 --- a/TensorFlow.NET.sln +++ b/TensorFlow.NET.sln @@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTest", "test\TensorFlow EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Keras", "src\TensorFlowNET.Keras\Tensorflow.Keras.csproj", "{6268B461-486A-460B-9B3C-86493CBBAAF7}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tensorflow.Keras.UnitTest", "test\Tensorflow.Keras.UnitTest\Tensorflow.Keras.UnitTest.csproj", "{EB92DD90-6346-41FB-B967-2B33A860AD98}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -51,6 +53,14 @@ Global {6268B461-486A-460B-9B3C-86493CBBAAF7}.Release|Any CPU.Build.0 = Release|Any CPU {6268B461-486A-460B-9B3C-86493CBBAAF7}.Release|x64.ActiveCfg = Release|Any CPU {6268B461-486A-460B-9B3C-86493CBBAAF7}.Release|x64.Build.0 = Release|Any CPU + {EB92DD90-6346-41FB-B967-2B33A860AD98}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EB92DD90-6346-41FB-B967-2B33A860AD98}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EB92DD90-6346-41FB-B967-2B33A860AD98}.Debug|x64.ActiveCfg = Debug|Any CPU + {EB92DD90-6346-41FB-B967-2B33A860AD98}.Debug|x64.Build.0 = Debug|Any CPU + {EB92DD90-6346-41FB-B967-2B33A860AD98}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EB92DD90-6346-41FB-B967-2B33A860AD98}.Release|Any CPU.Build.0 = Release|Any CPU + {EB92DD90-6346-41FB-B967-2B33A860AD98}.Release|x64.ActiveCfg = Release|Any CPU + {EB92DD90-6346-41FB-B967-2B33A860AD98}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/TensorFlowNET.Keras/Engine/CallContext.cs b/src/TensorFlowNET.Keras/Engine/CallContext.cs index 5547c6de..8cc38df7 100644 --- a/src/TensorFlowNET.Keras/Engine/CallContext.cs +++ b/src/TensorFlowNET.Keras/Engine/CallContext.cs @@ -20,7 +20,7 @@ namespace Tensorflow.Keras.Engine } - public void enter(Layer layer, Tensor[] inputs, Graph build_graph, bool training, Saving saving = null) => throw new NotImplementedException(); + public void enter(Layer layer, Tensor[] inputs, Graph build_graph, bool training) => throw new NotImplementedException(); public bool training_arg_passed_to_call(string[] argspec, Dictionary args, Dictionary kwargs) => throw new NotImplementedException(); diff --git a/src/TensorFlowNET.Keras/Engine/Saving.cs b/src/TensorFlowNET.Keras/Engine/Saving.cs deleted file mode 100644 index 8ba804c3..00000000 --- a/src/TensorFlowNET.Keras/Engine/Saving.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Tensorflow.Keras.Engine -{ - public class Saving - { - } -} diff --git a/src/TensorFlowNET.Keras/KwArgs.cs b/src/TensorFlowNET.Keras/KwArgs.cs new file mode 100644 index 00000000..11a90dd8 --- /dev/null +++ b/src/TensorFlowNET.Keras/KwArgs.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras +{ + public class KwArgs + { + private Dictionary args = new Dictionary(); + + public object this[string name] + { + get + { + return args.ContainsKey(name) ? args[name] : null; + } + set + { + args[name] = value; + } + } + + public T Get(string name) + { + if (!args.ContainsKey(name)) + return default(T); + + return (T)args[name]; + } + + public static explicit operator KwArgs(ValueTuple[] p) + { + KwArgs kwArgs = new KwArgs(); + kwArgs.args = new Dictionary(); + foreach (var item in p) + { + kwArgs.args[item.Item1] = item.Item2; + } + + return kwArgs; + } + } +} diff --git a/src/TensorFlowNET.Keras/Optimizer/Adadelta.cs b/src/TensorFlowNET.Keras/Optimizer/Adadelta.cs new file mode 100644 index 00000000..e5d72976 --- /dev/null +++ b/src/TensorFlowNET.Keras/Optimizer/Adadelta.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras +{ + public class Adadelta : Optimizer + { + public Adadelta(float lr= 0.01f, float rho = 0.95f, float? epsilon = null, float decay = 0) : base(null) + { + throw new NotImplementedException(); + } + + public override Tensor[] get_updates(Tensor loss, variables @params) + { + throw new NotImplementedException(); + } + + public override Hashtable get_config() + { + throw new NotImplementedException(); + } + } +} diff --git a/src/TensorFlowNET.Keras/Optimizer/Adagrad.cs b/src/TensorFlowNET.Keras/Optimizer/Adagrad.cs new file mode 100644 index 00000000..4353d79b --- /dev/null +++ b/src/TensorFlowNET.Keras/Optimizer/Adagrad.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras +{ + public class Adagrad : Optimizer + { + public Adagrad(float lr= 0.01f, float? epsilon = null, float decay = 0) : base(null) + { + throw new NotImplementedException(); + } + + public override Tensor[] get_updates(Tensor loss, variables @params) + { + throw new NotImplementedException(); + } + + public override Hashtable get_config() + { + throw new NotImplementedException(); + } + } +} diff --git a/src/TensorFlowNET.Keras/Optimizer/Adam.cs b/src/TensorFlowNET.Keras/Optimizer/Adam.cs new file mode 100644 index 00000000..15053284 --- /dev/null +++ b/src/TensorFlowNET.Keras/Optimizer/Adam.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras +{ + public class Adam : Optimizer + { + public Adam(float lr= 0.001f, float beta_1 = 0.9f, float beta_2 = 0.99f, float? epsilon = null, float decay = 0) : base(null) + { + throw new NotImplementedException(); + } + + public override Tensor[] get_updates(Tensor loss, variables @params) + { + throw new NotImplementedException(); + } + + public override Hashtable get_config() + { + throw new NotImplementedException(); + } + } +} diff --git a/src/TensorFlowNET.Keras/Optimizer/Adamax.cs b/src/TensorFlowNET.Keras/Optimizer/Adamax.cs new file mode 100644 index 00000000..9581c6dc --- /dev/null +++ b/src/TensorFlowNET.Keras/Optimizer/Adamax.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras +{ + public class Adamax : Optimizer + { + public Adamax(float lr = 0.002f, float beta_1 = 0.9f, float beta_2 = 0.999f, float? epsilon = null, float decay = 0) : base(null) + { + throw new NotImplementedException(); + } + + public override Tensor[] get_updates(Tensor loss, variables @params) + { + throw new NotImplementedException(); + } + + public override Hashtable get_config() + { + throw new NotImplementedException(); + } + } +} diff --git a/src/TensorFlowNET.Keras/Optimizer/Nadam.cs b/src/TensorFlowNET.Keras/Optimizer/Nadam.cs new file mode 100644 index 00000000..b933570f --- /dev/null +++ b/src/TensorFlowNET.Keras/Optimizer/Nadam.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras +{ + public class Nadam : Optimizer + { + public Nadam(float lr = 0.002f, float beta_1 = 0.9f, float beta_2 = 0.999f, float? epsilon = null, float schedule_decay = 0.004f) : base(null) + { + throw new NotImplementedException(); + } + + public override Tensor[] get_updates(Tensor loss, variables @params) + { + throw new NotImplementedException(); + } + + public override Hashtable get_config() + { + throw new NotImplementedException(); + } + } +} diff --git a/src/TensorFlowNET.Keras/Optimizer/Optimizer.cs b/src/TensorFlowNET.Keras/Optimizer/Optimizer.cs new file mode 100644 index 00000000..14223c5e --- /dev/null +++ b/src/TensorFlowNET.Keras/Optimizer/Optimizer.cs @@ -0,0 +1,36 @@ +using NumSharp; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras +{ + public class Optimizer + { + public Optimizer(KwArgs kwargs) + { + throw new NotImplementedException(); + } + + public virtual Tensor[] get_updates(Tensor loss, variables @params) + { + return null; + } + + public virtual Tensor[] get_gradients(Tensor loss, variables @params) => throw new NotImplementedException(); + + public virtual void set_weights(NDArray[] weights) => throw new NotImplementedException(); + + public virtual NDArray[] get_weights() => throw new NotImplementedException(); + + public virtual Hashtable get_config() => throw new NotImplementedException(); + + public static string serialize(Optimizer optimizer) => throw new NotImplementedException(); + + public static string deserialize(string config, object custom_objects = null) => throw new NotImplementedException(); + + public static Optimizer get(object identifier) => throw new NotImplementedException(); + + } +} diff --git a/src/TensorFlowNET.Keras/Optimizer/RMSprop.cs b/src/TensorFlowNET.Keras/Optimizer/RMSprop.cs new file mode 100644 index 00000000..79894831 --- /dev/null +++ b/src/TensorFlowNET.Keras/Optimizer/RMSprop.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras +{ + public class RMSprop : Optimizer + { + public RMSprop(float lr= 0.01f, float rho = 0f, float? epsilon = null, float decay = 0) : base(null) + { + throw new NotImplementedException(); + } + + public override Tensor[] get_updates(Tensor loss, variables @params) + { + throw new NotImplementedException(); + } + + public override Hashtable get_config() + { + throw new NotImplementedException(); + } + } +} diff --git a/src/TensorFlowNET.Keras/Optimizer/SGD.cs b/src/TensorFlowNET.Keras/Optimizer/SGD.cs new file mode 100644 index 00000000..17063c54 --- /dev/null +++ b/src/TensorFlowNET.Keras/Optimizer/SGD.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras +{ + public class SGD : Optimizer + { + public SGD(float lr= 0.01f, float momentum= 0, float decay= 0, bool nesterov= false) : base(null) + { + throw new NotImplementedException(); + } + + public override Tensor[] get_updates(Tensor loss, variables @params) + { + throw new NotImplementedException(); + } + + public override Hashtable get_config() + { + throw new NotImplementedException(); + } + } +} diff --git a/src/TensorFlowNET.Keras/OptimizersV2/BaseOptimizerV2.cs b/src/TensorFlowNET.Keras/OptimizersV2/OptimizerV2.cs similarity index 83% rename from src/TensorFlowNET.Keras/OptimizersV2/BaseOptimizerV2.cs rename to src/TensorFlowNET.Keras/OptimizersV2/OptimizerV2.cs index e84acc00..ecb9780a 100644 --- a/src/TensorFlowNET.Keras/OptimizersV2/BaseOptimizerV2.cs +++ b/src/TensorFlowNET.Keras/OptimizersV2/OptimizerV2.cs @@ -4,7 +4,7 @@ using System.Text; namespace Tensorflow.Keras.OptimizersV2 { - class BaseOptimizerV2 + class OptimizerV2 { } } diff --git a/src/TensorFlowNET.Keras/Regularizers/L1L2.cs b/src/TensorFlowNET.Keras/Regularizers/L1L2.cs index 4984790c..927b3319 100644 --- a/src/TensorFlowNET.Keras/Regularizers/L1L2.cs +++ b/src/TensorFlowNET.Keras/Regularizers/L1L2.cs @@ -1,10 +1,25 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Text; namespace Tensorflow.Keras.Regularizers { - class L1L2 + public class L1L2 : Regularizer { + public L1L2(float l1 = 0f, float l2 = 0f) + { + throw new NotImplementedException(); + } + + public override float call(Tensor x) + { + throw new NotImplementedException(); + } + + public override Hashtable get_config() + { + throw new NotImplementedException(); + } } } diff --git a/src/TensorFlowNET.Keras/Regularizers/Regularizer.cs b/src/TensorFlowNET.Keras/Regularizers/Regularizer.cs index ef670912..047b035f 100644 --- a/src/TensorFlowNET.Keras/Regularizers/Regularizer.cs +++ b/src/TensorFlowNET.Keras/Regularizers/Regularizer.cs @@ -1,10 +1,40 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Text; namespace Tensorflow.Keras.Regularizers { - public class Regularizer + public abstract class Regularizer { + public virtual float call(Tensor x) + { + return 0f; + } + + public static Regularizer from_config(Hashtable hashtable) => throw new NotImplementedException(); + + public virtual Hashtable get_config() => throw new NotImplementedException(); + + public static Regularizer l1(float l = 0.01f) + { + return new L1L2(l1: l); + } + + public static Regularizer l2(float l = 0.01f) + { + return new L1L2(l2: l); + } + + public static Regularizer l1_l2(float l1 = 0.01f, float l2 = 0.01f) + { + return new L1L2(l1, l2); + } + + public static string serialize(Regularizer regularizer) => throw new NotImplementedException(); + + public static string deserialize(string config, dynamic custom_objects = null) => throw new NotImplementedException(); + + public static Regularizer get(object identifier) => throw new NotImplementedException(); } } diff --git a/test/Tensorflow.Keras.UnitTest/OptimizerTest.cs b/test/Tensorflow.Keras.UnitTest/OptimizerTest.cs new file mode 100644 index 00000000..1aad1868 --- /dev/null +++ b/test/Tensorflow.Keras.UnitTest/OptimizerTest.cs @@ -0,0 +1,14 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Collections.Generic; + +namespace Tensorflow.Keras.UnitTest +{ + [TestClass] + public class OptimizerTest + { + [TestMethod] + public void BaseConstruct() + { + } + } +} diff --git a/test/Tensorflow.Keras.UnitTest/Tensorflow.Keras.UnitTest.csproj b/test/Tensorflow.Keras.UnitTest/Tensorflow.Keras.UnitTest.csproj new file mode 100644 index 00000000..41dbf2e4 --- /dev/null +++ b/test/Tensorflow.Keras.UnitTest/Tensorflow.Keras.UnitTest.csproj @@ -0,0 +1,20 @@ + + + + netcoreapp3.1 + + false + + + + + + + + + + + + + + From 4deb320885afedd3aa859b4c2ea1f51941245b39 Mon Sep 17 00:00:00 2001 From: Deepak Kumar Date: Sun, 12 Jan 2020 02:06:43 +1030 Subject: [PATCH 2/5] Sequential, Metric base and Metric Util class skeleton methods added --- src/TensorFlowNET.Keras/Args.cs | 29 +++++++++++ src/TensorFlowNET.Keras/Engine/Node.cs | 2 +- src/TensorFlowNET.Keras/Engine/Sequential.cs | 3 +- src/TensorFlowNET.Keras/Metrics/Metric.cs | 31 ++++++++++- src/TensorFlowNET.Keras/Models.cs | 36 +++++++++++-- src/TensorFlowNET.Keras/Ops.cs | 10 ---- src/TensorFlowNET.Keras/Utils/MetricsUtils.cs | 52 ++++++++++++++++++- 7 files changed, 145 insertions(+), 18 deletions(-) create mode 100644 src/TensorFlowNET.Keras/Args.cs delete mode 100644 src/TensorFlowNET.Keras/Ops.cs diff --git a/src/TensorFlowNET.Keras/Args.cs b/src/TensorFlowNET.Keras/Args.cs new file mode 100644 index 00000000..f2d9d27b --- /dev/null +++ b/src/TensorFlowNET.Keras/Args.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras +{ + public class Args + { + private List args = new List(); + + public object this[int index] + { + get + { + return args.Count < index ? args[index] : null; + } + } + + public T Get(int index) + { + return args.Count < index ? (T)args[index] : default(T); + } + + public void Add(T arg) + { + args.Add(arg); + } + } +} diff --git a/src/TensorFlowNET.Keras/Engine/Node.cs b/src/TensorFlowNET.Keras/Engine/Node.cs index 47befd5b..d74e98b6 100644 --- a/src/TensorFlowNET.Keras/Engine/Node.cs +++ b/src/TensorFlowNET.Keras/Engine/Node.cs @@ -4,7 +4,7 @@ using System.Text; namespace Tensorflow.Keras.Engine { - class Node + public class Node { } } diff --git a/src/TensorFlowNET.Keras/Engine/Sequential.cs b/src/TensorFlowNET.Keras/Engine/Sequential.cs index 4de7a34c..611ab18b 100644 --- a/src/TensorFlowNET.Keras/Engine/Sequential.cs +++ b/src/TensorFlowNET.Keras/Engine/Sequential.cs @@ -4,7 +4,8 @@ using System.Text; namespace Tensorflow.Keras.Engine { - class Sequential + public class Sequential { + } } diff --git a/src/TensorFlowNET.Keras/Metrics/Metric.cs b/src/TensorFlowNET.Keras/Metrics/Metric.cs index 394d76f0..83af1fde 100644 --- a/src/TensorFlowNET.Keras/Metrics/Metric.cs +++ b/src/TensorFlowNET.Keras/Metrics/Metric.cs @@ -1,10 +1,39 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Text; namespace Tensorflow.Keras.Metrics { - public abstract class Metric + public abstract class Metric : Layers.Layer { + public string dtype + { + get + { + throw new NotImplementedException(); + } + } + + public Metric(string name, string dtype) + { + throw new NotImplementedException(); + } + + public void __new__ (Metric cls, Args args, KwArgs kwargs) => throw new NotImplementedException(); + + public Tensor __call__(Metric cls, Args args, KwArgs kwargs) => throw new NotImplementedException(); + + public virtual Hashtable get_config() => throw new NotImplementedException(); + + public virtual void reset_states() => throw new NotImplementedException(); + + public abstract void update_state(Args args, KwArgs kwargs); + + public abstract Tensor result(); + + public void add_weight(string name, TensorShape shape= null, VariableAggregation aggregation= VariableAggregation.Sum, + VariableSynchronization synchronization = VariableSynchronization.OnRead, Initializers.Initializer initializer= null, + string dtype= null) => throw new NotImplementedException(); } } diff --git a/src/TensorFlowNET.Keras/Models.cs b/src/TensorFlowNET.Keras/Models.cs index 03c06ac6..0ee59976 100644 --- a/src/TensorFlowNET.Keras/Models.cs +++ b/src/TensorFlowNET.Keras/Models.cs @@ -1,14 +1,42 @@ -using System; +using Keras.Layers; +using System; +using System.Collections; using System.Collections.Generic; using System.Text; +using Tensorflow.Keras.Engine; namespace Tensorflow.Keras { class Models { - public class Model : Keras.Engine.Training.Model - { + public class Model : Keras.Engine.Training.Model{} - } + public static Layer share_weights(Layer layer) => throw new NotImplementedException(); + + private static Layer _clone_layer(Layer layer) => throw new NotImplementedException(); + + private static Layer _insert_ancillary_layers(Model model, Layer ancillary_layers, string[] metrics_names, Node[] new_nodes) => throw new NotImplementedException(); + + private static Node[] _make_new_nodes(Node[] nodes_by_depth, Func layer_fn, Hashtable layer_map, Hashtable tensor_map) => throw new NotImplementedException(); + + private static Model _clone_functional_model(Model model, Tensor[] input_tensors = null, Func layer_fn = null) => throw new NotImplementedException(); + + private static (Hashtable, Layer[]) _clone_layers_and_model_config(Model model, Layer[] input_layers, Func layer_fn) => throw new NotImplementedException(); + + private static (Layer[], Layer[]) _remove_ancillary_layers(Model model, Hashtable layer_map, Layer[] layers) => throw new NotImplementedException(); + + private static Sequential _clone_sequential_model(Model model, Tensor[] input_tensors = null, Func layer_fn = null) => throw new NotImplementedException(); + + public static Model clone_model(Model model, Tensor[] input_tensors = null, Func layer_fn = null) => throw new NotImplementedException(); + + private static void _in_place_subclassed_model_reset(Model model) => throw new NotImplementedException(); + + private static void _reset_build_compile_trackers(Model model) => throw new NotImplementedException(); + + public static void in_place_subclassed_model_state_restoration(Model model) => throw new NotImplementedException(); + + public static void clone_and_build_model(Model model, Tensor[] input_tensors= null, Tensor[] target_tensors= null, object custom_objects= null, + bool compile_clone= true, bool in_place_reset= false, VariableV1 optimizer_iterations= null, Hashtable optimizer_config= null) + => throw new NotImplementedException(); } } diff --git a/src/TensorFlowNET.Keras/Ops.cs b/src/TensorFlowNET.Keras/Ops.cs deleted file mode 100644 index 921a1e65..00000000 --- a/src/TensorFlowNET.Keras/Ops.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Tensorflow.Keras -{ - class Ops - { - } -} diff --git a/src/TensorFlowNET.Keras/Utils/MetricsUtils.cs b/src/TensorFlowNET.Keras/Utils/MetricsUtils.cs index 7273f140..1e51b099 100644 --- a/src/TensorFlowNET.Keras/Utils/MetricsUtils.cs +++ b/src/TensorFlowNET.Keras/Utils/MetricsUtils.cs @@ -1,10 +1,60 @@ using System; using System.Collections.Generic; +using System.Reflection; using System.Text; namespace Tensorflow.Keras.Utils { - class MetricsUtils + public class MetricsUtils { + public static class Reduction + { + public const string SUM = "sum"; + public const string SUM_OVER_BATCH_SIZE = "sum_over_batch_size"; + public const string WEIGHTED_MEAN = "weighted_mean"; + } + + public static class ConfusionMatrix + { + public const string TRUE_POSITIVES = "tp"; + public const string FALSE_POSITIVES = "fp"; + public const string TRUE_NEGATIVES = "tn"; + public const string FALSE_NEGATIVES = "fn"; + } + + public static class AUCCurve + { + public const string ROC = "ROC"; + public const string PR = "PR"; + + public static string from_str(string key) => throw new NotImplementedException(); + } + + public static class AUCSummationMethod + { + public const string INTERPOLATION = "interpolation"; + public const string MAJORING = "majoring"; + public const string MINORING = "minoring"; + + public static string from_str(string key) => throw new NotImplementedException(); + } + + public static dynamic update_state_wrapper(Func> update_state_fn) => throw new NotImplementedException(); + + public static dynamic result_wrapper(Func result_fn) => throw new NotImplementedException(); + + public static WeakReference weakmethod(MethodInfo method) => throw new NotImplementedException(); + + public static void assert_thresholds_range(float[] thresholds) => throw new NotImplementedException(); + + public static void parse_init_thresholds(float[] thresholds, float default_threshold = 0.5f) => throw new NotImplementedException(); + + public static Operation update_confusion_matrix_variables(variables variables_to_update, Tensor y_true, Tensor y_pred, float[] thresholds, + int? top_k= null,int? class_id= null, Tensor sample_weight= null, bool multi_label= false, + Tensor label_weights= null) => throw new NotImplementedException(); + + private static Tensor _filter_top_k(Tensor x, int k) => throw new NotImplementedException(); + + private static (Tensor[], Tensor) ragged_assert_compatible_and_get_flat_values(Tensor[] values, Tensor mask = null) => throw new NotImplementedException(); } } From 2706b79edc06a2fe6739f5a662505e5f5cdadead Mon Sep 17 00:00:00 2001 From: Deepak Kumar Date: Mon, 13 Jan 2020 08:39:34 +1030 Subject: [PATCH 3/5] Mteric and losses skeletonized --- src/TensorFlowNET.Keras/Losses/Loss.cs | 31 +++++++++++++++++ src/TensorFlowNET.Keras/Metrics/AUC.cs | 33 ++++++++++++++++++- src/TensorFlowNET.Keras/Metrics/Accuracy.cs | 6 +++- .../Metrics/BinaryAccuracy.cs | 11 ++++++- .../Metrics/CategoricalAccuracy.cs | 6 +++- .../Metrics/CategoricalHinge.cs | 6 +++- .../Metrics/CosineSimilarity.cs | 11 ++++++- .../Metrics/FalseNegatives.cs | 6 +++- .../Metrics/FalsePositives.cs | 6 +++- src/TensorFlowNET.Keras/Metrics/Hinge.cs | 6 +++- .../Metrics/LogCoshError.cs | 6 +++- src/TensorFlowNET.Keras/Metrics/Mean.cs | 7 +++- .../Metrics/MeanAbsoluteError.cs | 6 +++- .../Metrics/MeanAbsolutePercentageError.cs | 6 +++- .../Metrics/MeanMetricWrapper.cs | 17 +++++++++- .../Metrics/MeanRelativeError.cs | 22 ++++++++++++- .../Metrics/MeanSquaredError.cs | 6 +++- .../Metrics/MeanSquaredLogarithmicError.cs | 6 +++- src/TensorFlowNET.Keras/Metrics/Metric.cs | 24 ++++++++++++++ src/TensorFlowNET.Keras/Metrics/Poisson.cs | 6 +++- src/TensorFlowNET.Keras/Metrics/Precision.cs | 33 ++++++++++++++++++- .../Metrics/PrecisionAtRecall.cs | 17 +++++++++- src/TensorFlowNET.Keras/Metrics/Recall.cs | 33 ++++++++++++++++++- src/TensorFlowNET.Keras/Metrics/Reduce.cs | 17 +++++++++- .../Metrics/RootMeanSquaredError.cs | 6 +++- .../Metrics/SensitivityAtSpecificity.cs | 17 +++++++++- .../Metrics/SensitivitySpecificityBase.cs | 21 +++++++++++- .../Metrics/SparseCategoricalAccuracy.cs | 7 +++- .../Metrics/SparseTopKCategoricalAccuracy.cs | 12 ++++++- .../Metrics/SquaredHinge.cs | 6 +++- src/TensorFlowNET.Keras/Metrics/Sum.cs | 6 +++- .../Metrics/TopKCategoricalAccuracy.cs | 11 ++++++- .../Metrics/TrueNegatives.cs | 6 +++- .../Metrics/TruePositives.cs | 6 +++- .../Metrics/_ConfusionMatrixConditionCount.cs | 29 +++++++++++++++- .../Optimizer/Optimizer.cs | 2 +- 36 files changed, 423 insertions(+), 34 deletions(-) diff --git a/src/TensorFlowNET.Keras/Losses/Loss.cs b/src/TensorFlowNET.Keras/Losses/Loss.cs index 49246027..8acee5ba 100644 --- a/src/TensorFlowNET.Keras/Losses/Loss.cs +++ b/src/TensorFlowNET.Keras/Losses/Loss.cs @@ -6,5 +6,36 @@ namespace Tensorflow.Keras.Losses { public abstract class Loss { + public static Tensor mean_squared_error(Tensor y_true, Tensor y_pred) => throw new NotImplementedException(); + + public static Tensor mean_absolute_error(Tensor y_true, Tensor y_pred) => throw new NotImplementedException(); + + public static Tensor mean_absolute_percentage_error(Tensor y_true, Tensor y_pred) => throw new NotImplementedException(); + + public static Tensor mean_squared_logarithmic_error(Tensor y_true, Tensor y_pred) => throw new NotImplementedException(); + + public static Tensor _maybe_convert_labels(Tensor y_true) => throw new NotImplementedException(); + + public static Tensor squared_hinge(Tensor y_true, Tensor y_pred) => throw new NotImplementedException(); + + public static Tensor hinge(Tensor y_true, Tensor y_pred) => throw new NotImplementedException(); + + public static Tensor categorical_hinge(Tensor y_true, Tensor y_pred) => throw new NotImplementedException(); + + public static Tensor huber_loss(Tensor y_true, Tensor y_pred, float delta = 1) => throw new NotImplementedException(); + + public static Tensor logcosh(Tensor y_true, Tensor y_pred) => throw new NotImplementedException(); + + public static Tensor categorical_crossentropy(Tensor y_true, Tensor y_pred, bool from_logits = false, float label_smoothing = 0) => throw new NotImplementedException(); + + public static Tensor sparse_categorical_crossentropy(Tensor y_true, Tensor y_pred, bool from_logits = false, float axis = -1) => throw new NotImplementedException(); + + public static Tensor binary_crossentropy(Tensor y_true, Tensor y_pred, bool from_logits = false, float label_smoothing = 0) => throw new NotImplementedException(); + + public static Tensor kullback_leibler_divergence(Tensor y_true, Tensor y_pred) => throw new NotImplementedException(); + + public static Tensor poisson(Tensor y_true, Tensor y_pred) => throw new NotImplementedException(); + + public static Tensor cosine_similarity(Tensor y_true, Tensor y_pred, int axis = -1) => throw new NotImplementedException(); } } diff --git a/src/TensorFlowNET.Keras/Metrics/AUC.cs b/src/TensorFlowNET.Keras/Metrics/AUC.cs index e7e03626..c34f61c8 100644 --- a/src/TensorFlowNET.Keras/Metrics/AUC.cs +++ b/src/TensorFlowNET.Keras/Metrics/AUC.cs @@ -1,10 +1,41 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Text; namespace Tensorflow.Keras.Metrics { - class AUC + public class AUC : Metric { + public AUC(int num_thresholds= 200, string curve= "ROC", string summation_method= "interpolation", + string name= null, string dtype= null, float thresholds= 0.5f, + bool multi_label= false, Tensor label_weights= null) : base(name, dtype) + { + throw new NotImplementedException(); + } + + private void _build(TensorShape shape) => throw new NotImplementedException(); + + public Tensor interpolate_pr_auc() => throw new NotImplementedException(); + + public override Tensor result() + { + throw new NotImplementedException(); + } + + public override void update_state(Args args, KwArgs kwargs) + { + throw new NotImplementedException(); + } + + public override void reset_states() + { + throw new NotImplementedException(); + } + + public override Hashtable get_config() + { + throw new NotImplementedException(); + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/Accuracy.cs b/src/TensorFlowNET.Keras/Metrics/Accuracy.cs index 66a774e5..cb58ae91 100644 --- a/src/TensorFlowNET.Keras/Metrics/Accuracy.cs +++ b/src/TensorFlowNET.Keras/Metrics/Accuracy.cs @@ -4,7 +4,11 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class Accuracy + public class Accuracy : MeanMetricWrapper { + public Accuracy(string name = "accuracy", string dtype = null) + : base(Metric.accuracy, name, dtype) + { + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/BinaryAccuracy.cs b/src/TensorFlowNET.Keras/Metrics/BinaryAccuracy.cs index b6e564ff..682ed236 100644 --- a/src/TensorFlowNET.Keras/Metrics/BinaryAccuracy.cs +++ b/src/TensorFlowNET.Keras/Metrics/BinaryAccuracy.cs @@ -4,7 +4,16 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class BinaryAccuracy + public class BinaryAccuracy : MeanMetricWrapper { + public BinaryAccuracy(string name = "binary_accuracy", string dtype = null, float threshold = 0.5f) + : base(Fn, name, dtype) + { + } + + internal static Tensor Fn(Tensor y_true, Tensor y_pred) + { + return Metric.binary_accuracy(y_true, y_pred); + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/CategoricalAccuracy.cs b/src/TensorFlowNET.Keras/Metrics/CategoricalAccuracy.cs index cdc62dd9..64b31f64 100644 --- a/src/TensorFlowNET.Keras/Metrics/CategoricalAccuracy.cs +++ b/src/TensorFlowNET.Keras/Metrics/CategoricalAccuracy.cs @@ -4,7 +4,11 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class CategoricalAccuracy + public class CategoricalAccuracy : MeanMetricWrapper { + public CategoricalAccuracy(string name = "categorical_accuracy", string dtype = null) + : base(Metric.categorical_accuracy, name, dtype) + { + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/CategoricalHinge.cs b/src/TensorFlowNET.Keras/Metrics/CategoricalHinge.cs index 2f50ea76..1f82d725 100644 --- a/src/TensorFlowNET.Keras/Metrics/CategoricalHinge.cs +++ b/src/TensorFlowNET.Keras/Metrics/CategoricalHinge.cs @@ -4,7 +4,11 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class CategoricalHinge + public class CategoricalHinge : MeanMetricWrapper { + public CategoricalHinge(string name = "categorical_hinge", string dtype = null) + : base(Losses.Loss.categorical_hinge, name, dtype) + { + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/CosineSimilarity.cs b/src/TensorFlowNET.Keras/Metrics/CosineSimilarity.cs index c1dd618a..abce27c8 100644 --- a/src/TensorFlowNET.Keras/Metrics/CosineSimilarity.cs +++ b/src/TensorFlowNET.Keras/Metrics/CosineSimilarity.cs @@ -4,7 +4,16 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class CosineSimilarity + public class CosineSimilarity : MeanMetricWrapper { + public CosineSimilarity(string name = "cosine_similarity", string dtype = null, int axis = -1) + : base(Fn, name, dtype) + { + } + + internal static Tensor Fn(Tensor y_true, Tensor y_pred) + { + return Metric.cosine_proximity(y_true, y_pred); + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/FalseNegatives.cs b/src/TensorFlowNET.Keras/Metrics/FalseNegatives.cs index 075a8373..fb27484e 100644 --- a/src/TensorFlowNET.Keras/Metrics/FalseNegatives.cs +++ b/src/TensorFlowNET.Keras/Metrics/FalseNegatives.cs @@ -4,7 +4,11 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class FalseNegatives + public class FalseNegatives : _ConfusionMatrixConditionCount { + public FalseNegatives(float thresholds = 0.5F, string name = null, string dtype = null) + : base(Utils.MetricsUtils.ConfusionMatrix.FALSE_NEGATIVES, thresholds, name, dtype) + { + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/FalsePositives.cs b/src/TensorFlowNET.Keras/Metrics/FalsePositives.cs index fc7ad152..1b97e556 100644 --- a/src/TensorFlowNET.Keras/Metrics/FalsePositives.cs +++ b/src/TensorFlowNET.Keras/Metrics/FalsePositives.cs @@ -4,7 +4,11 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class FalsePositives + public class FalsePositives : _ConfusionMatrixConditionCount { + public FalsePositives(float thresholds = 0.5F, string name = null, string dtype = null) + : base(Utils.MetricsUtils.ConfusionMatrix.FALSE_POSITIVES, thresholds, name, dtype) + { + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/Hinge.cs b/src/TensorFlowNET.Keras/Metrics/Hinge.cs index f8d7eef2..21ebe067 100644 --- a/src/TensorFlowNET.Keras/Metrics/Hinge.cs +++ b/src/TensorFlowNET.Keras/Metrics/Hinge.cs @@ -4,7 +4,11 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class Hinge + public class Hinge : MeanMetricWrapper { + public Hinge(string name = "hinge", string dtype = null) + : base(Losses.Loss.hinge, name, dtype) + { + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/LogCoshError.cs b/src/TensorFlowNET.Keras/Metrics/LogCoshError.cs index b2f8d040..595f4aa7 100644 --- a/src/TensorFlowNET.Keras/Metrics/LogCoshError.cs +++ b/src/TensorFlowNET.Keras/Metrics/LogCoshError.cs @@ -4,7 +4,11 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class LogCoshError + public class LogCoshError : MeanMetricWrapper { + public LogCoshError(string name = "logcosh", string dtype = null) + : base(Losses.Loss.logcosh, name, dtype) + { + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/Mean.cs b/src/TensorFlowNET.Keras/Metrics/Mean.cs index 6a61846d..64b8b5db 100644 --- a/src/TensorFlowNET.Keras/Metrics/Mean.cs +++ b/src/TensorFlowNET.Keras/Metrics/Mean.cs @@ -4,7 +4,12 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class Mean + public class Mean : Reduce { + public Mean(string name, string dtype = null) + : base(Reduction.MEAN, name, dtype) + { + } + } } diff --git a/src/TensorFlowNET.Keras/Metrics/MeanAbsoluteError.cs b/src/TensorFlowNET.Keras/Metrics/MeanAbsoluteError.cs index 04a44dcd..c326a6dd 100644 --- a/src/TensorFlowNET.Keras/Metrics/MeanAbsoluteError.cs +++ b/src/TensorFlowNET.Keras/Metrics/MeanAbsoluteError.cs @@ -4,7 +4,11 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class MeanAbsoluteError + public class MeanAbsoluteError : MeanMetricWrapper { + public MeanAbsoluteError(string name = "mean_absolute_error", string dtype = null) + : base(Losses.Loss.mean_absolute_error, name, dtype) + { + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/MeanAbsolutePercentageError.cs b/src/TensorFlowNET.Keras/Metrics/MeanAbsolutePercentageError.cs index 1d75096d..0c51a5be 100644 --- a/src/TensorFlowNET.Keras/Metrics/MeanAbsolutePercentageError.cs +++ b/src/TensorFlowNET.Keras/Metrics/MeanAbsolutePercentageError.cs @@ -4,7 +4,11 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class MeanAbsolutePercentageError + public class MeanAbsolutePercentageError : MeanMetricWrapper { + public MeanAbsolutePercentageError(string name = "mean_absolute_percentage_error", string dtype = null) + : base(Losses.Loss.mean_absolute_percentage_error, name, dtype) + { + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/MeanMetricWrapper.cs b/src/TensorFlowNET.Keras/Metrics/MeanMetricWrapper.cs index 7bfdfcdc..ccc7922b 100644 --- a/src/TensorFlowNET.Keras/Metrics/MeanMetricWrapper.cs +++ b/src/TensorFlowNET.Keras/Metrics/MeanMetricWrapper.cs @@ -1,10 +1,25 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Text; namespace Tensorflow.Keras.Metrics { - class MeanMetricWrapper + public class MeanMetricWrapper : Mean { + public MeanMetricWrapper(Func fn, string name, string dtype = null) : base(name, dtype) + { + throw new NotImplementedException(); + } + + public override Tensor result() + { + throw new NotImplementedException(); + } + + public override Hashtable get_config() + { + throw new NotImplementedException(); + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/MeanRelativeError.cs b/src/TensorFlowNET.Keras/Metrics/MeanRelativeError.cs index 8cbad89a..9ae76a6a 100644 --- a/src/TensorFlowNET.Keras/Metrics/MeanRelativeError.cs +++ b/src/TensorFlowNET.Keras/Metrics/MeanRelativeError.cs @@ -1,10 +1,30 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Text; namespace Tensorflow.Keras.Metrics { - class MeanRelativeError + public class MeanRelativeError : Metric { + public MeanRelativeError(Tensor normalizer, string name, string dtype) : base(name, dtype) + { + throw new NotImplementedException(); + } + + public override Tensor result() + { + throw new NotImplementedException(); + } + + public override void update_state(Args args, KwArgs kwargs) + { + throw new NotImplementedException(); + } + + public override Hashtable get_config() + { + throw new NotImplementedException(); + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/MeanSquaredError.cs b/src/TensorFlowNET.Keras/Metrics/MeanSquaredError.cs index f0a8ed86..e23b0f41 100644 --- a/src/TensorFlowNET.Keras/Metrics/MeanSquaredError.cs +++ b/src/TensorFlowNET.Keras/Metrics/MeanSquaredError.cs @@ -4,7 +4,11 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class MeanSquaredError + public class MeanSquaredError : MeanMetricWrapper { + public MeanSquaredError(string name = "mean_squared_error", string dtype = null) + : base(Losses.Loss.mean_squared_error, name, dtype) + { + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/MeanSquaredLogarithmicError.cs b/src/TensorFlowNET.Keras/Metrics/MeanSquaredLogarithmicError.cs index 6139f216..9f56b9d8 100644 --- a/src/TensorFlowNET.Keras/Metrics/MeanSquaredLogarithmicError.cs +++ b/src/TensorFlowNET.Keras/Metrics/MeanSquaredLogarithmicError.cs @@ -4,7 +4,11 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class MeanSquaredLogarithmicError + public class MeanSquaredLogarithmicError : MeanMetricWrapper { + public MeanSquaredLogarithmicError(string name = "mean_squared_logarithmic_error", string dtype = null) + : base(Losses.Loss.mean_squared_logarithmic_error, name, dtype) + { + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/Metric.cs b/src/TensorFlowNET.Keras/Metrics/Metric.cs index 83af1fde..10a3676b 100644 --- a/src/TensorFlowNET.Keras/Metrics/Metric.cs +++ b/src/TensorFlowNET.Keras/Metrics/Metric.cs @@ -35,5 +35,29 @@ namespace Tensorflow.Keras.Metrics public void add_weight(string name, TensorShape shape= null, VariableAggregation aggregation= VariableAggregation.Sum, VariableSynchronization synchronization = VariableSynchronization.OnRead, Initializers.Initializer initializer= null, string dtype= null) => throw new NotImplementedException(); + + public static Tensor accuracy(Tensor y_true, Tensor y_pred) => throw new NotImplementedException(); + + public static Tensor binary_accuracy(Tensor y_true, Tensor y_pred, float threshold = 0.5f) => throw new NotImplementedException(); + + public static Tensor categorical_accuracy(Tensor y_true, Tensor y_pred) => throw new NotImplementedException(); + + public static Tensor sparse_categorical_accuracy(Tensor y_true, Tensor y_pred) => throw new NotImplementedException(); + + public static Tensor top_k_categorical_accuracy(Tensor y_true, Tensor y_pred, int k = 5) => throw new NotImplementedException(); + + public static Tensor sparse_top_k_categorical_accuracy(Tensor y_true, Tensor y_pred, int k = 5) => throw new NotImplementedException(); + + public static Tensor cosine_proximity(Tensor y_true, Tensor y_pred, int axis = -1) => throw new NotImplementedException(); + + public static Metric clone_metric(Metric metric) => throw new NotImplementedException(); + + public static Metric[] clone_metrics(Metric[] metric) => throw new NotImplementedException(); + + public static string serialize(Metric metric) => throw new NotImplementedException(); + + public static Metric deserialize(string config, object custom_objects = null) => throw new NotImplementedException(); + + public static Metric get(object identifier) => throw new NotImplementedException(); } } diff --git a/src/TensorFlowNET.Keras/Metrics/Poisson.cs b/src/TensorFlowNET.Keras/Metrics/Poisson.cs index 80bf5c52..7cdf5bd9 100644 --- a/src/TensorFlowNET.Keras/Metrics/Poisson.cs +++ b/src/TensorFlowNET.Keras/Metrics/Poisson.cs @@ -4,7 +4,11 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class Poisson + public class Poisson : MeanMetricWrapper { + public Poisson(string name = "logcosh", string dtype = null) + : base(Losses.Loss.logcosh, name, dtype) + { + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/Precision.cs b/src/TensorFlowNET.Keras/Metrics/Precision.cs index 8c5838c5..3d5c7248 100644 --- a/src/TensorFlowNET.Keras/Metrics/Precision.cs +++ b/src/TensorFlowNET.Keras/Metrics/Precision.cs @@ -1,10 +1,41 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Text; namespace Tensorflow.Keras.Metrics { - class Precision + public class Precision : Metric { + public Precision(float? thresholds = null, int? top_k = null, int? class_id = null, string name = null, string dtype = null) : base(name, dtype) + { + throw new NotImplementedException(); + } + + public Precision(float[] thresholds = null, int? top_k = null, int? class_id = null, string name = null, string dtype = null) : base(name, dtype) + { + throw new NotImplementedException(); + } + + public override Tensor result() + { + throw new NotImplementedException(); + } + + public override void update_state(Args args, KwArgs kwargs) + { + throw new NotImplementedException(); + } + + public override void reset_states() + { + throw new NotImplementedException(); + } + + public override Hashtable get_config() + { + throw new NotImplementedException(); + } + } } diff --git a/src/TensorFlowNET.Keras/Metrics/PrecisionAtRecall.cs b/src/TensorFlowNET.Keras/Metrics/PrecisionAtRecall.cs index 2191c7c1..05558232 100644 --- a/src/TensorFlowNET.Keras/Metrics/PrecisionAtRecall.cs +++ b/src/TensorFlowNET.Keras/Metrics/PrecisionAtRecall.cs @@ -1,10 +1,25 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Text; namespace Tensorflow.Keras.Metrics { - class PrecisionAtRecall + public class PrecisionAtRecall : SensitivitySpecificityBase { + public PrecisionAtRecall(float recall, int num_thresholds = 200, string name = null, string dtype = null) : base(recall, num_thresholds, name, dtype) + { + throw new NotImplementedException(); + } + + public override Tensor result() + { + throw new NotImplementedException(); + } + + public override Hashtable get_config() + { + throw new NotImplementedException(); + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/Recall.cs b/src/TensorFlowNET.Keras/Metrics/Recall.cs index ac65d250..804d4461 100644 --- a/src/TensorFlowNET.Keras/Metrics/Recall.cs +++ b/src/TensorFlowNET.Keras/Metrics/Recall.cs @@ -1,10 +1,41 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Text; namespace Tensorflow.Keras.Metrics { - class Recall + public class Recall : Metric { + public Recall(float? thresholds = null, int? top_k = null, int? class_id = null, string name = null, string dtype = null) : base(name, dtype) + { + throw new NotImplementedException(); + } + + public Recall(float[] thresholds = null, int? top_k = null, int? class_id = null, string name = null, string dtype = null) : base(name, dtype) + { + throw new NotImplementedException(); + } + + public override Tensor result() + { + throw new NotImplementedException(); + } + + public override void update_state(Args args, KwArgs kwargs) + { + throw new NotImplementedException(); + } + + public override void reset_states() + { + throw new NotImplementedException(); + } + + public override Hashtable get_config() + { + throw new NotImplementedException(); + } + } } diff --git a/src/TensorFlowNET.Keras/Metrics/Reduce.cs b/src/TensorFlowNET.Keras/Metrics/Reduce.cs index e383dc69..143f441e 100644 --- a/src/TensorFlowNET.Keras/Metrics/Reduce.cs +++ b/src/TensorFlowNET.Keras/Metrics/Reduce.cs @@ -4,7 +4,22 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class Reduce + public class Reduce : Metric { + public Reduce(string reduction, string name, string dtype= null) + : base(name, dtype) + { + throw new NotImplementedException(); + } + + public override Tensor result() + { + throw new NotImplementedException(); + } + + public override void update_state(Args args, KwArgs kwargs) + { + throw new NotImplementedException(); + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/RootMeanSquaredError.cs b/src/TensorFlowNET.Keras/Metrics/RootMeanSquaredError.cs index 1ca548c2..cd7a6968 100644 --- a/src/TensorFlowNET.Keras/Metrics/RootMeanSquaredError.cs +++ b/src/TensorFlowNET.Keras/Metrics/RootMeanSquaredError.cs @@ -4,7 +4,11 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class RootMeanSquaredError + public class RootMeanSquaredError : Mean { + public RootMeanSquaredError(string name = "root_mean_squared_error", string dtype = null) + : base(name, dtype) + { + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/SensitivityAtSpecificity.cs b/src/TensorFlowNET.Keras/Metrics/SensitivityAtSpecificity.cs index 93bef74e..72793d79 100644 --- a/src/TensorFlowNET.Keras/Metrics/SensitivityAtSpecificity.cs +++ b/src/TensorFlowNET.Keras/Metrics/SensitivityAtSpecificity.cs @@ -1,10 +1,25 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Text; namespace Tensorflow.Keras.Metrics { - class SensitivityAtSpecificity + public class SensitivityAtSpecificity : SensitivitySpecificityBase { + public SensitivityAtSpecificity(float specificity, int num_thresholds = 200, string name = null, string dtype = null) : base(specificity, num_thresholds, name, dtype) + { + throw new NotImplementedException(); + } + + public override Tensor result() + { + throw new NotImplementedException(); + } + + public override Hashtable get_config() + { + throw new NotImplementedException(); + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/SensitivitySpecificityBase.cs b/src/TensorFlowNET.Keras/Metrics/SensitivitySpecificityBase.cs index 16aec141..7531cdbb 100644 --- a/src/TensorFlowNET.Keras/Metrics/SensitivitySpecificityBase.cs +++ b/src/TensorFlowNET.Keras/Metrics/SensitivitySpecificityBase.cs @@ -4,7 +4,26 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class SensitivitySpecificityBase + public class SensitivitySpecificityBase : Metric { + public SensitivitySpecificityBase(float value, int num_thresholds= 200, string name = null, string dtype = null) : base(name, dtype) + { + throw new NotImplementedException(); + } + + public override Tensor result() + { + throw new NotImplementedException(); + } + + public override void update_state(Args args, KwArgs kwargs) + { + throw new NotImplementedException(); + } + + public override void reset_states() + { + throw new NotImplementedException(); + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/SparseCategoricalAccuracy.cs b/src/TensorFlowNET.Keras/Metrics/SparseCategoricalAccuracy.cs index 32abcd2c..5a57907d 100644 --- a/src/TensorFlowNET.Keras/Metrics/SparseCategoricalAccuracy.cs +++ b/src/TensorFlowNET.Keras/Metrics/SparseCategoricalAccuracy.cs @@ -4,7 +4,12 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class SparseCategoricalAccuracy + public class SparseCategoricalAccuracy : MeanMetricWrapper { + public SparseCategoricalAccuracy(string name = "sparse_categorical_accuracy", string dtype = null) + : base(Metric.sparse_categorical_accuracy, name, dtype) + { + } + } } diff --git a/src/TensorFlowNET.Keras/Metrics/SparseTopKCategoricalAccuracy.cs b/src/TensorFlowNET.Keras/Metrics/SparseTopKCategoricalAccuracy.cs index 2f1eba09..b02049ad 100644 --- a/src/TensorFlowNET.Keras/Metrics/SparseTopKCategoricalAccuracy.cs +++ b/src/TensorFlowNET.Keras/Metrics/SparseTopKCategoricalAccuracy.cs @@ -4,7 +4,17 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class SparseTopKCategoricalAccuracy + public class SparseTopKCategoricalAccuracy : MeanMetricWrapper { + public SparseTopKCategoricalAccuracy(int k = 5, string name = "sparse_top_k_categorical_accuracy", string dtype = null) + : base(Fn, name, dtype) + { + + } + + internal static Tensor Fn(Tensor y_true, Tensor y_pred) + { + return Metric.sparse_top_k_categorical_accuracy(y_true, y_pred); + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/SquaredHinge.cs b/src/TensorFlowNET.Keras/Metrics/SquaredHinge.cs index 15041d9b..04a7bef8 100644 --- a/src/TensorFlowNET.Keras/Metrics/SquaredHinge.cs +++ b/src/TensorFlowNET.Keras/Metrics/SquaredHinge.cs @@ -4,7 +4,11 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class SquaredHinge + public class SquaredHinge : MeanMetricWrapper { + public SquaredHinge(string name = "squared_hinge", string dtype = null) + : base(Losses.Loss.squared_hinge, name, dtype) + { + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/Sum.cs b/src/TensorFlowNET.Keras/Metrics/Sum.cs index 10396867..f466a136 100644 --- a/src/TensorFlowNET.Keras/Metrics/Sum.cs +++ b/src/TensorFlowNET.Keras/Metrics/Sum.cs @@ -4,7 +4,11 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class Sum + public class Sum : Reduce { + public Sum(string name, string dtype = null) + : base(Reduction.SUM, name, dtype) + { + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/TopKCategoricalAccuracy.cs b/src/TensorFlowNET.Keras/Metrics/TopKCategoricalAccuracy.cs index a14e6575..e2c80fad 100644 --- a/src/TensorFlowNET.Keras/Metrics/TopKCategoricalAccuracy.cs +++ b/src/TensorFlowNET.Keras/Metrics/TopKCategoricalAccuracy.cs @@ -4,7 +4,16 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class TopKCategoricalAccuracy + public class TopKCategoricalAccuracy : MeanMetricWrapper { + public TopKCategoricalAccuracy(int k = 5, string name = "top_k_categorical_accuracy", string dtype = null) + : base(Fn, name, dtype) + { + } + + internal static Tensor Fn(Tensor y_true, Tensor y_pred) + { + return Metric.top_k_categorical_accuracy(y_true, y_pred); + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/TrueNegatives.cs b/src/TensorFlowNET.Keras/Metrics/TrueNegatives.cs index 53e9a894..7e81a2fd 100644 --- a/src/TensorFlowNET.Keras/Metrics/TrueNegatives.cs +++ b/src/TensorFlowNET.Keras/Metrics/TrueNegatives.cs @@ -4,7 +4,11 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class TrueNegatives + public class TrueNegatives : _ConfusionMatrixConditionCount { + public TrueNegatives(float thresholds = 0.5F, string name = null, string dtype = null) + : base(Utils.MetricsUtils.ConfusionMatrix.TRUE_NEGATIVES, thresholds, name, dtype) + { + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/TruePositives.cs b/src/TensorFlowNET.Keras/Metrics/TruePositives.cs index 286c4be7..867049be 100644 --- a/src/TensorFlowNET.Keras/Metrics/TruePositives.cs +++ b/src/TensorFlowNET.Keras/Metrics/TruePositives.cs @@ -4,7 +4,11 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class TruePositives + public class TruePositives : _ConfusionMatrixConditionCount { + public TruePositives(float thresholds = 0.5F, string name = null, string dtype = null) + : base(Utils.MetricsUtils.ConfusionMatrix.TRUE_POSITIVES, thresholds, name, dtype) + { + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/_ConfusionMatrixConditionCount.cs b/src/TensorFlowNET.Keras/Metrics/_ConfusionMatrixConditionCount.cs index ab9e2c08..3d2be961 100644 --- a/src/TensorFlowNET.Keras/Metrics/_ConfusionMatrixConditionCount.cs +++ b/src/TensorFlowNET.Keras/Metrics/_ConfusionMatrixConditionCount.cs @@ -1,10 +1,37 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Text; +using static Tensorflow.Keras.Utils.MetricsUtils; namespace Tensorflow.Keras.Metrics { - class _ConfusionMatrixConditionCount + public class _ConfusionMatrixConditionCount : Metric { + public _ConfusionMatrixConditionCount(string confusion_matrix_cond, float thresholds= 0.5f, string name= null, string dtype= null) + : base(name, dtype) + { + throw new NotImplementedException(); + } + + public override Tensor result() + { + throw new NotImplementedException(); + } + + public override void update_state(Args args, KwArgs kwargs) + { + throw new NotImplementedException(); + } + + public override void reset_states() + { + throw new NotImplementedException(); + } + + public override Hashtable get_config() + { + throw new NotImplementedException(); + } } } diff --git a/src/TensorFlowNET.Keras/Optimizer/Optimizer.cs b/src/TensorFlowNET.Keras/Optimizer/Optimizer.cs index 14223c5e..ec8bd68a 100644 --- a/src/TensorFlowNET.Keras/Optimizer/Optimizer.cs +++ b/src/TensorFlowNET.Keras/Optimizer/Optimizer.cs @@ -28,7 +28,7 @@ namespace Tensorflow.Keras public static string serialize(Optimizer optimizer) => throw new NotImplementedException(); - public static string deserialize(string config, object custom_objects = null) => throw new NotImplementedException(); + public static Optimizer deserialize(string config, object custom_objects = null) => throw new NotImplementedException(); public static Optimizer get(object identifier) => throw new NotImplementedException(); From 7c587e40fec8569d98723b4e533973a55ae055c4 Mon Sep 17 00:00:00 2001 From: Deepak Battini Date: Fri, 17 Jan 2020 17:37:04 +1030 Subject: [PATCH 4/5] Metrics methods skeletonizing completed. --- .../Metrics/BinaryCrossentropy.cs | 11 +++++- .../Metrics/CategoricalCrossentropy.cs | 11 +++++- .../Metrics/KLDivergence.cs | 6 ++- src/TensorFlowNET.Keras/Metrics/MeanIoU.cs | 26 ++++++++++++- src/TensorFlowNET.Keras/Metrics/MeanTensor.cs | 39 ++++++++++++++++++- .../Metrics/SparseCategoricalCrossentropy.cs | 11 +++++- .../Metrics/SumOverBatchSize.cs | 5 ++- .../Metrics/SumOverBatchSizeMetricWrapper.cs | 17 +++++++- 8 files changed, 118 insertions(+), 8 deletions(-) diff --git a/src/TensorFlowNET.Keras/Metrics/BinaryCrossentropy.cs b/src/TensorFlowNET.Keras/Metrics/BinaryCrossentropy.cs index 5d26058f..14ef73b9 100644 --- a/src/TensorFlowNET.Keras/Metrics/BinaryCrossentropy.cs +++ b/src/TensorFlowNET.Keras/Metrics/BinaryCrossentropy.cs @@ -4,7 +4,16 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class BinaryCrossentropy + public class BinaryCrossentropy : MeanMetricWrapper { + public BinaryCrossentropy(string name = "binary_crossentropy", string dtype = null, bool from_logits = false, float label_smoothing = 0) + : base(Fn, name, dtype) + { + } + + internal static Tensor Fn(Tensor y_true, Tensor y_pred) + { + return Losses.Loss.binary_crossentropy(y_true, y_pred); + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/CategoricalCrossentropy.cs b/src/TensorFlowNET.Keras/Metrics/CategoricalCrossentropy.cs index 977d5368..c83bb5d5 100644 --- a/src/TensorFlowNET.Keras/Metrics/CategoricalCrossentropy.cs +++ b/src/TensorFlowNET.Keras/Metrics/CategoricalCrossentropy.cs @@ -4,7 +4,16 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class CategoricalCrossentropy + public class CategoricalCrossentropy : MeanMetricWrapper { + public CategoricalCrossentropy(string name = "categorical_crossentropy", string dtype = null, bool from_logits = false, float label_smoothing = 0) + : base(Fn, name, dtype) + { + } + + internal static Tensor Fn(Tensor y_true, Tensor y_pred) + { + return Losses.Loss.categorical_crossentropy(y_true, y_pred); + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/KLDivergence.cs b/src/TensorFlowNET.Keras/Metrics/KLDivergence.cs index c6447d1e..814b14ce 100644 --- a/src/TensorFlowNET.Keras/Metrics/KLDivergence.cs +++ b/src/TensorFlowNET.Keras/Metrics/KLDivergence.cs @@ -4,7 +4,11 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class KLDivergence + public class KLDivergence : MeanMetricWrapper { + public KLDivergence(string name = "kullback_leibler_divergence", string dtype = null) + : base(Losses.Loss.logcosh, name, dtype) + { + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/MeanIoU.cs b/src/TensorFlowNET.Keras/Metrics/MeanIoU.cs index eda95a34..d8975218 100644 --- a/src/TensorFlowNET.Keras/Metrics/MeanIoU.cs +++ b/src/TensorFlowNET.Keras/Metrics/MeanIoU.cs @@ -1,10 +1,34 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Text; namespace Tensorflow.Keras.Metrics { - class MeanIoU + public class MeanIoU : Metric { + public MeanIoU(int num_classes, string name, string dtype) : base(name, dtype) + { + } + + public override void reset_states() + { + throw new NotImplementedException(); + } + + public override Hashtable get_config() + { + throw new NotImplementedException(); + } + + public override Tensor result() + { + throw new NotImplementedException(); + } + + public override void update_state(Args args, KwArgs kwargs) + { + throw new NotImplementedException(); + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/MeanTensor.cs b/src/TensorFlowNET.Keras/Metrics/MeanTensor.cs index 9bcab008..283b516f 100644 --- a/src/TensorFlowNET.Keras/Metrics/MeanTensor.cs +++ b/src/TensorFlowNET.Keras/Metrics/MeanTensor.cs @@ -4,7 +4,44 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class MeanTensor + public class MeanTensor : Metric { + public int total + { + get + { + throw new NotImplementedException(); + } + } + + public int count + { + get + { + throw new NotImplementedException(); + } + } + + public MeanTensor(int num_classes, string name = "mean_tensor", string dtype) : base(name, dtype) + { + } + + + private void _build(TensorShape shape) => throw new NotImplementedException(); + + public override void reset_states() + { + throw new NotImplementedException(); + } + + public override Tensor result() + { + throw new NotImplementedException(); + } + + public override void update_state(Args args, KwArgs kwargs) + { + throw new NotImplementedException(); + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/SparseCategoricalCrossentropy.cs b/src/TensorFlowNET.Keras/Metrics/SparseCategoricalCrossentropy.cs index 7001a11b..b2513fd8 100644 --- a/src/TensorFlowNET.Keras/Metrics/SparseCategoricalCrossentropy.cs +++ b/src/TensorFlowNET.Keras/Metrics/SparseCategoricalCrossentropy.cs @@ -4,7 +4,16 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class SparseCategoricalCrossentropy + public class SparseCategoricalCrossentropy : MeanMetricWrapper { + public SparseCategoricalCrossentropy(string name = "sparse_categorical_crossentropy", string dtype = null, bool from_logits = false, int axis = -1) + : base(Fn, name, dtype) + { + } + + internal static Tensor Fn(Tensor y_true, Tensor y_pred) + { + return Losses.Loss.sparse_categorical_crossentropy(y_true, y_pred); + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/SumOverBatchSize.cs b/src/TensorFlowNET.Keras/Metrics/SumOverBatchSize.cs index 5faa76f8..d25654c5 100644 --- a/src/TensorFlowNET.Keras/Metrics/SumOverBatchSize.cs +++ b/src/TensorFlowNET.Keras/Metrics/SumOverBatchSize.cs @@ -4,7 +4,10 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class SumOverBatchSize + public class SumOverBatchSize : Reduce { + public SumOverBatchSize(string name = "sum_over_batch_size", string dtype = null) : base(Reduction.SUM_OVER_BATCH_SIZE, name, dtype) + { + } } } diff --git a/src/TensorFlowNET.Keras/Metrics/SumOverBatchSizeMetricWrapper.cs b/src/TensorFlowNET.Keras/Metrics/SumOverBatchSizeMetricWrapper.cs index 03fe2668..ff1c0497 100644 --- a/src/TensorFlowNET.Keras/Metrics/SumOverBatchSizeMetricWrapper.cs +++ b/src/TensorFlowNET.Keras/Metrics/SumOverBatchSizeMetricWrapper.cs @@ -1,10 +1,25 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Text; namespace Tensorflow.Keras.Metrics { - class SumOverBatchSizeMetricWrapper + public class SumOverBatchSizeMetricWrapper : SumOverBatchSize { + public SumOverBatchSizeMetricWrapper(Func fn, string name, string dtype = null) + { + throw new NotImplementedException(); + } + + public override void update_state(Args args, KwArgs kwargs) + { + throw new NotImplementedException(); + } + + public override Hashtable get_config() + { + throw new NotImplementedException(); + } } } From 45f5b25e6d4cf85ccd23523371ae0e1796f559ca Mon Sep 17 00:00:00 2001 From: Deepak Kumar Date: Sun, 19 Jan 2020 08:13:29 +1030 Subject: [PATCH 5/5] FIxed build error --- src/TensorFlowNET.Keras/Metrics/MeanTensor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TensorFlowNET.Keras/Metrics/MeanTensor.cs b/src/TensorFlowNET.Keras/Metrics/MeanTensor.cs index 283b516f..114329b1 100644 --- a/src/TensorFlowNET.Keras/Metrics/MeanTensor.cs +++ b/src/TensorFlowNET.Keras/Metrics/MeanTensor.cs @@ -22,7 +22,7 @@ namespace Tensorflow.Keras.Metrics } } - public MeanTensor(int num_classes, string name = "mean_tensor", string dtype) : base(name, dtype) + public MeanTensor(int num_classes, string name = "mean_tensor", string dtype = null) : base(name, dtype) { }