神经网络-前向传播

前向传播神经网络搭建

1.tensorflow库搭建神经网络

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

         

  • 相关介绍
    • tf.truncated_normal():去掉过大偏离点的正太分布
    • tf.random_normal()正太分布
    • tf.random_uniform():平均分布
    • tf.zeros:全零数组,tf.zeros([3,2],int32)
    • tf.ones:全一数组,tf.ones([3,2],int32)
    • tf.fill:全定值数组,tf.ones([3,2],6)
    • tf.constant:直接给值,tf.constant([3,2,1])
  •    Variable(tf.random_normal([2,3],stddev=2,mean=0,seed=1)),参数介绍: random_normal:生成正态分布随机数,2*3矩阵,标准差为2,均值为0,随机种子1;随机种子如果去掉,每次生成的随机数将不一致
  • 变量初始化,计算图结点运算需要用会话(with结构)实现;

2.神经网络的实现过程:

  • 准备数据集,提取特征,最为输入喂给神经网络(Neural Network,NN)
  • 搭建NN结构,从输入到输出(先搭建计算图,在用会话执行),NN 前向传播算法----->计算输出。
  • 大量特征数据喂给NN,迭代优化NN参数,NN反向传播算法----->优化参数训练模型
  • 使用训练好的模型预测和分类。

3.前向传播----->搭建模型,实现推理(以全连接网络为例)

  • eg.生产一批零件将体积x1,重量x2为特征输入NN,通过NN后输出一个值.
    • 运算结果:y=XW^1W^2
    • 中间结点值a:XW1
    • 第二层权重W2:\begin{bmatrix} w_{11}^2\\ w_{12}^2\\ w_{13}^2 \end{bmatrix}
    • 第一层权重W1:\begin{bmatrix} w_{11}^1& w_{12}^1&w_{13}^1\\ w_{21}^1& w_{22}^1&w_{23}^1\\ \end{bmatrix}
    • 输入体积X:\begin{bmatrix} x_1 &x_2\\ \end{bmatrix}
  • 实现
    #实例:两层简单全连接神经网络
    import tensorflow as tf
    
    #定义输入和参数:
    x=tf.constant([[0.7,0.8]])#一行两列的张量存储体积和重量
    w1=tf.Variable(tf.random.normal([2,3],stddev=1,seed=1))
    w2=tf.Variable(tf.random.normal([3,1],stddev=1,seed=1))
    print(w1)
    print(w2)
    #定义前向传播
    a=tf.matmul(x,w1)
    y=tf.matmul(a,w2)
    
    #用会话计算结果
    with tf.compat.v1.Session() as sess:#tf.Session()
        #因版本不同,tf.Session使用tf.compat.v1.Session()代替
        init_op=tf.compat.v1.global_variables_initializer()
        #tf.global_variables_initializer()用tf.compat.v1.global_variables_initializer()
        sess.run(init_op)
        print("result is \n",sess.run(y))
        
    
    
    
    #实例:两层简单全连接神经网络
    import tensorflow as tf
    
    #定义输入和参数:
    #x=tf.placeholder(tf.float32,shape=(1,2))
    x=tf.compat.v1.placeholder(tf.float32,shape=(1,2))#通过placeholder实行定义输入(sess.run喂入一组数据)
    w1=tf.Variable(tf.random.normal([2,3],stddev=1,seed=1))
    w2=tf.Variable(tf.random.normal([3,1],stddev=1,seed=1))
    print(w1)
    print(w2)
    #定义前向传播
    a=tf.matmul(x,w1)
    y=tf.matmul(a,w2)
    
    #用会话计算结果
    with tf.compat.v1.Session() as sess:#tf.Session()
        #init_op=tf.global_variables_initializer()
        init_op=tf.compat.v1.global_variables_initializer()#tf.global_Variables_initializer()
        sess.run(init_op)
        print("result is \n",sess.run(y,feed_dict={x:[[0.7,0.5]]}))#x的一组特征喂入神经网络。
    
    
    
    
    #实例:两层简单全连接神经网络
    import tensorflow as tf
    
    #定义输入和参数:
    #x=tf.placeholder(tf.float32,shape=(1,2))
    x=tf.compat.v1.placeholder(tf.float32,shape=(None,2))#通过placeholder实行定义输入(sess.run喂入多组数据),不知道维度可以None
    w1=tf.Variable(tf.random.normal([2,3],stddev=1,seed=1))
    w2=tf.Variable(tf.random.normal([3,1],stddev=1,seed=1))
    print(w1)
    print(w2)
    #定义前向传播
    a=tf.matmul(x,w1)
    y=tf.matmul(a,w2)
    
    #用会话计算结果
    with tf.compat.v1.Session() as sess:#tf.Session()
        #init_op=tf.global_variables_initializer()
        init_op=tf.compat.v1.global_variables_initializer()#tf.global_Variables_initializer()
        sess.run(init_op)
        print("result is \n",sess.run(y,feed_dict={x:[[0.7,0.5],[0.2,0.3],[0.3,0.4],[0.4,0.5]]}))#x的一组特征喂入神经网络。
    
        print("w1\n",sess.run(w1))
        print("w2\n",sess.run(w2))   
    
    

     运算结果:

发布了89 篇原创文章 · 获赞 8 · 访问量 8896

猜你喜欢

转载自blog.csdn.net/TxyITxs/article/details/94386810
今日推荐