tensorflow API:tf.Variable
来源:互联网 发布:男生不喜欢女生 知乎 编辑:程序博客网 时间:2024/05/19 09:01
原文:官网
一个Variable变量通过run()保存流程图中的状态;创建一个变量,即创建一个Variable类的实例加入graph中
Variable()构造器要求变量有初始化值,初始化值给出了Tensor的类型和形状。变量创建以后,变量的type和shape都被固定,变量的值可以通过assign方法改变。
如果想改变变量的shape,通过使用assign方法,并且设置validate_shape = False
可以通过其他变量初始化现有的变量,并且变量之间的算术运算也能再现有的graph中加入新节点。
流程如下:
import tensorflow as tf
#创建变量
w = tf.Variable(, name=)
# 用变量构建graph
y = tf.matmul(w, …another variable or tensor…)
#操作符被重载
z = tf.sigmoid(w + y)
# 通过assign()
或者related method给变量赋新值。
w.assign(w + 1.0)
w.assign_add(1.0)
当我们启动graph的时候,运行各个操作(ops)用到了图里的变量值,那变量必须在这之前被明确的初始化。可以运行variable的initializer ,从文件恢复(restoring)变量,或者赋值(assigne)操作给变量赋值。
例子:
在一个会话里启动图。
with tf.Session() as sess: # Run the variable initializer. sess.run(w.initializer)
..现在可以用使用操作w的运算符了(ops)…**
The most common initialization pattern is to use the convenience function global_variables_initializer() to add an Op to the graph that initializes all the variables. You then run that Op after launching the graph.
最常使用的初始化模式是用global_variables_initializer()添加一个op到已有的图中,这个操作会初始化图中所有的变量,
添加一个op作为全局变量初始化操作符。
init_op = tf.global_variables_initializer()
在会话(session)里启动图。
with tf.Session() as sess: # 运行可以初始化全局变量的这个操作符。 sess.run(init_op) ...现在可以运行任意的变量了...
当需要用其他变量初始化现有变量时,用其他变量的initialized_value()方法,这保证变量正确的被初始化。
全部的变量都自动被收集到创建得图上。默认的构造器添加新变量到collection GraphKeys.GLOBAL_VARIABLES中。可以用global_variables()函数得到collection全局的上下文
在构建一个机器学习模型的时候,经常需要区别哪些变量是该模型控制的可训练的参数,哪些变量是全局变量用来计数训练步骤等,因此所有的变量构造器支持一个trainable=的参数。如果设为True,这个新变量添加到collection GraphKeys.TRAINABLE_VARIABLES中,表示是模型控制的可训练参数。可以用trainable_variables()函数得到collection可训练的上下文。
各种优化器类使用此collection优化变量的默认列表
- tensorflow API:tf.Variable
- TensorFlow--tf.Variable
- TensorFlow 插曲-- tf.placeholder 与 tf.Variable
- tensorflow API:tf.truncated_normal
- tensorflow API:tf.set_random_seed
- tensorflow API:tf.split
- tensorflow API:tf.tile
- tensorflow API:tf.slice
- tensorflow API: tf.unsorted_segment_sum
- tensorflow API:tf.control_dependencies
- tensorflow API:tf.reduce_sum
- tensorflow API:tf.not_equal
- tensorflow API:tf.argmin
- tensorflow API: tf.unsorted_segment_sum
- tensorflow API: tf.concat
- tensorflow API:tf.group
- tensorflow variable_scope,tf.name_scope, tf.variable, tf.get_varible
- TensorFlow函数——tf.variable( )
- String是否为空的注意点
- Winform登陆+验证码+链接数据库+窗口切换
- C++学习之Pair
- 局域网技术
- 在C++中调用Jieba进行中文分词
- tensorflow API:tf.Variable
- 点击编辑 弹出弹框(iframe)
- docker centos 使用 systemctl Failed to get D-Bus connection: Operation not permitted
- J2EE相关概念
- 土豪向往技术宅,迪拜欲成中东的区块链老大
- python 高度健壮性爬虫的异常和超时问题
- PHP关于时间的时间戳
- 利用某网站注册短信验证码漏洞,抓包改包实现短信轰炸骚扰
- Android 定时任务刷新的多种实现方式