zl程序教程

您现在的位置是:首页 >  后端

当前栏目

【TensorFlow 入门】7、定义图变量的方法

方法变量入门 定义 Tensorflow
2023-09-14 09:13:56 时间

1. tf.Variable

tf.Variable.init(initial_value, trainable=True, collections=None, validate_shape=True, name=None)
参数名称参数类型参数含义
initial_value所有可以转换为Tensor的类型变量的初始值(必须给定的值)
trainablebool如果为True,会把它加入到GraphKeys.TRAINABLE_VARIABLES,才能对它使用Optimizer
collectionslist指定该图变量的类型、默认为[GraphKeys.GLOBAL_VARIABLES]
validate_shapebool如果为False,则不进行类型和维度检查
namestring变量的名称,如果没有指定则系统会自动分配一个唯一的值

assign函数:给图变量赋值

2. tf.get_variable

tf.get_variable(name, shape=None, dtype=None, initializer=None, regularizer=None, trainable=True, collections=None, caching_device=None, partitioner=None, validate_shape=True, custom_getter=None)

区别:

由于tf.Variable() 每次都在创建新对象,所有reuse=True 和它并没有什么关系。对于get_variable(),来说,如果已经创建的变量对象,就把那个对象返回,如果没有创建变量对象的话,就创建一个新的。