Perceptron感知器

Introduction

Perceptron is a single layer neural network and a multi-layer perceptron is called Neural Networks. It is a supervised learning of binary linear classifier which means that the traing dataset must be linear separable. 感知器是神經(jīng)網(wǎng)絡(luò)最簡單的一種形式.同時(shí)它是一個(gè)簡易的二分線性分類器,意味被訓(xùn)練數(shù)據(jù)集必須線性可分姑食。

The perceptron consists of 4 parts .

  • Input : n-dimensional feature vector
  • Output : {-1,1}
  • Weights, Bias, NetSum
  • Activation Function

How does it work?

Definitions: sign(z) = 1 if z ≥ 0, ?1 otherwise.
Inputs: Training examples (Xt,Yt) for t ∈ {1 . . . n} where x ∈ R d is an input, and yt ∈ {?1, +1} is a label.
Initialization: w = [0,0,...0,], b = 0, rate = 0.0001
Algorithm:

while there exist y*sign(w·x+b) <= 0 do
    x,y = random pick from misclassification set M
    w <- w + rate*y*x
    b <- b + rate*y 
end

Loss Function

損失函數(shù)的自然選擇是誤分類點(diǎn)的總數(shù),顯然這個(gè)函數(shù)不是連續(xù)可導(dǎo)的序芦,不易優(yōu)化圃郊。
所以,一般感知機(jī)采用是誤分類點(diǎn)到超平面S的總距離


Convergence Proof for the Perceptron Algorithm

Assumption:

  • w^* is unit normal vector of the perfect separable line.

  • R is the maximum Euclidean length of x_{t} \Leftrightarrow R = max||x_{t}||

  • \gamma the minimum distatnce of any examples to the plane w^* \cdot x = 0
    \gamma = min \frac {|w^* \cdot x_{t}|}{||x_{t}||}

Claim 1: wk+1·w ≥ wk·w + γ

That is, every time we make a mistake, the dot-product of our weight vector with the target increases by at least γ.

Since w* is the target perfect function hence every x_{n} correctly away from the line, we can conclude that:
y_{t}w^*x_{t} \geq min(y_{t}w^*x_{t}) \geq γ > 0

Proof:
w^* \cdot w_{k+1} = w^* \cdot (w_{k}+y_{t}x_{t}) w^* \cdot w_{k+1} = w^* \cdot w_{k} + y_{t}w^*x_{t} w^* \cdot w_{k+1} \geq w^* \cdot w_{k} + min(y_{t}w^*x_{t}) w^* \cdot w_{k+1} \geq w^* \cdot w_{k} + \gamma

Claim 1 implies that after M mistakes, w_{M} · w^* ≥ \gamma M.

Claim 2: ||wk+1||2 ≤ ||wk||2 + R2

That is, every time we make a mistake, the length squared of our weight vector increases by at most R2.
Note that, we only update w_{t} changed only when mistake, so we can have:
sign(w_{k} \cdot x_{t}) \neq y_{t} \Leftrightarrow y_{t}w_{k}\cdot x_{t} \leq 0
Proof:
||w_{k+1}||^2 = ||w_{k} + y_{t}x_{t}||^2 ||w_{k+1}||^2 = ||w_{k}||^2 + 2y_{t}w_{k}x_{t} + ||y_{t}x_{t}||^2 Mistakes limit ||w_{k}||^2 growth, even when updating with the longest x_{t} ||w_{k+1}||^2 \leq ||w_{k}||^2 + 0 + ||y_{t}x_{t}||^2 ||w_{k+1}||^2 \leq ||w_{k}||^2 + max||y_{t}x_{t}||^2 Because y_{t} ∈ \{1,-1\}, so ||w_{k+1}||^2 \leq ||w_{k}||^2 + R^2

On the other hand, Claim 2 implies that after M mistakes, ||w_{M}||^2 ≤ MR^2.

Convergence Proof

Combing the bounds together, it gives
||w_{M} \cdot w^*||^2 \geq \gamma^2M^2 ||w_{M}||^2 ≤ MR^2
Since w^* is a unit-length vector, ||w^*||^2=1
\gamma^2M^2 \leq ||w_{M}||^2 ≤ MR^2
then, it comes to M \leq \frac {R^2}{ \gamma^2}


Perceptron in Python

class Perceptron(object):
    def __init__(self, input_num):
        '''
        初始化感知器浦辨,設(shè)置輸入?yún)?shù)的個(gè)數(shù)
        '''
        # 權(quán)重向量初始化為0
        self.weights = np.array([0.0 for _ in range(input_num)])
        # 偏置項(xiàng)初始化為0
        self.bias = 0.0
        self.iteration = 0
    def __str__(self):
        '''
        打印學(xué)習(xí)到的權(quán)重、偏置項(xiàng)
        '''
        return 'interation:%s\nweights\t:%s\nbias\t:%f\n' % (self.iteration, self.weights, self.bias)
    def predict(self, input_vecs):
        '''
        輸入向量沼沈,輸出感知器的計(jì)算結(jié)果
        '''
        return np.array(list(map(lambda x: np.dot(self.weights,x) + self.bias,input_vecs)))
    def train(self, input_vecs, labels, rate):
        '''
        輸入訓(xùn)練數(shù)據(jù):一組向量流酬、與每個(gè)向量對應(yīng)的label币厕、學(xué)習(xí)率
        '''
        flag = True
        while(flag):
            '''
            一次迭代,把所有的訓(xùn)練數(shù)據(jù)過一遍
            '''
            outputs = self.predict(input_vecs)
            #初始化誤分類點(diǎn)集合M
            M = []
            for data in zip(input_vecs,outputs,labels):
                if data[1]*data[2] <= 0:
                    M.append(data)
                    
            if len(M) == 0:
                #如果訓(xùn)練集中沒有誤分類點(diǎn)芽腾,則停止訓(xùn)練
                flag = False
            else:
                self.iteration += 1
                #隨機(jī)挑選M中的點(diǎn)旦装,
                if len(M) == 1:
                    index = 0
                else:
                    index = np.random.randint(0,len(M)-1)
                self._update_weights(M[index][0],M[index][1],M[index][2],rate)
            
            draw(input_vecs,labels,self.weights,self.bias)
                    
    def _update_weights(self, input_vec, output, label, rate):
        '''
        按照感知器規(guī)則更新權(quán)重
        '''
        # 更新weights
        self.weights += rate* label* input_vec
        # 更新bias
        self.bias += rate * label
    
    def accuracy_score(self, input_vecs, labels):
        count = 0
        outputs = self.predict(input_vecs)
        for output,label in zip(outputs,labels):
            if output*label <= 0:
                count += 1
                
        return count/len(labels)*100
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市晦嵌,隨后出現(xiàn)的幾起案子同辣,更是在濱河造成了極大的恐慌,老刑警劉巖惭载,帶你破解...
    沈念sama閱讀 222,104評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件旱函,死亡現(xiàn)場離奇詭異,居然都是意外死亡描滔,警方通過查閱死者的電腦和手機(jī)棒妨,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來含长,“玉大人券腔,你說我怎么就攤上這事【信ⅲ” “怎么了纷纫?”我有些...
    開封第一講書人閱讀 168,697評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長陪腌。 經(jīng)常有香客問我辱魁,道長,這世上最難降的妖魔是什么诗鸭? 我笑而不...
    開封第一講書人閱讀 59,836評論 1 298
  • 正文 為了忘掉前任染簇,我火速辦了婚禮,結(jié)果婚禮上强岸,老公的妹妹穿的比我還像新娘锻弓。我一直安慰自己,他們只是感情好蝌箍,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,851評論 6 397
  • 文/花漫 我一把揭開白布青灼。 她就那樣靜靜地躺著,像睡著了一般妓盲。 火紅的嫁衣襯著肌膚如雪聚至。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,441評論 1 310
  • 那天本橙,我揣著相機(jī)與錄音扳躬,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛贷币,可吹牛的內(nèi)容都是我干的击胜。 我是一名探鬼主播,決...
    沈念sama閱讀 40,992評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼役纹,長吁一口氣:“原來是場噩夢啊……” “哼偶摔!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起促脉,我...
    開封第一講書人閱讀 39,899評論 0 276
  • 序言:老撾萬榮一對情侶失蹤辰斋,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后瘸味,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體宫仗,經(jīng)...
    沈念sama閱讀 46,457評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,529評論 3 341
  • 正文 我和宋清朗相戀三年旁仿,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了藕夫。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,664評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡枯冈,死狀恐怖毅贮,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情尘奏,我是刑警寧澤滩褥,帶...
    沈念sama閱讀 36,346評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站炫加,受9級特大地震影響铸题,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜琢感,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,025評論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望探熔。 院中可真熱鬧驹针,春花似錦、人聲如沸诀艰。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽其垄。三九已至苛蒲,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間绿满,已是汗流浹背臂外。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人漏健。 一個(gè)月前我還...
    沈念sama閱讀 49,081評論 3 377
  • 正文 我出身青樓嚎货,卻偏偏與公主長得像,于是被迫代替她去往敵國和親蔫浆。 傳聞我的和親對象是個(gè)殘疾皇子殖属,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,675評論 2 359

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,346評論 0 10
  • “燦燦快點(diǎn)原环,再不到外婆家去挠唆,老太又要急死了!”媽媽著急的喊道扮念。 我揉了揉朦朧的眼睛损搬,穿好了舊衣服,(因...
    阿紫_9a18閱讀 194評論 0 0
  • 早上做早餐時(shí)間很趕柜与,起床晚了巧勤,時(shí)間沒有規(guī)劃好,學(xué)會跟人合作是件很重要的事情弄匕,早上一個(gè)人做了很多事情颅悉,超級累,想有人...
    葉子卷閱讀 339評論 2 2
  • -1- 這兩天風(fēng)很大,滴水成冰城丧,快遞都堵在路上進(jìn)不來延曙。有讀者給我發(fā)消息說,男朋友被要求三天之內(nèi)搬離出租屋亡哄,工作可能...
    黎飯飯閱讀 780評論 7 13