Browse Source

add Embedding layer

tags/v0.8.0
Oceania2018 6 years ago
parent
commit
ac523edda0
13 changed files with 179 additions and 16 deletions
  1. +21
    -0
      src/TensorFlowNET.Core/APIs/keras.layers.cs
  2. +2
    -1
      src/TensorFlowNET.Core/APIs/tf.init.cs
  3. +6
    -1
      src/TensorFlowNET.Core/Keras/Engine/Model.cs
  4. +24
    -0
      src/TensorFlowNET.Core/Keras/Engine/Network.cs
  5. +19
    -5
      src/TensorFlowNET.Core/Keras/Engine/Sequential.cs
  6. +25
    -0
      src/TensorFlowNET.Core/Keras/Layers/Embedding.cs
  7. +0
    -5
      src/TensorFlowNET.Core/Operations/ControlFlows/CondContext.cs
  8. +28
    -0
      src/TensorFlowNET.Core/Operations/ControlFlows/ControlFlowContext.cs
  9. +1
    -0
      src/TensorFlowNET.Core/Operations/ControlFlows/IControlFlowContext.cs
  10. +38
    -0
      src/TensorFlowNET.Core/Operations/Initializers/RandomUniform.cs
  11. +10
    -4
      src/TensorFlowNET.Core/Operations/Operation.Control.cs
  12. +4
    -0
      src/TensorFlowNET.Core/TensorFlowNET.Core.csproj
  13. +1
    -0
      test/TensorFlowNET.Examples/TextClassificationWithMovieReviews.cs

+ 21
- 0
src/TensorFlowNET.Core/APIs/keras.layers.cs View File

@@ -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);
}
}
}

+ 2
- 1
src/TensorFlowNET.Core/APIs/tf.init.cs View File

@@ -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,


+ 6
- 1
src/TensorFlowNET.Core/Keras/Engine/Model.cs View File

@@ -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)
{

}
}
}

+ 24
- 0
src/TensorFlowNET.Core/Keras/Engine/Network.cs View File

@@ -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;
}
}
}

+ 19
- 5
src/TensorFlowNET.Core/Keras/Engine/Sequential.cs View File

@@ -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()
{

}
}
}

+ 25
- 0
src/TensorFlowNET.Core/Keras/Layers/Embedding.cs View File

@@ -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;
}
}
}

+ 0
- 5
src/TensorFlowNET.Core/Operations/ControlFlows/CondContext.cs View File

@@ -17,11 +17,6 @@ namespace Tensorflow.Operations
private Tensor _pred;
public Tensor pred => _pred;

/// <summary>
/// The predicate tensor in this branch
/// </summary>
private Tensor _pivot;

/// <summary>
/// 0 or 1 representing this branch
/// </summary>


+ 28
- 0
src/TensorFlowNET.Core/Operations/ControlFlows/ControlFlowContext.cs View File

@@ -6,6 +6,11 @@ namespace Tensorflow.Operations
{
public abstract class ControlFlowContext : IPython, IControlFlowContext
{
/// <summary>
/// The predicate tensor in this branch
/// </summary>
protected Tensor _pivot;

protected Stack<IControlFlowContext> _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();


+ 1
- 0
src/TensorFlowNET.Core/Operations/ControlFlows/IControlFlowContext.cs View File

@@ -6,5 +6,6 @@ namespace Tensorflow
{
public interface IControlFlowContext
{
void AddOp(Operation op);
}
}

+ 38
- 0
src/TensorFlowNET.Core/Operations/Initializers/RandomUniform.cs View File

@@ -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
};
}
}
}

+ 10
- 4
src/TensorFlowNET.Core/Operations/Operation.Control.cs View File

@@ -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)


+ 4
- 0
src/TensorFlowNET.Core/TensorFlowNET.Core.csproj View File

@@ -54,4 +54,8 @@ Docs: https://tensorflownet.readthedocs.io</Description>
<ProjectReference Include="..\..\..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Keras\Initializers\" />
</ItemGroup>

</Project>

+ 1
- 0
test/TensorFlowNET.Examples/TextClassificationWithMovieReviews.cs View File

@@ -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()


Loading…
Cancel
Save