Tensorflow框架-前向传播

                                                 前向传播

一、参数

    线上的权重W,用变量表示,随机给初值。

    1、w = tf.Variable(tf.random_normal([2,3], stddev=2, mean=0, seed=1))

  •             tf.random_normal():正太分布
  •             [2,3]:产生2*3矩阵
  •             stddev=2:标准差为2
  •             mean=0:均值为0
  •             seed=1:随机种子       
w = tf.Variable(tf.random_normal([2,3], stddev=2, mean=0, seed=1, dtype=tf.float32, name=None))
print(w)
with tf.Session() as sess:
    # tf.global_variables_initializer()添加节点用于初始化所有的变量(GraphKeys.VARIABLES)。
    # 返回一个初始化所有全局变量的操作(Op)。在你构建完整个模型并在会话中加载模型后,运行这个节点。
    # 函数中调用了 variable_initializer() 和 global_variables()
    sess.run(tf.global_variables_initializer())
    ov = sess.run(w)
    plt.plot(ov)
    plt.show()

        运行结果:
            <tf.Variable 'Variable:0' shape=(2, 3) dtype=float32_ref>
            [[-1.6226364   2.9691975   0.13065875]
             [-4.8854084   0.1984968   1.1824486 ]] 

            

    2、 tf.truncated_normal():

        (1) 语法:

            truncated_normal( shape,  mean=0.0,  stddev=1.0,  dtype=tf.float32,  seed=None,  name=None)

        (2) 作用:

            产生截断正态分布随机数,取值范围为 [ mean - 2 * stddev, mean + 2 * stddev ]

            如果随机生成的数据,偏离平均值超过2个标准差,这个数据将重新生成。

        (3) 参数说明:

参数是否必选类型说明
shape1 维整形张量或 array输出张量的维度
mean0 维张量或数值均值
stddev0 维张量或数值标准差
dtypedtype输出类型
seed数值随机种子,若 seed 赋值,每次产生相同随机数
namestring运算名称

 

        (4) 示例:        

import tensorflow as tf
import matplotlib.pyplot as plt

w = tf.Variable(tf.truncated_normal([2,3], stddev=2, mean=0, seed=1, dtype=tf.float32, name=None))
print(w)

with tf.Session() as sess:
    # tf.global_variables_initializer()添加节点用于初始化所有的变量(GraphKeys.VARIABLES)。
    # 返回一个初始化所有全局变量的操作(Op)。在你构建完整个模型并在会话中加载模型后,运行这个节点。
    # 函数中调用了 variable_initializer() 和 global_variables()
    sess.run(tf.global_variables_initializer())
    ov = sess.run(w)
    print(ov)
    plt.plot(ov)
    plt.show()

        运行结果:

            <tf.Variable 'Variable:0' shape=(2, 3) dtype=float32_ref>

            [[-1.6226364   2.9691975   0.13065875]
             [ 0.1984968   1.2793941   3.2217424 ]]

            

    3、tf.random_uniform():平均分布

        (1) 语法:

            random_uniform(shape, minval=0, maxval=None, dtype=float32, seed=None, name=None)

        (2) 作用:

            从均匀分布中输出随机值。

            生成的值在该 [minval, maxval] 范围内遵循均匀分布,下限 minval 包含在范围内,而上限 maxval 被排除在外。

            对于浮点数,默认范围是 [0, 1]。对于整数,maxval 必须明确的指定。

            在整数情况下,随机整数稍有偏差,除非 maxval - minval 是 2 的精确幂。对于 maxval - minval 的值,偏差很小,明显小于输出(2**32 或者 2**64)的范围。

        (3) 参数说明:

        (4) 示例:          

import tensorflow as tf
import matplotlib.pyplot as plt

w = tf.Variable(tf.random_uniform([3, 3], minval=0, maxval=10, dtype=tf.float32))
print(w)
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    ov = sess.run(w)
    print(ov)
    plt.plot(ov)
    plt.show()

            运行结果:

                <tf.Variable 'Variable:0' shape=(3, 3) dtype=float32_ref>

                [[5.6415024  4.1729546  4.3173895 ]
                 [1.0088015  0.05193353 5.0753546 ]
                 [0.7271564  3.0730283  5.066066  ]]

                

    4、tf.zeros:全0数组        

import tensorflow as tf
import matplotlib.pyplot as plt

w =tf.zeros([3, 3], tf.int32)
print(w)
with tf.Session() as sess:
    ov = sess.run(w)
    print(ov)
    plt.plot(ov)
    plt.show()

        运行结果:
            Tensor("zeros:0", shape=(3, 3), dtype=int32)

            [[0 0 0]
             [0 0 0]
             [0 0 0]]

            

    5、tf.ones:全1数组        

import tensorflow as tf
import matplotlib.pyplot as plt

w = tf.ones([3, 3], tf.int32)
print(w)
with tf.Session() as sess:
    ov = sess.run(w)
    print(ov)
    plt.plot(ov)
    plt.show()

        运行结果:

            Tensor("ones:0", shape=(3, 3), dtype=int32)
            [[1 1 1]
             [1 1 1]
             [1 1 1]]

            

    6、tf.fill:全定值数组  

import tensorflow as tf
import matplotlib.pyplot as plt

w = tf.fill([3, 3], 6)
print(w)
with tf.Session() as sess:
    ov = sess.run(w)
    print(ov)
    plt.plot(ov)
    plt.show()

        运行结果:

            Tensor("Fill:0", shape=(3, 3), dtype=int32)
            [[6 6 6]
             [6 6 6]
             [6 6 6]]

            

    7、tf.constant:直接给值        

import tensorflow as tf
import matplotlib.pyplot as plt

w = tf.constant([3, 2, 1])
print(w)
with tf.Session() as sess:
    ov = sess.run(w)
    print(ov)
    plt.plot(ov)
    plt.show()

    运行结果:

        Tensor("Const:0", shape=(3,), dtype=int32)
        [3 2 1]

        

二、神经网络实现过程

    1、准备数据集,提取特征,作为输入喂给神经网络(Neural Network, NN)

    2、搭建 NN 结构,从输入到输出(先搭建计算图,再用会话执行)

        (NN 前向传播算法 ------> 计算输出)

    3、大量特征数据喂给 NN,迭代优化参 NN 参数

        (NN 反向传播算法 ------> 优化参数训练模型)

    4、使用训练好的模型预测和分类

三、前向传播

    1、定义:搭建模型,实现推理(以全连接网络为例)

    2、实例:

        eg、生产一批零件将体积 x_1 和重量 x_2 为特征输入NN,通过NN后输出一个数值。

            

        (1) 推导

            a、x是输入为1x2矩阵;{w_{q,h}}^{(c)} 为待优化的参数(q:前节点编号;h:后节点编号;c:层数)。

            b、w^{(1)}=\begin{bmatrix} {w_{1.1}}^{(1)} & {w_{1.2}}^{(1)} & {w_{1.3}}^{(1)}\\ {w_{2.1}}^{(1)} & {w_{2.2}}^{(1)} & {w_{2.3}}^{(1)} \end{bmatrix} 为2x3矩阵

            c、a^{(1)}=\begin{bmatrix} a_{11} & a_{12} & a_{13} \end{bmatrix} 为1x3矩阵

            d、w^{(2)}=\begin{bmatrix} {w_{1.1}}^{(2)} \\ {w_{2.1}}^{(2)} \\ {w_{3.1}}^{(2)} \end{bmatrix} 为3x1矩阵 。\Rightarrow\Rightarrow\Rightarrowy=a^{(1)}w^{(2)}

                a = tf.matmul(x, w1)

                y = tf.matmul(a, w2)

            e、变量初始化、计算图节点运算 都要用会话(with结构)实现:

                with tf.Session() as sess:

                    sess.run()

            f、变量初始化:在sess.run函数中使用 tf.global_variables_initializer()

                init_op = tf.global_variables_initializer()

                sess.run(init_op)

            g、计算图节点运算:在sess.run函数中写入待运算的节点

                sess.run(y)

            h、用 tf.placeholder 占位,在 sess.run 函数中用 feed_dict 喂数据:

                喂一组数据

                x = tf.placeholder(tf.float32, shape=(1, 2))

                sess.run(y, feed_dict={x: [[0.5, 0.6]]})

                喂多组数据

                x = tf.placeholder(tf.float32, shape=(None, 2))

                sess.run(y, feed_dict={x: [[0.1, 0.2], [0.2, 0.3], [0.3, 0.4], [0.4, 0.5],]})

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值