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