tensorflow學習筆記:tf.control_dependencies,tf.GraphKeys.UPDATE_OPS,tf.get_collection

tf.control_dependencies(control_inputs):
control_dependencies(control_inputs)

ARGS:

  • control_inputs:在運行上下文中定義的操作之前必須執行或計算的 Operation 列表或 Tensor 對象.也可以是不清除控件依賴項.

返回:

指定上下文中構建的所有操作的控制依賴關係的上下文管理器.

這個上下文就是with裏邊的內容

Use with the with keyword to specify that all operations constructed within the context should have control dependencies on control_inputs. For example:

with g.control_dependencies([a, b, c]):
  # `d` and `e` will only run after `a`, `b`, and `c` have executed.
  d = ...
  e = ...

關於tf.GraphKeys.UPDATE_OPS,這是一個tensorflow的計算圖中內置的一個集合,其中會保存一些需要在訓練操作之前完成的操作,並配合tf.control_dependencies函數使用。

tf.get_collection():

get_collection(
    name,
    scope=None
)

Args:

  • name: The key for the collection. For example, the GraphKeys class contains many standard names for collections.
  • scope: (Optional.) A string. If supplied, the resulting list is filtered to include only items whose nameattribute matches scope using re.match. Items without a name attribute are never returned if a scope is supplied. The choice of re.match means that a scope without special tokens filters by prefix.
# 在'My-TensorFlow-tutorials-master/02 CIFAR10/cifar10.py'代碼中

  variables = tf.get_collection(tf.GraphKeys.VARIABLES)
  for i in variables:
  print(i)

>>>   <tf.Variable 'conv1/weights:0' shape=(3, 3, 3, 96) dtype=float32_ref>
      <tf.Variable 'conv1/biases:0' shape=(96,) dtype=float32_ref>
      <tf.Variable 'conv2/weights:0' shape=(3, 3, 96, 64) dtype=float32_ref>
      <tf.Variable 'conv2/biases:0' shape=(64,) dtype=float32_ref>
      <tf.Variable 'local3/weights:0' shape=(16384, 384) dtype=float32_ref>
      <tf.Variable 'local3/biases:0' shape=(384,) dtype=float32_ref>
      <tf.Variable 'local4/weights:0' shape=(384, 192) dtype=float32_ref>
      <tf.Variable 'local4/biases:0' shape=(192,) dtype=float32_ref>
      <tf.Variable 'softmax_linear/softmax_linear:0' shape=(192, 10) dtype=float32_ref>
      <tf.Variable 'softmax_linear/biases:0' shape=(10,) dtype=float32_ref>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章