参数:即权重,用变量表示,随机给初值
w=tf.Variable(tf.random_normal([2,3],stddev=2,mean=0,seed=1))
random_normal表示正态分布,[2,3]表示产生2*3矩阵,stddev表示标准差为2,mean表示均值为0,seed表示随机种子
tf.truncated_normal()表示去掉过大偏离点的正态分布
tf.random_uniform()表示平均分布
tf.zeros ——全0数组:tf.zeros([3,2],int32)生成[[0,0],[0,0],[0,0]]
tf.ones——全1数组
tf.fill——全定值数组
tf.constant——直接给值
神经网络实现过程
1.准备数据集,提取特征,作为输入喂给神经网络
2.搭建NN结构,从输入到输出(前向传播算法——>计算输出)
3.大量特征数据喂给NN,迭代优化NN参数(反向传播算法——>优化参数训练模型)
4.使用训练好的模型预测和分类
前向传播
搭建模型,实现推理
以下为一个例子:搭建神经网络
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 前向传播 is :\n",sess.run(y))
结果如下:
给神经网络喂一组数据:
喂n组数据: