您的位置:首页 > 其它

感知机章节课后习题 + 证明感知机算法收敛性

2013-07-04 23:40 465 查看
看了下统计学习方法P36 的3个习题,试着做了下,下面给出自己的解答: 有不对的地方欢迎提出:)

Q 2.1 感知机是线性模型,因此不能表示复杂的函数。请解释感知机为什么不能学习异或XOR函数?

A 2.1 XOR:这个想必大家都很清楚了,输入的两个逻辑值一真一假XOR结果为真,两个输入逻辑值同真同假XOR得假。

下面这个图解释的很清楚:



训练集线性不可分,当然不能用感知机表示出XOR函数。

Q 2.2 模仿书中2.1例,构建从训练数据集合求解感知机的例子

A 2.2 可以构建一个3维的特征空间,取在I卦限的任意两点为正例,取其在第VII卦限一点为负例,比如可以这么取:

正例:(1,1,1) (2,5,7)

负例: (-2,-1,-1)

使用感知机原始形式求解:

import os
import sys
import random

#This algorithm learns a perceptron model from trainingdataset
#note: the trainset is linearly seperable, and different choicesof initial parameters or false-classified points
#may lead to different perceptron model, which is reasonableunder this framework.
#DATE:2013-7-4, by zhl
if __name__ == "__main__":
trainset =[(1,1,1,1),(2,5,7,1),(-2,-1,-1,-1)]
#initialize
w1 = w2 = w3 = b = 0
id = 0
# we set learning rate = 1

while True:
id += 1
flag = False
for choice in range(3):
if trainset[choice][3] * (w1 * trainset[choice][0] + w2 * trainset[choice][1] + w3 * trainset[choice][2] + b)<= 0:
flag = True
break
if flag == False:
break
#randomlyselect a point from trainset
choice = random.randint(0,2)

#judge whether it's false-classified
if trainset[choice][3] * (w1 *trainset[choice][0] + w2 * trainset[choice][1] + w3 * trainset[choice][2] + b) <= 0:
w1 = w1 + trainset[choice][3] * trainset[choice][0]
w2 = w2 + trainset[choice][3] * trainset[choice][1]
w3 = w3 + trainset[choice][3] * trainset[choice][2]

b = b + trainset[choice][3]

#print out current values
print 'Round ',id,':','Flase-Classified Point:',choice + 1,',w1:',w1,',w2:',w2,',w3:',w3,',b:',b,'\n'

print 'Theperceptron model learned is sign(%d x1 + %d x2 + %d x3 + %d)\n' % (w1,w2,w3,b)


Round 1 : Flase-Classified Point: 3 ,w1: 2 ,w2: 1 ,w3: 1 ,b: -1

Theperceptron model learned is sign(2 x1 + 1 x2 + 1 x3 + -1)

Q 2.3 一道证明题,暂略~ 周末有空了做下~

-----------------------------------proof of divergence theorem 2.1----------------------------------

好长时间不写字了,这笔迹真是不忍直视啊....





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: