You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ops.name_scope.cs 4.2 kB

6 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*****************************************************************************
  2. Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ******************************************************************************/
  13. using System.Collections.Generic;
  14. using System.Diagnostics;
  15. using Tensorflow.Eager;
  16. using static Tensorflow.Binding;
  17. namespace Tensorflow
  18. {
  19. public partial class ops
  20. {
  21. public static NameScope name_scope(string name,
  22. string default_name = "",
  23. object values = null) => new NameScope(name, default_name, values);
  24. /// <summary>
  25. /// Returns a context manager that creates hierarchical names for operations.
  26. /// </summary>
  27. public class NameScope : ITensorFlowObject
  28. {
  29. public string _name;
  30. public string _default_name;
  31. public object _values;
  32. public string scope_name;
  33. public string old_scope_name = "";
  34. public NameScope(string name, string default_name = "", object values = null)
  35. {
  36. _name = name;
  37. _default_name = default_name;
  38. _values = values;
  39. }
  40. public void __enter__()
  41. {
  42. _name = _name ?? _default_name;
  43. if (tf.context.executing_eagerly())
  44. {
  45. (scope_name, old_scope_name) = enter_eager_name_scope(tf.context, _name);
  46. }
  47. else
  48. {
  49. Graph g = null;
  50. if (_values is List<Tensor> vList)
  51. g = _get_graph_from_inputs(vList.ToArray());
  52. else if (_values is Tensor[] vArray)
  53. g = _get_graph_from_inputs(vArray);
  54. if (g == null)
  55. g = get_default_graph();
  56. old_scope_name = g._name_stack;
  57. scope_name = g.name_scope(_name);
  58. }
  59. }
  60. private (string, string) enter_eager_name_scope(Context ctx, string name)
  61. {
  62. if (name == null)
  63. name = "";
  64. var scope_name = name;
  65. var old_name = ctx.scope_name;
  66. // A trailing slash breaks out of nested name scopes, indicating a
  67. // fully specified scope name, for compatibility with Graph.name_scope.
  68. if (!name.EndsWith("/"))
  69. {
  70. scope_name = name + "/";
  71. if (!string.IsNullOrEmpty(old_name))
  72. scope_name = old_name + scope_name;
  73. }
  74. ctx.scope_name = scope_name;
  75. return (scope_name, old_name);
  76. }
  77. public void Dispose()
  78. {
  79. if (tf.context.executing_eagerly())
  80. tf.context.scope_name = old_scope_name;
  81. else
  82. get_default_graph()._name_stack = old_scope_name;
  83. }
  84. [DebuggerNonUserCode]
  85. public void __exit__()
  86. {
  87. }
  88. [DebuggerNonUserCode]
  89. public void __init__()
  90. {
  91. }
  92. [DebuggerNonUserCode]
  93. public void __del__()
  94. {
  95. }
  96. /// <summary>
  97. /// __enter__()
  98. /// </summary>
  99. /// <param name="ns"></param>
  100. public static implicit operator string(NameScope ns)
  101. {
  102. return ns.scope_name;
  103. }
  104. }
  105. }
  106. }