Tensorflow中張量,節點,命名理解

張量,節點,命名理解

在定義靜態圖的時候,

a = tf.constant([1.0,2.0],name='x')

b = tf.constant([1.0,2.0],name='y')

c = tf.add(a,b,name='nmsl')

print(c)

<tensor>:nmsl:0,shape,dtype)

可以看出,c是一個tensor類型,所以tensor其實代表一個數據流,說白了就是一個節點輸出,其中每個tensor的名字爲對應節點的名字加:0,0代表該節點的第一個輸出tensor.

其實一般一個節點只有一個輸出,所以後面基本都是補零,因爲如果寫個d=tf.add(a,b,name='nmsl'),這個節點被改名字爲nmsl_1, 所以d這個tensor的名字就做nmsl_1:0.

總結:<張量> tensor就是指輸出的數據,名字就是其對應節點名字加:0。節點(op)包括兩種屬性:1.name(這個那麼可以自己指定,不指定話就被默認把op換成小寫的字母作爲name). 2.OP(有時也叫type,可以理解爲tensorflow中的基本類型,常見的有Add,Mul。。。。。)

注意:如果在同一個命名空間中,如果命名name已經存在,則name會自動的在名字後面補充_1,_2....依次往後添加。

測試代碼:

>>> import tensorflow as tf
>>> a=tf.constant([1.0,2.0],name='test1')
>>> b=tf.constant([1.0,2.0],name='test2')
>>> result=a+b
>>> sess=tf.Session()
2019-05-30 19:36:03.027829: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
>>> tf.train.write_graph(tf.get_default_graph(),"./","model.pbtxt")
'./model'
>>> print(result)
Tensor("add:0", shape=(2,), dtype=float32)
>>> sess.close()
>>> result2=tf.multiply(a,b,name="nmsl")
>>> sess=tf.Session()
>>> tf.train.write_graph(tf.get_default_graph(),"./","model.pbtxt")
'./model.pbtxt'
>>> print(result2)
Tensor("nmsl:0", shape=(2,), dtype=float32)
>>> sess.close()
>>> d=tf.multiply(a,b,name="nmsl")
>>> sess=tf.Session()
>>> tf.train.write_graph(tf.get_default_graph(),"./","model.pbtxt")
'./model.pbtxt'
>>> print(d)
Tensor("nmsl_1:0", shape=(2,), dtype=float32)
>>> sess.close()
>>> e=a+b
>>> f=d+a
>>> sess=tf.Session()
>>> tf.train.write_graph(tf.get_default_graph(),"./","model.pbtxt")
'./model.pbtxt'
>>> print(e)
Tensor("add_1:0", shape=(2,), dtype=float32)
>>> print(f)
Tensor("add_2:0", shape=(2,), dtype=float32)

>>> sess.close()

視圖網址:

https://lutzroeder.github.io/netron/

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