Browse Source

_random still in progress.

overload random_uniform.
add more on random_seed.get_seed.
tags/v0.9
Oceania2018 6 years ago
parent
commit
3bd928cabb
5 changed files with 98 additions and 2 deletions
  1. +9
    -1
      src/TensorFlowNET.Core/Clustering/_InitializeClustersOpFactory.cs
  2. +6
    -1
      src/TensorFlowNET.Core/Framework/random_seed.py.cs
  3. +34
    -0
      src/TensorFlowNET.Core/Operations/embedding_ops.cs
  4. +24
    -0
      src/TensorFlowNET.Core/Operations/gen_random_ops.py.cs
  5. +25
    -0
      src/TensorFlowNET.Core/Operations/random_ops.py.cs

+ 9
- 1
src/TensorFlowNET.Core/Clustering/_InitializeClustersOpFactory.cs View File

@@ -121,7 +121,15 @@ namespace Tensorflow.Clustering

private Tensor _random()
{
throw new NotImplementedException("");
var reshape = array_ops.reshape(_num_remaining, new int[] { -1 });
var cast = math_ops.cast(_num_data, TF_DataType.TF_INT64);
var indices = random_ops.random_uniform(
reshape,
minval: 0,
maxval: cast,
seed: _random_seed,
dtype: TF_DataType.TF_INT64);
return embedding_ops.embedding_lookup(_inputs, indices, partition_strategy: "div");
}
}
}

+ 6
- 1
src/TensorFlowNET.Core/Framework/random_seed.py.cs View File

@@ -6,9 +6,14 @@ namespace Tensorflow
{
public class random_seed
{
private static int DEFAULT_GRAPH_SEED = 87654321;

public static (int?, int?) get_seed(int? op_seed = null)
{
return (null, null);
if (op_seed.HasValue)
return (DEFAULT_GRAPH_SEED, 0);
else
return (null, null);
}
}
}

+ 34
- 0
src/TensorFlowNET.Core/Operations/embedding_ops.cs View File

@@ -37,6 +37,27 @@ namespace Tensorflow
});
}

public static Tensor _embedding_lookup_and_transform(Tensor[] @params,
Tensor ids,
string partition_strategy = "mod",
string name = null,
string max_norm = null)
{
return with(ops.name_scope(name, "embedding_lookup", new { @params, ids }), scope =>
{
name = scope;
int np = @params.Length;
@params = ops.convert_n_to_tensor_or_indexed_slices(@params, name: "params");
ids = ops.convert_to_tensor(ids, name: "ids");
if (np == 1)
{

}
return array_ops.identity(null);
throw new NotImplementedException("_embedding_lookup_and_transform");
});
}

public static Tensor _clip(Tensor @params, Tensor ids, string max_norm = null)
{
if (max_norm == null)
@@ -44,5 +65,18 @@ namespace Tensorflow

throw new NotImplementedException("_clip");
}

public static Tensor embedding_lookup(Tensor[] @params, Tensor ids,
string partition_strategy = "mod",
string name = null,
bool validate_indices = true,
string max_norm = null)
{
return _embedding_lookup_and_transform(@params: @params,
ids: ids,
partition_strategy: partition_strategy,
name: name,
max_norm: max_norm);
}
}
}

+ 24
- 0
src/TensorFlowNET.Core/Operations/gen_random_ops.py.cs View File

@@ -31,6 +31,30 @@ namespace Tensorflow
return _op.outputs[0];
}

/// <summary>
/// Outputs random integers from a uniform distribution.
/// </summary>
/// <param name="shape"></param>
/// <param name="minval"></param>
/// <param name="maxval"></param>
/// <param name="seed"></param>
/// <param name="seed2"></param>
/// <param name="name"></param>
/// <returns></returns>
public static Tensor random_uniform_int(Tensor shape, Tensor minval, Tensor maxval, int? seed = 0, int? seed2 = 0, string name = null)
{
if (!seed.HasValue)
seed = 0;
if (!seed2.HasValue)
seed2 = 0;

var _op = _op_def_lib._apply_op_helper("RandomUniformInt",
name: name,
args: new { shape, minval, maxval, seed, seed2 });

return _op.outputs[0];
}

/// <summary>
/// Outputs random values from a uniform distribution.
/// </summary>


+ 25
- 0
src/TensorFlowNET.Core/Operations/random_ops.py.cs View File

@@ -64,6 +64,31 @@ namespace Tensorflow
});
}

public static Tensor random_uniform(Tensor shape,
long minval = 0,
Tensor maxval = null,
TF_DataType dtype = TF_DataType.TF_FLOAT,
int? seed = null,
string name = null)
{
return with(ops.name_scope(name, "random_uniform", new { shape, minval, maxval }), scope =>
{
name = scope;
var minTensor = ops.convert_to_tensor(minval, dtype: dtype, name: "min");
var maxTensor = ops.convert_to_tensor(maxval, dtype: dtype, name: "max");
var (seed1, seed2) = random_seed.get_seed(seed);
if (dtype.is_integer())
{
return gen_random_ops.random_uniform_int(shape, minTensor, maxTensor, seed: seed1, seed2: seed2, name: name);
}
else
{
var rnd = gen_random_ops.random_uniform(shape, dtype);
return math_ops.add(rnd * (maxTensor - minTensor), minTensor, name: name);
}
});
}

public static Tensor truncated_normal(int[] shape,
float mean = 0.0f,
float stddev = 1.0f,


Loading…
Cancel
Save