Skip to content

Commit f0e0a84

Browse files
committed
tf.embedding_lookup
1 parent b15b98f commit f0e0a84

11 files changed

Lines changed: 179 additions & 10 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Tensorflow.Layers;
5+
using Tensorflow.Operations.Activation;
6+
7+
namespace Tensorflow
8+
{
9+
public static partial class tf
10+
{
11+
public static class layers
12+
{
13+
public static Tensor conv2d(Tensor inputs,
14+
int filters,
15+
int[] kernel_size,
16+
int[] strides = null,
17+
string padding = "valid",
18+
string data_format= "channels_last",
19+
int[] dilation_rate = null,
20+
bool use_bias = true,
21+
IActivation activation = null,
22+
IInitializer kernel_initializer = null)
23+
{
24+
if (strides == null)
25+
strides = new int[] { 1, 1 };
26+
if (dilation_rate == null)
27+
dilation_rate = new int[] { 1, 1 };
28+
29+
var layer = new Conv2D(filters, kernel_size);
30+
31+
return layer.apply(inputs);
32+
}
33+
}
34+
}
35+
}
Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Text;
4+
using Tensorflow.Operations.Activation;
45

56
namespace Tensorflow
67
{
@@ -9,9 +10,22 @@ public static partial class tf
910
public static class nn
1011
{
1112
public static (Tensor, Tensor) moments(Tensor x,
12-
int[] axes,
13-
string name = null,
14-
bool keep_dims = false) => nn_impl.moments(x, axes, name: name, keep_dims: keep_dims);
13+
int[] axes,
14+
string name = null,
15+
bool keep_dims = false) => nn_impl.moments(x,
16+
axes,
17+
name: name,
18+
keep_dims: keep_dims);
19+
20+
public static Tensor embedding_lookup(RefVariable @params,
21+
Tensor ids,
22+
string partition_strategy = "mod",
23+
string name = null) => embedding_ops._embedding_lookup_and_transform(@params,
24+
ids,
25+
partition_strategy: partition_strategy,
26+
name: name);
27+
28+
public static IActivation relu => new relu();
1529
}
1630
}
1731
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Tensorflow.Operations.Activation;
5+
6+
namespace Tensorflow.Layers
7+
{
8+
public class Conv2D
9+
{
10+
private int filters;
11+
private int[] kernel_size;
12+
private int[] strides;
13+
14+
public Conv2D(int filters,
15+
int[] kernel_size,
16+
int[] strides = null,
17+
string padding = "valid",
18+
string data_format = "channels_last",
19+
int[] dilation_rate = null,
20+
IActivation activation = null,
21+
bool use_bias = true,
22+
IInitializer kernel_initializer = null,
23+
IInitializer bias_initializer = null,
24+
bool trainable = true,
25+
string name = null)
26+
{
27+
28+
}
29+
30+
public Tensor apply(Tensor inputs)
31+
{
32+
throw new NotImplementedException("apply");
33+
}
34+
}
35+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Tensorflow.Operations.Activation
6+
{
7+
public interface IActivation
8+
{
9+
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Tensorflow.Operations.Activation
6+
{
7+
public class relu : IActivation
8+
{
9+
10+
}
11+
}

‎src/TensorFlowNET.Core/Operations/OpDefLibrary.cs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,13 @@ private Dictionary<string, object> ConvertToDict(dynamic dyn)
364364
object obj = propertyDescriptor.GetValue(dyn);
365365
string name = propertyDescriptor.Name;
366366
// avoid .net keyword
367-
if (name == "_ref_")
368-
name = "ref";
367+
switch (name)
368+
{
369+
case "_ref_":
370+
name = "ref";
371+
break;
372+
}
373+
369374
dictionary.Add(name, obj);
370375
}
371376
return dictionary;

‎src/TensorFlowNET.Core/Operations/array_ops.py.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,5 +268,9 @@ public static Tensor broadcast_static_shape(Tensor shape_x, Tensor shape_y)
268268
return Framework.common_shapes.broadcast_shape(shape_x, shape_y);
269269
}
270270

271+
public static Tensor gather(Tensor @params, Tensor indices, string name = null, int axis = 0)
272+
{
273+
return gen_array_ops.gather_v2(@params, indices, axis, name: name);
274+
}
271275
}
272276
}

‎src/TensorFlowNET.Core/Operations/embedding_ops.cs‎

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,45 @@
44

55
namespace Tensorflow
66
{
7-
public class embedding_ops
7+
public class embedding_ops : Python
88
{
9-
public Tensor _embedding_lookup_and_transform()
9+
/// <summary>
10+
/// Helper function for embedding_lookup and _compute_sampled_logits.
11+
/// </summary>
12+
/// <param name="params"></param>
13+
/// <param name="ids"></param>
14+
/// <param name="partition_strategy"></param>
15+
/// <param name="name"></param>
16+
/// <returns></returns>
17+
public static Tensor _embedding_lookup_and_transform(RefVariable @params,
18+
Tensor ids,
19+
string partition_strategy = "mod",
20+
string name = null,
21+
string max_norm = null)
1022
{
11-
throw new NotImplementedException("");
23+
return with(new ops.name_scope(name, "embedding_lookup", new { @params, ids }), scope =>
24+
{
25+
name = scope;
26+
int np = 1;
27+
ids = ops.convert_to_tensor(ids, name: "ids");
28+
if(np == 1)
29+
{
30+
var gather = array_ops.gather(@params, ids, name: name);
31+
var result = _clip(@params, ids, max_norm);
32+
33+
return array_ops.identity(result);
34+
}
35+
36+
throw new NotImplementedException("_embedding_lookup_and_transform");
37+
});
38+
}
39+
40+
public static Tensor _clip(RefVariable @params, Tensor ids, string max_norm = null)
41+
{
42+
if (max_norm == null)
43+
return @params;
44+
45+
throw new NotImplementedException("_clip");
1246
}
1347
}
1448
}

‎src/TensorFlowNET.Core/Operations/gen_array_ops.cs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ public static Tensor expand_dims(Tensor input, int axis, string name = null)
1919
return _op.outputs[0];
2020
}
2121

22+
public static Tensor gather_v2(Tensor @params, Tensor indices, int axis, string name = null)
23+
{
24+
var _op = _op_def_lib._apply_op_helper("GatherV2", name: name, new { @params, indices, axis });
25+
26+
return _op.outputs[0];
27+
}
28+
2229
public static Tensor greater<Tx, Ty>(Tx x, Ty y, string name = null)
2330
{
2431
var _op = _op_def_lib._apply_op_helper("Greater", name: name, args: new { x, y });

‎src/TensorFlowNET.Core/Variables/_VariableStore.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private RefVariable _true_getter(string name,
5151
VariableSynchronization synchronization = VariableSynchronization.AUTO,
5252
VariableAggregation aggregation = VariableAggregation.NONE)
5353
{
54-
bool is_scalar = shape.NDim == 0;
54+
bool is_scalar = !(shape is null) && shape.NDim == 0;
5555

5656
if (initializer is IInitializer init)
5757
{

0 commit comments

Comments
 (0)