python人工智能——深度学习——TensorFlow——图和会话
圖
圖默認(rèn)已經(jīng)注冊,一組表示 tf.Operation計(jì)算單位的對象和tf.Tensor表示操作之間流動(dòng)的數(shù)據(jù)單元的對象。
獲取調(diào)用:
tf.get_default_graph() op、sess或者tensor 的graph屬性 import tensorflow as tf import os os.environ['TF_CPP_MIN_LOG_LEVEL']='2'#實(shí)現(xiàn)一個(gè)加法運(yùn)算 a=tf.constant(5.0) b=tf.constant(6.0)print(a,b)sum1=tf.add(a,b)#默認(rèn)的這張圖,相當(dāng)于是給程序分配一段內(nèi)存 graph=tf.get_default_graph()print(graph)print(sum1)with tf.Session() as sess:print(sess.run(sum1))print(a.graph)print(sum1.graph)print(sess.graph) Tensor("Const:0", shape=(), dtype=float32) Tensor("Const_1:0", shape=(), dtype=float32) <tensorflow.python.framework.ops.Graph object at 0x00000184549AA390> Tensor("Add:0", shape=(), dtype=float32) 11.0 <tensorflow.python.framework.ops.Graph object at 0x00000184549AA390> <tensorflow.python.framework.ops.Graph object at 0x00000184549AA390> <tensorflow.python.framework.ops.Graph object at 0x00000184549AA390>圖的創(chuàng)建
tf.Graph()
使用新創(chuàng)建的圖g = tf.Graph() with g.as_default(): a = tf.constant(1.0) assert c.graph is g g=tf.Graph()print(g) with g.as_default():c=tf.constant(11.0)print(c.graph) <tensorflow.python.framework.ops.Graph object at 0x000001BF67EBB390> <tensorflow.python.framework.ops.Graph object at 0x000001BF67EBB390>OP
op:只要使用TensorFlow的API定義的函數(shù)都是OP
會(huì)話
1.運(yùn)行圖的結(jié)構(gòu)
2.分配資源計(jì)算
3.掌握資源(變量的資源,隊(duì)列,線程)
tf.Session()
運(yùn)行TensorFlow操作圖的類,使用默認(rèn)注冊的圖(可以指定運(yùn)行圖)
會(huì)話資源
會(huì)話可能擁有很多資源,如 tf.Variable,tf.QueueBase
和tf.ReaderBase,會(huì)話結(jié)束后需要進(jìn)行資源釋放
sess = tf.Session() sess.run(…) sess.close()
使用上下文管理器
with tf.Session() as sess:
sess.run(…)
config=tf.ConfigProto(log_device_placement=True)
交互式:tf.InteractiveSession()
會(huì)話的run()方法
run(fetches, feed_dict=None,graph=None)
運(yùn)行ops和計(jì)算tensor
嵌套列表,元組,
namedtuple,dict或OrderedDict(重載的運(yùn)算符也能運(yùn)行)
feed_dict 允許調(diào)用者覆蓋圖中指定張量的值,提供給
placeholder使用
返回值異常
RuntimeError:如果它Session處于無效狀態(tài)(例如已關(guān)閉)。
TypeError:如果fetches或feed_dict鍵是不合適的類型。
ValueError:如果fetches或feed_dict鍵無效或引用 Tensor不存在。
總結(jié)
以上是生活随笔為你收集整理的python人工智能——深度学习——TensorFlow——图和会话的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TensorFlow错误:TypeErr
- 下一篇: Andy's First Diction