diff --git a/TensorFlow.NET.sln b/TensorFlow.NET.sln index 94d2a3aa..e50bb267 100644 --- a/TensorFlow.NET.sln +++ b/TensorFlow.NET.sln @@ -9,7 +9,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Examples", "t EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Core", "src\TensorFlowNET.Core\TensorFlowNET.Core.csproj", "{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Visualization", "TensorFlowNET.Visualization\TensorFlowNET.Visualization.csproj", "{4BB2ABD1-635E-41E4-B534-CB5B6A2D754D}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Visualization", "src\TensorFlowNET.Visualization\TensorFlowNET.Visualization.csproj", "{0254BFF9-453C-4FE0-9609-3644559A79CE}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -29,10 +29,10 @@ Global {FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Debug|Any CPU.Build.0 = Debug|Any CPU {FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Release|Any CPU.ActiveCfg = Release|Any CPU {FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Release|Any CPU.Build.0 = Release|Any CPU - {4BB2ABD1-635E-41E4-B534-CB5B6A2D754D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {4BB2ABD1-635E-41E4-B534-CB5B6A2D754D}.Debug|Any CPU.Build.0 = Debug|Any CPU - {4BB2ABD1-635E-41E4-B534-CB5B6A2D754D}.Release|Any CPU.ActiveCfg = Release|Any CPU - {4BB2ABD1-635E-41E4-B534-CB5B6A2D754D}.Release|Any CPU.Build.0 = Release|Any CPU + {0254BFF9-453C-4FE0-9609-3644559A79CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0254BFF9-453C-4FE0-9609-3644559A79CE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0254BFF9-453C-4FE0-9609-3644559A79CE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0254BFF9-453C-4FE0-9609-3644559A79CE}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/TensorFlowNET.Core/APIs/tf.layers.cs b/src/TensorFlowNET.Core/APIs/tf.layers.cs index 0b17ca83..e9d9546d 100644 --- a/src/TensorFlowNET.Core/APIs/tf.layers.cs +++ b/src/TensorFlowNET.Core/APIs/tf.layers.cs @@ -19,14 +19,30 @@ namespace Tensorflow int[] dilation_rate = null, bool use_bias = true, IActivation activation = null, - IInitializer kernel_initializer = null) + IInitializer kernel_initializer = null, + IInitializer bias_initializer = null, + bool trainable = true, + string name = null) { if (strides == null) strides = new int[] { 1, 1 }; if (dilation_rate == null) dilation_rate = new int[] { 1, 1 }; + if (bias_initializer == null) + bias_initializer = tf.zeros_initializer; - var layer = new Conv2D(filters, kernel_size); + var layer = new Conv2D(filters, + kernel_size: kernel_size, + strides: strides, + padding: padding, + data_format: data_format, + dilation_rate: dilation_rate, + activation: activation, + use_bias: use_bias, + kernel_initializer: kernel_initializer, + bias_initializer: bias_initializer, + trainable: trainable, + name: name); return layer.apply(inputs); } diff --git a/src/TensorFlowNET.Core/Keras/Engine/InputSpec.cs b/src/TensorFlowNET.Core/Keras/Engine/InputSpec.cs new file mode 100644 index 00000000..ccfc7187 --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/Engine/InputSpec.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras.Engine +{ + /// + /// Specifies the ndim, dtype and shape of every input to a layer. + /// + public class InputSpec + { + public InputSpec(TF_DataType dtype = TF_DataType.DtInvalid) + { + + } + } +} diff --git a/src/TensorFlowNET.Core/Keras/Engine/Layer.cs b/src/TensorFlowNET.Core/Keras/Engine/Layer.cs index aa53c0ae..2f00fa0b 100644 --- a/src/TensorFlowNET.Core/Keras/Engine/Layer.cs +++ b/src/TensorFlowNET.Core/Keras/Engine/Layer.cs @@ -12,6 +12,77 @@ namespace Tensorflow.Keras.Engine /// public class Layer : CheckpointableBase { + protected bool trainable; + protected string _name; + protected TF_DataType _dtype; + protected Graph _graph; + protected string _base_name; + protected VariableScope _scope; + /// + /// A stateful layer is a layer whose updates are run during inference too, + /// for instance stateful RNNs. + /// + protected bool stateful; + /// + /// Indicates whether `build` needs to be called upon layer call, to create + /// the layer's weights. + /// + protected bool built; + /// + /// Provides information about which inputs are compatible with the layer. + /// + protected InputSpec input_spec; + protected bool supports_masking; + public Layer(bool trainable = true, + string name = null, + TF_DataType dtype = TF_DataType.DtInvalid) + { + this.trainable = trainable; + this.stateful = false; + this.built = false; + this.supports_masking = false; + _init_set_name(name); + } + + public Tensor apply(Tensor inputs) + { + return __call__(inputs); + } + + public Tensor __call__(Tensor inputs, + VariableScope scope = null) + { + _set_scope(scope); + _graph = ops._get_graph_from_inputs(new List { inputs }, graph: _graph); + var scope_context_manager = tf.variable_scope(_scope); + + throw new NotImplementedException(""); + } + + private void _init_set_name(string name) + { + if (string.IsNullOrEmpty(name)) + (_name, _base_name) = _make_unique_name(); + } + + private (string, string) _make_unique_name() + { + string base_name = "conv2d"; + string name = base_layer_utils.unique_layer_name(base_name); + return (name, base_name); + } + + private void _set_scope(VariableScope scope = null) + { + if (_scope == null) + { + Python.with(tf.variable_scope(scope, default_name: _base_name), captured_scope => + { + _scope = captured_scope; + }); + } + + } } } diff --git a/src/TensorFlowNET.Core/Keras/Engine/base_layer_utils.cs b/src/TensorFlowNET.Core/Keras/Engine/base_layer_utils.cs new file mode 100644 index 00000000..1f397425 --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/Engine/base_layer_utils.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras.Engine +{ + public class base_layer_utils + { + /// + /// Makes a layer name (or arbitrary string) unique within a TensorFlow graph. + /// + /// + /// + public static string unique_layer_name(string name) + { + int number = get_default_graph_uid_map(); + return $"{name}_{number}"; + } + + public static int get_default_graph_uid_map() + { + var graph = ops.get_default_graph(); + return graph._next_id(); + } + } +} diff --git a/src/TensorFlowNET.Core/Keras/Layers/Conv.cs b/src/TensorFlowNET.Core/Keras/Layers/Conv.cs index 70df628b..6d46f472 100644 --- a/src/TensorFlowNET.Core/Keras/Layers/Conv.cs +++ b/src/TensorFlowNET.Core/Keras/Layers/Conv.cs @@ -1,10 +1,50 @@ using System; using System.Collections.Generic; using System.Text; +using Tensorflow.Keras.Engine; +using Tensorflow.Operations.Activation; namespace Tensorflow.Keras.Layers { - public class Conv + public class Conv : Layer { + protected int rank; + protected int filters; + protected int[] kernel_size; + protected int[] strides; + protected string padding; + protected string data_format; + protected int[] dilation_rate; + protected IActivation activation; + protected bool use_bias; + protected IInitializer kernel_initializer; + protected IInitializer bias_initializer; + + public Conv(int rank, + int filters, + int[] kernel_size, + int[] strides = null, + string padding = "valid", + string data_format = null, + int[] dilation_rate = null, + IActivation activation = null, + bool use_bias = true, + IInitializer kernel_initializer = null, + IInitializer bias_initializer = null, + bool trainable = true, + string name = null) : base(trainable: trainable, name: name) + { + this.rank = rank; + this.filters = filters; + this.kernel_size = kernel_size; + this.strides = strides; + this.padding = padding; + this.data_format = data_format; + this.dilation_rate = dilation_rate; + this.activation = activation; + this.use_bias = use_bias; + this.kernel_initializer = kernel_initializer; + this.bias_initializer = bias_initializer; + } } } diff --git a/src/TensorFlowNET.Core/Keras/Layers/Conv2D.cs b/src/TensorFlowNET.Core/Keras/Layers/Conv2D.cs index b9d884ce..be6d551b 100644 --- a/src/TensorFlowNET.Core/Keras/Layers/Conv2D.cs +++ b/src/TensorFlowNET.Core/Keras/Layers/Conv2D.cs @@ -7,10 +7,6 @@ namespace Tensorflow.Keras.Layers { public class Conv2D : Conv { - private int filters; - private int[] kernel_size; - private int[] strides; - public Conv2D(int filters, int[] kernel_size, int[] strides = null, @@ -22,14 +18,21 @@ namespace Tensorflow.Keras.Layers IInitializer kernel_initializer = null, IInitializer bias_initializer = null, bool trainable = true, - string name = null) + string name = null) : base(2, + filters, + kernel_size, + strides: strides, + padding: padding, + data_format: data_format, + dilation_rate: dilation_rate, + activation: activation, + use_bias: use_bias, + kernel_initializer: kernel_initializer, + bias_initializer: bias_initializer, + trainable: trainable, + name: name) { } - - public Tensor apply(Tensor inputs) - { - throw new NotImplementedException("apply"); - } } } diff --git a/TensorFlowNET.Visualization/Controllers/ValuesController.cs b/src/TensorFlowNET.Visualization/Controllers/ValuesController.cs similarity index 100% rename from TensorFlowNET.Visualization/Controllers/ValuesController.cs rename to src/TensorFlowNET.Visualization/Controllers/ValuesController.cs diff --git a/TensorFlowNET.Visualization/Program.cs b/src/TensorFlowNET.Visualization/Program.cs similarity index 100% rename from TensorFlowNET.Visualization/Program.cs rename to src/TensorFlowNET.Visualization/Program.cs diff --git a/TensorFlowNET.Visualization/Startup.cs b/src/TensorFlowNET.Visualization/Startup.cs similarity index 100% rename from TensorFlowNET.Visualization/Startup.cs rename to src/TensorFlowNET.Visualization/Startup.cs diff --git a/TensorFlowNET.Visualization/TensorFlowNET.Visualization.csproj b/src/TensorFlowNET.Visualization/TensorFlowNET.Visualization.csproj similarity index 100% rename from TensorFlowNET.Visualization/TensorFlowNET.Visualization.csproj rename to src/TensorFlowNET.Visualization/TensorFlowNET.Visualization.csproj diff --git a/TensorFlowNET.Visualization/appsettings.Development.json b/src/TensorFlowNET.Visualization/appsettings.Development.json similarity index 100% rename from TensorFlowNET.Visualization/appsettings.Development.json rename to src/TensorFlowNET.Visualization/appsettings.Development.json diff --git a/TensorFlowNET.Visualization/appsettings.json b/src/TensorFlowNET.Visualization/appsettings.json similarity index 100% rename from TensorFlowNET.Visualization/appsettings.json rename to src/TensorFlowNET.Visualization/appsettings.json