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.

Graph.cs 5.4 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
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using TF_DataType = Tensorflow.DataType;
  7. namespace Tensorflow
  8. {
  9. /// <summary>
  10. /// TensorFlow uses a dataflow graph to represent your computation in terms of the dependencies between individual operations.
  11. /// This leads to a low-level programming model in which you first define the dataflow graph,
  12. /// then create a TensorFlow session to run parts of the graph across a set of local and remote devices.
  13. /// https://www.tensorflow.org/guide/graphs
  14. /// </summary>
  15. public class Graph
  16. {
  17. private IntPtr _handle;
  18. private Dictionary<int, Operation> _nodes_by_id;
  19. private Dictionary<string, Operation> _nodes_by_name;
  20. private Dictionary<string, int> _names_in_use;
  21. public int _version;
  22. private int _next_id_counter;
  23. private List<String> _unfetchable_ops = new List<string>();
  24. private string _name_stack;
  25. public Graph(IntPtr graph)
  26. {
  27. _handle = graph;
  28. _nodes_by_id = new Dictionary<int, Operation>();
  29. _nodes_by_name = new Dictionary<string, Operation>();
  30. _names_in_use = new Dictionary<string, int>();
  31. }
  32. public T as_graph_element<T>(T obj, bool allow_tensor = true, bool allow_operation = true)
  33. {
  34. return _as_graph_element_locked(obj, allow_tensor, allow_operation);
  35. }
  36. private Func<object> _as_graph_element(object obj)
  37. {
  38. return null;
  39. }
  40. private T _as_graph_element_locked<T>(T obj, bool allow_tensor = true, bool allow_operation = true)
  41. {
  42. string types_str = "";
  43. if (allow_tensor && allow_operation)
  44. {
  45. types_str = "Tensor or Operation";
  46. }
  47. else if (allow_tensor)
  48. {
  49. types_str = "Tensor";
  50. }
  51. else if (allow_operation)
  52. {
  53. types_str = "Operation";
  54. }
  55. var temp_obj = _as_graph_element(obj);
  56. if(obj is Tensor && allow_tensor)
  57. {
  58. if ((obj as Tensor).graph.Equals(this))
  59. {
  60. return obj;
  61. }
  62. else
  63. {
  64. throw new Exception($"Tensor {obj} is not an element of this graph.");
  65. }
  66. }
  67. throw new Exception($"Can not convert a {typeof(T).Name} into a {types_str}.");
  68. }
  69. public unsafe Operation create_op(string op_type, List<Tensor> inputs, TF_DataType[] dtypes,
  70. TF_DataType[] input_types = null, string name = "",
  71. Dictionary<string, AttrValue> attrs = null, OpDef op_def = null)
  72. {
  73. if (String.IsNullOrEmpty(name))
  74. {
  75. name = op_type;
  76. }
  77. name = name.EndsWith("/") ? ops._name_from_scope_name(name) : unique_name(name);
  78. var node_def = ops._NodeDef(op_type, name, device: "", attrs: attrs);
  79. var op = new Operation(node_def,
  80. this,
  81. inputs: inputs,
  82. output_types: dtypes,
  83. control_inputs: new object[] { },
  84. input_types: input_types,
  85. original_op: null,
  86. op_def: op_def);
  87. return op;
  88. }
  89. public void _add_op(Operation op)
  90. {
  91. _nodes_by_id[op._id] = op;
  92. //_nodes_by_name[op.name] = op;
  93. _version = Math.Max(_version, op._id);
  94. }
  95. public int _next_id()
  96. {
  97. return ++_next_id_counter;
  98. }
  99. public bool is_fetchable<T>(T tensor_or_op)
  100. {
  101. if (tensor_or_op is Tensor)
  102. {
  103. return !_unfetchable_ops.Contains((tensor_or_op as Tensor).name); ;
  104. }
  105. else if (tensor_or_op is Operation)
  106. {
  107. return !_unfetchable_ops.Contains((tensor_or_op as Operation).name);
  108. }
  109. return false;
  110. }
  111. public string name_scope(string name)
  112. {
  113. string new_stack = "";
  114. if (name.EndsWith("/"))
  115. {
  116. new_stack = ops._name_from_scope_name(name);
  117. }
  118. else
  119. {
  120. new_stack = unique_name(name);
  121. }
  122. _name_stack = new_stack;
  123. return String.IsNullOrEmpty(new_stack) ? "" : new_stack + "/";
  124. }
  125. public string unique_name(string name)
  126. {
  127. if (!String.IsNullOrEmpty(_name_stack))
  128. {
  129. name = _name_stack + "/" + name;
  130. }
  131. var name_key = name.ToLower();
  132. if (_names_in_use.ContainsKey(name_key))
  133. {
  134. _names_in_use[name_key]++;
  135. }
  136. else
  137. {
  138. _names_in_use[name_key] = 1;
  139. return name;
  140. }
  141. return $"{name}_{_names_in_use[name_key]}";
  142. }
  143. public Operation[] get_operations()
  144. {
  145. return _nodes_by_name.Values.Select(x => x).ToArray();
  146. }
  147. public static implicit operator IntPtr(Graph graph)
  148. {
  149. return graph._handle;
  150. }
  151. }
  152. }

tensorflow框架的.NET版本,提供了丰富的特性和API,可以借此很方便地在.NET平台下搭建深度学习训练与推理流程。

Contributors (1)