| @@ -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); | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -10,7 +10,8 @@ namespace Tensorflow | |||||
| public static IInitializer zeros_initializer => new Zeros(); | public static IInitializer zeros_initializer => new Zeros(); | ||||
| public static IInitializer ones_initializer => new Ones(); | public static IInitializer ones_initializer => new Ones(); | ||||
| public static IInitializer glorot_uniform_initializer => new GlorotUniform(); | public static IInitializer glorot_uniform_initializer => new GlorotUniform(); | ||||
| public static IInitializer uniform_initializer => new RandomUniform(); | |||||
| public static variable_scope variable_scope(string name, | public static variable_scope variable_scope(string name, | ||||
| string default_name = null, | string default_name = null, | ||||
| object values = null, | object values = null, | ||||
| @@ -4,7 +4,12 @@ using System.Text; | |||||
| namespace Tensorflow.Keras.Engine | namespace Tensorflow.Keras.Engine | ||||
| { | { | ||||
| internal class Model : Network | |||||
| public class Model : Network | |||||
| { | { | ||||
| public Model(string name = null) | |||||
| : base(name: name) | |||||
| { | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| @@ -7,5 +7,29 @@ namespace Tensorflow.Keras.Engine | |||||
| { | { | ||||
| public class Network : Layer | 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; | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| @@ -1,24 +1,38 @@ | |||||
| using System; | using System; | ||||
| using System.Collections.Generic; | using System.Collections.Generic; | ||||
| using System.Text; | using System.Text; | ||||
| using Tensorflow.Keras.Layers; | |||||
| namespace Tensorflow.Keras.Engine | 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__() | public void __enter__() | ||||
| { | { | ||||
| throw new NotImplementedException(); | |||||
| } | |||||
| public void add(Layer layer) | |||||
| { | |||||
| built = false; | |||||
| var set_inputs = false; | |||||
| } | } | ||||
| public void __exit__() | public void __exit__() | ||||
| { | { | ||||
| throw new NotImplementedException(); | |||||
| } | |||||
| public void Dispose() | |||||
| { | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| @@ -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; | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -17,11 +17,6 @@ namespace Tensorflow.Operations | |||||
| private Tensor _pred; | private Tensor _pred; | ||||
| public Tensor pred => _pred; | public Tensor pred => _pred; | ||||
| /// <summary> | |||||
| /// The predicate tensor in this branch | |||||
| /// </summary> | |||||
| private Tensor _pivot; | |||||
| /// <summary> | /// <summary> | ||||
| /// 0 or 1 representing this branch | /// 0 or 1 representing this branch | ||||
| /// </summary> | /// </summary> | ||||
| @@ -6,6 +6,11 @@ namespace Tensorflow.Operations | |||||
| { | { | ||||
| public abstract class ControlFlowContext : IPython, IControlFlowContext | public abstract class ControlFlowContext : IPython, IControlFlowContext | ||||
| { | { | ||||
| /// <summary> | |||||
| /// The predicate tensor in this branch | |||||
| /// </summary> | |||||
| protected Tensor _pivot; | |||||
| protected Stack<IControlFlowContext> _context_stack; | protected Stack<IControlFlowContext> _context_stack; | ||||
| public ControlFlowContext() | public ControlFlowContext() | ||||
| { | { | ||||
| @@ -28,6 +33,29 @@ namespace Tensorflow.Operations | |||||
| graph._set_control_flow_context(this); | 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() | public void Exit() | ||||
| { | { | ||||
| var graph = ops.get_default_graph(); | var graph = ops.get_default_graph(); | ||||
| @@ -6,5 +6,6 @@ namespace Tensorflow | |||||
| { | { | ||||
| public interface IControlFlowContext | public interface IControlFlowContext | ||||
| { | { | ||||
| void AddOp(Operation op); | |||||
| } | } | ||||
| } | } | ||||
| @@ -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 | |||||
| }; | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -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) | 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) | public void _set_control_flow_context(IControlFlowContext ctx) | ||||
| @@ -54,4 +54,8 @@ Docs: https://tensorflownet.readthedocs.io</Description> | |||||
| <ProjectReference Include="..\..\..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj" /> | <ProjectReference Include="..\..\..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj" /> | ||||
| </ItemGroup> | </ItemGroup> | ||||
| <ItemGroup> | |||||
| <Folder Include="Keras\Initializers\" /> | |||||
| </ItemGroup> | |||||
| </Project> | </Project> | ||||
| @@ -37,6 +37,7 @@ namespace TensorFlowNET.Examples | |||||
| int vocab_size = 10000; | int vocab_size = 10000; | ||||
| var model = keras.Sequential(); | var model = keras.Sequential(); | ||||
| model.add(keras.layers.Embedding(vocab_size, 16)); | |||||
| } | } | ||||
| private ((NDArray, NDArray), (NDArray, NDArray)) PrepareData() | private ((NDArray, NDArray), (NDArray, NDArray)) PrepareData() | ||||