Python中TensorFlow神经网络的示例分析

小编给大家分享一下Python中TensorFlow神经网络的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

    一、基础理论

    1、TensorFlow

    tensor:张量(数据)

    flow:流动

    Tensor-Flow:数据流

    Python中TensorFlow神经网络的示例分析

    2、TensorFlow过程

    TensorFlow构成:图和会话

    1、构建图阶段

    构建阶段:定义了数据(张量tensor)与操作(节点operation),构成图(静态)

    张量:TensorFlow中的基本数据对象。

    节点:提供图中执行的操作。

    2、执行图阶段(会话)

    执行阶段:使用会话执行定义好的数据与操作。

    二、TensorFlow实例(执行加法)

    1、构造静态图

    1-1、创建数据(张量)
    #图(静态)
    a = tf.constant(2)    #数据1(张量)
    b = tf.constant(6)    #数据2(张量)
    1-2、创建操作(节点)
    c = a + b              #操作(节点)

    2、会话(执行)

    API:

    Python中TensorFlow神经网络的示例分析

    普通执行
    #会话(执行)
    with tf.Session() as sess:
        print(sess.run(a + b))

    Python中TensorFlow神经网络的示例分析

    fetches(多参数执行)
    #会话(执行)
    with tf.Session() as sess:
        print(sess.run([a,b,c]))

    Python中TensorFlow神经网络的示例分析

    feed_dict(参数补充)
    def Feed_Add():
        #创建静态图
        a = tf.placeholder(tf.float32)
        b = tf.placeholder(tf.float32)
        c = tf.add(a,b)
        
        #会话(执行)
        with tf.Session() as sess:
            print(sess.run(c, feed_dict={a:0.5, b:2.0}))

    Python中TensorFlow神经网络的示例分析

    总代码

    import tensorflow as tf
    def Add():
        #图(静态)
        a = tf.constant(2)    #数据1(张量)
        b = tf.constant(6)    #数据2(张量)
        c = a + b              #操作(节点) 
        #会话(执行)
        with tf.Session() as sess:
            print(sess.run([a,b,c])) 
    def Feed_Add():
        #创建静态图
        a = tf.placeholder(tf.float32)
        b = tf.placeholder(tf.float32)
        c = tf.add(a,b)    
        #会话(执行)
        with tf.Session() as sess:
            print(sess.run(c, feed_dict={a:0.5, b:2.0}))        
    Add()
    Feed_Add()

    以上是“Python中TensorFlow神经网络的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!