前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用感知机训练加法模型

使用感知机训练加法模型

作者头像
Gxjun
发布2018-03-27 12:08:40
7380
发布2018-03-27 12:08:40
举报
文章被收录于专栏:ml

感知机此处不介绍,这里只是简单的做了一个使用感知机思路,训练一个y=a+b计算模型. 

代码语言:javascript
复制
 1 # -*-coding:utf-8-*-
 2 '@author: xijun.gong'
 3 import numpy as np
 4 import random
 5 import math
 6 
 7 
 8 class Perceptron:
 9     def __init__(self, learnRate, maxIter, bit_len):
10         """
11         :param bit_len
12         :param learnRate:
13         :param maxIter:  最大迭代次数
14         """
15         self.learmRate = learnRate;
16         self.weight = None;
17         self.maxIter = maxIter;
18         # produce map
19         self.bit_len = bit_len;
20         self.nummap = None;
21         self.initMap()
22         pass
23 
24     def initMap(self):
25         maxNum = (1 << self.bit_len);  # 该位数下的最大值
26         self.nummap = np.zeros((maxNum, self.bit_len), dtype=np.int);  # include zero
27         for _id in xrange(maxNum):
28             for index in xrange(self.bit_len):
29                 self.nummap[_id][index] = 1 & (_id >> index);
30         pass
31 
32     def initWeight(self):
33         """
34         :return:
35         """
36         self.weight = np.ones(self.bit_len) / self.bit_len;
37 
38     def fit(self, fds, labels):
39         """
40         :param fds: 训练样本集合
41         :param labels:
42         :return:
43         """
44         feature_nums = fds.shape[1]  # 样本中的特征参数数量
45         self.initWeight()
46         for iter in xrange(self.maxIter):
47             print 'train as iter is {} '.format(iter)
48             acc_cnt = 0
49             for _ind, sample in enumerate(fds):
50                 a = self.nummap[int(sample[0])];
51                 b = self.nummap[int(sample[1])];
52                 label_y = sum(self.weight * (a + b))
53                 # 计算var_w 表示倒三角w
54                 print 'the reality:{} , predict {}'.format(labels[_ind], label_y);
55                 if math.fabs(labels[_ind] - label_y) <= 0.000001:
56                     acc_cnt += 1;
57                     continue;
58                 var_w = self.learmRate * (labels[_ind] - label_y) * (a + b)
59                 self.weight += var_w;
60             print 'accuary is {}'.format(acc_cnt / (len(fds) * 1.0))
61             if acc_cnt == len(fds):
62                 np.save('weight.npy', {'weight': self.weight});
63                 return;
64         pass
65 
66     def load(self, path='weight.npy'):
67         return np.load(path)['weight']
68 
69     def predict(self, fd):
70         a = self.nummap[fd[0]];
71         b = self.nummap[fd[1]];
72         return sum(self.weight * (a + b))
73 
74     def predict_prod(self):
75         pass
76 
77 
78 if __name__ == '__main__':
79     import time
80 
81     perceptron = Perceptron(learnRate=0.01, maxIter=2000, bit_len=5);
82     xa = np.arange(31);
83     xb = np.zeros(31);
84     labels = np.zeros(31)
85     for i in xrange(31):
86         xb[i] = random.randint(0, (int(time.time() + 1)) % 31)
87         labels[i] = xb[i] + xa[i]
88     perceptron.fit(np.array([xa, xb]).T, labels)
89     print 'predict is {}'.format(perceptron.predict([24, 13]))

运行结果:

代码语言:javascript
复制
train as iter is 277 
the reality:0.0 , predict 0.0
the reality:16.0 , predict 16.0000005749
the reality:16.0 , predict 15.9999994995
the reality:3.0 , predict 3.00000059084
the reality:18.0 , predict 17.999999818
the reality:15.0 , predict 15.0000000195
the reality:20.0 , predict 19.9999998534
the reality:22.0 , predict 22.0000009642
the reality:10.0 , predict 9.99999911021
the reality:22.0 , predict 21.9999996143
the reality:23.0 , predict 22.9999990943
the reality:17.0 , predict 17.0000000549
the reality:25.0 , predict 24.9999994128
the reality:18.0 , predict 18.0000008934
the reality:20.0 , predict 19.9999998534
the reality:15.0 , predict 15.0000000195
the reality:27.0 , predict 26.999999038
the reality:31.0 , predict 30.9999993919
the reality:25.0 , predict 25.0000003525
the reality:21.0 , predict 20.9999999986
the reality:35.0 , predict 34.9999997457
the reality:29.0 , predict 28.9999993564
the reality:39.0 , predict 38.9999996894
the reality:26.0 , predict 26.0000009079
the reality:31.0 , predict 30.9999993919
the reality:25.0 , predict 24.9999990026
the reality:33.0 , predict 32.9999994273
the reality:32.0 , predict 31.9999999473
the reality:32.0 , predict 31.9999991549
the reality:34.0 , predict 34.0000002657
the reality:33.0 , predict 32.9999994273
accuary is 1.0
predict is 36.9999984312
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-03-31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档