tensorflow笔记4:函数:tf.assign()、tf.assign_add()、tf.identity()、tf.control_dependencies()
函数原型:
tf.assign(ref, value, validate_shape=None, use_locking=None, name=None)
Defined in tensorflow/python/ops/state_ops.py.
将 value 赋值给 ref,并输出 ref,即 ref = value;
这使得需要使用复位值的连续操作变简单
Defined in tensorflow/python/framework/tensor_shape.py.
| Args | Annotations |
|---|---|
| ref | A mutable Tensor. Should be from a Variable node. May be uninitialized. |
| value | A Tensor. Must have the same type as ref. The value to be assigned to the variable. |
| validate_shape | An optional bool. Defaults to True. If true, the operation will validate that the shape of ‘value’ matches the shape of the Tensor being assigned to. If false, ‘ref’ will take on the shape of ‘value’. |
| use_locking | An optional bool. Defaults to True. If True, the assignment will be protected by a lock; otherwise the behavior is undefined, but may exhibit less contention. |
| name | A name for the operation (optional). |
Returns :
Same as “ref”. Returned as a convenience for operations that want to use the new value after the variable has been reset.
函数原型:
tf.assign_add(ref,value,use_locking=None,name=None)
Defined in tensorflow/python/ops/state_ops.py.
See the guide: Variables > Variable helper functions
Update 'ref' by adding 'value' to it.
更新ref的值,通过增加value,即:ref = ref + value;
This operation outputs "ref" after the update is done. This makes it easier to chain operations that need to use the reset value.
函数原型:tf.identity
tf.identity(input,name=None)
Return a tensor with the same shape and contents as input.
返回一个具有相同形状张量和内容作为输入;
Args:
input: ATensor.name: A name for the operation (optional).
Returns:
A Tensor. Has the same type as input.
函数原型:tf.control_dependencies
tf.control_dependencies(control_inputs)
tf.control_dependencies()设计是用来控制计算流图的,给图中的某些计算指定顺序。比如:我们想要获取参数更新后的值,那么我们可以这么组织我们的代码。自己的理解:如果不是tf的tensor,并且没有加入到整个图中,则不会执行;
Defined in tensorflow/python/framework/ops.py.
See the guide: Building Graphs > Utility functions
Wrapper for Graph.control_dependencies() using the default graph.
See tf.Graph.control_dependencies for more details.
举个例子:
下面程序要做的是,5次循环,每次循环给x加1,赋值给y,然后打印出来,
x = tf.Variable(0.0)
#返回一个op,表示给变量x加1的操作
x_plus_1 = tf.assign_add(x, 1) #control_dependencies的意义是,在执行with包含的内容(在这里就是 y = x)前
#先执行control_dependencies中的内容(在这里就是 x_plus_1)
with tf.control_dependencies([x_plus_1]):
y = x
init = tf.initialize_all_variables() with tf.Session() as session:
init.run()
for i in xrange(5):
print(y.eval())
由于control_dependencies的所以执行print前都会先执行x_plus_1。
这个打印的是0,0,0,0,0 ,也就是说没有达到我们预期的效果,这是因为此时的y是一个复制了x变量的变量,并未和图上的节点相联系不接受流程控制函数的调遣,
改成如下,
import tensorflow as tf
x = tf.Variable(0.0)
print(x)
x_plus_1 = tf.assign_add(x, 1)
with tf.control_dependencies([x_plus_1]):
y = x + 0.0
print(y) #z=tf.identity(x,name='x')
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for i in range(5):
print(sess.run(y))
<tf.Variable 'Variable:0' shape=() dtype=float32_ref>
Tensor("add:0", shape=(), dtype=float32)
1.0 2.0 3.0 4.0 5.0
可以看到当y定义为节点的输出后,就可以顺利执行操作了,此时y成为节点的输出,可以被图识别。
如果改成这样:
x = tf.Variable(0.0)
x_plus_1 = tf.assign_add(x, 1) with tf.control_dependencies([x_plus_1]):
y = tf.identity(x)#修改部分
init = tf.initialize_all_variables() with tf.Session() as session:
init.run()
for i in range(5):
print(y.eval())
This works: it prints 1, 2, 3, 4, 5.
这时候打印的是1,2,3,4,5
解释:
查询y为:Tensor("Identity_1:0", shape=(), dtype=float32),和节点联系起来了。
tf.identity是返回了一个一模一样新的tensor,再control_dependencies的作用块下,需要增加一个新节点到gragh中。
tensorflow笔记4:函数:tf.assign()、tf.assign_add()、tf.identity()、tf.control_dependencies()的更多相关文章
- (四) tensorflow笔记:常用函数说明
tensorflow笔记系列: (一) tensorflow笔记:流程,概念和简单代码注释 (二) tensorflow笔记:多层CNN代码分析 (三) tensorflow笔记:多层LSTM代码分析 ...
- TensorFlow常用的函数
TensorFlow中维护的集合列表 在一个计算图中,可以通过集合(collection)来管理不同类别的资源.比如通过 tf.add_to_collection 函数可以将资源加入一个 或多个集合中 ...
- TensorFlow 常用的函数
TensorFlow 中维护的集合列表 在一个计算图中,可以通过集合(collection)来管理不同类别的资源.比如通过 tf.add_to_collection 函数可以将资源加入一个或多个集合中 ...
- tensorflow笔记3:CRF函数:tf.contrib.crf.crf_log_likelihood()
在分析训练代码的时候,遇到了,tf.contrib.crf.crf_log_likelihood,这个函数,于是想简单理解下: 函数的目的:使用crf 来计算损失,里面用到的优化方法是:最大似然估计 ...
- tensorflow笔记:使用tf来实现word2vec
(一) tensorflow笔记:流程,概念和简单代码注释 (二) tensorflow笔记:多层CNN代码分析 (三) tensorflow笔记:多层LSTM代码分析 (四) tensorflow笔 ...
- tensorflow学习之tf.assign
tf.assign(ref, value, validate_shape=None, use_locking=None, name=None), 函数功能是将value赋值给ref ref必须是tf. ...
- Tensorflow常用的函数:tf.cast
1.tf.cast(x,dtype,name) 此函数的目的是为了将x数据,准换为dtype所表示的类型,例如tf.float32,tf.bool,tf.uint8等 example: import ...
- tensorflow 笔记11:tf.nn.dropout() 的使用
tf.nn.dropout:函数官网说明: tf.nn.dropout( x, keep_prob, noise_shape=None, seed=None, name=None ) Defined ...
- tensorflow笔记6:tf.nn.dynamic_rnn 和 bidirectional_dynamic_rnn:的输出,output和state,以及如何作为decoder 的输入
一.tf.nn.dynamic_rnn :函数使用和输出 官网:https://www.tensorflow.org/api_docs/python/tf/nn/dynamic_rnn 使用说明: A ...
随机推荐
- JavaScript初见
警告alert() 确认confirm() 提问prompt() 空格 JavaScript-打开新窗口(window.open) open() 方法可以查找一个已经存在或者新建的浏览器窗口. 语法: ...
- java 日志体系目录
java 日志体系目录 1.1 java 日志体系(一)log4j1.log4j2.logback.jul.jcl.slf4j 1.2 java 日志体系(二)jcl 和 slf4j 2.1 java ...
- MySql共享锁和排它锁
共享锁和排他锁 1.共享锁: 读锁.X锁,在查询时生效,多个事务在查询同一个数据时共享一把锁,但是不能作用于修改数据,在select语句后添加 lock in share mode : 2.排他锁:在 ...
- redis多实例
1.首先在发布系统: 2.安装多实例利用cmd命令安装,切换目录到redis下 (1)首先拷贝一个redis的conf文件(如redis_6380.conf),并且修改里面的服务端口号.日志端口号,以 ...
- mysql数据库保存sesison会话
<?php header('Content-type:text/html;charset=gbk;'); date_default_timezone_set('PRC'); class db{ ...
- Effective C++ 笔记:条款 33 避免继承导致的名称遮掩
Avoid hiding inherited names 作用域(scopes)所带来的名称二义性,c++编译器会寻找指涉(refer to)的对象并实现名称遮掩规则(name-hiding rule ...
- SAS 分组与排序
SAS 分组与排序 SAS对数据集进行操作时,经常需要在SET.MERGE.MODIFY或 UPDATE语句中使用分组数据.使用分组数据最基本的方法是使用BY 语句,其基本形式如下: BY 变量列表; ...
- flex弹性盒子布局
一.在需要使用弹性盒子的容器上添加属性:display:flex 或者 display:inline-flex; 二.在父容器上添加flex-direction设置子元素主轴方向: 不写默认值是X轴从 ...
- Vue-箭头函数
03-箭头函数 箭头函数 基本语法: ES6允许使用“箭头”(=>)定义函数 var f = a = > a //等同于 var f = function(a){ return a; ...
- Appium之Android功能脚本
Android功能脚本 注:这里只写了登录和退出功能,以下不提供app的包名,下面我使用的是线上包 准备:1.Eclipse的Java环境:2.Appium环境:3.Android真机一台. 创建一个 ...