使用感知机训练加法模型
2017-03-31 22:27
183 查看
感知机此处不介绍,这里只是简单的做了一个使用感知机思路,训练一个y=a+b计算模型.
运行结果:
# -*-coding:utf-8-*- '@author: xijun.gong' import numpy as np import random import math class Perceptron: def __init__(self, learnRate, maxIter, bit_len): """ :param bit_len :param learnRate: :param maxIter: 最大迭代次数 """ self.learmRate = learnRate; self.weight = None; self.maxIter = maxIter; # produce map self.bit_len = bit_len; self.nummap = None; self.initMap() pass def initMap(self): maxNum = (1 << self.bit_len); # 该位数下的最大值 self.nummap = np.zeros((maxNum, self.bit_len), dtype=np.int); # include zero for _id in xrange(maxNum): for index in xrange(self.bit_len): self.nummap[_id][index] = 1 & (_id >> index); pass def initWeight(self): """ :return: """ self.weight = np.ones(self.bit_len) / self.bit_len; def fit(self, fds, labels): """ :param fds: 训练样本集合 :param labels: :return: """ feature_nums = fds.shape[1] # 样本中的特征参数数量 self.initWeight() for iter in xrange(self.maxIter): print 'train as iter is {} '.format(iter) acc_cnt = 0 for _ind, sample in enumerate(fds): a = self.nummap[int(sample[0])]; b = self.nummap[int(sample[1])]; label_y = sum(self.weight * (a + b)) # 计算var_w 表示倒三角w print 'the reality:{} , predict {}'.format(labels[_ind], label_y); if math.fabs(labels[_ind] - label_y) <= 0.000001: acc_cnt += 1; continue; var_w = self.learmRate * (labels[_ind] - label_y) * (a + b) self.weight += var_w; print 'accuary is {}'.format(acc_cnt / (len(fds) * 1.0)) if acc_cnt == len(fds): np.save('weight.npy', {'weight': self.weight}); return; pass def load(self, path='weight.npy'): return np.load(path)['weight'] def predict(self, fd): a = self.nummap[fd[0]]; b = self.nummap[fd[1]]; return sum(self.weight * (a + b)) def predict_prod(self): pass if __name__ == '__main__': import time perceptron = Perceptron(learnRate=0.01, maxIter=2000, bit_len=5); xa = np.arange(31); xb = np.zeros(31); labels = np.zeros(31) for i in xrange(31): xb[i] = random.randint(0, (int(time.time() + 1)) % 31) labels[i] = xb[i] + xa[i] perceptron.fit(np.array([xa, xb]).T, labels) 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
相关文章推荐
- 从头开始编写基于隐含马尔可夫模型HMM的中文分词器之二 - 模型训练与使用
- 使用判别训练的部件模型进行目标检测 Object Detection with Discriminatively Trained Part Based Models
- Caffe使用step by step:使用自己数据对已经训练好的模型进行finetuning
- SRILM使用之训练无平滑语言模型
- 使用openface训练自己的第一个模型
- 使用MapReduce对svm模型进行训练
- tensorflow中mnist 使用cnn模型训练的输出层数为7x7的原因
- Caffe学习3-使用预训练模型finetuning
- 使用DPM训练自己的模型
- Caffe学习总结(四)——使用mnist训练模型识别一张手写数字图像
- (译)使用判别训练的部件模型进行目标检测 Object Detection with Discriminatively Trained Part Based Models
- 有关Caffe训练好的模型在Python接口下使用分类不准确的问题解决
- openNLP的POSTagger使用(一)训练模型
- 使用caffe训练好的模型进行分类
- Caffe使用step by step:使用自己数据对已经训练好的模型进行finetuning
- 最简单的方式使用Discriminatively Trained Deformable Part Models训练自己的模型(原创)
- 使用判别训练的部件模型进行目标检测 Object Detection with Discriminatively Trained Part Based Models
- 使用DPM(Deformable Part Model,voc-release3.1)算法INRIA通过训练你的身体检测模型数据集
- Windos 7 64位下通过VS2013配置caffe模型,并使用MATLAB调用训练好的ImgaeNet接口做测试
- 【神经网络与深度学习】Caffe使用step by step:使用自己数据对已经训练好的模型进行finetuning