tf.Variable

Class Variable

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.

实例化Variable对象时,必须要给一个初始值,即tensor;这个初始值指定了variable对象的数据类型和维度。构造Variable对象后,variable对象的数据类型和维度就确定了,不能在改变。但variable中的值可以使用assign相关函数修改。

If you want to change the shape of a variable later you have to use an assign Op with validate_shape=False.

如果你定义的variable变量的维度后面可能会被改变,那么你需要指定 validate_shape=False

Just like any Tensor, variables created with Variable() can be used as inputs for other Ops in the graph. Additionally, all the operators overloaded for the Tensor class are carried over to variables, so you can also add nodes to the graph by just doing arithmetic on variables.

variable支持算术运算,支持运算符重载。

variable一般用于graph的 训练模型变量,如模型的权重(weight)。

 1 import tensorflow as tf
 2 
 3 w = tf.Variable([[2],[1]])
 4 print(type(w))
 5 print(w)
 6 
 7 #print(w.constraint)
 8 print("device:{}".format(w.device))
 9 print("dtype:{}".format(w.dtype))
10 print("graph:{}".format(w.graph))
11 print("initial_value:{},{}".format(w.initial_value, type(w.initial_value)))
12 print("initializer:{}".format(w.initializer))
13 print("name:{}".format(w.name))
14 print("op:{}".format(w.op))
15 print("shape:{}".format(w.shape)                                           

 

猜你喜欢

转载自www.cnblogs.com/black-mamba/p/9108448.html