Python实现感知器模型、两层神经网络


Posted in Python onDecember 19, 2017

本文实例为大家分享了Python实现感知器模型、两层神经网络,供大家参考,具体内容如下

python 3.4 因为使用了 numpy

这里我们首先实现一个感知器模型来实现下面的对应关系

[[0,0,1], ——- 0
[0,1,1], ——- 1
[1,0,1], ——- 0
[1,1,1]] ——- 1

从上面的数据可以看出:输入是三通道,输出是单通道。

Python实现感知器模型、两层神经网络

这里的激活函数我们使用 sigmoid 函数 f(x)=1/(1+exp(-x))

其导数推导如下所示:

Python实现感知器模型、两层神经网络

L0=W*X;
z=f(L0);
error=y-z;
delta =error * f'(L0) * X;
W=W+delta;

python 代码如下:

import numpy as np

#sigmoid function

def nonlin(x, deriv = False):
  if(deriv==True):
    return x*(1-x)
  return 1/(1+np.exp(-x))


# input dataset

X=np.array([[0,0,1],
      [0,1,1],
      [1,0,1],
      [1,1,1]])

# output dataset

y=np.array([[0,1,0,1]]).T

#seed( ) 用于指定随机数生成时所用算法开始的整数值,
#如果使用相同的seed( )值,则每次生成的随即数都相同,
#如果不设置这个值,则系统根据时间来自己选择这个值,
#此时每次生成的随机数因时间差异而不同。
np.random.seed(1)  

# init weight value with mean 0

syn0 = 2*np.random.random((3,1))-1   

for iter in range(1000):
  # forward propagation
  L0=X
  L1=nonlin(np.dot(L0,syn0))

  # error
  L1_error=y-L1

  L1_delta = L1_error*nonlin(L1,True)

  # updata weight
  syn0+=np.dot(L0.T,L1_delta)

print("Output After Training:")
print(L1)

从输出结果可以看出基本实现了对应关系。

下面再用两层网络来实现上面的任务,这里加了一个隐层,隐层包含4个神经元。

import numpy as np

def nonlin(x, deriv = False):
  if(deriv == True):
    return x*(1-x)
  else:
    return 1/(1+np.exp(-x))

#input dataset
X = np.array([[0,0,1],
       [0,1,1],
       [1,0,1],
       [1,1,1]])

#output dataset
y = np.array([[0,1,1,0]]).T

#the first-hidden layer weight value
syn0 = 2*np.random.random((3,4)) - 1 

#the hidden-output layer weight value
syn1 = 2*np.random.random((4,1)) - 1 

for j in range(60000):
  l0 = X      
  #the first layer,and the input layer 
  l1 = nonlin(np.dot(l0,syn0)) 
  #the second layer,and the hidden layer
  l2 = nonlin(np.dot(l1,syn1)) 
  #the third layer,and the output layer


  l2_error = y-l2    
  #the hidden-output layer error

  if(j%10000) == 0:
    print "Error:"+str(np.mean(l2_error))

  l2_delta = l2_error*nonlin(l2,deriv = True)

  l1_error = l2_delta.dot(syn1.T)   
  #the first-hidden layer error

  l1_delta = l1_error*nonlin(l1,deriv = True)

  syn1 += l1.T.dot(l2_delta)
  syn0 += l0.T.dot(l1_delta)

print "outout after Training:"
print l2

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python基础教程之实现石头剪刀布游戏示例
Feb 11 Python
python+django加载静态网页模板解析
Dec 12 Python
Python实现监控Nginx配置文件的不同并发送邮件报警功能示例
Feb 26 Python
值得收藏的10道python 面试题
Apr 15 Python
对Python中一维向量和一维向量转置相乘的方法详解
Aug 26 Python
python爬虫爬取笔趣网小说网站过程图解
Nov 18 Python
python GUI库图形界面开发之PyQt5中QWebEngineView内嵌网页与Python的数据交互传参详细方法实例
Feb 26 Python
python实现引用其他路径包里面的模块
Mar 09 Python
MATLAB数学建模之画图汇总
Jul 16 Python
使用python把xmind转换成excel测试用例的实现代码
Oct 12 Python
python 基于selenium实现鼠标拖拽功能
Dec 24 Python
Python绘制分类图的方法
Apr 20 Python
python实现感知器
Dec 19 #Python
python绘制简单折线图代码示例
Dec 19 #Python
matplotlib设置legend图例代码示例
Dec 19 #Python
matplotlib中legend位置调整解析
Dec 19 #Python
python实现感知器算法详解
Dec 19 #Python
python绘制条形图方法代码详解
Dec 19 #Python
Python实现两款计算器功能示例
Dec 19 #Python
You might like
简单介绍PHP的责任链编程模式
2015/08/11 PHP
php版微信公众号自定义分享内容实现方法
2016/09/22 PHP
一些常用的Javascript函数
2006/12/22 Javascript
JS字符串函数扩展代码
2011/09/13 Javascript
编写针对IE的JS代码两种编写方法
2013/01/30 Javascript
jQuery 中$(this).index与$.each的使用指南
2014/11/20 Javascript
简介JavaScript中fixed()方法的使用
2015/06/08 Javascript
BootStrap的table表头固定tbody滚动的实例代码
2016/08/24 Javascript
jQuery插件ContextMenu自定义图标
2017/03/15 Javascript
详解Angular2响应式表单
2017/06/14 Javascript
js实现rem自动匹配计算font-size的示例
2017/11/18 Javascript
微信小程序实现列表页的点赞和取消点赞功能
2018/11/02 Javascript
vue  directive定义全局和局部指令及指令简写
2018/11/20 Javascript
详解vue引入子组件方法
2019/02/12 Javascript
js实现计时器秒表功能
2019/12/16 Javascript
python实现的各种排序算法代码
2013/03/04 Python
python实现简单的TCP代理服务器
2014/10/08 Python
Python使用time模块实现指定时间触发器示例
2017/05/18 Python
在pycharm 中添加运行参数的操作方法
2019/01/19 Python
python 猴子补丁(monkey patch)
2019/06/26 Python
python调用pyaudio使用麦克风录制wav声音文件的教程
2019/06/26 Python
django mysql数据库及图片上传接口详解
2019/07/18 Python
Python 获取项目根路径的代码
2019/09/27 Python
英国标志性奢侈品牌:Burberry
2016/07/28 全球购物
俄罗斯优惠券网站:BIGLION
2017/05/21 全球购物
ONLY瑞典官网:世界知名服装品牌
2018/06/19 全球购物
美国瑜伽服装和装备购物网站:Mukha Yoga
2019/02/22 全球购物
家居设计专业个人自荐信范文
2013/11/26 职场文书
金融行业务员的自我评价
2013/12/13 职场文书
九年级化学教学反思
2014/01/28 职场文书
中介业务员岗位职责
2014/04/09 职场文书
施工安全汇报材料
2014/08/17 职场文书
品牌推广活动策划方案
2014/08/19 职场文书
煤矿安全协议书
2014/08/20 职场文书
本科毕业论文致谢词
2015/05/14 职场文书
Python用any()函数检查字符串中的字母以及如何使用all()函数
2022/04/14 Python