Browse Source

variance_scaling_initializer

tags/v0.13
Oceania2018 6 years ago
parent
commit
c909310838
8 changed files with 48 additions and 18 deletions
  1. +6
    -6
      src/TensorFlowNET.Core/APIs/tf.init.cs
  2. +6
    -3
      src/TensorFlowNET.Core/APIs/tf.random.cs
  3. +3
    -0
      src/TensorFlowNET.Core/Binding.Util.cs
  4. +7
    -1
      src/TensorFlowNET.Core/Data/DatasetV2.cs
  5. +10
    -0
      src/TensorFlowNET.Core/Graphs/Graph.cs
  6. +1
    -1
      src/TensorFlowNET.Core/Keras/Initializers.cs
  7. +4
    -1
      src/TensorFlowNET.Core/Operations/Initializers/GlorotUniform.cs
  8. +11
    -6
      src/TensorFlowNET.Core/Operations/Initializers/VarianceScaling.cs

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

@@ -66,20 +66,20 @@ namespace Tensorflow
/// <summary>
/// Initializer capable of adapting its scale to the shape of weights tensors.
/// </summary>
/// <param name="scale"></param>
/// <param name="factor"></param>
/// <param name="mode"></param>
/// <param name="distribution"></param>
/// <param name="seed"></param>
/// <param name="dtype"></param>
/// <returns></returns>
public IInitializer variance_scaling_initializer(float scale = 1.0f,
string mode = "fan_in",
string distribution = "truncated_normal",
public IInitializer variance_scaling_initializer(float factor = 1.0f,
string mode = "FAN_IN",
bool uniform = false,
int? seed = null,
TF_DataType dtype = TF_DataType.TF_FLOAT) => new VarianceScaling(
scale: scale,
factor: factor,
mode: mode,
distribution: distribution,
uniform: uniform,
seed: seed,
dtype: dtype);
}


+ 6
- 3
src/TensorFlowNET.Core/APIs/tf.random.cs View File

@@ -28,21 +28,21 @@ namespace Tensorflow
/// <param name="seed"></param>
/// <param name="name"></param>
/// <returns></returns>
public Tensor random_normal(int[] shape,
public Tensor random_normal(TensorShape shape,
float mean = 0.0f,
float stddev = 1.0f,
TF_DataType dtype = TF_DataType.TF_FLOAT,
int? seed = null,
string name = null) => random_ops.random_normal(shape, mean, stddev, dtype, seed, name);

public Tensor random_uniform(int[] shape,
public Tensor random_uniform(TensorShape shape,
float minval = 0,
float maxval = 1,
TF_DataType dtype = TF_DataType.TF_FLOAT,
int? seed = null,
string name = null) => random_ops.random_uniform(shape, minval, maxval, dtype, seed, name);

public Tensor truncated_normal(int[] shape,
public Tensor truncated_normal(TensorShape shape,
float mean = 0.0f,
float stddev = 1.0f,
TF_DataType dtype = TF_DataType.TF_FLOAT,
@@ -62,5 +62,8 @@ namespace Tensorflow
/// </returns>
public Tensor random_shuffle(Tensor value, int? seed = null, string name = null)
=> random_ops.random_shuffle(value, seed: seed, name: name);

public void set_random_seed(int seed)
=> ops.get_default_graph().seed = seed;
}
}

+ 3
- 0
src/TensorFlowNET.Core/Binding.Util.cs View File

@@ -273,6 +273,9 @@ namespace Tensorflow
return sum;
}

public static double sum(IEnumerable<int> enumerable)
=> enumerable.Sum();

public static double sum<TKey, TValue>(Dictionary<TKey, TValue> values)
{
return sum(values.Keys);


+ 7
- 1
src/TensorFlowNET.Core/Data/DatasetV2.cs View File

@@ -1,4 +1,6 @@
namespace Tensorflow.Data
using System;

namespace Tensorflow.Data
{
/// <summary>
/// Represents a potentially large set of elements.
@@ -11,5 +13,9 @@
/// </summary>
public class DatasetV2
{
public static DatasetV2 from_generator()
{
throw new NotImplementedException("");
}
}
}

+ 10
- 0
src/TensorFlowNET.Core/Graphs/Graph.cs View File

@@ -107,6 +107,16 @@ namespace Tensorflow

public bool building_function;
int _seed;
public int seed
{
get => _seed;
set
{
_seed = value;
}
}
public Graph()
{
_handle = c_api.TF_NewGraph();


+ 1
- 1
src/TensorFlowNET.Core/Keras/Initializers.cs View File

@@ -27,7 +27,7 @@ namespace Tensorflow.Keras
/// <returns></returns>
public IInitializer he_normal(int? seed = null)
{
return new VarianceScaling(scale: 2.0f, mode: "fan_in", distribution: "truncated_normal", seed: seed);
return new VarianceScaling(factor: 2.0f, mode: "fan_in", seed: seed);
}
}
}

+ 4
- 1
src/TensorFlowNET.Core/Operations/Initializers/GlorotUniform.cs View File

@@ -22,7 +22,10 @@ namespace Tensorflow.Operations.Initializers
string mode = "fan_avg",
string distribution = "uniform",
int? seed = null,
TF_DataType dtype = TF_DataType.TF_FLOAT) : base(scale, mode, distribution, seed, dtype)
TF_DataType dtype = TF_DataType.TF_FLOAT) : base(factor: scale,
mode: mode,
seed: seed,
dtype: dtype)
{

}


+ 11
- 6
src/TensorFlowNET.Core/Operations/Initializers/VarianceScaling.cs View File

@@ -31,17 +31,22 @@ namespace Tensorflow.Operations.Initializers
protected int? _seed;
protected TF_DataType _dtype;

public VarianceScaling(float scale = 1.0f,
string mode = "fan_in",
string distribution = "truncated_normal",
public VarianceScaling(float factor = 2.0f,
string mode = "FAN_IN",
bool uniform = false,
int? seed = null,
TF_DataType dtype = TF_DataType.TF_FLOAT)
{
if (scale < 0)
if (!dtype.is_floating())
throw new TypeError("Cannot create initializer for non-floating point type.");
if (!new string[] { "FAN_IN", "FAN_OUT", "FAN_AVG" }.Contains(mode))
throw new TypeError($"Unknown {mode} %s [FAN_IN, FAN_OUT, FAN_AVG]");

if (factor < 0)
throw new ValueError("`scale` must be positive float.");
_scale = scale;

_scale = factor;
_mode = mode;
_distribution = distribution;
_seed = seed;
_dtype = dtype;
}


Loading…
Cancel
Save