基本介紹
圖的介紹
圖是數(shù)據(jù)結(jié)構(gòu)和算法學(xué)里最強(qiáng)大的框架之一, 可以用來表示所有類型的結(jié)構(gòu)和系統(tǒng).
- 頂點/節(jié)點
- 邊(edge): 頂點之間的線段就是邊,表示事物之間的關(guān)系. 比如社交網(wǎng)絡(luò)的關(guān)注粉絲關(guān)系.
- 有向圖和無向圖: 區(qū)別就是有向圖中的邊的關(guān)系是有方向的.
- 環(huán)圖和無環(huán)圖: 關(guān)系的傳遞
Computation Graph : 是邊(data)和節(jié)點(operation)組成的有向數(shù)據(jù)流圖
- 邊代表數(shù)據(jù)的流向, 也可是說的data的傳遞
- 節(jié)點代表對數(shù)據(jù)的操作和計算, 對數(shù)據(jù)的操作
Graph常規(guī)操作
- 默認(rèn)圖中創(chuàng)建操作/函數(shù)
import tensorflow as tf
c = tf.constant(1.0)
a = 1.0
print(c)
print(a)
print(c.graph)
print(tf.get_default_graph)
print(c.name)
#輸出結(jié)果
Tensor("Const:0", shape=(), dtype=float32)
1.0
<tensorflow.python.framework.ops.Graph object at 0x0000022D5F630BE0>
<function get_default_graph at 0x0000022D70A6DBF8>
Const:0
- 在不通的圖里創(chuàng)建函數(shù)
#上下文管理器選擇圖
g = tf.Graph()
print('g:', g)
with g.as_default():
d = tf.constant(1.0)
print(d.graph)
#運(yùn)行結(jié)果:
g: <tensorflow.python.framework.ops.Graph object at 0x000002413EB3BC50>
<tensorflow.python.framework.ops.Graph object at 0x000002413EB3BC50>
- 為圖分配不同的運(yùn)行設(shè)備
g2 = tf.Graph()
with g2.device('/gpu:0'):
a = tf.constant(1.5)
- namescope, 能是神經(jīng)網(wǎng)絡(luò)的節(jié)點標(biāo)簽清晰
with tf.name_scope('A'):
a1 = tf.Variable([1], name='a1')
with tf.name_scope('B'):
a2 = tf.Variable([1], name='a2')
with tf.name_scope('A'):
a3 = tf.Variable([1], name='a3')
print(a1)
print(a2.name)
print(a3)
# 運(yùn)行結(jié)果
<tf.Variable 'A/a1:0' shape=(1,) dtype=int32_ref>
A/B/a2:0
<tf.Variable 'A_1/a3:0' shape=(1,) dtype=int32_ref>