121、TensorFlow張量命名


# tf.Graph對象定義了一個命名空間對於它自身包含的tf.Operation對象
# TensorFlow自動選擇一個獨一無二的名字,對於數據流圖中的每一個操作
# 但是給操作添加一個描述性的名字可以使你的程序更容易來閱讀和調試
# TensorFlow api提供兩種方式來重寫一個操作的名字
# 1、每一個api函數創建了一個新的tf.Operation或者返回一個新的tf.Tensor接收一個可選的name參數
#     列如tf.constant(42.0,name="answer")創建了一個新的tf.Operation叫做answer
#     並且返回以叫做"answer:0"的tf.Tensor .如果默認的數據流圖已經包含了叫做"answer"的操作
#     TensorFlow會在后面append,"_1","_2"來使它變得唯一
# 2、tf.name_scope函數使得在名字后面添加一個后綴變得可能
import tensorflow as tf
c_0 = tf.constant(0, name="c")  # operation named "c"
# already-used names will be "uniquified" 
c_1 = tf.constant(2, name="c")  # => operation named "c_1"
# Name scopes add a prefix to all operations created in the same context
with tf.name_scope("outer"):
    c_2 = tf.constant(2, name="c")  # =>operation nemed "outer/c"
    
    # 在層次化文件系統中,名稱范圍嵌套類似的路徑
    with tf.name_scope("inner"):
        c_3 = tf.constant(3, name="c")
    
    # 已經存在的變量名字會返回到前一個變量名加上一個后綴
    c_4 = tf.constant(4, name="c")  # =>operation named "outer/c_1"
    
    # 已經使用的命名空間會被 "uniquified"
    with tf.name_scope("inner"):
        c_5 = tf.constant(5, name="c")  # =>operation named "outer/inner_1/c"

init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    print(c_0)
    print(c_1)
    print(c_2)
    print(c_3)
    print(c_4)
    print(c_5)

下面是上面代碼的輸出結果:

2018-02-17 11:01:55.084300: I C:\tf_jenkins\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
Tensor("c:0", shape=(), dtype=int32)
Tensor("c_1:0", shape=(), dtype=int32)
Tensor("outer/c:0", shape=(), dtype=int32)
Tensor("outer/inner/c:0", shape=(), dtype=int32)
Tensor("outer/c_1:0", shape=(), dtype=int32)
Tensor("outer/inner_1/c:0", shape=(), dtype=int32)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM