感知器 机器学习

In this post, we are going to have a look at a program written in Python3 using numpy. We will discuss the basics of what a perceptron is, what is the delta rule and how to use it to converge the learning of the perceptron.

在本文中,我们将看一下使用numpyPython3编写的程序。 我们将讨论什么是感知器 ,什么是增量规则以及如何使用它来融合感知器学习的基础知识。

什么是感知器? (What is a perceptron?)

The perceptron is an algorithm for supervised learning of binary classifiers (let’s assumer {1, 0}). We have a linear combination of weight vector and the input data vector that is passed through an activation function and then compared to a threshold value. If the linear combination is greater than the threshold, we predict the class as 1 otherwise 0. Mathematically,

感知器是一种用于监督学习二进制分类器(让我们的假设{1, 0} )的算法。 我们具有权重向量和输入数据向量的线性组合,该向量通过激活函数传递,然后与阈值进行比较。 如果线性组合大于阈值,则我们将类别预测为1否则预测为0. Mathematically,

Source: stackexchange.com
资料来源:stackexchange.com

Perceptrons only represent linearly separable problems. They fail to converge if the training examples are not linearly separable. This brings into picture the delta rule.

感知器仅代表线性可分离的问题。 如果训练示例不是线性可分离的,则它们无法收敛。 这使增量规则成为现实。

The delta rule converges towards a best-fit approximation of the target concept. The key idea is to use gradient descent to search the hypothesis space of all possible weight vectors.

增量法则趋向于达到目标概念的最佳拟合。 关键思想是使用梯度下降 搜索所有可能的权重向量的假设空间。

Note: This provides the basis for “Backpropogation” algorithm.

注意:这为“反向传播”算法提供了基础。

Now, let’s discuss the problem at hand. The program will read a dataset(tab separated file) and treat the first column as the target concept. The values present in the target concept are A and B, we will consider A as +ve class or 1 and B as -ve class or 0. The program implements the perceptron training rule in batch mode with a constant learning rate and an annealing(decreasing as the number of iterations increase) learning rate, starting with learning rate as 1.

现在,让我们讨论眼前的问题。 该程序将读取数据集 (制表符分隔的文件),并将第一列视为目标概念 。 目标概念中的值是A和B,我们将A视为+ ve类或1 ,将B视为-ve类或0 。 该程序以恒定的学习率和退火(随着迭代次数增加而减少)的学习率以批处理模式实施感知器训练规则,从学习率开始为1。

where Y(x, w) is the set of samples which are misclassified. We will use the count or the number of misclassified points as our error rate(i.e. | Y(x, w)|). The output will also be a tab separated(tsv) file containing the error for each iteration, i.e. it will have 100 columns. Also, it will have 2 rows, one for normal learning rate and one for annealing learning rate.

其中Y(x,w)是错误分类的样本集。 我们将使用计数或错误分类的点数作为错误率(即| Y(x,w)|)。 输出还将是一个制表符分隔的(tsv)文件,其中包含每次迭代的错误,即它将有100列。 另外,它将有两行,一行用于正常学习率,另一行用于退火学习率。

Now, the understanding of what perceptron is, what delta rule and how we are going to use it. Let’s get started with Python3 implementation.

现在,了解什么是感知器,什么是增量规则以及我们将如何使用它。 让我们开始使用Python3实现。

In the program, we are providing two inputs from the command line. They are:

在程序中,我们从命令行提供了两个输入。 他们是:

1. data — The location of the data file.

1. 数据 —数据文件的位置。

2. output — Where to write the tsv solution to

2. 输出 -将tsv解决方案写入何处

Therefore, the program should be able to start like this:

因此,该程序应该能够像这样启动:

python3 perceptron.py --data data.tsv --output solution.tsv

The program consists of 8 parts and we are going to have a look at them one at a time.

该程序包括8个部分,我们将一次对其进行介绍。

导入声明 (The import statements)

import argparse # to read inputs from command lineimport csv # to read and process datasetimport numpy as np # to perform mathematical functions

代码执行初始化块 (The code execution initializer block)

# initialise argument parser and read arguments from command line with the respective flags and then call the main() functionif __name__ == '__main__':        parser = argparse.ArgumentParser()        parser.add_argument("-d", "--data", help="Data File")            parser.add_argument("-o", "--output", help="output")        main()

main()函数 (The main() function)

def main():args = parser.parse_args()file, outputFile = args.data, args.outputlearningRate = 1with open(file) as tsvFile:reader = csv.reader(tsvFile, delimiter='\t')X = []Y = []for row in reader:X.append([1.0] + row[1:])if row[0] == 'A':Y.append([1])else:Y.append([0])n = len(X)X = np.array(X).astype(float)Y = np.array(Y).astype(float)W = np.zeros(X.shape[1]).astype(float)W = W.reshape(X.shape[1], 1).astype(float)normalError = calculateNormalBatchLearning(X, Y, W, learningRate)annealError = calculateAnnealBatchLearning(X, Y, W, learningRate)with open(outputFile, 'w') as tsvFile:writer = csv.writer(tsvFile, delimiter='\t')writer.writerow(normalError)writer.writerow(annealError)

The flow of the main() function is as follows:

main()函数的流程如下:

  1. Save respective command line inputs into variables将相应的命令行输入保存到变量中
  2. Set starting learningRate = 1设置开始学习率= 1
  3. Read the dataset using csv and delimiter='\t', store independent variables in X and dependent variable in Y. We are adding 1.0 as bias to our independent data

    使用csvdelimiter='\t'读取数据集 ,在X存储自变量,在Y存储因变量。 我们将1.0作为对独立数据的偏见

  4. The independent and dependent data is converted to float独立和从属数据转换为浮点型
  5. The weight vector is initialised with zeroes with same dimensions as X

    权重向量以与X相同尺寸的零初始化

  6. The normalError and annealError are calculated by calling their respective methods

    normalErrorannealError 通过调用它们各自的方法来计算

  7. Finally, the output is saved into a tsv file最后,将输出保存到tsv文件中

computeNormalBatchLearning()函数 (The calculateNormalBatchLearning() function)

def calculateNormalBatchLearning(X, Y, W, learningRate):e = []for i in range(101):f_x = calculatePredicatedValue(X, W)errorCount = calculateError(Y, f_x)e.append(errorCount)gradient, W = calculateGradient(W, X, Y, f_x, learningRate)return e

The flow of calculateNormalBatchLearning() is as follows:

calculateNormalBatchLearning()的流程如下:

  1. Initialisation of a variable e to store the error count

    初始化变量e以存储错误计数

  2. A loop is run for 100 iterations循环运行100次迭代
  3. Predicted value is computed based on the perceptron rule described earlier using calculatePredicatedValue() method

    预测值是根据前面所述的感知器规则使用calculatePredicatedValue()方法计算的

  4. Error count is calculated using the calculateError() method

    错误计数是使用calculateError()方法计算的

  5. Weights are updated based on the equation above using calculateGradient() method

    权重根据上面的等式使用calculateGradient()方法进行更新

computeAnnealBatchLearning()函数 (The calculateAnnealBatchLearning() function)

def calculateAnnealBatchLearning(X, Y, W, learningRate):e = []for i in range(101):f_x = calculatePredicatedValue(X, W)errorCount = calculateError(Y, f_x)e.append(errorCount)learningRate = 1 / (i + 1)gradient, W = calculateGradient(W, X, Y, f_x, learningRate)return e

The flow of calculateNormalBatchLearning() is as follows:

calculateNormalBatchLearning()的流程如下:

  1. Initialisation of a variable e to store the error count

    初始化变量e以存储错误计数

  2. A loop is run for 100 iterations循环运行100次迭代
  3. Predicted value is computed based on the perceptron rule described earlier using calculatePredicatedValue() method

    预测值是根据前面所述的感知器规则使用calculatePredicatedValue()方法计算的

  4. Error count is calculated using the calculateError() method

    错误计数是使用calculateError()方法计算的

  5. Learning rate is divided by the number of the iteration学习率除以迭代次数
  6. Weights are updated based on the equation above using calculateGradient() method

    权重根据上面的等式使用calculateGradient()方法进行更新

computePredictedValue()函数 (The calculatePredictedValue() function)

def calculatePredicatedValue(X, W):f_x = np.dot(X, W)for i in range(len(f_x)):if f_x[i][0] > 0:f_x[i][0] = 1else:f_x[i][0] = 0return f_x

As described in the perceptron image, if the linear combination of W and X is greater than 0, then we predict the class as 1 otherwise 0.

如感知器图像中所述,如果WX的线性组合大于0 ,则我们将类别预测为1否则为0

computeError()函数 (The calculateError() function)

def calculateError(Y, f_x):errorCount = 0for i in range(len(f_x)):if Y[i][0] != f_x[i][0]:errorCount += 1return errorCount

We count the number of instances where the predicted value and the true value do not match and this becomes our error count.

我们计算了预测值和真实值不匹配的实例数,这就是我们的错误计数。

computeGradient()函数 (The calculateGradient() function)

def calculateGradient(W, X, Y, f_x, learningRate):gradient = (Y - f_x) * Xgradient = np.sum(gradient, axis=0)# gradient = np.array([float("{0:.4f}".format(val)) for val in gradient])temp = np.array(learningRate * gradient).reshape(W.shape)W = W + tempreturn gradient, W.astype(float)

This method is translation of the weight update formula mentioned above.

该方法是上述权重更新公式的转换。

Now, that the whole code is out there. Let’s have a look at the execution of the program.

现在,整个代码就在那里。 让我们看一下程序的执行。

Here is how the output looks like:

输出结果如下所示:

The final program

最终程序

import argparse
import csv
import numpy as npdef main():args = parser.parse_args()file, outputFile = args.data, args.outputlearningRate = 1with open(file) as tsvFile:reader = csv.reader(tsvFile, delimiter='\t')X = []Y = []for row in reader:X.append([1.0] + row[1:])if row[0] == 'A':Y.append([1])else:Y.append([0])n = len(X)X = np.array(X).astype(float)Y = np.array(Y).astype(float)W = np.zeros(X.shape[1]).astype(float)W = W.reshape(X.shape[1], 1).astype(float)normalError = calculateNormalBatchLearning(X, Y, W, learningRate)annealError = calculateAnnealBatchLearning(X, Y, W, learningRate)with open(outputFile, 'w') as tsvFile:writer = csv.writer(tsvFile, delimiter='\t')writer.writerow(normalError)writer.writerow(annealError)def calculateNormalBatchLearning(X, Y, W, learningRate):e = []for i in range(101):f_x = calculatePredicatedValue(X, W)errorCount = calculateError(Y, f_x)e.append(errorCount)gradient, W = calculateGradient(W, X, Y, f_x, learningRate)return edef calculateAnnealBatchLearning(X, Y, W, learningRate):e = []for i in range(101):f_x = calculatePredicatedValue(X, W)errorCount = calculateError(Y, f_x)e.append(errorCount)learningRate = 1 / (i + 1)gradient, W = calculateGradient(W, X, Y, f_x, learningRate)return edef calculateGradient(W, X, Y, f_x, learningRate):gradient = (Y - f_x) * Xgradient = np.sum(gradient, axis=0)# gradient = np.array([float("{0:.4f}".format(val)) for val in gradient])temp = np.array(learningRate * gradient).reshape(W.shape)W = W + tempreturn gradient, W.astype(float)def calculateError(Y, f_x):errorCount = 0for i in range(len(f_x)):if Y[i][0] != f_x[i][0]:errorCount += 1return errorCountdef calculatePredicatedValue(X, W):f_x = np.dot(X, W)for i in range(len(f_x)):if f_x[i][0] > 0:f_x[i][0] = 1else:f_x[i][0] = 0return f_xif __name__ == '__main__':parser = argparse.ArgumentParser()parser.add_argument("-d", "--data", help="Data File")parser.add_argument("-o", "--output", help="output")main()

翻译自: https://towardsdatascience.com/machine-learning-perceptron-implementation-b867016269ec

感知器 机器学习


http://www.taodudu.cc/news/show-997489.html

相关文章:

  • 快速排序简便记_建立和测试股票交易策略的快速简便方法
  • 美剧迷失_迷失(机器)翻译
  • 我如何预测10场英超联赛的确切结果
  • 深度学习数据自动编码器_如何学习数据科学编码
  • 图深度学习-第1部分
  • 项目经济规模的估算方法_估算英国退欧的经济影响
  • 机器学习 量子_量子机器学习:神经网络学习
  • 爬虫神经网络_股市筛选和分析:在投资中使用网络爬虫,神经网络和回归分析...
  • 双城记s001_双城记! (使用数据讲故事)
  • rfm模型分析与客户细分_如何使用基于RFM的细分来确定最佳客户
  • 数据仓库项目分析_数据分析项目:仓库库存
  • 有没有改期末考试成绩的软件_如果考试成绩没有正常分配怎么办?
  • 探索性数据分析(EDA):Python
  • 写作工具_4种加快数据科学写作速度的工具
  • 大数据(big data)_如何使用Big Query&Data Studio处理和可视化Google Cloud上的财务数据...
  • 多元时间序列回归模型_多元时间序列分析和预测:将向量自回归(VAR)模型应用于实际的多元数据集...
  • 数据分析和大数据哪个更吃香_处理数据,大数据甚至更大数据的17种策略
  • 批梯度下降 随机梯度下降_梯度下降及其变体快速指南
  • 生存分析简介:Kaplan-Meier估计器
  • 使用r语言做garch模型_使用GARCH估计货币波动率
  • 方差偏差权衡_偏差偏差权衡:快速介绍
  • 分节符缩写p_p值的缩写是什么?
  • 机器学习 预测模型_使用机器学习模型预测心力衰竭的生存时间-第一部分
  • Diffie Hellman密钥交换
  • linkedin爬虫_您应该在LinkedIn上关注的8个人
  • 前置交换机数据交换_我们的数据科学交换所
  • 量子相干与量子纠缠_量子分类
  • 知识力量_网络分析的力量
  • marlin 三角洲_带火花的三角洲湖:什么和为什么?
  • eda分析_EDA理论指南

感知器 机器学习_机器学习感知器实现相关推荐

  1. pytorch机器学习_机器学习— PyTorch

    pytorch机器学习 PyTorch : It is an open source machine learning library based on the Torch library (whic ...

  2. knn 机器学习_机器学习:通过预测意大利葡萄酒的品种来观察KNN的工作方式

    knn 机器学习 Introduction 介绍 For this article, I'd like to introduce you to KNN with a practical example ...

  3. 机器学习与分布式机器学习_机器学习的歧义

    机器学习与分布式机器学习 超越最高精度 (Beyond Achieving Top Accuracy) We are familiar with the idea of using machine l ...

  4. 信号处理深度学习机器学习_机器学习和信号处理如何融合?

    信号处理深度学习机器学习 As a design engineer, I am increasingly exposed to more complex engineering challenges ...

  5. 信号处理深度学习机器学习_机器学习与信号处理

    信号处理深度学习机器学习 机器学习性能与两种关键信号处理算法(快速傅里叶变换和最小均方预测)的有趣对比. (A fun comparison of machine learning performan ...

  6. 工业人工智能与机器学习_机器学习与第四次工业革命

    工业人工智能与机器学习 数据科学与机器学习(DATA SCIENCE AND MACHINE LEARNING) Undoubtedly, a machine learning algorithm i ...

  7. 从零学习机器学习_机器学习:如何从零变英雄

    从零学习机器学习 以"为什么?"开头 并以"我准备好了!"结尾 (Start with "Why?" and end with " ...

  8. python 图片识别 机械手_6图片识别物品_清华编程高手尹成带你用python大战机器学习_机器学习视频-51CTO学院...

    清华编程高手尹成带你用python大战机器学习 机器学习是一门多领域交叉学科,涉及概率论.统计学.逼近论.凸分析.算法复杂度理论等多门学科.专门研究计算机怎样模拟或实现人类的学习行为,以获取新的知识或 ...

  9. 学习曲线 机器学习_机器学习的学习曲线

    学习曲线 机器学习 Diagnose Bias and Variance to Reduce Error 诊断偏差和方差以减少误差 When building machine learning mod ...

最新文章

  1. Esper学习之十二:EPL语法(八)
  2. 【工具】Excel 表格数据转换成Json格式的实用工具 excel2json
  3. Java8 lambda函数式编程
  4. Linux下实现脚本监测特定进程占用内存情况
  5. uefi linux开发环境,UEFI开发学习1 - Ubuntu下搭建UDK2018开发环境
  6. plsql连接mysql教程_PLSQL Developer连接oracle数据库配置教程
  7. 『矩阵论笔记』雅可比矩阵(Jacobian)和海森矩阵(Hessian)
  8. 分数加减乘除混合运算带答案_分数加减乘除混合运算专项训练
  9. 计算机笔记本硬盘,笔记本取证之--笔记本硬盘拆卸
  10. 陈强教授《机器学习及R应用》课程 第六章作业
  11. 利用jink的驱动软件j-flash 合并两个hex的方法,bootloader+app -(转载)
  12. 2018/10/25 模拟赛 纸牌
  13. Vue使用Electron获取电脑MAC地址
  14. MoveIt! 学习笔记13 - KDL/IKFAST/TRAC-IK运动学求解器区别
  15. cesium模型纹理替换
  16. ffmpeg详细安装教程
  17. 【MySQL 8.0 OCP 1Z0-908认证考试】 题库精讲--第一讲mysqlbackup
  18. 张家口北方学院计算机是专科,河北北方学院有哪些专科专业
  19. 24 孔复音 C 调口琴
  20. 一起来“泡博”[--老沙]

热门文章

  1. fiddler抓包1-抓小程序https包
  2. BZOJ.2738.矩阵乘法(整体二分 二维树状数组)
  3. 搭建Maven私服那点事
  4. Numpy 新手教程(2)
  5. linux驱动分离分层的概念
  6. jar包直接拷贝到WEB-INF/lib下和以userLibrary引入的区别
  7. 弹出层之1:JQuery.Boxy (二)
  8. C#编号的ActiveX控件采用CAB的布署方式实例
  9. 在WinCE中实现Screen Rotation(屏幕旋转)
  10. Iphone NSMutableArray,NSMutableDictionary AND 动态添加按钮