一、Introduction

Perceptron can represent AND,OR,NOT

用初中的线性规划问题理解

异或的里程碑意义

想学的通透,先学历史!

据说在人工神经网络(artificial neural network, ANN)发展初期,由于无法实现对多层神经网络(包括异或逻辑)的训练而造成了一场ANN危机,到最后BP算法的出现,才让训练带有隐藏层的多层神经网络成为可能。因此异或的实现在ANN的发展史是也是具有里程碑意义的。异或之所以重要,是因为它相对于其他逻辑关系,例如与(AND), 或(OR)等,异或是线性不可分的。如下图:

要解决非线性可分问题,需考虑使用多层功能神经元. 例如下图中这个

简单的两层感知机就能解决异或问题。在图中,输出层与输入层之间的一

层神经元,被称为隐含层(hidden layer) ,隐含层和输出层神经元都是拥

有激活函数的功能神经元.

能解决异或问题的两层感知机

参考周志华老师西瓜书


二、Python 代码实现

异或肯定是不能通过一条直线区分的,因此单层网络无法实现异或,但两层(包含一个隐藏层)就可以了。

在实际应用中,异或门(Exclusive-OR gate, XOR gate)是数字逻辑中实现逻辑异或的逻辑门,这一函数能实现模为2的加法。因此,异或门可以实现计算机中的二进制加法。

可以有多种方法实现Xor功能,本代码采用的算法图示如下

将上图转化为神经网络层形式便于理解:



# ----------
#
# In this exercise, you will create a network of perceptrons that can represent
# the XOR function, using a network structure like those shown in the previous
# quizzes.
#
# You will need to do two things:
# First, create a network of perceptrons with the correct weights
# Second, define a procedure EvalNetwork() which takes in a list of inputs and
# outputs the value of this network.
#
# ---------- import numpy as np class Perceptron:
"""
This class models an artificial neuron with step activation function.
""" def __init__(self, weights = np.array([1]), threshold = 0):
"""
Initialize weights and threshold based on input arguments. Note that no
type-checking is being performed here for simplicity.
"""
self.weights = weights
self.threshold = threshold def activate(self, values):
"""
Takes in @param values, a list of numbers equal to length of weights.
@return the output of a threshold perceptron with given inputs based on
perceptron weights and threshold.
""" # First calculate the strength with which the perceptron fires
strength = np.dot(values,self.weights) # Then return 0 or 1 depending on strength compared to threshold
return int(strength >= self.threshold)#this row changed by myself # Part 1: Set up the perceptron network
Network = [ # input layer, declare input layer perceptrons here
[ Perceptron([1,0],1),Perceptron([1,1],2),Perceptron([0,1],1) ], \
# output node, declare output layer perceptron here
[ Perceptron([1, -2, 1], 1) ] ]
# Part 2: Define a procedure to compute the output of the network, given inputs
def EvalNetwork(inputValues, Network):
"""
Takes in @param inputValues, a list of input values, and @param Network
that specifies a perceptron network. @return the output of the Network for
the given set of inputs.
""" # MY MAIN CODE HERE # Be sure your output value is a single number #Method1 :
return Network[1][0].activate([p.activate(inputValues) for p in Network[0]])
# p is an instance of Perceptron.
# inner brackets -->input layer
# Network[1][0] -->Perceptron([1, -2, 1], 1) -- Only one element #Method2 :
# OutputValue = inputValues
# for layer in Network:
# OutputValue = map(lambda p:p.activate(OutputValue), layer)
# return OutputValue
## but warning:this method return a list ,not a single number
## to review Python Grammar? def test():
"""
A few tests to make sure that the perceptron class performs as expected.
"""
print "0 XOR 0 = 0?:", EvalNetwork(np.array([0,0]), Network)
print "0 XOR 1 = 1?:", EvalNetwork(np.array([0,1]), Network)
print "1 XOR 0 = 1?:", EvalNetwork(np.array([1,0]), Network)
print "1 XOR 1 = 0?:", EvalNetwork(np.array([1,1]), Network) if __name__ == "__main__":
test()

OUTPUT:

Running test()...
0 XOR 0 = 0?: 0
0 XOR 1 = 1?: 1
1 XOR 0 = 1?: 1
1 XOR 1 = 0?: 0
All done!

ANN神经网络——实现异或XOR (Python实现)的更多相关文章

  1. BP神经网络求解异或问题(Python实现)

    反向传播算法(Back Propagation)分二步进行,即正向传播和反向传播.这两个过程简述如下: 1.正向传播 输入的样本从输入层经过隐单元一层一层进行处理,传向输出层:在逐层处理的过程中.在输 ...

  2. 简单多层神经网络实现异或XOR

    最近在看<Neural Network Design_Hagan> 然后想自己实现一个XOR 的网络. 由于单层神经网络不能将异或的判定分为两类. 根据 a^b=(a&~b)|(~ ...

  3. ANN神经网络——Sigmoid 激活函数编程练习 (Python实现)

    # ---------- # # There are two functions to finish: # First, in activate(), write the sigmoid activa ...

  4. 目前所有的ANN神经网络算法大全

    http://blog.sina.com.cn/s/blog_98238f850102w7ik.html 目前所有的ANN神经网络算法大全 (2016-01-20 10:34:17) 转载▼ 标签: ...

  5. 【机器学习】神经网络实现异或(XOR)

    注:在吴恩达老师讲的[机器学习]课程中,最开始介绍神经网络的应用时就介绍了含有一个隐藏层的神经网络可以解决异或问题,而这是单层神经网络(也叫感知机)做不到了,当时就觉得非常神奇,之后就一直打算自己实现 ...

  6. OpenCV——ANN神经网络

    ANN-- Artificial Neural Networks 人工神经网络 //定义人工神经网络 CvANN_MLP bp; // Set up BPNetwork's parameters Cv ...

  7. 神经网络BP算法C和python代码

    上面只显示代码. 详BP原理和神经网络的相关知识,请参阅:神经网络和反向传播算法推导 首先是前向传播的计算: 输入: 首先为正整数 n.m.p.t,分别代表特征个数.训练样本个数.隐藏层神经元个数.输 ...

  8. 【xsy1147】 异或(xor) 可持久化trie

    我的脑回路可能比较奇怪. 我们对这些询问离线,将所得序列${a}$的后缀和建$n$棵可持久化$trie$. 对于一组询问$(l,r,x)$,我们在主席树上询问第$l$棵树$-$第r$+1$棵树中与$s ...

  9. 图片异或(xor)getflag

    题目地址:https://files.cnblogs.com/files/nul1/flag_enc.png.tar 这题是源于:网鼎杯minified 经过测试隧道红色最低通道异常.其余均正常.所以 ...

随机推荐

  1. 在乌班图中将py3设置为默认解释器

    ubuntu16.04中将python3设置为默认 直接执行这两个命令即可: sudo update-alternatives --install /usr/bin/python python /us ...

  2. restsharp 组件调用返回 gbk 编码的api,中文乱码解决方法。(restsharp response 中文乱码 gbk)

    最近要调一个restful风格的api 用了 一个开源第三方组件,组件还是蛮好用的, 支持直接按参数定义实体类,然后发起请求之前直接 addobject 的方式就把请求参数给添加进去了, 解码的时候可 ...

  3. Python多线程&进程

    一.线程&进程 对于操作系统来说,一个任务就是一个进程(Process),比如打开一个浏览器就是启动一个浏览器进程,打开一个记事本就启动了一个记事本进程,打开两个记事本就启动了两个记事本进程, ...

  4. CentOS下MySQL的安装过程

    1 查看 CentOS 自带的 mysql 输入命令: rpm -qa | grep mysql 2 将自带的MySQL卸载了 输入命令: rpm -e --nodeps mysql-libs-5.1 ...

  5. java.utils.UUID类介绍

    1 UUID介绍 UUID (Universally Unique Identifier)缩写,即通用唯一识别码,也是被开源软件基金会 (Open Software Foundation, OSF) ...

  6. emacs 配置 clojure

    安装JDK,Version >= 6.0 java -version 2 安装EMACS,Version>=24 在脚本~/.emacs.d/init.el中增加如下内容 (require ...

  7. FLUSH TABLES WITH READ LOCK 和 LOCK TABLES 之种种

    1.FLUSH TABLES WITH READ LOCK 这个命令是全局读锁定,执行了命令之后所有库所有表都被锁定只读.一般都是用在数据库联机备份,这个时候数据库的写操作将被阻塞,读操作顺利进行. ...

  8. Android OpenGL教程-第四课【转】

    第四课 旋转: 在这一课里,我将教会你如何旋转三角形和四边形.左图中的三角形沿Y轴旋转,四边形沿着X轴旋转. 我们增加两个变量来控制这两个对象的旋转.这两个变量加在程序的开始处其他变量的后面.它们是浮 ...

  9. RPA流程自动化-UIPath简介

    UiPath简介 转自: http://www.cnblogs.com/mxue/p/UiPath_To147_Road.html 最近RPA比较火,UiPath工具排名前几位并且免费试用,很多朋友们 ...

  10. 快速清除SQL2008日志文件

    USE [master] --把数据库调整为简单模式 GO ALTER DATABASE krisvision SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DA ...