/TensorFlowSharp/Variable.cs

https://github.com/migueldeicaza/TensorFlowSharp · C# · 109 lines · 43 code · 7 blank · 59 comment · 1 complexity · 7c80774546f29ec2b90565e62d16bba3 MD5 · raw file

  1. //
  2. // TensorFlow.cs; Bindings to the TensorFlow C API for .NET
  3. //
  4. // Authors:
  5. // Miguel de Icaza (miguel@microsoft.com)
  6. //
  7. using System;
  8. using System.Numerics;
  9. using System.Runtime.InteropServices;
  10. using System.Text;
  11. using size_t = System.UIntPtr;
  12. using TF_Tensor = System.IntPtr;
  13. namespace TensorFlow
  14. {
  15. /// <summary>
  16. /// The Variable class holds the TFOutput nodes that are used to initialize, read and assign a value to a variable.
  17. /// </summary>
  18. /// <remarks>
  19. /// A variable maintains state in the graph across calls to `run()`. You add a
  20. /// variable to the graph by constructing an instance of the class `Variable`.
  21. ///
  22. /// The `Variable()` constructor requires an initial value for the variable,
  23. /// which can be a `Tensor` of any type and shape. The initial value defines the
  24. /// type and shape of the variable. After construction, the type and shape of
  25. /// the variable are fixed. The value can be changed using one of the assign
  26. /// methods.
  27. ///
  28. /// When a variable is created a VarHandleOp is created which is returned as
  29. /// the VariableOp property, an assign operation is created that can be accessed
  30. /// using the assignHandle and you can read the value of the variable using the
  31. /// ReadHandle.
  32. ///
  33. /// When you launch the graph, variables have to be explicitly initialized before
  34. /// you can run Ops that use their value. You can initialize a variable by
  35. /// running its *initializer op*, restoring the variable from a save file, or
  36. /// simply running an `assign` Op that assigns a value to the variable. In fact,
  37. /// the variable *initializer op* is just an `assign` Op that assigns the
  38. /// variable's initial value to the variable itself.
  39. ///
  40. /// There is an implicit conversion from the Variable into the VarHandleOp if
  41. /// used.
  42. /// </remarks>
  43. public class Variable
  44. {
  45. TFOutput variableHandle;
  46. TFOutput readHandle;
  47. TFOperation assignOp;
  48. /// <summary>
  49. /// Returns the ReadVariableOp that is used to fetch the value of the variable from the graph.
  50. /// </summary>
  51. /// <value>The read op.</value>
  52. public TFOutput Read => readHandle;
  53. /// <summary>
  54. /// Returns the ReadVariableOp that is used to fetch the value of the variable from the graph.
  55. /// </summary>
  56. /// <value>The read op.</value>
  57. public TFOutput ReadAfter(params TFOperation[] dependencies)
  58. {
  59. if(dependencies.Length > 0)
  60. {
  61. var graph = dependencies[0].graph;
  62. using (graph.WithDependencies(dependencies))
  63. {
  64. return graph.ReadVariableOp(variableHandle, readHandle.OutputType);
  65. }
  66. }
  67. else
  68. {
  69. return readHandle;
  70. }
  71. }
  72. /// <summary>
  73. /// Returns the AssignVariableOp that is used to assign the initial value to the variable from the graph.
  74. /// </summary>
  75. /// <value>The assign op.</value>
  76. public TFOperation Assign => assignOp;
  77. /// <summary>
  78. /// Returns the VarHandleOp that was created using the shape of the initial value.
  79. /// </summary>
  80. /// <value>The variable op.</value>
  81. public TFOutput VariableOp => variableHandle;
  82. internal Variable (TFOutput variableHandle, TFOutput readHandle, TFOperation assignOp)
  83. {
  84. this.variableHandle = variableHandle;
  85. this.readHandle = readHandle;
  86. this.assignOp = assignOp;
  87. }
  88. /// <summary>
  89. /// Returns the VarHandleOp (the VariableOp property).
  90. /// </summary>
  91. /// <returns>The variable handle created for the variable.</returns>
  92. /// <param name="variable">Variable reference.</param>
  93. /// <remarks>
  94. /// This implicit operator exists to preserve the compatibility with code that
  95. /// created Variables and expected the result to be the VariableOp.
  96. /// </remarks>
  97. public static implicit operator TFOutput (Variable variable)
  98. {
  99. return variable.VariableOp;
  100. }
  101. }
  102. }