多層感知機(jī)
算法簡介:
多層感知機(jī)是基于反向人工神經(jīng)網(wǎng)絡(luò)(feedforwardartificial neural network)疼约。多層感知機(jī)含有多層節(jié)點,每層節(jié)點與網(wǎng)絡(luò)的下一層節(jié)點完全連接蝙泼。輸入層的節(jié)點代表輸入數(shù)據(jù)程剥,其他層的節(jié)點通過將輸入數(shù)據(jù)與層上節(jié)點的權(quán)重w以及偏差b線性組合且應(yīng)用一個激活函數(shù),得到該層輸出汤踏。多層感知機(jī)通過方向傳播來學(xué)習(xí)模型织鲸,其中我們使用邏輯損失函數(shù)以及L-BFGS。K+1層多層感知機(jī)分類器可以寫成矩陣形式如下:
Sigmoid函數(shù)
Sigmoid函數(shù)在ReLU函數(shù)未出現(xiàn)前一直作為神經(jīng)網(wǎng)絡(luò)的激活函數(shù)溪胶,Sigmoid具有限制性搂擦,輸出數(shù)值在0~1之間,最符合概率的定義哗脖。非線性的Sigmoid函數(shù)在信號的特征空間映射上瀑踢,對中央?yún)^(qū)信號的增益較大,對兩側(cè)區(qū)的信號增益較小才避。
-
Sigmoid函數(shù)方程:
ReLU函數(shù)
相對于Sigmoid函數(shù)優(yōu)勢有:
- 單側(cè)抑制
- 相對寬闊的興奮邊界
- 稀疏的激活性
多層感知機(jī)示例
#coding:utf-8
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
#數(shù)據(jù)集加載
mnist = input_data.read_data_sets('MNIST_data/',one_hot = True)
sess = tf.InteractiveSession()
# 定義算法公式
in_units = 784
h1_units = 300
W1 = tf.Variable(tf.truncated_normal([in_units,h1_units],stddev = 0.1))
b1 = tf.Variable(tf.zeros([h1_units]))
W2 = tf.Variable(tf.zeros([h1_units,10]))
b2 = tf.Variable(tf.zeros([10]))
x = tf.placeholder(tf.float32,[None,in_units])
keep_prob = tf.placeholder(tf.float32)
hidden1 = tf.nn.relu(tf.matmul(x,W1)+b1)
hidden1_drop = tf.nn.dropout(hidden1,keep_prob)
y = tf.nn.softmax(tf.matmul(hidden1_drop,W2)+b2)
#定義損失函數(shù)和選擇優(yōu)化器來優(yōu)化loss
y_ = tf.placeholder(tf.float32,[None,10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y),reduction_indices = [1]))
train_step = tf.train.AdagradOptimizer(0.3).minimize(cross_entropy)
#訓(xùn)練,加入keep_prop作為計算圖輸入,并在訓(xùn)練時設(shè)為0.75,保留75%的節(jié)點
tf.global_variables_initializer().run()
for i in range(3000):
batch_xs,batch_ys = mnist.train.next_batch(100)
train_step.run({x:batch_xs,y_:batch_ys,keep_prob:0.75})
#對模型進(jìn)行評測
correnct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correnct_prediction,tf.float32))
print (accuracy.eval({x:mnist.test.images,y_:mnist.test.labels,keep_prob:1.0}))