一、什么是圖結(jié)構(gòu)
1、圖包含了一組tf.Operation代表的計(jì)算單元對(duì)象和tf.Tensor代表的計(jì)算單元之間流動(dòng)的數(shù)據(jù)
圖結(jié)構(gòu):數(shù)據(jù)(Tensor) + 操作(Operation)
二、圖相關(guān)操作
1、默認(rèn)圖
通常TensorFlow會(huì)默認(rèn)幫我們創(chuàng)建一張圖
查看默認(rèn)圖的兩種方法:
(1)通過調(diào)用tf.compat.v1.get_default_graph()訪問,要將操作添加到默認(rèn)圖形中,直接創(chuàng)建OP即可
(2)op、sess都含有g(shù)raph屬性,默認(rèn)都在一張圖中
注:2.x版本(使用默認(rèn)圖)不支持調(diào)用屬性,會(huì)報(bào)錯(cuò)“AttributeError: Tensor.graph is meaningless when eager execution is enabled.”
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import tensorflow as tf
def tensorflow_demo():
"""
TensorFlow的基本結(jié)構(gòu)
"""
# TensorFlow實(shí)現(xiàn)加減法運(yùn)算
a_t = tf.constant(2)
b_t = tf.constant(3)
c_t = a_t + b_t
print("TensorFlow加法運(yùn)算結(jié)果:\n", c_t)
print(c_t.numpy())
# 2.0版本不需要開啟會(huì)話,已經(jīng)沒有會(huì)話模塊了
return None
def graph_demo():
"""
圖的演示
"""
# TensorFlow實(shí)現(xiàn)加減法運(yùn)算
a_t = tf.constant(2)
b_t = tf.constant(3)
c_t = a_t + b_t
print("TensorFlow加法運(yùn)算結(jié)果:\n", c_t)
print(c_t.numpy())
# 查看默認(rèn)圖
# 方法1:調(diào)用方法
default_g = tf.compat.v1.get_default_graph()
print("default_g:\n", default_g)
# 方法2:查看屬性
# print("a_t的圖屬性:\n", a_t.graph)
# print("c_t的圖屬性:\n", c_t.graph)
return None
if __name__ == "__main__":
# 代碼1:TensorFlow的基本結(jié)構(gòu)
# tensorflow_demo()
# 代碼2:圖的演示
graph_demo()
python3 day01_deeplearning.py
TensorFlow加法運(yùn)算結(jié)果:
tf.Tensor(5, shape=(), dtype=int32)
5
default_g:
<tensorflow.python.framework.ops.Graph object at 0x7f27651b5be0>
2、創(chuàng)建圖
(1)可以通過tf.Graph()自定義創(chuàng)建圖
(2)如果要在這張圖中創(chuàng)建OP,典型用法是使用tf.Graph.as_default()上下文管理器文章來源:http://www.zghlxwxcb.cn/news/detail-829799.html
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import tensorflow as tf
def tensorflow_demo():
"""
TensorFlow的基本結(jié)構(gòu)
"""
# TensorFlow實(shí)現(xiàn)加減法運(yùn)算
a_t = tf.constant(2)
b_t = tf.constant(3)
c_t = a_t + b_t
print("TensorFlow加法運(yùn)算結(jié)果:\n", c_t)
print(c_t.numpy())
# 2.0版本不需要開啟會(huì)話,已經(jīng)沒有會(huì)話模塊了
return None
def graph_demo():
"""
圖的演示
"""
# TensorFlow實(shí)現(xiàn)加減法運(yùn)算
a_t = tf.constant(2)
b_t = tf.constant(3)
c_t = a_t + b_t
print("TensorFlow加法運(yùn)算結(jié)果:\n", c_t)
print(c_t.numpy())
# 查看默認(rèn)圖
# 方法1:調(diào)用方法
default_g = tf.compat.v1.get_default_graph()
print("default_g:\n", default_g)
# 方法2:查看屬性
# print("a_t的圖屬性:\n", a_t.graph)
# print("c_t的圖屬性:\n", c_t.graph)
# 自定義圖
new_g = tf.Graph()
# 在自己的圖中定義數(shù)據(jù)和操作
with new_g.as_default():
a_new = tf.constant(20)
b_new = tf.constant(30)
c_new = a_new + b_new
print("c_new:\n", c_new)
print("a_new的圖屬性:\n", a_new.graph)
print("b_new的圖屬性:\n", b_new.graph)
# 開啟new_g的會(huì)話
with tf.compat.v1.Session(graph=new_g) as sess:
c_new_value = sess.run(c_new)
print("c_new_value:\n", c_new_value)
print("我們自己創(chuàng)建的圖為:\n", sess.graph)
return None
if __name__ == "__main__":
# 代碼1:TensorFlow的基本結(jié)構(gòu)
# tensorflow_demo()
# 代碼2:圖的演示
graph_demo()
python3 day01_deeplearning.py
TensorFlow加法運(yùn)算結(jié)果:
tf.Tensor(5, shape=(), dtype=int32)
5
default_g:
<tensorflow.python.framework.ops.Graph object at 0x7f19806c4d68>
c_new:
Tensor("add:0", shape=(), dtype=int32)
a_new的圖屬性:
<tensorflow.python.framework.ops.Graph object at 0x7f19809f5748>
b_new的圖屬性:
<tensorflow.python.framework.ops.Graph object at 0x7f19809f5748>
c_new_value:
50
我們自己創(chuàng)建的圖為:
<tensorflow.python.framework.ops.Graph object at 0x7f19809f5748>
說明:
(1)默認(rèn)圖執(zhí)行結(jié)果是tf.Tensor(5, shape=(), dtype=int32)
(2)自定義圖執(zhí)行結(jié)果是Tensor("add:0", shape=(), dtype=int32)
(3)自定義圖沒有即時(shí)執(zhí)行,需要開啟Session指定圖來執(zhí)行
(4)可以看到默認(rèn)圖地址為0x7f19806c4d68,自定義圖地址為0x7f19809f5748
?文章來源地址http://www.zghlxwxcb.cn/news/detail-829799.html
到了這里,關(guān)于深度學(xué)習(xí)基礎(chǔ)之《TensorFlow框架(2)—圖》的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!