• TensorFlow Tutorial: Practical TensorFlow lesson for quick learners


    这篇教程分为两部分,第一部分用例子解释基础概念,第二部分构建线性回归模型。

    Part-1: TensorFlow基础

    TensorFlow是一个数据流经过的图,数据表示为n维向量,图由数据和操作组成。

    • 节点:数学操作
    • 边:数据

    TensorFlow跟其他编程语言不同之处在于不论你想创建什么都必须先创建一幅蓝图,默认初始化是不包含任何变量的。

    sankit@sankit:~$ python
    Python 2.7.6 (default, Oct 26 2016, 20:30:19) 
    [GCC 4.8.4] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 
    >>> import tensorflow as tf
    

      

    (i) Graph in TensorFlow:

    图是TensorFlow的基石,每一个计算,操作,变量都存储在图中,你可以这样使用图:

    graph = tf.get_default_graph()

    你可以获取图的全部操作:

    graph.get_operations()  

     

     目前它是空的
     
    for op in graph.get_operations(): 
        print(op.name)
    

     你可以打印出它们的名字,当然在我们没有添加操作之前,它还是空的。

     当然你可以创建多幅图,但这是之后的事情了。
     

    (ii) TensorFlow Session:

    图用于定义操作,但是操作必须在会话中执行,它们之间是独立创建的,你可以把图想象为蓝图,把会话想象为建筑工地。

    图只定义了操作,但并没有变量,更没有值,只有我们在会话中运行图,这一切才有意义。

    你可以像这样创建会话:

    sess=tf.Session()
    ... your code ...
    ... your code ...
    sess.close()

    记得一定要关闭会话,或者你可以像这样使用:

    with tf.Session() as sess:
        sess.run(f)

    这样会话就自动关闭了,当然我们也推荐你这样使用。

    iii). Tensors in TensorFlow:

     TF使用张量表示数据就像在numpy中使用多维数组一样。

    a) 常量:

    常量不能改变,像这样定义:

    a=tf.constant(1.0)
    a
    <tf.Tensor'Const:0' shape=() dtype=float32>
     print(a)
    Tensor("Const:0", shape=(), dtype=float32)
    

      

    它不能像Python一样打印或者访问,除非你在会话中使用:

    with tf.Session() as sess:
        print(sess.run(a))
    

      

     这将打印1.0

    b) 变量:

    >>>b = tf.Variable(2.0,name="test_var")
    >>>b
    <tensorflow.python.ops.variables.Variable object at 0x7f37ebda1990>
    

      

    变量就像常量一样可以设置不同的值,但是变量通过init op来用作初始化,分别初始化所有变量会显得很麻烦,所以TensorFlow提供了init op

    0.11 版本
    >>>init_op = tf.initialize_all_variables()
     
    0.12 之后的版本
    >>>init_op = tf.global_variables_initializer()

     

     现在我们可以访问变量了
    with tf.Session() as sess:
        sess.run(init_op)
        print(sess.run(b))
    

      

    这将输出2

    现在我们来打印图的操作:

    graph = tf.get_default_graph()
    for op in graph.get_operations(): 
        print(op.name)
    

      

    输出:

    Const
    test_var/initial_value
    test_var
    test_var/Assign
    test_var/read
    init

     就像你看到的,因为我们使用了常量a,所以图中加入了Const.同样因为我们定义了变量b,所以test_var开头的操作被加入到图中,你可以使用名叫TensorBoard的工具来可视化一张图或者训练过程。

    c)占位符: 

     等待被初始化或者fed的向量,喂给占位符的叫做feed_dict

    >>>a = tf.placeholder("float")
    >>>b = tf.placeholder("float")
    >>>y = tf.multiply(a, b)
     // Earlier this used to be tf.mul which has changed with Tensorflow 1.0
     
    //Typically we load feed_dict from somewhere else, 
     //may be reading from a training data folder. etc
     //For simplicity, we have put values in feed_dict here
    >>>feed_dict ={a:2,b:3}
    >>>with tf.Session() as sess:
           print(sess.run(y,feed_dict))
    

      

    iv) TensorFlow设施

    TensorFlow内建的的能力非常强大,为你提供了运行在GPU或者CPU集群的选择

     reference:https://cv-tricks.com/artificial-intelligence/deep-learning/deep-learning-frameworks/tensorflow/tensorflow-tutorial/

     
  • 相关阅读:
    Python 标准库 BaseHTTPServer 中文翻译
    DBA Scripts
    cordova开发自己定义插件
    ASUS主板ALC887声卡,RTL81XX网卡,黑苹果驱动安装
    com.apple.installer.pagecontroller 错误 -1 pkg安装错误
    Apple 远程推送APNS 服务
    bitbucket git push 项目503失败
    在xcode5下利用Source Control 做 git 项目管理
    iOS设备定位服务开启判定
    has been modified since the precompiled header地图错误
  • 原文地址:https://www.cnblogs.com/lnas01/p/10405831.html
Copyright © 2020-2023  润新知