如何搭建自己的神經(jīng)網(wǎng)絡(luò)
例如一個(gè)神經(jīng)元對(duì)貓的眼睛敏感,那當(dāng)它看到貓的眼睛的時(shí)候,就被激勵(lì)了,相應(yīng)的參數(shù)就會(huì)被調(diào)優(yōu),它的貢獻(xiàn)就會(huì)越大。
下面是幾種常見(jiàn)的激活函數(shù):
x軸表示傳遞過(guò)來(lái)的值,y軸表示它傳遞出去的值:
激勵(lì)函數(shù)在預(yù)測(cè)層,判斷哪些值要被送到預(yù)測(cè)結(jié)果那里:
TensorFlow 常用的 acTIvaTIon funcTIon
(2)添加神經(jīng)層:輸入?yún)?shù)有 inputs, in_size, out_size, 和 acTIvation_function
分類問(wèn)題的 loss 函數(shù) cross_entropy :
overfitting:
下面第三個(gè)圖就是 overfitting,就是過(guò)度準(zhǔn)確地?cái)M合了歷史數(shù)據(jù),而對(duì)新數(shù)據(jù)預(yù)測(cè)時(shí)就會(huì)有很大誤差:
Tensorflow 有一個(gè)很好的工具, 叫做dropout, 只需要給予它一個(gè)不被 drop 掉的百分比,就能很好地降低 overfitting。
dropout 是指在深度學(xué)習(xí)網(wǎng)絡(luò)的訓(xùn)練過(guò)程中,按照一定的概率將一部分神經(jīng)網(wǎng)絡(luò)單元暫時(shí)從網(wǎng)絡(luò)中丟棄,相當(dāng)于從原始的網(wǎng)絡(luò)中找到一個(gè)更瘦的網(wǎng)絡(luò),這篇博客中講的非常詳細(xì)
5. 可視化 Tensorboard
Tensorflow 自帶 tensorboard ,可以自動(dòng)顯示我們所建造的神經(jīng)網(wǎng)絡(luò)流程圖:
就是用 with tf.name_scope 定義各個(gè)框架,注意看代碼注釋中的區(qū)別:
import tensorflow as tf
def add_layer(inputs, in_size, out_size, activation_function=None):
# add one more layer and return the output of this layer
# 區(qū)別:大框架,定義層 layer,里面有 小部件
with tf.name_scope(‘layer’):
# 區(qū)別:小部件
with tf.name_scope(‘weights’):
Weights = tf.Variable(tf.random_normal([in_size, out_size]), name=‘W’)
with tf.name_scope(‘biases’):
biases = tf.Variable(tf.zeros([1, out_size]) + 0.1, name=‘b’)
with tf.name_scope(‘Wx_plus_b’):
Wx_plus_b = tf.add(tf.matmul(inputs, Weights), biases)
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b, )
return outputs
# define placeholder for inputs to network
# 區(qū)別:大框架,里面有 inputs x,y
with tf.name_scope(‘inputs’):
xs = tf.placeholder(tf.float32, [None, 1], name=‘x_input’)
ys = tf.placeholder(tf.float32, [None, 1], name=‘y_input’)
# add hidden layer
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)
# add output layer
prediction = add_layer(l1, 10, 1, activation_function=None)
# the error between prediciton and real data
# 區(qū)別:定義框架 loss
with tf.name_scope(‘loss’):
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),
reduction_indices=[1]))
# 區(qū)別:定義框架 train
with tf.name_scope(‘train’):
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
sess = tf.Session()
# 區(qū)別:sess.graph 把所有框架加載到一個(gè)文件中放到文件夾“logs/”里
# 接著打開(kāi)terminal,進(jìn)入你存放的文件夾地址上一層,運(yùn)行命令 tensorboard --logdir=‘logs/’
# 會(huì)返回一個(gè)地址,然后用瀏覽器打開(kāi)這個(gè)地址,在 graph 標(biāo)簽欄下打開(kāi)
writer = tf.train.SummaryWriter(“logs/”, sess.graph)
# important step
sess.run(tf.initialize_all_variables())
運(yùn)行完上面代碼后,打開(kāi) terminal,進(jìn)入你存放的文件夾地址上一層,運(yùn)行命令 tensorboard --logdir=‘logs/’ 后會(huì)返回一個(gè)地址,然后用瀏覽器打開(kāi)這個(gè)地址,點(diǎn)擊 graph 標(biāo)簽欄下就可以看到流程圖了
6. 保存和加載訓(xùn)練好了一個(gè)神經(jīng)網(wǎng)絡(luò)后,可以保存起來(lái)下次使用時(shí)再次加載:import tensorflow as tf
import numpy as np
## Save to file
# remember to define the same dtype and shape when restore
W = tf.Variable([[1,2,3],[3,4,5]], dtype=tf.float32, name=‘weights’)
b = tf.Variable([[1,2,3]], dtype=tf.float32, name=‘biases’)
init= tf.initialize_all_variables()
saver = tf.train.Saver()
# 用 saver 將所有的 variable 保存到定義的路徑
with tf.Session() as sess:
sess.run(init)
save_path = saver.save(sess, “my_net/save_net.ckpt”)
print(“Save to path: ”, save_path)
################################################
# restore variables
# redefine the same shape and same type for your variables
W = tf.Variable(np.arange(6).reshape((2, 3)), dtype=tf.float32, name=“weights”)
b = tf.Variable(np.arange(3).reshape((1, 3)), dtype=tf.float32, name=“biases”)
# not need init step
saver = tf.train.Saver()
# 用 saver 從路徑中將 save_net.ckpt 保存的 W 和 b restore 進(jìn)來(lái)
with tf.Session() as sess:
saver.restore(sess, “my_net/save_net.ckpt”)
print(“weights:”, sess.run(W))
print(“biases:”, sess.run(b))