Skip to content

Commit 3bd928c

Browse files
committed
_random still in progress.
overload random_uniform. add more on random_seed.get_seed.
1 parent 2461d2f commit 3bd928c

5 files changed

Lines changed: 98 additions & 2 deletions

File tree

src/TensorFlowNET.Core/Clustering/_InitializeClustersOpFactory.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,15 @@ private Tensor _greedy_batch_sampler()
121121

122122
private Tensor _random()
123123
{
124-
throw new NotImplementedException("");
124+
var reshape = array_ops.reshape(_num_remaining, new int[] { -1 });
125+
var cast = math_ops.cast(_num_data, TF_DataType.TF_INT64);
126+
var indices = random_ops.random_uniform(
127+
reshape,
128+
minval: 0,
129+
maxval: cast,
130+
seed: _random_seed,
131+
dtype: TF_DataType.TF_INT64);
132+
return embedding_ops.embedding_lookup(_inputs, indices, partition_strategy: "div");
125133
}
126134
}
127135
}

src/TensorFlowNET.Core/Framework/random_seed.py.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@ namespace Tensorflow
66
{
77
public class random_seed
88
{
9+
private static int DEFAULT_GRAPH_SEED = 87654321;
10+
911
public static (int?, int?) get_seed(int? op_seed = null)
1012
{
11-
return (null, null);
13+
if (op_seed.HasValue)
14+
return (DEFAULT_GRAPH_SEED, 0);
15+
else
16+
return (null, null);
1217
}
1318
}
1419
}

src/TensorFlowNET.Core/Operations/embedding_ops.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,46 @@ public static Tensor _embedding_lookup_and_transform(RefVariable @params,
3737
});
3838
}
3939

40+
public static Tensor _embedding_lookup_and_transform(Tensor[] @params,
41+
Tensor ids,
42+
string partition_strategy = "mod",
43+
string name = null,
44+
string max_norm = null)
45+
{
46+
return with(ops.name_scope(name, "embedding_lookup", new { @params, ids }), scope =>
47+
{
48+
name = scope;
49+
int np = @params.Length;
50+
@params = ops.convert_n_to_tensor_or_indexed_slices(@params, name: "params");
51+
ids = ops.convert_to_tensor(ids, name: "ids");
52+
if (np == 1)
53+
{
54+
55+
}
56+
return array_ops.identity(null);
57+
throw new NotImplementedException("_embedding_lookup_and_transform");
58+
});
59+
}
60+
4061
public static Tensor _clip(Tensor @params, Tensor ids, string max_norm = null)
4162
{
4263
if (max_norm == null)
4364
return @params;
4465

4566
throw new NotImplementedException("_clip");
4667
}
68+
69+
public static Tensor embedding_lookup(Tensor[] @params, Tensor ids,
70+
string partition_strategy = "mod",
71+
string name = null,
72+
bool validate_indices = true,
73+
string max_norm = null)
74+
{
75+
return _embedding_lookup_and_transform(@params: @params,
76+
ids: ids,
77+
partition_strategy: partition_strategy,
78+
name: name,
79+
max_norm: max_norm);
80+
}
4781
}
4882
}

src/TensorFlowNET.Core/Operations/gen_random_ops.py.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,30 @@ public static Tensor random_standard_normal(Tensor shape, TF_DataType dtype = TF
3131
return _op.outputs[0];
3232
}
3333

34+
/// <summary>
35+
/// Outputs random integers from a uniform distribution.
36+
/// </summary>
37+
/// <param name="shape"></param>
38+
/// <param name="minval"></param>
39+
/// <param name="maxval"></param>
40+
/// <param name="seed"></param>
41+
/// <param name="seed2"></param>
42+
/// <param name="name"></param>
43+
/// <returns></returns>
44+
public static Tensor random_uniform_int(Tensor shape, Tensor minval, Tensor maxval, int? seed = 0, int? seed2 = 0, string name = null)
45+
{
46+
if (!seed.HasValue)
47+
seed = 0;
48+
if (!seed2.HasValue)
49+
seed2 = 0;
50+
51+
var _op = _op_def_lib._apply_op_helper("RandomUniformInt",
52+
name: name,
53+
args: new { shape, minval, maxval, seed, seed2 });
54+
55+
return _op.outputs[0];
56+
}
57+
3458
/// <summary>
3559
/// Outputs random values from a uniform distribution.
3660
/// </summary>

src/TensorFlowNET.Core/Operations/random_ops.py.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,31 @@ public static Tensor random_uniform(int[] shape,
6464
});
6565
}
6666

67+
public static Tensor random_uniform(Tensor shape,
68+
long minval = 0,
69+
Tensor maxval = null,
70+
TF_DataType dtype = TF_DataType.TF_FLOAT,
71+
int? seed = null,
72+
string name = null)
73+
{
74+
return with(ops.name_scope(name, "random_uniform", new { shape, minval, maxval }), scope =>
75+
{
76+
name = scope;
77+
var minTensor = ops.convert_to_tensor(minval, dtype: dtype, name: "min");
78+
var maxTensor = ops.convert_to_tensor(maxval, dtype: dtype, name: "max");
79+
var (seed1, seed2) = random_seed.get_seed(seed);
80+
if (dtype.is_integer())
81+
{
82+
return gen_random_ops.random_uniform_int(shape, minTensor, maxTensor, seed: seed1, seed2: seed2, name: name);
83+
}
84+
else
85+
{
86+
var rnd = gen_random_ops.random_uniform(shape, dtype);
87+
return math_ops.add(rnd * (maxTensor - minTensor), minTensor, name: name);
88+
}
89+
});
90+
}
91+
6792
public static Tensor truncated_normal(int[] shape,
6893
float mean = 0.0f,
6994
float stddev = 1.0f,

0 commit comments

Comments
 (0)