版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36078885/article/details/79778293
前向传播
目的:搭建模型,实现推理
一些重要的概念
- 参数
上图中的w(权重)即为参数,一般随机赋给初值 - 计算
对于这么一个神经元,y的值为
事实上用矩阵表达更方便也跟通常一点
tensorflow中提供的一些重要函数
- 生成变量
w = tf.Varable(tf.random_normal([2,3],stddev = 2,mean=0,seed=1))
# 生成正态分布 2x3的矩阵 标准差等于2 均值为0 种子为1
# 与之类似的还有
tf.zeros([2,3],int32) #生成全为0的矩阵
tf.ones([2,3],int32) #生成全为1的矩阵
tf.fill([2,3],6) #生成为定值的矩阵
tf.constant([3,2]) #生成给定的常数矩阵
例子
生产一批零件以体积和重量为特征输入NN,通过NN后输出一个值
所以y为
用tensorflow表达
1 #coding:utf-8
2 import tensorflow as tf
3
4 #定义输入和参数
5 #一行两列的输入代表重量和体积
6 x = tf.constant([[0.7,0.5]])
7 w1 = tf.Variable(tf.random_normal([2,3],stddev=1,seed=1))
8 w2 = tf.Variable(tf.random_normal([3,1],stddev=1,seed=1))
9
10 #定义向前传播过程
11 a = tf.matmul(x,w1)
12 y = tf.matmul(a,w2)
13
14 #用会话计算结果
15
16 with tf.Session() as sess:
17 init_op = tf.global_variables_initializer()
18 sess.run(init_op)
19 print('y in tensorflow2.py is:\n',sess.run(y))