一、神经网络的参数
神经网络的参数即神经网络线上的权重W,用变量表示,随机给初值。
W = tf.Variable(tf.random_normal([2,3],stddev=2,mean=0,seed=1))
将生成方式写在Variable方法中,其中:
tf.random_normal():生成正太分布的随机数
[2,3]:产生2行3列矩阵
stddev=2:标准差为2
mean=0:均值为0。随机种子如果去掉,每次生成的随机数会不一致
seed=1:随机种子为1
tf.random_normal()函数可以用tf.truncated_normal()函数替换,表示去掉过大偏离点的正太分布,如果随机生成的数据偏离平均值超过两个标准差,这个数据将重新生成。用平均分布函数tf.random_uniform()替换。
除了生成随机数,还可以生成常量:
tf.zeros生成全0数组:tf.zeros([3,2],int(32))生成[[0,0],[0,0],[0,0]]
tf.ones生成全1数组:tf.ones([3,2],iint(32))生成[[1,1],[1,1],[1,1]]
tf.fill生成全定值数组:tf.fill([3,2],6)生成[[6,6],[6,6],[6,6]]
tf.constant直接给出数组的值,tf.contant([3,2,1])生成[3,2,1]
二、神经网络的实现过程
1、准备数据集,提取特征,作为输入,喂给神经网络(NN)
2、搭建NN结构,从输入到输出(先搭建计算图,再用会话执行)(NN前向传播算法,计算输出)
3、大量数据喂给NN,迭代优化NN参数(NN反向传播算法,优化参数训练模型)
4、使训练好的模型预测分类
三、前向传播
前向传播:搭建模型,实现推理(以全连接网络为例)
如:生成一批零件,将体积x1和重量x2为特征输入NN,通过NN后输出一个值。
1、Tensorflow描述前向传播
X是输入为1x2的矩阵,W前节点编号,后节点编号(层数)为待优化的参数。
W(1)为2x3的矩阵:
a(1) = [a11,a12,a13]为1x3的矩阵=XW(1)
W(2)是3x1的矩阵:
y = a(1)W(2)
则:
a = tf.matmul(X,W1)
y = tf.matmul(a,W2)
2、两层简单神经网络的计算
向神经网络喂入一组输入特征:
# coding:utf-8
# 两层简单神经网络(1)
import tensorflow as tf
# 定义输入和参数
x = tf.constant([[0.5, 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))
# 定义前向传播的过程
a = tf.matmul(x, w1)
y = tf.matmul(a, w2)
# 用会话计算结果
with tf.Session() as sess:
init_op = tf.global_variables_initializer() # 初始化所有变量的函数简写为初始化节点,用init_op表示
sess.run(init_op)
print(sess.run(y)) # [[3.1674924]]
下面使用placeholder方法实现输入定义,可以在with结构中喂入多组输入特征,方便批量处理:
# coding:utf-8
# 两层简单神经网络
import tensorflow as tf
# 定义输入和参数
# 使用placeholder方法实现输入定义,可以在with结构中喂入多组输入特征,方便批量处理
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(sess.run(y, feed_dict={x: [[0.5, 0.8]]})) # [[3.1674924]]
向神经网络喂入多组特征:
# coding:utf-8
# 两层简单神经网络
import tensorflow as tf
# 定义输入和参数
# 使用placeholder方法实现输入定义,可以在with结构中喂入多组输入特征,方便批量处理.session.run喂入多组数据
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(sess.run(y, feed_dict={x: [[0.5, 0.8], [0.1, 0.2], [0.3, 0.4], [0.3, 0.6], [0.2, 0.9]]})) # [[3.1674924]]
print()
print("w1:", sess.run(w1))
print()
print('w2:', sess.run(w2))
'''
[[3.1674924 ]
[0.72020966]
[1.7270732 ]
[2.160629 ]
[2.5243084 ]]
w1: [[-0.8113182 1.4845988 0.06532937]
[-2.4427042 0.0992484 0.5912243 ]]
w2: [[-0.8113182 ]
[ 1.4845988 ]
[ 0.06532937]]
'''