類別 | 解釋 | 樣例 |
---|---|---|
tf.truncated_normal() | 去掉過大偏離點(diǎn)的正態(tài)分布 | |
tf.random_uniform() | 平均分布 | |
tf.zeros | 全0數(shù)組 | tf.zeros([3,2],int32)生成[[0,0],[0,0],[0,0]] |
tf.ones | 全1數(shù)組 | tf.ones([3,2],int32)生成[[1,1],[1,1],[1,1]] |
tf.fill | 全定值數(shù)組 | tf.fill([3,2],6)生成[[6,6],[6,6],[6,6]] |
tf.constant | 直接給值 | tf.constant([3,2,1])生成[3,2,1] |
神經(jīng)網(wǎng)絡(luò)實(shí)現(xiàn)過程:
1.準(zhǔn)備數(shù)據(jù)集,提取特征,作為輸入,傳給神經(jīng)網(wǎng)絡(luò)
2.搭建NN結(jié)構(gòu),從輸入到輸出(先搭建計(jì)算圖,再用會(huì)話執(zhí)行)(NN前向傳播算法——計(jì)算輸出)
3.大量特征數(shù)據(jù)傳給NN,迭代優(yōu)化NN參數(shù)(NN反向傳播算法——優(yōu)化參數(shù)訓(xùn)練模型)
4.使用訓(xùn)練好的模型預(yù)測(cè)和分類
前向傳播 搭建模型,實(shí)現(xiàn)推理
變量初始化、計(jì)算圖節(jié)點(diǎn)運(yùn)算都要用會(huì)話(with結(jié)構(gòu))實(shí)現(xiàn)
with tf.Session() as sess:
sess.run()
變量初始化:在sess.run函數(shù)中用tf.global_variables_initializer()
init_op = tf.global_variables_initializer()
sess.run(init_op)
計(jì)算圖節(jié)點(diǎn)運(yùn)算:在sess.run函數(shù)中寫入待運(yùn)算的節(jié)點(diǎn)
\sess.run()
用tf.placeholder占位,在sess.run函數(shù)中用feed_dict喂數(shù)據(jù)
喂一組數(shù)據(jù)
x = tf.placeholder(tf.float32,shape=(1,2))
sess.run(y,feed_dict={x:[[0.5,0.6]]})
喂多組數(shù)據(jù)
x = tf.placeholder(tf.float32,shape=(1,2))
sess.run(y,feed_dict = {x:[[0.5,0.6]]})
#兩層簡(jiǎn)單神經(jīng)網(wǎng)絡(luò)(全連接)
#定義輸入和參數(shù)
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)
#用會(huì)話計(jì)算結(jié)果
with tf.Session() as sess:
init_op = tf.global_variables_initializer()
sess.run(init_op)
print(sess.run(y))
# 用placeholder實(shí)現(xiàn)輸入定義(sess.run中喂一組數(shù)據(jù))
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)
#用會(huì)話計(jì)算結(jié)果
with tf.Session() as sess:
init_op = tf.global_variables_initializer()
sess.run(init_op)
print(sess.run(y, feed_dict={x:[[0.7,0.5]]}))
# 用placeholder實(shí)現(xiàn)輸入定義(sess.run中喂多組數(shù)據(jù))
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)
#用會(huì)話計(jì)算結(jié)果
with tf.Session() as sess:
init_op = tf.global_variables_initializer()
sess.run(init_op)
print(sess.run(y, feed_dict={x:[[0.7,0.5],[0.2,0.3],[0.3,0.4],[0.4,0.5]]}))
print(sess.run(w1))
print(sess.run(w2))