首页 > 其他 > 详细

TensorFlow笔记-04-神经网络的实现过程,前向传播

时间:2018-09-08 14:33:10      阅读:212      评论:0      收藏:0      [点我收藏+]

TensorFlow笔记-04-神经网络的实现过程

  • 基于TensorFlow的NN:用张量表示数据,用计算图搭建神经网络,用会话执行计算图,优化线上的权重(参数),得到模型
  • 张量(tensor):多维数组(列表)
  • 阶:张量的维数
  • 计算图(Graph):搭建神经网络的计算过程,只搭建,不运算
  • 会话(Session):执行计算图中的结点运算
  • 参数:即计算图中的权重,用变量表示,随机给初值

  • 其中Variable有4种:zeros,ones,fill,constant

    tf.zeros····全0数组··············tf.zeros([3,2],int32) 生成[[0,0],[0,0],[0,0]]
    tf.ones·····全1数组··············tf.ones([3,2],int32) 生成[[1,1],[1,1],[1,1]]
    tf.fill·······全定值数组··············tf.fill([3,2],6) 生成[[6,6],[6,6],[6,6]]
    tf.constant··直接给值··············tf.zeros([3,2,1]) 生成[3,2,1]

神经网络的实现过程

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

前向传播

  • 搭建模型计算过程,让模型具有推理能力(以全连接网络为例)
  • eg.生产一批零件将体积想x1和重量x2为特征的输入NN,通过NN后输出一个数值

  • 使用TensorFlow表示上例

  • 计算结果要用到会话

  • 代码前向传播文件:https://xpwi.github.io/py/TensorFlow/tf05forward.py
# coding:utf-8
# 前向传播
# 两层简单神经网络(全连接)
import tensorflow as tf

# 定义输入和参数
x = tf.constant([[0.7, 0.5]])
w1 = tf.Variable(tf.random_normal([2, 3], stddev=1, seed=1))
w2 = tf.Variable(tf.random_normal([3, 1], stddev=1, seed=1))

# 定义前向传播的过程
a = tf.matmul(x, w1)
y = tf.matmul(a, w2)

# 用会话计算结果
with tf.Session() as sess:
    init_op = tf.global_variables_initializer()
    sess.run(init_op)
    print("y in tf05前向传播 is:\n", sess.run(y))
   
# 结果:
# [[3.0904665]] 

运行结果:

# coding:utf-8
# 前向传播
# 两层简单神经网络(全连接)
import tensorflow as tf

# 定义输入和参数
# 用placeholder实现输入自定义(sess.run中喂1组数据)
x = tf.placeholder(tf.float32, shape=(1, 2))
w1 = tf.Variable(tf.random_normal([2, 3], stddev=1, seed=1))
w2 = tf.Variable(tf.random_normal([3, 1], stddev=1, seed=1))

# 定义前向传播的过程
# 矩阵相乘,不运算
a = tf.matmul(x, w1)
y = tf.matmul(a, w2)

# 用会话计算结果
with tf.Session() as sess:
    init_op = tf.global_variables_initializer()
    sess.run(init_op)
    # 字典,喂入一组特征
    print("y in tf05forward2 is:\n", sess.run(y, feed_dict={x:[[0.7,0.5]]}))

# 结果:
# [[3.0904665]]

一次向神经网络喂入n组特征

# coding:utf-8
# 前向传播
# 两层简单神经网络(全连接)
# 向神经网络喂入n组特征
import tensorflow as tf

# 定义输入和参数
# 用placeholder实现输入自定义(sess.run中喂多组数据)None表示未知
x = tf.placeholder(tf.float32, shape=(None, 2))
w1 = tf.Variable(tf.random_normal([2, 3], stddev=1, seed=1))
w2 = tf.Variable(tf.random_normal([3, 1], stddev=1, seed=1))

# 定义前向传播的过程
# 矩阵相乘,不运算
a = tf.matmul(x, w1)
y = tf.matmul(a, w2)

# 用会话计算结果
with tf.Session() as sess:
    init_op = tf.global_variables_initializer()
    sess.run(init_op)
    # 字典,喂入多组特征
    print("y in tf05forward2 is:\n", sess.run(y, feed_dict={x:[[0.7,0.5],[0.2,0.3],[0.3,0.4],[0.4,0.5]]}))
    print("w1:", sess.run(w1))
    print("w2:", sess.run(w2))

运行结果

技术分享图片
前向传播就到这里了

更多文章:Tensorflow 笔记


  • 本笔记不允许任何个人和组织转载

TensorFlow笔记-04-神经网络的实现过程,前向传播

原文:https://www.cnblogs.com/xpwi/p/9609119.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!