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 2.2 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Tensorflow.Eager;
  6. namespace Tensorflow
  7. {
  8. public partial class ops
  9. {
  10. public static NameScope name_scope(string name,
  11. string default_name = "",
  12. object values = null) => new NameScope(name, default_name, values);
  13. /// <summary>
  14. /// Returns a context manager that creates hierarchical names for operations.
  15. /// </summary>
  16. public class NameScope : IPython
  17. {
  18. public string _name;
  19. public string _default_name;
  20. public object _values;
  21. public Context _ctx;
  22. public string _name_scope;
  23. public string old_stack = "";
  24. private object _g_manager;
  25. public NameScope(string name, string default_name = "", object values = null)
  26. {
  27. _name = name;
  28. _default_name = default_name;
  29. _values = values;
  30. // _ctx = new Context();
  31. }
  32. public void __enter__()
  33. {
  34. _name = _name == null ? _default_name : _name;
  35. Graph g = null;
  36. if (_values is List<Tensor> vList)
  37. g = _get_graph_from_inputs(vList.ToArray());
  38. else if (_values is Tensor[] vArray)
  39. g = _get_graph_from_inputs(vArray);
  40. if (g == null)
  41. g = get_default_graph();
  42. old_stack = g._name_stack;
  43. _name_scope = g.name_scope(_name);
  44. }
  45. public void Dispose()
  46. {
  47. var g = get_default_graph();
  48. g._name_stack = old_stack;
  49. // Console.WriteLine($"name_scope: {g._name_stack} -> {old_stack}");
  50. }
  51. public void __exit__()
  52. {
  53. }
  54. /// <summary>
  55. /// __enter__()
  56. /// </summary>
  57. /// <param name="ns"></param>
  58. public static implicit operator string(NameScope ns)
  59. {
  60. return ns._name_scope;
  61. }
  62. }
  63. }
  64. }