TensorFlow函数——tf.variable( )
来源:互联网 发布:nba最新数据统计 编辑:程序博客网 时间:2024/05/20 03:37
(本文是翻译TensorFlow官网中的tf.variable()函数)
(变量):创建,初始化,保存和加载
您可以通过构造类Variable的实例向图中添加变量。
Variable()构造函数需要变量的初始值,它可以是任何类型和形状的Tensor(张量)。 初始值定义变量的类型和形状。 施工后,变量的类型和形状是固定的。 该值可以使用其中一种赋值方式进行更改。
如果要稍后更改变量的形状,则必须使用带有validate_shape = False的赋值操作。
就像任何Tensor(张量)一样,使用Variable()创建的变量可以用作图中其他操作的输入。 另外,Tensor类重载的所有操作符都被转载到变量中,所以您也可以通过对变量进行算术,将节点添加到图形中。
import tensorflow as tf# Create a variable.(创建一个变量)w = tf.Variable(<initial-value>, name=<optional-name>)# Use the variable in the graph like any Tensor.(使用图形中的变量,如任何张量.)y = tf.matmul(w, ...another variable or tensor...)(tf.matmul参数为w,其他变量或张量)# The overloaded operators are available too.(重载运算符也可用。)z = tf.sigmoid(w + y)# Assign a new value to the variable with `assign()` or a related method.#使用`assign()'或相关方法为变量赋值一个新值。w.assign(w + 1.0)w.assign_add(1.0)
当您启动图时,Variables(变量)必须被明确的初始化在你使用它们的值进行操作之前,你可以通过1运行其初始化器操作初始化variables,2从保存文件中还原variable,或者3仅仅运行一个赋值操作给variable赋值。实际上,variable初始化器操作只是赋值操作,赋值variable自己的初始化值给他自己(In fact, the variable initializer op is just an assign Op that assigns the variable’s initial value to the variable itself.)
# Launch the graph in a session.(在会话中启动图形)with tf.Session() as sess: # Run the variable initializer.(运行variable初始化器。)sess.run(w.initializer) # ...you now can run ops that use the value of 'w'...(你现在可以运行使用'w'值的进行操作)最常见的初始化模式是使用一个便利函数global_variables_initializer( ),将一个可以初始化所有variable的操作添加到图中(the graph)。然后启动图(graph)后运行该操作# Add an Op to initialize global variables.(添加一个操作去初始化所以variable)init_op = tf.global_variables_initializer()# Launch the graph in a session.(在一个session中启动图)with tf.Session() as sess: # Run the Op that initializes global variables.(运行初始化全局Variable的操作)sess.run(init_op) # ...you can now run any Op that uses variable values...(现在你可以运行所有使用任何variable值的操作)
如果需要创建一个取决于另一个variable的初始值的variable,请使用另一个variable的initialized_value()。 这样可以确保以正确的顺序初始化变量。(下面仅是在官网找的一个例子,详细的可以去官网查一下这个函数)
# Initialize 'v' with a random tensor.v = tf.Variable(tf.truncated_normal([10, 40]))# Use `initialized_value` to guarantee that `v` has been# initialized before its value is used to initialize `w`.# The random values are picked only once.w = tf.Variable(v.initialized_value() * 2.0
所有variables都会自动收集到创建它们的图中。 默认情况下,构造函数将新变量添加到Graph集合GraphKeys.GLOBAL_VARIABLES。 这个便利函数global_variables()返回该集合的内容。
在构建机器学习模型时,通常可以方便地区分保存可训练模型参数的variable(变量)和其他variable,例如用于计算训练步骤的全局步长variable(变量)。为了使这更容易,变量构造函数支持trainable = 参数。如果为True,则新variable也将添加到图集合GraphKeys.TRAINABLE_VARIABLES中。
便利函数trainable_variables()返回此集合的内容。 各种Optimizer类使用此集合作为要优化的变量的默认列表。
- TensorFlow函数——tf.variable( )
- TensorFlow 辨异 —— tf.placeholder 与 tf.Variable
- TensorFlow 辨异 —— tf.placeholder 与 tf.Variable
- TensorFlow 辨异 —— tf.placeholder 与 tf.Variable
- TensorFlow 辨异 —— tf.placeholder 与 tf.Variable
- TensorFlow 学习(一)—— tf.get_variable() vs tf.Variable(),tf.name_scope() vs tf.variable_scope()
- TensorFlow--tf.Variable
- tensorflow API:tf.Variable
- tensorflow——tf.one_hot以及tf.sparse_to_dense函数
- TensorFlow函数——tf.placeholder()
- Tensorflow函数——tf.set_random_seed(seed)
- Tensorflow函数——tf.variable_scope()
- TensorFlow 插曲-- tf.placeholder 与 tf.Variable
- tensorflow variable_scope,tf.name_scope, tf.variable, tf.get_varible
- 转载!Tensorflow函数——tf.set_random_seed(seed)
- tensorflow之变量初始化(tf.Variable)
- 【Tensorflow】tf.placeholder函数
- 【Tensorflow】tf.reshape 函数
- SVN环境下快速导出增量包(未提交的情况下)
- Unity基础UI--Image
- 数据结构图之五(拓扑排序)
- POJ 1276 Cash Machine(?)
- 【测试】软件测试的流程图&&软件测试基本概念
- TensorFlow函数——tf.variable( )
- R-CNN论文详解
- 浅析shared_ptr 和weak_ptr、定制删除器
- AndroidStudio MVPPlugin
- JAVA-继承、覆盖class Student extends Person { }
- win10 家庭版无法使用IE浏览器
- linux网络编程(2)——文件系统
- opencv stitching算法分析
- jsoncpp编译并添加到vs2015工程