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.

GradientTape.cs 5.0 kB

4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using static Tensorflow.Binding;
  5. namespace Tensorflow.Gradients
  6. {
  7. /// <summary>
  8. /// Gradient Tape Set
  9. /// Record operations for automatic differentiation.
  10. ///
  11. /// Operations are recorded if they are executed within this context manager and
  12. /// at least one of their inputs is being "watched".
  13. ///
  14. /// Trainable variables (created by `tf.Variable` or `tf.compat.v1.get_variable`,
  15. /// where `trainable=True` is default in both cases) are automatically watched.
  16. /// Tensors can be manually watched by invoking the `watch` method on this context
  17. /// manager.
  18. /// </summary>
  19. public class GradientTape : IDisposable
  20. {
  21. int _nextTapeId;
  22. ITape _tape => _tapeSet.Peek();
  23. Stack<ITape> _tapeSet;
  24. public GradientTape()
  25. {
  26. _tapeSet = new Stack<ITape>();
  27. }
  28. /// <summary>
  29. /// New tape onto the tape stack.
  30. /// </summary>
  31. public ITape PushTape(bool persistent = false,
  32. bool watch_accessed_variables = true)
  33. {
  34. // Enters a context inside which operations are recorded on this tape.
  35. if (tf.Context.executing_eagerly())
  36. tf.Context.ensure_initialized();
  37. var tape = new Tape(persistent, watch_accessed_variables);
  38. tape.SetTapeId(_nextTapeId++);
  39. _tapeSet.Push(tape);
  40. return tape;
  41. }
  42. ITape PopTape()
  43. {
  44. _tape.StopRecord();
  45. return _tapeSet.Pop();
  46. }
  47. /// <summary>
  48. /// Marks this tensor to be watched by the given tape.
  49. /// </summary>
  50. /// <param name="x"></param>
  51. public void watch(Tensor x)
  52. {
  53. if (!_tapeSet.Any())
  54. return;
  55. _tape.Watch(x);
  56. }
  57. /// <summary>
  58. /// Computes the gradient using operations recorded in context of this tape.
  59. /// </summary>
  60. /// <param name="target"></param>
  61. /// <param name="source"></param>
  62. /// <returns></returns>
  63. public Tensor gradient(Tensor target, Tensor source, List<Tensor> output_gradients = null,
  64. string unconnected_gradients = null)
  65. {
  66. if(_tape is null)
  67. {
  68. throw new RuntimeError("A non-persistent GradientTape can only be used to " +
  69. "compute one set of gradients (or jacobians).");
  70. }
  71. ITape tape = stop_recording();
  72. var results = tf.Runner.TFE_TapeGradient(tape,
  73. new[] { target },
  74. new[] { source },
  75. output_gradients,
  76. new[] { source },
  77. unconnected_gradients);
  78. return results[0];
  79. }
  80. public Tensor gradient(Tensor target, ResourceVariable source, List<Tensor> output_gradients = null,
  81. string unconnected_gradients = null)
  82. {
  83. var results = gradient(target, new List<IVariableV1> { source }, output_gradients, unconnected_gradients);
  84. return results[0];
  85. }
  86. public (Tensor, Tensor) gradient(Tensor target, (ResourceVariable, ResourceVariable) sources, List<Tensor> output_gradients = null,
  87. string unconnected_gradients = null)
  88. {
  89. var results = gradient(target, new List<IVariableV1> { sources.Item1, sources.Item2 }, output_gradients, unconnected_gradients);
  90. return (results[0], results[1]);
  91. }
  92. public Tensor[] gradient(Tensor target, IEnumerable<IVariableV1> sources, List<Tensor> output_gradients = null,
  93. string unconnected_gradients = null)
  94. {
  95. if (_tape is null)
  96. {
  97. throw new RuntimeError("A non-persistent GradientTape can only be used to " +
  98. "compute one set of gradients (or jacobians).");
  99. }
  100. var tape = stop_recording();
  101. var results = tf.Runner.TFE_TapeGradient(tape,
  102. new[] { target },
  103. sources.Select(x => x.Handle).ToArray(),
  104. output_gradients,
  105. sources.Select(x => x.Handle).ToArray(),
  106. unconnected_gradients);
  107. if (!tape.Persistent)
  108. {
  109. // Keep track of watched variables before setting tape to None
  110. // _watched_variables = _tape.WatchedVariables();
  111. }
  112. return results;
  113. }
  114. /// <summary>
  115. /// Temporarily stops recording operations on this tape.
  116. /// </summary>
  117. public ITape stop_recording()
  118. {
  119. var tape = _tape;
  120. if (!tape.Persistent)
  121. tape = PopTape();
  122. return tape;
  123. }
  124. public Stack<ITape> GetTapeSet()
  125. => _tapeSet;
  126. public void Dispose()
  127. {
  128. _tapeSet.Clear();
  129. }
  130. }
  131. }