tensorflow笔记4:函数:tf.assign()、tf.assign_add()、tf.identity()、tf.control_dependencies()

时间:2022-03-25 16:31:42

函数原型:

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: A Tensor.
  • name: A name for the operation (optional).

Returns:

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()的更多相关文章

  1. (四) tensorflow笔记:常用函数说明

    tensorflow笔记系列: (一) tensorflow笔记:流程,概念和简单代码注释 (二) tensorflow笔记:多层CNN代码分析 (三) tensorflow笔记:多层LSTM代码分析 ...

  2. TensorFlow常用的函数

    TensorFlow中维护的集合列表 在一个计算图中,可以通过集合(collection)来管理不同类别的资源.比如通过 tf.add_to_collection 函数可以将资源加入一个 或多个集合中 ...

  3. TensorFlow 常用的函数

    TensorFlow 中维护的集合列表 在一个计算图中,可以通过集合(collection)来管理不同类别的资源.比如通过 tf.add_to_collection 函数可以将资源加入一个或多个集合中 ...

  4. tensorflow笔记3:CRF函数:tf&period;contrib&period;crf&period;crf&lowbar;log&lowbar;likelihood&lpar;&rpar;

    在分析训练代码的时候,遇到了,tf.contrib.crf.crf_log_likelihood,这个函数,于是想简单理解下: 函数的目的:使用crf 来计算损失,里面用到的优化方法是:最大似然估计 ...

  5. tensorflow笔记&colon;使用tf来实现word2vec

    (一) tensorflow笔记:流程,概念和简单代码注释 (二) tensorflow笔记:多层CNN代码分析 (三) tensorflow笔记:多层LSTM代码分析 (四) tensorflow笔 ...

  6. tensorflow学习之tf&period;assign

    tf.assign(ref, value, validate_shape=None, use_locking=None, name=None), 函数功能是将value赋值给ref ref必须是tf. ...

  7. Tensorflow常用的函数&colon;tf&period;cast

    1.tf.cast(x,dtype,name) 此函数的目的是为了将x数据,准换为dtype所表示的类型,例如tf.float32,tf.bool,tf.uint8等 example:  import ...

  8. tensorflow 笔记11:tf&period;nn&period;dropout() 的使用

    tf.nn.dropout:函数官网说明: tf.nn.dropout( x, keep_prob, noise_shape=None, seed=None, name=None ) Defined ...

  9. tensorflow笔记6:tf&period;nn&period;dynamic&lowbar;rnn 和 bidirectional&lowbar;dynamic&lowbar;rnn:的输出,output和state,以及如何作为decoder 的输入

    一.tf.nn.dynamic_rnn :函数使用和输出 官网:https://www.tensorflow.org/api_docs/python/tf/nn/dynamic_rnn 使用说明: A ...

随机推荐

  1. NGUI渲染流程

    1 渲染流程 NGUI的渲染流程其实就是把Widget组件生成Mesh所需要的缓存数据,然后生成对应的DrallCall组合对应数据,生成渲染需要的Mesh数据,提交渲染. Widget(数据) UI ...

  2. kernel&sol;info&period;c

    /* * linux/kernel/info.c * * Copyright (C) 1992 Darren Senn */ /* This implements the sysinfo() syst ...

  3. andrond mk通配符遍历文件夹

    define all-cpp-files-under$(patsubst ./%,%, \ $(shell cd $(LOCAL_PATH) ; \ find $(1) -name "*.c ...

  4. swift 属性

    属性将值和类,结构,枚举相关联.属性分为计算属性和存储属性.存储属性存储常量或变量作为实例的一部分 ,计算属性计算一个值.存储属性用于类和结构体,计算属性用于类,结构体和枚举. 1:存储属性 存储属性 ...

  5. 安全初始化MySql服务器

    我们在安装完MySql服务器,设置好MySql的root用户密码后,就直接开始使用了,其实这样的MySql服务器还存在着一些不安全因素, 本篇演示一下用命令mysql_secure_installat ...

  6. Django架设blog步骤(转)

    最近在研究Python,起初是因为想做个爬虫,昨天看了点基础教程,*辅仁大学的视频,了解了python的语法规范及语言特性,主要有三: 1.动态脚本语言: 2.语法简洁,强制缩进: 3.应用广泛,w ...

  7. 2013~2014年度 NOIP~GDOI总结

    滚回去撸一年中考撸完之后就迎来了NOIP2013(话说初赛差点被坑了有木有= =)想想当年10月还是那样的天真的去了广州,结果就被虐成翔的回来了= =只做了三道签到题的渣渣就是弱= =DAY1T2死活 ...

  8. 函数调用过程&amp&semi;生成器解释

    摘自马哥解答,感谢. 函数调用过程: 假设程序是单进程,单执行流,在某一时刻,能运行的程序流只能有一个.但函数调用会打开新的执行上下文,因此,为了确保main函数可以恢复现场,在main函数调用其它函 ...

  9. Lucene 5&period;X 版本索引文件格式

    原文链接:https://my.oschina.net/rickylau/blog/527602 名称 文件拓展名 描述 段文件 segments_N 保存了索引包含的多少段,每个段包含多少文档. 段 ...

  10. UVa - 1616 - Caravan Robbers

    二分找到最大长度,最后输出的时候转化成分数,比较有技巧性. AC代码: #include <iostream> #include <cstdio> #include <c ...