ANN神经网络——实现异或XOR (Python实现)
一、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实现)的更多相关文章
- BP神经网络求解异或问题(Python实现)
反向传播算法(Back Propagation)分二步进行,即正向传播和反向传播.这两个过程简述如下: 1.正向传播 输入的样本从输入层经过隐单元一层一层进行处理,传向输出层:在逐层处理的过程中.在输 ...
- 简单多层神经网络实现异或XOR
最近在看<Neural Network Design_Hagan> 然后想自己实现一个XOR 的网络. 由于单层神经网络不能将异或的判定分为两类. 根据 a^b=(a&~b)|(~ ...
- ANN神经网络——Sigmoid 激活函数编程练习 (Python实现)
# ---------- # # There are two functions to finish: # First, in activate(), write the sigmoid activa ...
- 目前所有的ANN神经网络算法大全
http://blog.sina.com.cn/s/blog_98238f850102w7ik.html 目前所有的ANN神经网络算法大全 (2016-01-20 10:34:17) 转载▼ 标签: ...
- 【机器学习】神经网络实现异或(XOR)
注:在吴恩达老师讲的[机器学习]课程中,最开始介绍神经网络的应用时就介绍了含有一个隐藏层的神经网络可以解决异或问题,而这是单层神经网络(也叫感知机)做不到了,当时就觉得非常神奇,之后就一直打算自己实现 ...
- OpenCV——ANN神经网络
ANN-- Artificial Neural Networks 人工神经网络 //定义人工神经网络 CvANN_MLP bp; // Set up BPNetwork's parameters Cv ...
- 神经网络BP算法C和python代码
上面只显示代码. 详BP原理和神经网络的相关知识,请参阅:神经网络和反向传播算法推导 首先是前向传播的计算: 输入: 首先为正整数 n.m.p.t,分别代表特征个数.训练样本个数.隐藏层神经元个数.输 ...
- 【xsy1147】 异或(xor) 可持久化trie
我的脑回路可能比较奇怪. 我们对这些询问离线,将所得序列${a}$的后缀和建$n$棵可持久化$trie$. 对于一组询问$(l,r,x)$,我们在主席树上询问第$l$棵树$-$第r$+1$棵树中与$s ...
- 图片异或(xor)getflag
题目地址:https://files.cnblogs.com/files/nul1/flag_enc.png.tar 这题是源于:网鼎杯minified 经过测试隧道红色最低通道异常.其余均正常.所以 ...
随机推荐
- element-ui table多选CheckBox参数解析
element-UI里的table表格与多选框CheckBox的组合很常用,官网也给了很多参数,自己总结了一下,方便日后使用 1.简易用法,没有附加的功能 要在表格里使用CheckBox很简单,只需设 ...
- prim和kruskal算法
//邻接矩阵 int n,G[MAXV][MAXN]; int d[MAXV];//表示到树的距离 bool vis[MAXV]={false}; int prim(){ fill(d,d+MAXV, ...
- oracle--等待事件
1. Buffer busy waits 从本质上讲,这个等待事件的产生仅说明了一个会话在等待一个Buffer(数据块),但是导致这个现象的原因却有很多种,常见的两种是:当一个会话试图修改一个数据块, ...
- LinuxShell脚本编程基础1-vi编辑器的使用
1.输入模式与命令模式的切换 按 [Esc]键 切换到 命令模式: 2.保存与退出 :w mytest.txt 保存文件名 :q 退出 :q! 强制退出 :wq 保存并退出 3.插入文本命令 i 在 ...
- 遇到Caused by: java.lang.NoClassDefFoundError: javax/validation/ParameterNameProvider
今天在做spring和hibernate整合的时候遇到这个问题 网上搜找到这里有解决办法 http://blog.csdn.net/jueshengtianya/article/details/122 ...
- 【随笔】win7下安装Apache服务器
1.首先下载64位的apache服务器httpd-2.4.10-win64.zip 2.解压,可以是默认路径,也可以自定义路径,我这里是E:/ 3.打开E:/Apache24/conf文件夹 4.随便 ...
- MacOS系统下的图形化工具
MacOS系统下的图形化工具 MacOS系统下安装了Git后,发现如果Git中有中文文档操作还是比较麻烦(需要输入中文的文件名).图形化对Git的操作还是相对于方便一些.所以准备找一个图形化的工具. ...
- Bash编程(6) String操作
1. 拼接 1) 简单的字符串拼接如:PATH=$PATH:$HOME/bin.如果拼接的字符串包含空格或特殊字符,需要使用双引号括起,如: var=$HOME/bin # 注释并不是赋值的一部分 v ...
- How to Install Eclipse C/C++ Development Tool--转
http://www3.ntu.edu.sg/home/ehchua/programming/howto/EclipseCpp_HowTo.html Eclipse 4.3 (Kepler) for ...
- bzoj 5369: [Pkusc2018]最大前缀和
Description 小C是一个算法竞赛爱好者,有一天小C遇到了一个非常难的问题:求一个序列的最大子段和. 但是小C并不会做这个题,于是小C决定把序列随机打乱,然后取序列的最大前缀和作为答案. 小C ...