神经网络-前向传播
前向傳播神經(jīng)網(wǎng)絡(luò)搭建
1.tensorflow庫搭建神經(jīng)網(wǎng)絡(luò)
- 參數(shù):線上的權(quán)重W,用變量表示,隨機給初值。
? ? ? ? ?
- 相關(guān)介紹
- tf.truncated_normal():去掉過大偏離點的正太分布
- tf.random_normal()正太分布
- tf.random_uniform():平均分布
- tf.zeros:全零數(shù)組,tf.zeros([3,2],int32)
- tf.ones:全一數(shù)組,tf.ones([3,2],int32)
- tf.fill:全定值數(shù)組,tf.ones([3,2],6)
- tf.constant:直接給值,tf.constant([3,2,1])
- ? ?Variable(tf.random_normal([2,3],stddev=2,mean=0,seed=1)),參數(shù)介紹:?random_normal:生成正態(tài)分布隨機數(shù),2*3矩陣,標(biāo)準(zhǔn)差為2,均值為0,隨機種子1;隨機種子如果去掉,每次生成的隨機數(shù)將不一致
- 變量初始化,計算圖結(jié)點運算需要用會話(with結(jié)構(gòu))實現(xiàn);
2.神經(jīng)網(wǎng)絡(luò)的實現(xiàn)過程:
?
- 準(zhǔn)備數(shù)據(jù)集,提取特征,最為輸入喂給神經(jīng)網(wǎng)絡(luò)(Neural Network,NN)
- 搭建NN結(jié)構(gòu),從輸入到輸出(先搭建計算圖,在用會話執(zhí)行),NN 前向傳播算法----->計算輸出。
- 大量特征數(shù)據(jù)喂給NN,迭代優(yōu)化NN參數(shù),NN反向傳播算法----->優(yōu)化參數(shù)訓(xùn)練模型
- 使用訓(xùn)練好的模型預(yù)測和分類。
3.前向傳播----->搭建模型,實現(xiàn)推理(以全連接網(wǎng)絡(luò)為例)
- eg.生產(chǎn)一批零件將體積x1,重量x2為特征輸入NN,通過NN后輸出一個值.
- 運算結(jié)果:
- 中間結(jié)點值a:XW1
- 第二層權(quán)重W2:
- 第一層權(quán)重W1:
- 輸入體積X:
- 實現(xiàn) #實例:兩層簡單全連接神經(jīng)網(wǎng)絡(luò)
import tensorflow as tf#定義輸入和參數(shù):
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)#用會話計算結(jié)果
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))#實例:兩層簡單全連接神經(jīng)網(wǎng)絡(luò)
import tensorflow as tf#定義輸入和參數(shù):
#x=tf.placeholder(tf.float32,shape=(1,2))
x=tf.compat.v1.placeholder(tf.float32,shape=(1,2))#通過placeholder實行定義輸入(sess.run喂入一組數(shù)據(jù))
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)#用會話計算結(jié)果
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的一組特征喂入神經(jīng)網(wǎng)絡(luò)。#實例:兩層簡單全連接神經(jīng)網(wǎng)絡(luò)
import tensorflow as tf#定義輸入和參數(shù):
#x=tf.placeholder(tf.float32,shape=(1,2))
x=tf.compat.v1.placeholder(tf.float32,shape=(None,2))#通過placeholder實行定義輸入(sess.run喂入多組數(shù)據(jù)),不知道維度可以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)#用會話計算結(jié)果
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的一組特征喂入神經(jīng)網(wǎng)絡(luò)。print("w1\n",sess.run(w1))print("w2\n",sess.run(w2))
?運算結(jié)果:
-
?
總結(jié)
- 上一篇: 定长掩码地址划分与VLSM子网划分
- 下一篇: 线程同步-事件内核对象