Tensorflow
基础2:图graph
图状结构空间,一个庞大数据结构集合,每个图使用随机内存空间,图都是相对独立的
图的主要作业就是分配内存空间,对于编码本身无意义
一个图中包含 tensor(张量) op(operation-Node,运算符)
tensorflow中有一个默认的图
获取默认的图
tf.get_default_graph()
<tensorflow.python.framework.ops.Graph at 0x23ff54ab400>
创建一个图
graph1 = tf.Graph()
在指定的图中进行张量的声明
with graph1.as_default():
c = tf.constant([1,2,3])
d = tf.constant(789)
graph=None 打开默认的图
with tf.Session() as sess:
add = tf.add(a,b)
display(sess.run(add))
690
使用指定的图
with tf.Session(graph=graph1) as sess:
add = tf.add(c,d)
display(sess.run(add))
array([790, 791, 792])
图之间互相运算 python-numpy
#默认图中的a张量 和 graph1 中的 d进行运算
with tf.Session() as sess:
g1 = sess.run(a)
with tf.Session(graph=graph1) as sess:
g2 = sess.run(d)
#g1和g2是ndarray不会收到tf的约束
g1+g2
912