From e16ecdf04ecca649d49fff6b2aa80631a740624c Mon Sep 17 00:00:00 2001 From: Deepak Battini Date: Thu, 9 Jan 2020 17:40:32 +1030 Subject: [PATCH] Base Layer class sketonizing --- .../Constraints/ConstraintBase.cs | 2 +- src/TensorFlowNET.Keras/Engine/BaseLayer.cs | 2 +- .../Engine/BaseLayerUtils.cs | 16 ++++++++- .../Initializer/BaseInitializer.cs | 7 ---- .../Initializers/Initializer.cs | 2 +- src/TensorFlowNET.Keras/Layers/Core/Dense.cs | 4 +-- src/TensorFlowNET.Keras/Layers/ILayer.cs | 12 ------- src/TensorFlowNET.Keras/Layers/Layer.cs | 36 +++++++++++++++++++ src/TensorFlowNET.Keras/Metrics/Metric.cs | 2 +- src/TensorFlowNET.Keras/Model.cs | 14 ++++---- .../Regularizers/Regularizer.cs | 2 +- 11 files changed, 65 insertions(+), 34 deletions(-) delete mode 100644 src/TensorFlowNET.Keras/Initializer/BaseInitializer.cs delete mode 100644 src/TensorFlowNET.Keras/Layers/ILayer.cs create mode 100644 src/TensorFlowNET.Keras/Layers/Layer.cs diff --git a/src/TensorFlowNET.Keras/Constraints/ConstraintBase.cs b/src/TensorFlowNET.Keras/Constraints/ConstraintBase.cs index 1a58a10e..dd100cef 100644 --- a/src/TensorFlowNET.Keras/Constraints/ConstraintBase.cs +++ b/src/TensorFlowNET.Keras/Constraints/ConstraintBase.cs @@ -4,7 +4,7 @@ using System.Text; namespace Tensorflow.Keras.Constraints { - class ConstraintBase + public abstract class ConstraintBase { } } diff --git a/src/TensorFlowNET.Keras/Engine/BaseLayer.cs b/src/TensorFlowNET.Keras/Engine/BaseLayer.cs index fd323fe8..35f65357 100644 --- a/src/TensorFlowNET.Keras/Engine/BaseLayer.cs +++ b/src/TensorFlowNET.Keras/Engine/BaseLayer.cs @@ -4,7 +4,7 @@ using System.Text; namespace Tensorflow.Keras.Engine { - class BaseLayer + public class Layer { } } diff --git a/src/TensorFlowNET.Keras/Engine/BaseLayerUtils.cs b/src/TensorFlowNET.Keras/Engine/BaseLayerUtils.cs index 6ca2e695..914aee71 100644 --- a/src/TensorFlowNET.Keras/Engine/BaseLayerUtils.cs +++ b/src/TensorFlowNET.Keras/Engine/BaseLayerUtils.cs @@ -1,10 +1,24 @@ using System; using System.Collections.Generic; using System.Text; +using Tensorflow.Keras.Initializers; +using Tensorflow.Keras.Metrics; namespace Tensorflow.Keras.Engine { - class BaseLayerUtils + public class BaseLayerUtils { + public static (Metric, Metric) create_mean_metric(Tensor value, string name = null) => throw new NotImplementedException(); + + public static VariableV1 make_variable(string name, TensorShape shape= null, TF_DataType dtype= TF_DataType.TF_FLOAT, Initializer initializer= null, + bool trainable= true, string caching_device= null, bool validate_shape= true, Constraints.ConstraintBase constraint= null, + bool use_resource= false, Graph[] collections= null, VariableSynchronization synchronization= VariableSynchronization.Auto, + VariableAggregation aggregation= VariableAggregation.None) => throw new NotImplementedException(); + + public static Tensor[] collect_previous_mask(TensorArray input_tensors) => throw new NotImplementedException(); + + public bool have_all_keras_metadata(Tensor[] tensors) => throw new NotImplementedException(); + + public static dynamic generate_placeholders_from_shape(TensorShape shape) => throw new NotImplementedException(); } } diff --git a/src/TensorFlowNET.Keras/Initializer/BaseInitializer.cs b/src/TensorFlowNET.Keras/Initializer/BaseInitializer.cs deleted file mode 100644 index cd3e473c..00000000 --- a/src/TensorFlowNET.Keras/Initializer/BaseInitializer.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Keras.Initializer -{ - class BaseInitializer : IInitializer - { - public int seed; - } -} diff --git a/src/TensorFlowNET.Keras/Initializers/Initializer.cs b/src/TensorFlowNET.Keras/Initializers/Initializer.cs index fac6e90a..5a432be1 100644 --- a/src/TensorFlowNET.Keras/Initializers/Initializer.cs +++ b/src/TensorFlowNET.Keras/Initializers/Initializer.cs @@ -4,7 +4,7 @@ using System.Text; namespace Tensorflow.Keras.Initializers { - class Initializer + public abstract class Initializer { } } diff --git a/src/TensorFlowNET.Keras/Layers/Core/Dense.cs b/src/TensorFlowNET.Keras/Layers/Core/Dense.cs index 893dbc10..47ec17cf 100644 --- a/src/TensorFlowNET.Keras/Layers/Core/Dense.cs +++ b/src/TensorFlowNET.Keras/Layers/Core/Dense.cs @@ -23,7 +23,7 @@ using static Tensorflow.Binding; namespace Keras.Layers { - public class Dense : ILayer + public class Dense : Layer { RefVariable W; int units; @@ -37,7 +37,7 @@ namespace Keras.Layers this.units = units; this.name = (string.IsNullOrEmpty(name) || string.IsNullOrWhiteSpace(name))?this.GetType().Name + "_" + this.GetType().GUID:name; } - public ILayer __build__(TensorShape input_shape, int seed = 1, float stddev = -1f) + public Layer __build__(TensorShape input_shape, int seed = 1, float stddev = -1f) { Console.WriteLine("Building Layer \"" + name + "\" ..."); if (stddev == -1) diff --git a/src/TensorFlowNET.Keras/Layers/ILayer.cs b/src/TensorFlowNET.Keras/Layers/ILayer.cs deleted file mode 100644 index 9148ca24..00000000 --- a/src/TensorFlowNET.Keras/Layers/ILayer.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Tensorflow; - -namespace Keras.Layers -{ - public interface ILayer - { - TensorShape __shape__(); - ILayer __build__(TensorShape input_shape, int seed = 1, float stddev = -1f); - Tensor __call__(Tensor x); - TensorShape output_shape(TensorShape input_shape); - } -} diff --git a/src/TensorFlowNET.Keras/Layers/Layer.cs b/src/TensorFlowNET.Keras/Layers/Layer.cs new file mode 100644 index 00000000..7e23fd71 --- /dev/null +++ b/src/TensorFlowNET.Keras/Layers/Layer.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using Tensorflow; +using Tensorflow.Keras.Constraints; +using Tensorflow.Keras.Initializers; +using Tensorflow.Keras.Regularizers; + +namespace Keras.Layers +{ + public abstract class Layer + { + public Layer(bool trainable = true, string name = null, string dtype = null, bool @dynamic = false, Dictionary kwargs = null) + { + + } + + public void build(TensorShape shape) => throw new NotImplementedException(); + + public void call(Tensor[] inputs) => throw new NotImplementedException(); + + public void _add_trackable(dynamic trackable_object, bool trainable) => throw new NotImplementedException(); + + public void add_weight(string name= null, TensorShape shape= null, string dtype= null, Initializer initializer = null, + Regularizer regularizer = null, bool? trainable = null, ConstraintBase constraint = null, + dynamic partitioner= null, bool? use_resource= null, VariableSynchronization synchronization= VariableSynchronization.Auto, + VariableAggregation aggregation= VariableAggregation.None, Dictionary kwargs = null) => throw new NotImplementedException(); + + public Dictionary get_config() => throw new NotImplementedException(); + + public Layer from_config(Dictionary config) => throw new NotImplementedException(); + + public TensorShape compute_output_shape(TensorShape input_shape) => throw new NotImplementedException(); + + public dynamic compute_output_signature(dynamic input_signature) => throw new NotImplementedException(); + } +} diff --git a/src/TensorFlowNET.Keras/Metrics/Metric.cs b/src/TensorFlowNET.Keras/Metrics/Metric.cs index 20d3cd41..394d76f0 100644 --- a/src/TensorFlowNET.Keras/Metrics/Metric.cs +++ b/src/TensorFlowNET.Keras/Metrics/Metric.cs @@ -4,7 +4,7 @@ using System.Text; namespace Tensorflow.Keras.Metrics { - class Metric + public abstract class Metric { } } diff --git a/src/TensorFlowNET.Keras/Model.cs b/src/TensorFlowNET.Keras/Model.cs index 8264c9e2..0eb7fbce 100644 --- a/src/TensorFlowNET.Keras/Model.cs +++ b/src/TensorFlowNET.Keras/Model.cs @@ -26,20 +26,20 @@ namespace Tensorflow.Keras public class Model { public Tensor Flow; - List layer_stack; + List layer_stack; public TensorShape InputShape; public Model() { - layer_stack = new List(); + layer_stack = new List(); } - public Model Add(ILayer layer) + public Model Add(Layer layer) { layer_stack.Add(layer); return this; } - public Model Add(IEnumerable layers) + public Model Add(IEnumerable layers) { layer_stack.AddRange(layers); return this; @@ -83,9 +83,9 @@ namespace Tensorflow.Keras Flow = features; for (int i = 0; i < layer_stack.Count; i++) { - layer_stack[i].__build__(flow_shape); - flow_shape = layer_stack[i].output_shape(flow_shape); - Flow = layer_stack[i].__call__(Flow); + //layer_stack[i].build(flow_shape); + //flow_shape = layer_stack[i].output_shape(flow_shape); + //Flow = layer_stack[i].__call__(Flow); } var predictions = tf.sigmoid(tf.squeeze(Flow)); diff --git a/src/TensorFlowNET.Keras/Regularizers/Regularizer.cs b/src/TensorFlowNET.Keras/Regularizers/Regularizer.cs index 0f570140..ef670912 100644 --- a/src/TensorFlowNET.Keras/Regularizers/Regularizer.cs +++ b/src/TensorFlowNET.Keras/Regularizers/Regularizer.cs @@ -4,7 +4,7 @@ using System.Text; namespace Tensorflow.Keras.Regularizers { - class Regularizer + public class Regularizer { } }