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