TensorFlow中tf.Graph()函數

TensorFlow是谷歌基於DistBelief進行研發的第二代人工智能學習系統,其命名來源於本身的運行原理。Tensor(張量)意味着N維數組,Flow(流)意味着基於數據流圖的計算,TensorFlow爲張量從流圖的一端流動到另一端計算過程。TensorFlow是將複雜的數據結構傳輸至人工智能神經網中進行分析和處理過程的系統。

TensorFlow可被用於語音識別或圖像識別等多項機器深度學習領域,對2011年開發的深度學習基礎架構DistBelief進行了各方面的改進,它可在小到一部智能手機、大到數千臺數據中心服務器的各種設備上運行。TensorFlow將完全開源,任何人都可以用。

tf.Graph()函數

tf.Graph() 函數非常重要,主要體現在兩個方面

  1. 它可以通過tensorboard用圖形化界面展示出來流程結構

  2. 它可以整合一段代碼爲一個整體存在於一個圖中

聲明情況大體有三種
  1. tensor:通過張量本身直接出graph
# -*- coding: utf-8 -*-  
import tensorflow as tf

c = tf.constant(4.0)

sess = tf.Session()
sess.run(tf.global_variables_initializer())
c_out = sess.run(c)
print(c_out)
print(c.graph == tf.get_default_graph())
print(c.graph)
print(tf.get_default_graph())

輸出

4.0
True
<tensorflow.python.framework.ops.Graph object at 0x7f382f9ef110>
<tensorflow.python.framework.ops.Graph object at 0x7f382f9ef110>

  1. 通過聲明一個默認的,然後定義張量內容,在後面可以調用或保存
# -*- coding: utf-8 -*-  
import tensorflow as tf

g = tf.Graph()
with g.as_default():
    c = tf.constant(4.0)

sess = tf.Session(graph=g)
c_out = sess.run(c)
print(c_out)
print(g)
print(tf.get_default_graph())


輸出

4.0
<tensorflow.python.framework.ops.Graph object at 0x7f65f1cb2fd0>
<tensorflow.python.framework.ops.Graph object at 0x7f65de447c90>


  1. 通過多個聲明,在後面通過變量名來分別調用

# -*- coding: utf-8 -*-  
import tensorflow as tf

g1 = tf.Graph()
with g1.as_default():
    c1 = tf.constant(4.0)

g2 = tf.Graph()
with g2.as_default():
    c2 = tf.constant(20.0)

with tf.Session(graph=g1) as sess1:
    print(sess1.run(c1))
with tf.Session(graph=g2) as sess2:
    print(sess2.run(c2))

輸出

4.0
20.0

對graph的操作大體有三種
  1. 保存

# -*- coding: utf-8 -*-  
import tensorflow as tf

g1 = tf.Graph()
with g1.as_default():
    # 需要加上名稱,在讀取pb文件的時候,是通過name和下標來取得對應的tensor的
    c1 = tf.constant(4.0, name='c1')

g2 = tf.Graph()
with g2.as_default():
    c2 = tf.constant(20.0)

with tf.Session(graph=g1) as sess1:
    print(sess1.run(c1))
with tf.Session(graph=g2) as sess2:
    print(sess2.run(c2))

# g1的圖定義,包含pb的path, pb文件名,是否是文本默認False
tf.train.write_graph(g1.as_graph_def(),'.','graph.pb',False)

輸出


4.0
20.0

並且在當前文件夾下面生成graph.pb文件

  1. 從pb文件中調用

# -*- coding: utf-8 -*-  
import tensorflow as tf
from tensorflow.python.platform import gfile

#load graph
with gfile.FastGFile("./graph.pb",'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    tf.import_graph_def(graph_def, name='')

sess = tf.Session()
c1_tensor = sess.graph.get_tensor_by_name("c1:0")
c1 = sess.run(c1_tensor)
print(c1)

輸出


4.0

  1. 穿插調用

# -*- coding: utf-8 -*-  
import tensorflow as tf

g1 = tf.Graph()
with g1.as_default():
    # 聲明的變量有名稱是一個好的習慣,方便以後使用
    c1 = tf.constant(4.0, name="c1")

g2 = tf.Graph()
with g2.as_default():
    c2 = tf.constant(20.0, name="c2")

with tf.Session(graph=g2) as sess1:
    # 通過名稱和下標來得到相應的值
    c1_list = tf.import_graph_def(g1.as_graph_def(), return_elements = ["c1:0"], name = '')
    print(sess1.run(c1_list[0]+c2))

輸出


24.0

當然還有很多比較好的地方,比如graph中自帶的名稱函數,通過with寫的內容豐富的代碼塊,這裏就不講了

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