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

看了下统计学习方法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----------------------------------

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




  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值