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

 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]))

运行结果:

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

使用感知机训练加法模型相关推荐

  1. 不可思议!英伟达新技术训练NeRF模型最快只需5秒,代码已开源

    英伟达将训练 NeRF 模型从 5 小时缩至 5 秒. 你曾想过在 5 秒内训练完成狐狸的 NeRF 模型吗?现在英伟达做到了! 令人不可思议的是,就如谷歌科学家 Jon Barron 在推特上表示的 ...

  2. 使用Java-Hanlp训练CRF模型

    文章目录 一.CRF分词 二.CRF模型训练 1.语料库准备 2.词性标注 3.训练 4.结果文件 5.BEMS标注 三.实验结果 CRF是序列标注场景中常用的一种语言模型,与基于隐马尔可夫模型(HM ...

  3. 超50篇论文串联起从VQA到多模态预训练大模型的前世今生—Part 1

    文章简介 本文从视觉问答(VQA)任务出发,讲述了 2015 年任务的定义开始,接踵出现的各种多模态技术.从无注意力机制的深度学习模型,发展到天然适合注意力机制的多模态场景的模型,再到基于 Trans ...

  4. 基于pytorch的模型稀疏训练与模型剪枝示例

    基于pytorch的模型稀疏训练与模型剪枝示例 稀疏训练+模型剪枝代码下载地址:下载地址 CIFAR10-VGG16BN Baseline Trained with Sparsity (1e-4) P ...

  5. 中文预训练ALBERT模型来了:小模型登顶GLUE,Base版模型小10倍、速度快1倍

    (图片由AI科技大本营付费下载自视觉中国) 作者 | 徐亮(实在智能算法专家)  来源 | AINLP(ID:nlpjob) 谷歌ALBERT论文刚刚出炉一周,中文预训练ALBERT模型来了,感兴趣的 ...

  6. R语言构建xgboost模型:交叉验证(cross validation)训练xgboost模型,配置自定义的损失函数评估函数并使用交叉验证训练xgboost模型

    R语言构建xgboost模型:交叉验证(cross validation)训练xgboost模型,配置自定义的损失函数(loss function).评估函数(evaluation function) ...

  7. R语言构建xgboost模型:交叉验证(cross validation)训练xgboost模型

    R语言构建xgboost模型:交叉验证(cross validation)训练xgboost模型 目录

  8. 是否有可能从python中的句子语料库重新训练word2vec模型(例如GoogleNews-vectors-negative300.bin)?

    是否有可能从python中的句子语料库重新训练word2vec模型(例如GoogleNews-vectors-negative300.bin)? http://www.voidcn.com/artic ...

  9. 超越Facebook、谷歌、微软,百度发布全球首个百亿参数对话预训练生成模型PLATO-XL...

    点击上方"AI遇见机器学习",选择"星标"公众号 重磅干货,第一时间送达 来自:机器之心 和 AI 进行无障碍的对话,是什么样的体验?你或许能够在这篇文章里找到 ...

最新文章

  1. python营销骗局_python案例:金融营销活动中欺诈用户行为分析
  2. 《图像处理实例》之 曲线之间距离求解
  3. 初学python还是swift-Python并不完美,Swift正在成长为深度学习语言 !
  4. Windows Mobile访问SQL Server CE 3.5(2)
  5. 春节快乐!iPhone11 128G抱回家!
  6. 坏道修复是不是硬盘东西全部都没有了_硬盘有坏道就不能用了吗?别再吃哑巴亏了,今天跟大家再说一次...
  7. 【原】常见的模块,你语义化了没
  8. 编译与运行ORB-SLAM的问题:1、unistd.h 2、virtual memory exhausted 3、internal compiler error 4、共享文件夹设置
  9. Windows Moible, Wince 使用.NET Compact Framework的进行蓝牙(Bluetooth)广播程序的开发
  10. python 运行另一个py_如何在python中执行另一个py文件
  11. 通过mtd读写flash_linuxmtd读写flash
  12. 手机麦克风结构原理图_麦克风的构造图解 麦克风偏置电路和滤波电路
  13. EDI的含义及其重要性
  14. 二十四节气之立秋时节常识介绍
  15. Power BI----综合应用
  16. 研究型论文框架及阅读文献方法
  17. 小甲鱼零基础学习python_19 【pickle -- 腌制一缸泡菜】
  18. win7 热点设置命令
  19. x64dbg和IDA pro 配置PDB 符号文件symbols
  20. horspool算法C语言代码,sfa3

热门文章

  1. web框架flask(12)——国际化和本地化
  2. leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal
  3. 视频分享:编码与代码评审-质量与现实的最激烈冲突点(完整版)
  4. python绘制动点_Python asyncore / asynchat 基本传输实验 - Jacky Liu's Blog
  5. Linux Kernel TCP/IP Stack — L1 Layer — Network Interface
  6. 互联网协议 — Segment Routing(分段路由网络)— SR-MPLS
  7. 5G 在轨道运输网络中的需求
  8. box-shadow比较美观的阴影
  9. 工业安全的未来——IT与OT的融合
  10. 医疗人工智能会替代医生吗