diff --git a/src/TensorFlowNET.Core/Framework/importer.py.cs b/src/TensorFlowNET.Core/Framework/importer.py.cs index 9070d27b..2b9dd6a4 100644 --- a/src/TensorFlowNET.Core/Framework/importer.py.cs +++ b/src/TensorFlowNET.Core/Framework/importer.py.cs @@ -26,7 +26,7 @@ namespace Tensorflow string prefix = ""; var graph = ops.get_default_graph(); - with(new ops.name_scope(name, "import", input_map.Values), scope => + with(ops.name_scope(name, "import", input_map.Values), scope => { prefix = scope; /*if (!string.IsNullOrEmpty(prefix)) diff --git a/src/TensorFlowNET.Core/Framework/tf.ops.cs b/src/TensorFlowNET.Core/Framework/tf.ops.cs index 1d5309ad..f789d473 100644 --- a/src/TensorFlowNET.Core/Framework/tf.ops.cs +++ b/src/TensorFlowNET.Core/Framework/tf.ops.cs @@ -16,8 +16,8 @@ namespace Tensorflow /// The default name to use if the name argument is None. /// The list of Tensor arguments that are passed to the op function. /// The scope name. - public static ops.name_scope name_scope(string name, + public static ops.NameScope name_scope(string name, string default_name = "", - object values = null) => new ops.name_scope(name, default_name, values); + object values = null) => new ops.NameScope(name, default_name, values); } } diff --git a/src/TensorFlowNET.Core/Gradients/gradients_impl.py.cs b/src/TensorFlowNET.Core/Gradients/gradients_impl.py.cs index a4af9caa..abe9ef2b 100644 --- a/src/TensorFlowNET.Core/Gradients/gradients_impl.py.cs +++ b/src/TensorFlowNET.Core/Gradients/gradients_impl.py.cs @@ -58,7 +58,7 @@ namespace Tensorflow **/ var grads = new Dictionary(); - with(new ops.name_scope(name, "gradients", values: all), scope => + with(ops.name_scope(name, "gradients", values: all), scope => { string grad_scope = scope; // Get a uid for this call to gradients that can be used to help @@ -131,7 +131,7 @@ namespace Tensorflow // for ops that do not have gradients. var grad_fn = ops.get_gradient_function(op); - with(new ops.name_scope(op.name + "_grad"), scope1 => + with(ops.name_scope(op.name + "_grad"), scope1 => { string name1 = scope1; if (grad_fn != null) diff --git a/src/TensorFlowNET.Core/Keras/Engine/Layer.cs b/src/TensorFlowNET.Core/Keras/Engine/Layer.cs index 53135900..941d6904 100644 --- a/src/TensorFlowNET.Core/Keras/Engine/Layer.cs +++ b/src/TensorFlowNET.Core/Keras/Engine/Layer.cs @@ -31,7 +31,7 @@ namespace Tensorflow.Keras.Engine bool build_graph = tf_utils.are_all_symbolic_tensors(input_list); // Handle Keras mask propagation from previous layer to current layer. - Python.with(new ops.name_scope(_name_scope()), delegate + Python.with(ops.name_scope(_name_scope()), delegate { if (!built) { diff --git a/src/TensorFlowNET.Core/Layers/Layer.cs b/src/TensorFlowNET.Core/Layers/Layer.cs index bd3e4e4d..128d0af1 100644 --- a/src/TensorFlowNET.Core/Layers/Layer.cs +++ b/src/TensorFlowNET.Core/Layers/Layer.cs @@ -92,7 +92,7 @@ namespace Tensorflow.Layers auxiliary_name_scope: false), scope => { _current_scope = scope; - Python.with(new ops.name_scope(_name_scope()), delegate + Python.with(ops.name_scope(_name_scope()), delegate { diff --git a/src/TensorFlowNET.Core/Operations/Distributions/normal.py.cs b/src/TensorFlowNET.Core/Operations/Distributions/normal.py.cs index 2c1caa27..4162ae25 100644 --- a/src/TensorFlowNET.Core/Operations/Distributions/normal.py.cs +++ b/src/TensorFlowNET.Core/Operations/Distributions/normal.py.cs @@ -33,7 +33,7 @@ namespace Tensorflow parameters.Add("validate_args", validate_args); parameters.Add("allow_nan_stats", allow_nan_stats); - with(new ops.name_scope(name, "", new { loc, scale }), scope => + with(ops.name_scope(name, "", new { loc, scale }), scope => { with(ops.control_dependencies(validate_args ? new Operation[] { scale.op} : new Operation[] { }), cd => { diff --git a/src/TensorFlowNET.Core/Operations/Losses/losses_impl.py.cs b/src/TensorFlowNET.Core/Operations/Losses/losses_impl.py.cs index 02d7f180..ecfd0984 100644 --- a/src/TensorFlowNET.Core/Operations/Losses/losses_impl.py.cs +++ b/src/TensorFlowNET.Core/Operations/Losses/losses_impl.py.cs @@ -12,7 +12,7 @@ namespace Tensorflow string scope = "", string loss_collection= "losses") { - with(new ops.name_scope(scope, + with(ops.name_scope(scope, "sparse_softmax_cross_entropy_loss", (logits, labels, weights)), namescope => diff --git a/src/TensorFlowNET.Core/Operations/OpDefLibrary.cs b/src/TensorFlowNET.Core/Operations/OpDefLibrary.cs index dcdda4bd..a18b109e 100644 --- a/src/TensorFlowNET.Core/Operations/OpDefLibrary.cs +++ b/src/TensorFlowNET.Core/Operations/OpDefLibrary.cs @@ -44,7 +44,7 @@ namespace Tensorflow var input_types = new List(); dynamic values = null; - return with(new ops.name_scope(name), scope => + return with(ops.name_scope(name), scope => { var inferred_from = new Dictionary(); var base_types = new List(); diff --git a/src/TensorFlowNET.Core/Operations/array_ops.py.cs b/src/TensorFlowNET.Core/Operations/array_ops.py.cs index 634278e3..c32bf397 100644 --- a/src/TensorFlowNET.Core/Operations/array_ops.py.cs +++ b/src/TensorFlowNET.Core/Operations/array_ops.py.cs @@ -12,7 +12,7 @@ namespace Tensorflow public static Tensor zeros(Shape shape, TF_DataType dtype = TF_DataType.TF_FLOAT, string name = null) { dtype = dtype.as_base_dtype(); - return with(new ops.name_scope(name, "zeros", shape), scope => + return with(ops.name_scope(name, "zeros", shape), scope => { name = scope; switch (dtype) @@ -68,7 +68,7 @@ namespace Tensorflow private static Tensor ones_like_impl(T tensor, TF_DataType dtype, string name, bool optimize = true) { - return with(new ops.name_scope(name, "ones_like", new { tensor }), scope => + return with(ops.name_scope(name, "ones_like", new { tensor }), scope => { name = scope; var tensor1 = ops.convert_to_tensor(tensor, name: "tensor"); @@ -84,7 +84,7 @@ namespace Tensorflow public static Tensor ones(Tensor shape, TF_DataType dtype = TF_DataType.TF_FLOAT, string name = null) { dtype = dtype.as_base_dtype(); - return with(new ops.name_scope(name, "ones", new { shape }), scope => + return with(ops.name_scope(name, "ones", new { shape }), scope => { name = scope; var output = gen_array_ops.fill(shape, constant_op.constant(1.0f, dtype: dtype), name: name); @@ -130,7 +130,7 @@ namespace Tensorflow private static Tensor shape_internal(Tensor input, string name = null, bool optimize = true, TF_DataType out_type = TF_DataType.TF_INT32) { - return with(new ops.name_scope(name, "Shape", new { input }), scope => + return with(ops.name_scope(name, "Shape", new { input }), scope => { name = scope; @@ -151,7 +151,7 @@ namespace Tensorflow private static Tensor size_internal(Tensor input, string name = null, bool optimize = true, TF_DataType out_type = TF_DataType.TF_INT32) { - return with(new ops.name_scope(name, "Size", new Tensor[] { input }), scope => + return with(ops.name_scope(name, "Size", new Tensor[] { input }), scope => { name = scope; @@ -182,7 +182,7 @@ namespace Tensorflow public static Tensor zeros_like(Tensor tensor, TF_DataType dtype = TF_DataType.DtInvalid, string name = null, bool optimize = true) { - return with(new ops.name_scope(name, "zeros_like", new Tensor[] { tensor }), scope => + return with(ops.name_scope(name, "zeros_like", new Tensor[] { tensor }), scope => { name = scope; tensor = ops.convert_to_tensor(tensor, name: "tensor"); diff --git a/src/TensorFlowNET.Core/Operations/control_flow_ops.py.cs b/src/TensorFlowNET.Core/Operations/control_flow_ops.py.cs index 303104b5..129e3256 100644 --- a/src/TensorFlowNET.Core/Operations/control_flow_ops.py.cs +++ b/src/TensorFlowNET.Core/Operations/control_flow_ops.py.cs @@ -9,7 +9,7 @@ namespace Tensorflow { public static Operation group(T[] inputs, string name = null) where T : ITensorOrOperation { - return with(new ops.name_scope(name, "group_deps", inputs), scope => + return with(ops.name_scope(name, "group_deps", inputs), scope => { name = scope; @@ -83,7 +83,7 @@ namespace Tensorflow public static Tensor[] tuple(Tensor[] tensors, string name = null, Operation[] control_inputs = null) { - return with(new ops.name_scope(name, "tuple", tensors), scope => + return with(ops.name_scope(name, "tuple", tensors), scope => { name = scope; var gating_ops = tensors.Select(x => x.op).ToList(); @@ -115,7 +115,7 @@ namespace Tensorflow values.AddRange(dependencies); values.Add(output_tensor); - return with(new ops.name_scope(name, "control_dependency", values), scope => + return with(ops.name_scope(name, "control_dependency", values), scope => { name = scope; diff --git a/src/TensorFlowNET.Core/Operations/embedding_ops.cs b/src/TensorFlowNET.Core/Operations/embedding_ops.cs index 0c094b54..49b2d83d 100644 --- a/src/TensorFlowNET.Core/Operations/embedding_ops.cs +++ b/src/TensorFlowNET.Core/Operations/embedding_ops.cs @@ -20,7 +20,7 @@ namespace Tensorflow string name = null, string max_norm = null) { - return with(new ops.name_scope(name, "embedding_lookup", new { @params, ids }), scope => + return with(ops.name_scope(name, "embedding_lookup", new { @params, ids }), scope => { name = scope; int np = 1; diff --git a/src/TensorFlowNET.Core/Operations/math_ops.py.cs b/src/TensorFlowNET.Core/Operations/math_ops.py.cs index c20ba154..9b16983e 100644 --- a/src/TensorFlowNET.Core/Operations/math_ops.py.cs +++ b/src/TensorFlowNET.Core/Operations/math_ops.py.cs @@ -15,7 +15,7 @@ namespace Tensorflow if(base_type == x.dtype) return x; - return with(new ops.name_scope(name, "Cast", new { x }), scope => + return with(ops.name_scope(name, "Cast", new { x }), scope => { x = ops.convert_to_tensor(x, name: "x"); if (x.dtype.as_base_dtype() != base_type) @@ -166,7 +166,7 @@ namespace Tensorflow if (delta == null) delta = 1; - return with(new ops.name_scope(name, "Range", new object[] { start, limit, delta }), scope => + return with(ops.name_scope(name, "Range", new object[] { start, limit, delta }), scope => { name = scope; var start1 = ops.convert_to_tensor(start, name: "start"); @@ -179,7 +179,7 @@ namespace Tensorflow public static Tensor floordiv(Tensor x, Tensor y, string name = null) { - return with(new ops.name_scope(name, "floordiv", new { x, y }), scope => + return with(ops.name_scope(name, "floordiv", new { x, y }), scope => { return gen_math_ops.floor_div(x, y, scope); }); @@ -187,7 +187,7 @@ namespace Tensorflow public static Tensor rank_internal(Tensor input, string name = null, bool optimize = true) { - return with(new ops.name_scope(name, "Rank", new List { input }), scope => + return with(ops.name_scope(name, "Rank", new List { input }), scope => { name = scope; var input_tensor = ops.convert_to_tensor(input); @@ -207,7 +207,7 @@ namespace Tensorflow { Tensor result = null; - with(new ops.name_scope(name, "MatMul", new Tensor[] { a, b }), scope => + with(ops.name_scope(name, "MatMul", new Tensor[] { a, b }), scope => { name = scope; @@ -237,7 +237,7 @@ namespace Tensorflow if (dt.is_floating() || dt.is_integer()) return x; - return with(new ops.name_scope(name, "Conj", new List { x }), scope => + return with(ops.name_scope(name, "Conj", new List { x }), scope => { return x; diff --git a/src/TensorFlowNET.Core/Operations/nn_impl.py.cs b/src/TensorFlowNET.Core/Operations/nn_impl.py.cs index 802df5f9..fe0f9dcd 100644 --- a/src/TensorFlowNET.Core/Operations/nn_impl.py.cs +++ b/src/TensorFlowNET.Core/Operations/nn_impl.py.cs @@ -19,7 +19,7 @@ namespace Tensorflow string name = null, bool keep_dims = false) { - return with(new ops.name_scope(name, "moments", new { x, axes }), scope => + return with(ops.name_scope(name, "moments", new { x, axes }), scope => { // The dynamic range of fp16 is too limited to support the collection of // sufficient statistics. As a workaround we simply perform the operations diff --git a/src/TensorFlowNET.Core/Operations/random_ops.py.cs b/src/TensorFlowNET.Core/Operations/random_ops.py.cs index c6ea3aa9..62d4346a 100644 --- a/src/TensorFlowNET.Core/Operations/random_ops.py.cs +++ b/src/TensorFlowNET.Core/Operations/random_ops.py.cs @@ -23,7 +23,7 @@ namespace Tensorflow int? seed = null, string name = null) { - return with(new ops.name_scope(name, "random_normal", new { shape, mean, stddev }), scope => + return with(ops.name_scope(name, "random_normal", new { shape, mean, stddev }), scope => { var shape_tensor = _ShapeTensor(shape); var mean_tensor = ops.convert_to_tensor(mean, dtype: dtype, name: "mean"); @@ -53,7 +53,7 @@ namespace Tensorflow int? seed = null, string name = null) { - return with(new ops.name_scope(name, "random_uniform", new { shape, minval, maxval }), scope => + return with(ops.name_scope(name, "random_uniform", new { shape, minval, maxval }), scope => { name = scope; var tensorShape = _ShapeTensor(shape); diff --git a/src/TensorFlowNET.Core/Tensors/Tensor.Operators.cs b/src/TensorFlowNET.Core/Tensors/Tensor.Operators.cs index a357e5d5..cd3ac548 100644 --- a/src/TensorFlowNET.Core/Tensors/Tensor.Operators.cs +++ b/src/TensorFlowNET.Core/Tensors/Tensor.Operators.cs @@ -41,7 +41,7 @@ namespace Tensorflow if( y is Tensor tr) dtype = tr.dtype.as_base_dtype(); - var namescope = new ops.name_scope(null, name, new { x, y }); + var namescope = ops.name_scope(null, name, new { x, y }); return with(namescope, scope => { Tensor result = null; diff --git a/src/TensorFlowNET.Core/Train/Optimizer.cs b/src/TensorFlowNET.Core/Train/Optimizer.cs index 407315e2..fc76f7b5 100644 --- a/src/TensorFlowNET.Core/Train/Optimizer.cs +++ b/src/TensorFlowNET.Core/Train/Optimizer.cs @@ -87,7 +87,7 @@ namespace Tensorflow _create_slots(var_list); var update_ops = new List(); - return with(new ops.name_scope(name, Name), scope => + return with(ops.name_scope(name, Name), scope => { name = scope; _prepare(); @@ -98,7 +98,7 @@ namespace Tensorflow continue; var scope_name = var.op.name; - with(new ops.name_scope("update_" + scope_name), scope2 => + with(ops.name_scope("update_" + scope_name), scope2 => { update_ops.Add(processor.update_op(this, grad)); }); diff --git a/src/TensorFlowNET.Core/Train/Saving/BaseSaverBuilder.cs b/src/TensorFlowNET.Core/Train/Saving/BaseSaverBuilder.cs index 876bf856..0cc3c981 100644 --- a/src/TensorFlowNET.Core/Train/Saving/BaseSaverBuilder.cs +++ b/src/TensorFlowNET.Core/Train/Saving/BaseSaverBuilder.cs @@ -79,7 +79,7 @@ namespace Tensorflow Tensor save_tensor = null; Operation restore_op = null; - return with(new ops.name_scope(name, "save", saveables.Select(x => x.op).ToArray()), scope => + return with(ops.name_scope(name, "save", saveables.Select(x => x.op).ToArray()), scope => { name = scope; diff --git a/src/TensorFlowNET.Core/Variables/RefVariable.Operators.cs b/src/TensorFlowNET.Core/Variables/RefVariable.Operators.cs index 3b2f67c4..c0b23575 100644 --- a/src/TensorFlowNET.Core/Variables/RefVariable.Operators.cs +++ b/src/TensorFlowNET.Core/Variables/RefVariable.Operators.cs @@ -17,7 +17,7 @@ namespace Tensorflow private static Tensor op_helper(string default_name, RefVariable x, T y) { var tensor1 = x.value(); - return with(new ops.name_scope(null, default_name, new { tensor1, y }), scope => { + return with(ops.name_scope(null, default_name, new { tensor1, y }), scope => { var tensor2 = ops.convert_to_tensor(y, tensor1.dtype.as_base_dtype(), "y"); return gen_math_ops.add(tensor1, tensor2, scope); }); diff --git a/src/TensorFlowNET.Core/Variables/RefVariable.cs b/src/TensorFlowNET.Core/Variables/RefVariable.cs index 5eb03d3f..df068477 100644 --- a/src/TensorFlowNET.Core/Variables/RefVariable.cs +++ b/src/TensorFlowNET.Core/Variables/RefVariable.cs @@ -118,7 +118,7 @@ namespace Tensorflow ops.init_scope(); var values = init_from_fn ? new object[0] : new object[] { initial_value }; - with(new ops.name_scope(name, "Variable", values), scope => + with(ops.name_scope(name, "Variable", values), scope => { name = scope; if (init_from_fn) @@ -132,7 +132,7 @@ namespace Tensorflow List = new AttrValue.Types.ListValue() }; attr.List.S.Add(ByteString.CopyFromUtf8($"loc:{true_name}")); - with(new ops.name_scope("Initializer"), scope2 => + with(ops.name_scope("Initializer"), scope2 => { _initial_value = (initial_value as Func)(); _initial_value = ops.convert_to_tensor(_initial_value, name: "initial_value", dtype: dtype); diff --git a/src/TensorFlowNET.Core/Variables/VariableScope.cs b/src/TensorFlowNET.Core/Variables/VariableScope.cs index c3453744..33e4bcfc 100644 --- a/src/TensorFlowNET.Core/Variables/VariableScope.cs +++ b/src/TensorFlowNET.Core/Variables/VariableScope.cs @@ -39,7 +39,7 @@ namespace Tensorflow VariableAggregation aggregation= VariableAggregation.NONE) { string full_name = !string.IsNullOrEmpty(this._name) ? this._name + "/" + name : name; - return with(new ops.name_scope(null), scope => + return with(ops.name_scope(null), scope => { if (dtype == TF_DataType.DtInvalid) dtype = _dtype; diff --git a/src/TensorFlowNET.Core/Variables/variable_scope.py.cs b/src/TensorFlowNET.Core/Variables/variable_scope.py.cs index d7a1e6bc..3f92edd0 100644 --- a/src/TensorFlowNET.Core/Variables/variable_scope.py.cs +++ b/src/TensorFlowNET.Core/Variables/variable_scope.py.cs @@ -20,7 +20,7 @@ namespace Tensorflow private VariableScope _scope; private string _default_name; private object _values; - private ops.name_scope _current_name_scope; + private ops.NameScope _current_name_scope; private bool _auxiliary_name_scope; private PureVariableScope _cached_pure_variable_scope; private bool? _reuse; @@ -68,7 +68,7 @@ namespace Tensorflow private VariableScope _enter_scope_uncached() { - ops.name_scope current_name_scope; + ops.NameScope current_name_scope; PureVariableScope pure_variable_scope = null; VariableScope entered_pure_variable_scope; @@ -82,14 +82,14 @@ namespace Tensorflow if(!string.IsNullOrEmpty(name_scope)) // Hack to reenter name_scope += "/"; - current_name_scope = new ops.name_scope(name_scope); + current_name_scope = ops.name_scope(name_scope); } if (_name != null || _scope != null) { var name_scope = _name == null ? _scope._name.Split('/').Last() : _name; if (name_scope != null || current_name_scope != null) - current_name_scope = new ops.name_scope(name_scope); + current_name_scope = ops.name_scope(name_scope); current_name_scope.__enter__(); var current_name_scope_name = current_name_scope; _current_name_scope = current_name_scope; @@ -106,7 +106,7 @@ namespace Tensorflow } else { - current_name_scope = new ops.name_scope(_default_name); + current_name_scope = ops.name_scope(_default_name); current_name_scope.__enter__(); string current_name_scope_name = current_name_scope; _current_name_scope = current_name_scope; diff --git a/src/TensorFlowNET.Core/ops.name_scope.cs b/src/TensorFlowNET.Core/ops.name_scope.cs index 0b9bbf0f..03b9349e 100644 --- a/src/TensorFlowNET.Core/ops.name_scope.cs +++ b/src/TensorFlowNET.Core/ops.name_scope.cs @@ -7,10 +7,14 @@ namespace Tensorflow { public partial class ops { + public static NameScope name_scope(string name, + string default_name = "", + object values = null) => new NameScope(name, default_name, values); + /// /// Returns a context manager that creates hierarchical names for operations. /// - public class name_scope : IPython + public class NameScope : IPython { public string _name; public string _default_name; @@ -20,7 +24,7 @@ namespace Tensorflow public string old_stack = ""; private object _g_manager; - public name_scope(string name, string default_name = "", object values = null) + public NameScope(string name, string default_name = "", object values = null) { _name = name; _default_name = default_name; @@ -58,7 +62,7 @@ namespace Tensorflow /// __enter__() /// /// - public static implicit operator string(name_scope ns) + public static implicit operator string(NameScope ns) { return ns._name_scope; } diff --git a/test/TensorFlowNET.UnitTest/NameScopeTest.cs b/test/TensorFlowNET.UnitTest/NameScopeTest.cs index 01a5f031..003b5810 100644 --- a/test/TensorFlowNET.UnitTest/NameScopeTest.cs +++ b/test/TensorFlowNET.UnitTest/NameScopeTest.cs @@ -15,7 +15,7 @@ namespace TensorFlowNET.UnitTest [TestMethod] public void NestedNameScope() { - with(new ops.name_scope("scope1"), scope1 => + with(new ops.NameScope("scope1"), scope1 => { name = scope1; Assert.AreEqual("scope1", g._name_stack); @@ -24,7 +24,7 @@ namespace TensorFlowNET.UnitTest var const1 = tf.constant(1.0); Assert.AreEqual("scope1/Const:0", const1.name); - with(new ops.name_scope("scope2"), scope2 => + with(new ops.NameScope("scope2"), scope2 => { name = scope2; Assert.AreEqual("scope1/scope2", g._name_stack);