TensorFlow编程策略

1 计算图与张量

    TensorFlow程序中的计算过程可以表示为一个计算图(Computation Graph, 也可称为有向图(Directed Graph)),其作用与外观都可类比为程序流程图。

     张量是在边中流动的数据,其数据类型可以在编程时事先定义,也可根据计算图中的上下文来推断。

2 计算图——TensorFlow的计算模型

      计算图的作用可以类比为程序的流程图,可以使用TensorBoard工具可视化TensorFlow程序的计算图。用以下例子简单的向量相加介绍计算图:

import tensorflow as tf

a = tf.constant([1.0,2.0], name="a")

b = tf.constant([3.0,4.0], name="b")

result = a+b

print (a.graph is tf.get_default_graph())
print (b.graph is tf.get_default_graph())

       编写TensorFlow程序时,系统会自动维护一个默认的计算图,上面代码是判断 a 和 b 是否属于默认的计算图,结果会输出 True, 因为我们没有指定 a 和 b 属于哪一个计算图。

       使用默认计算图可以满足要求,当我们需要使用更到计算图时,可通过 Graph函数来生成新的计算图。对于生成的计算图,可通过 as_default()函数将其指定为默认。以下代码使用 Graph()函数来生成两个计算图及使用 as_default()函数将生成的计算图指定为默认:

import tensorflow as tf

#使用Graph()函数创建一个计算图
g1 = tf.Graph()
with g1.as_default():   #使用as_default()函数将定义的计算图使用为默认

    a = tf.get_variable("a", [2], initializer=tf.ones_initializer())
    b = tf.get_variable("b", [2], initializer=tf.zeros_initializer())

#使用Graph()函数创建另一个计算图
g2 = tf.Graph()
with g2.as_default():
    a = tf.get_variable("a", [2], initializer=tf.zeros_initializer())
    b = tf.get_variable("b", [2], initializer=tf.ones_initializer())\

with tf.Session(graph=g1) as sess:
    tf.global_variables_initializer().run()
                                #初始化计算图中的所有变量
    with tf.variable_scope("", reuse=True):
        print(sess.run(tf.get_variable("a")))
        print(sess.run(tf.get_variable("b")))

with tf.Session(graph=g2) as sess:
    tf.global_variables_initializer().run()
                                # 初始化计算图中的所有变量
    with tf.variable_scope("", reuse=True):
        print(sess.run(tf.get_variable("a")))
        print(sess.run(tf.get_variable("b")))

3 张量

     TensorFlow中的张量保存的时运算结果的属性,而不是真正的数字。用一个向量相加的例子进行说明:

import tensorflow as tf

a = tf.constant([1.0,2.0],name="a")
b = tf.constant([3.0,5.0],name="b")

result = a+b
print (result)

打印 result 张量,显示的结果不是数字,而是加法运算结果的属性:操作(op)、维度(shape)和数据类型(dtype)。

4 会话——TensorFlow运行模型

    Session就是用户使用TensorFlow时的交互接口,被实现为Session类。使用run()方法来执行计算图,用户给run()函数传入需要计算的节点,同时提供输入的数据,则能计算该节点数据。

    使用会话方式有两种,一是明确调用会话生成函数和会话关闭函数,通过Session类Session()构造函数创建会话实例,然后通过close()函数关闭会话,通过这种方式必须明确使用close结束会话。

可使用 with/as使用会话。

5 placeholder机制

     placeholder机制用于在会话运行时动态提供输入数据,以下代码展示变量相加如何在运行时提供值:

import tensorflow as tf

#用placehoder定义一个位置
#原型plaeshoder(dtype,shape,name)

a = tf.placeholder(tf.float32, shape=(2), name="input")
b = tf.placeholder(tf.float32, shape=(4,2), name="input")

result = a+b

with tf.Session() as sess:
    #run()函数原型run(self,fetches, feed_dict, options, run_metadata)
    #fetches参数接受result,feed_dict参数指定了需要提供的值
    res1 = sess.run(result, feed_dict={a:[1.0,2.0],b:[[3.0,4.0],[5.0,6.0],[7.0,8.0],[9.0,10.0]]})
    print(result)
    print(res1)

6 TensorFlow变量

     在TensorFlow中声明一个变量需要使用变量声明函数——Variable()。因为变量在声明时就需要进行初始化,所以函数内部需要给出变量初始化的方法。如下所示:

a = tf.Variable(tf.random_normal([3,4],stddev=1))

7 管理变量空间

get_variable()函数用于请求获取变量,其使用方法和Variable()函数基本相同。

varibale_scope()函数结合上下文管理器(with)生成一个变量空间。

name_scope()函数类似于variable_scope()函数的变量空间管理功能。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值