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.

RefVariable.cs 6.1 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Tensorflow
  6. {
  7. public partial class RefVariable : VariableV1
  8. {
  9. public bool _in_graph_mode = true;
  10. public Tensor _initial_value;
  11. public string _graph_key;
  12. public bool _trainable;
  13. public Tensor _variable;
  14. public Tensor _snapshot;
  15. private Operation _initializer_op;
  16. public Operation initializer => _initializer_op;
  17. public Operation op => _variable.op;
  18. public TF_DataType dtype => _variable.dtype;
  19. public TensorShape shape => tensor_util.to_shape(_variable.shape);
  20. public string name => _variable.name;
  21. public RefVariable(object initial_value,
  22. bool trainable = true,
  23. List<string> collections = null,
  24. bool validate_shape = true,
  25. string caching_device = "",
  26. string name = "",
  27. TF_DataType dtype = TF_DataType.DtInvalid) :
  28. base(initial_value, trainable, collections, validate_shape, caching_device, name, dtype)
  29. {
  30. _in_graph_mode = true;
  31. _init_from_args(initial_value, trainable, collections, validate_shape, caching_device, name, dtype);
  32. }
  33. private void _init_from_args(object initial_value,
  34. bool trainable = true,
  35. List<string> collections = null,
  36. bool validate_shape = true,
  37. string caching_device = "",
  38. string name = "",
  39. TF_DataType dtype = TF_DataType.DtInvalid)
  40. {
  41. if (initial_value is null)
  42. throw new ValueError("initial_value must be specified.");
  43. var init_from_fn = false;
  44. if(collections == null)
  45. {
  46. collections = new List<string> { ops.GraphKeys.GLOBAL_VARIABLES };
  47. }
  48. // Store the graph key so optimizers know how to only retrieve variables from
  49. // this graph.
  50. _graph_key = ops.get_default_graph()._graph_key;
  51. _trainable = trainable;
  52. if (!collections.Contains(ops.GraphKeys.TRAINABLE_VARIABLES))
  53. collections.Add(ops.GraphKeys.TRAINABLE_VARIABLES);
  54. ops.init_scope();
  55. var values = init_from_fn ? new List<object>() : new List<object> { initial_value };
  56. using (var namescope = new ops.name_scope<object>(name, "Variable", values))
  57. {
  58. name = namescope;
  59. if (init_from_fn)
  60. {
  61. }
  62. // Or get the initial value from a Tensor or Python object.
  63. else
  64. {
  65. _initial_value = ops.convert_to_tensor(initial_value, name: "initial_value");
  66. var shape = _initial_value.shape;
  67. dtype = _initial_value.dtype;
  68. _variable = gen_state_ops.variable_v2(shape, dtype.as_base_dtype(), name);
  69. }
  70. // Manually overrides the variable's shape with the initial value's.
  71. if (validate_shape)
  72. {
  73. var initial_value_shape = _initial_value.shape;
  74. }
  75. // If 'initial_value' makes use of other variables, make sure we don't
  76. // have an issue if these other variables aren't initialized first by
  77. // using their initialized_value() method.
  78. var _initial_value2 = _try_guard_against_uninitialized_dependencies(_initial_value);
  79. _initializer_op = gen_state_ops.assign(_variable, _initial_value2, validate_shape).op;
  80. if (!String.IsNullOrEmpty(caching_device))
  81. {
  82. }
  83. else
  84. {
  85. ops.colocate_with(_initializer_op);
  86. _snapshot = gen_array_ops.identity(_variable, name = "read");
  87. }
  88. ops.add_to_collections(collections, this);
  89. }
  90. }
  91. public Tensor _ref()
  92. {
  93. return _variable;
  94. }
  95. public Tensor _AsTensor()
  96. {
  97. return _snapshot;
  98. }
  99. /// <summary>
  100. /// Attempt to guard against dependencies on uninitialized variables.
  101. /// </summary>
  102. /// <param name="initial_value"></param>
  103. private Tensor _try_guard_against_uninitialized_dependencies(Tensor initial_value)
  104. {
  105. return _safe_initial_value_from_tensor(initial_value, new Dictionary<string, Operation>());
  106. }
  107. /// <summary>
  108. /// Replace dependencies on variables with their initialized values.
  109. /// </summary>
  110. /// <param name="tensor">A `Tensor`. The tensor to replace.</param>
  111. /// <param name="op_cache">A dict mapping operation names to `Operation`s.</param>
  112. /// <returns>A `Tensor` compatible with `tensor`.</returns>
  113. private Tensor _safe_initial_value_from_tensor(Tensor tensor, Dictionary<string, Operation> op_cache)
  114. {
  115. var op = tensor.op;
  116. var new_op = op_cache.ContainsKey(op.Name) ? op_cache[op.Name] : null;
  117. if(new_op == null)
  118. {
  119. new_op = _safe_initial_value_from_op(op, op_cache);
  120. op_cache[op.Name] = new_op;
  121. }
  122. return new_op.outputs[tensor.value_index];
  123. }
  124. private Operation _safe_initial_value_from_op(Operation op, Dictionary<string, Operation> op_cache)
  125. {
  126. var op_type = op.node_def.Op;
  127. switch (op_type)
  128. {
  129. case "IsVariableInitialized":
  130. case "VarIsInitializedOp":
  131. case "ReadVariableOp":
  132. return op;
  133. case "Variable":
  134. case "VariableV2":
  135. case "VarHandleOp":
  136. break;
  137. }
  138. // Recursively build initializer expressions for inputs.
  139. return op;
  140. }
  141. }
  142. }

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