TensorFlow三:构建和执行tensorFlow张量图

 

一、tensorflow图的构建阶段:

创建tensorflow图:

import tensorflow as tf
# 1.定义两个常量a, b
a = tf.constant([[1, 3], [5, 6]], dtype=tf.int32)
b = tf.constant([4, 5, 6, 7], dtype=tf.int32, shape=[2, 2])
print(type(a))  # <class tensorflow.python.framework.ops.tensor>
print(a)  # Tensor("a:0", shape=(2, 2), dtype=int32)
print(b)  # Tensor("b:0", shape=(2, 2), dtype=int32)

# 2.以a, b 作为输入,进行矩阵的乘法运算
c = tf.matmul(a, b)
print(type(c))  # <class tensorflow.python.framework.ops.tensor>
print(c)  # Tensor("MatMul:0", shape=(2, 2), dtype=int32)

# 3.以a, c作为输入,进行矩阵的相加操作
g = tf.add(a, c)
print(g)    # Tensor("MatAdd:0", shape=(2, 2), dtype=int32)

# 4.查看常量是否在默认图中
print("查看变量a是否在默认图中: ", a.graph is tf.get_default_graph())
print("查看变量c是否在默认图中: ", c.graph is tf.get_default_graph())

# 5.创建一张新图
graph1 = tf.Graph()
with graph1.as_default():
    # 在这个代码块里面,使用的就是graph1这张图
    e = tf.constant(5.0, name="const_e")
    print(e)  # Tensor("const_e:0", shape=(), dtype=float32)
    print("查看变量e是否在graph1图中: ", e.graph is graph1)  # True
    print("查看变量e是否在默认图中: ", tf.graph is e.get_default_graph())  # True
    # 为什么e.graph也是默认图,因此with...graph1.as_default(),将在代码块内,将graph1设置了为默认图
print("查看变量e是否还在默认图中: ", e.graph is tf.get_default_graph())  # False
# 在外面就是使用的默认图

graph2 = tf.Graph()
with graph2.as_default():
    f = tf.constant(3.0, name="const_f")
    print("查看变量f是否在新图graph2图中: ", f.graph is graph2)  # True
    print("查看变量f是否在默认图中: ", f.graph is f.get_default_graph())  # True
# 6.注意:不能使用两个图中的变量进行操作!!!
dd = tf.add(e, f)  # error
ddd = tf.add(e, a)  # error

 

 二、tensorflow图的执行阶段

执行阶段:会话的上下文中,Seesion会话的创建、运行、关闭

默认情况下,创建的session属于默认图

一个图可以有多个session,一个session只能属于一个图

# 7.执行阶段:会话的上下文中,Seesion会话的创建、运行、关闭
# 默认情况下,创建的session属于默认图

# 会话的创建
session = tf.Session()
# session = tf.Session(graph=tf.get_default_graph())
print(session)  # <tensorflow.python.client.session.Session object at 0x2341234f0>

# 会话的运行:
# 调用session对象的run方法,执行矩阵a*b,得到c的结果值(所以需要将c作为参数传入)
# 在运行的时侯,不需要考虑图中的运算过程,只需要关注最终结果对应的对象fetches,以及所需要的输入数据值feed_dict
result = session.run(fetches=c)
print("type:{}, \nvalue: {}".format(type(result), result))
# type: "<class 'numpy.ndarray'>"
# value: [[19 22] [43 50]]
result2 = session.run(fetches=g)
print("type:{}, \nvalue: {}".format(type(result2), result2))
result12 = session.run(fetches=[c, g])
print("type:{}, \nvalue: {}".format(type(result12), result12))

# 会话的关闭
session.close()
# ee = session.run(c)   # 会话已关闭,不能再运行会话,将抛出异常

 

 

 

增加图的复杂程序:

# 增加图的复杂程序
h = tf.subtract(b, a, name='b-a')
i = tf.matmul(h, c, name='h*c')
j = tf.add(g, i, name='g+i')

 

通过with session上下文,可以自动关闭session会话。

 

with tf.Session() as sess:
    print(sess)
    print(sess.run(c))
    print(sess.run(g))
    print(sess.run([c, g]))
    print(sess.run([h, i, j]))
with tf.Session(graph=graph1) as sess:
    print(sess.run(e))
with tf.Session(graph=graph2) as sess:
    print(sess.run(f))

 

会话的eval()方法,同run()方法,是一样的。

 

如果仅安装的cpu版本的tensorflow,运算时,可能会提示无GPU的一些提示信息。关闭tensorflow提示信息的方法:

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

 

 构建图和执行图的案例:

交互式会话,用得比较少:

 

 

 

posted on 2019-01-31 21:02  myworldworld  阅读(313)  评论(0)    收藏  举报

导航