函数: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.

函数原型:

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;

函数原型: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中。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值