diff --git a/src/TensorFlowNET.Core/APIs/keras.layers.cs b/src/TensorFlowNET.Core/APIs/keras.layers.cs new file mode 100644 index 00000000..016c883e --- /dev/null +++ b/src/TensorFlowNET.Core/APIs/keras.layers.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Tensorflow.Keras; +using Tensorflow.Keras.Engine; +using Tensorflow.Keras.Layers; + +namespace Tensorflow +{ + public static partial class keras + { + public static class layers + { + public static Embedding Embedding(int input_dim, int output_dim, + string embeddings_initializer = "uniform", + bool mask_zero = false) => new Embedding(input_dim, output_dim, + embeddings_initializer, + mask_zero); + } + } +} diff --git a/src/TensorFlowNET.Core/APIs/tf.init.cs b/src/TensorFlowNET.Core/APIs/tf.init.cs index c6455341..11ae5fe4 100644 --- a/src/TensorFlowNET.Core/APIs/tf.init.cs +++ b/src/TensorFlowNET.Core/APIs/tf.init.cs @@ -10,7 +10,8 @@ namespace Tensorflow public static IInitializer zeros_initializer => new Zeros(); public static IInitializer ones_initializer => new Ones(); public static IInitializer glorot_uniform_initializer => new GlorotUniform(); - + public static IInitializer uniform_initializer => new RandomUniform(); + public static variable_scope variable_scope(string name, string default_name = null, object values = null, diff --git a/src/TensorFlowNET.Core/Keras/Engine/Model.cs b/src/TensorFlowNET.Core/Keras/Engine/Model.cs index a0ad4a53..697a1938 100644 --- a/src/TensorFlowNET.Core/Keras/Engine/Model.cs +++ b/src/TensorFlowNET.Core/Keras/Engine/Model.cs @@ -4,7 +4,12 @@ using System.Text; namespace Tensorflow.Keras.Engine { - internal class Model : Network + public class Model : Network { + public Model(string name = null) + : base(name: name) + { + + } } } diff --git a/src/TensorFlowNET.Core/Keras/Engine/Network.cs b/src/TensorFlowNET.Core/Keras/Engine/Network.cs index 43594022..e50d7dd9 100644 --- a/src/TensorFlowNET.Core/Keras/Engine/Network.cs +++ b/src/TensorFlowNET.Core/Keras/Engine/Network.cs @@ -7,5 +7,29 @@ namespace Tensorflow.Keras.Engine { public class Network : Layer { + protected bool _is_compiled; + protected bool _expects_training_arg; + protected bool _compute_output_and_mask_jointly; + + public Network(string name = null) + : base(name: name) + { + + } + + protected virtual void _init_subclassed_network(string name = null) + { + _base_init(name: name); + } + + protected virtual void _base_init(string name = null) + { + _init_set_name(name); + trainable = true; + _is_compiled = false; + _expects_training_arg = false; + _compute_output_and_mask_jointly = false; + supports_masking = false; + } } } diff --git a/src/TensorFlowNET.Core/Keras/Engine/Sequential.cs b/src/TensorFlowNET.Core/Keras/Engine/Sequential.cs index d3762bfb..a83c06e3 100644 --- a/src/TensorFlowNET.Core/Keras/Engine/Sequential.cs +++ b/src/TensorFlowNET.Core/Keras/Engine/Sequential.cs @@ -1,24 +1,38 @@ using System; using System.Collections.Generic; using System.Text; +using Tensorflow.Keras.Layers; namespace Tensorflow.Keras.Engine { - public class Sequential : Network, IPython + public class Sequential : Model, IPython { - public void Dispose() + public Sequential(string name = null) + : base(name: name) { - throw new NotImplementedException(); + supports_masking = true; + _compute_output_and_mask_jointly = true; } public void __enter__() { - throw new NotImplementedException(); + + } + + public void add(Layer layer) + { + built = false; + var set_inputs = false; } public void __exit__() { - throw new NotImplementedException(); + + } + + public void Dispose() + { + } } } diff --git a/src/TensorFlowNET.Core/Keras/Layers/Embedding.cs b/src/TensorFlowNET.Core/Keras/Layers/Embedding.cs new file mode 100644 index 00000000..c7285def --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/Layers/Embedding.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras.Layers +{ + public class Embedding : Layer + { + private int input_dim; + private int output_dim; + private bool mask_zero; + + public Embedding(int input_dim, int output_dim, + IInitializer embeddings_initializer = null, + bool mask_zero = false) + { + this.input_dim = input_dim; + this.output_dim = output_dim; + if (embeddings_initializer == null) + embeddings_initializer = tf.uniform_initializer; + this.mask_zero = mask_zero; + supports_masking = mask_zero; + } + } +} diff --git a/src/TensorFlowNET.Core/Operations/ControlFlows/CondContext.cs b/src/TensorFlowNET.Core/Operations/ControlFlows/CondContext.cs index 9516c42f..c1a87224 100644 --- a/src/TensorFlowNET.Core/Operations/ControlFlows/CondContext.cs +++ b/src/TensorFlowNET.Core/Operations/ControlFlows/CondContext.cs @@ -17,11 +17,6 @@ namespace Tensorflow.Operations private Tensor _pred; public Tensor pred => _pred; - /// - /// The predicate tensor in this branch - /// - private Tensor _pivot; - /// /// 0 or 1 representing this branch /// diff --git a/src/TensorFlowNET.Core/Operations/ControlFlows/ControlFlowContext.cs b/src/TensorFlowNET.Core/Operations/ControlFlows/ControlFlowContext.cs index 7079606f..8776f171 100644 --- a/src/TensorFlowNET.Core/Operations/ControlFlows/ControlFlowContext.cs +++ b/src/TensorFlowNET.Core/Operations/ControlFlows/ControlFlowContext.cs @@ -6,6 +6,11 @@ namespace Tensorflow.Operations { public abstract class ControlFlowContext : IPython, IControlFlowContext { + /// + /// The predicate tensor in this branch + /// + protected Tensor _pivot; + protected Stack _context_stack; public ControlFlowContext() { @@ -28,6 +33,29 @@ namespace Tensorflow.Operations graph._set_control_flow_context(this); } + public void AddOp(Operation op) + { + _AddOpInternal(op); + } + + protected virtual void _AddOpInternal(Operation op) + { + if(op.inputs.Length == 0) + { + _RemoveExternalControlEdges(op); + op._add_control_input(_pivot.op); + } + else + { + + } + } + + protected virtual void _RemoveExternalControlEdges(Operation op) + { + var internal_control_inputs = op.control_inputs; + } + public void Exit() { var graph = ops.get_default_graph(); diff --git a/src/TensorFlowNET.Core/Operations/ControlFlows/IControlFlowContext.cs b/src/TensorFlowNET.Core/Operations/ControlFlows/IControlFlowContext.cs index 52719538..6bd8c6e2 100644 --- a/src/TensorFlowNET.Core/Operations/ControlFlows/IControlFlowContext.cs +++ b/src/TensorFlowNET.Core/Operations/ControlFlows/IControlFlowContext.cs @@ -6,5 +6,6 @@ namespace Tensorflow { public interface IControlFlowContext { + void AddOp(Operation op); } } diff --git a/src/TensorFlowNET.Core/Operations/Initializers/RandomUniform.cs b/src/TensorFlowNET.Core/Operations/Initializers/RandomUniform.cs new file mode 100644 index 00000000..2055bf83 --- /dev/null +++ b/src/TensorFlowNET.Core/Operations/Initializers/RandomUniform.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Operations.Initializers +{ + public class RandomUniform : IInitializer + { + private int? seed; + private float minval; + private float maxval; + private TF_DataType dtype; + + public RandomUniform() + { + + } + + public Tensor call(TensorShape shape, TF_DataType dtype = TF_DataType.DtInvalid) + { + return random_ops.random_uniform(shape, + minval: minval, + maxval: maxval, + dtype: dtype, + seed: seed); + } + + public object get_config() + { + return new { + minval, + maxval, + seed, + dtype + }; + } + } +} diff --git a/src/TensorFlowNET.Core/Operations/Operation.Control.cs b/src/TensorFlowNET.Core/Operations/Operation.Control.cs index fec3c89d..39a011a8 100644 --- a/src/TensorFlowNET.Core/Operations/Operation.Control.cs +++ b/src/TensorFlowNET.Core/Operations/Operation.Control.cs @@ -18,14 +18,20 @@ namespace Tensorflow { } + + if (_control_flow_context != null) + _control_flow_context.AddOp(this); + } + + public void _add_control_input(Operation op) + { + c_api.TF_AddControlInput(_handle, op); } public void _add_control_inputs(Operation[] ops) { - foreach(var op in ops) - { - c_api.TF_AddControlInput(graph, op); - } + foreach (var op in ops) + _add_control_input(op); } public void _set_control_flow_context(IControlFlowContext ctx) diff --git a/src/TensorFlowNET.Core/TensorFlowNET.Core.csproj b/src/TensorFlowNET.Core/TensorFlowNET.Core.csproj index b9b980f9..6989c08c 100644 --- a/src/TensorFlowNET.Core/TensorFlowNET.Core.csproj +++ b/src/TensorFlowNET.Core/TensorFlowNET.Core.csproj @@ -54,4 +54,8 @@ Docs: https://tensorflownet.readthedocs.io + + + + diff --git a/test/TensorFlowNET.Examples/TextClassificationWithMovieReviews.cs b/test/TensorFlowNET.Examples/TextClassificationWithMovieReviews.cs index cd59287b..634cf66a 100644 --- a/test/TensorFlowNET.Examples/TextClassificationWithMovieReviews.cs +++ b/test/TensorFlowNET.Examples/TextClassificationWithMovieReviews.cs @@ -37,6 +37,7 @@ namespace TensorFlowNET.Examples int vocab_size = 10000; var model = keras.Sequential(); + model.add(keras.layers.Embedding(vocab_size, 16)); } private ((NDArray, NDArray), (NDArray, NDArray)) PrepareData()