Python编程实现的简单神经网络算法示例


Posted in Python onJanuary 26, 2018

本文实例讲述了Python编程实现的简单神经网络算法。分享给大家供大家参考,具体如下:

python实现二层神经网络

包括输入层和输出层

# -*- coding:utf-8 -*-
#! python2
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,0,1,1]]).T
np.random.seed(1)
#init weight value
syn0 = 2*np.random.random((3,1))-1
print "三水点靠木测试结果:"
for iter in xrange(100000):
 l0 = x       #the first layer,and the input layer
 l1 = nonlin(np.dot(l0,syn0)) #the second layer,and the output layer
 l1_error = y-l1
 l1_delta = l1_error*nonlin(l1,True)
 syn0 += np.dot(l0.T, l1_delta)
print "outout after Training:"
print l1

这里,

l0:输入层
l1:输出层
syn0:初始权值
l1_error:误差
l1_delta:误差校正系数
func nonlin:sigmoid函数

这里迭代次数为100时,预测结果为

Python编程实现的简单神经网络算法示例

迭代次数为1000时,预测结果为:

Python编程实现的简单神经网络算法示例

迭代次数为10000,预测结果为:

Python编程实现的简单神经网络算法示例

迭代次数为100000,预测结果为:

Python编程实现的简单神经网络算法示例

可见迭代次数越多,预测结果越接近理想值,当时耗时也越长。

python实现三层神经网络

包括输入层、隐含层和输出层

# -*- coding:utf-8 -*-
#! python2
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
syn0 = 2*np.random.random((3,4)) - 1 #the first-hidden layer weight value
syn1 = 2*np.random.random((4,1)) - 1 #the hidden-output layer weight value
print "三水点靠木测试结果:"
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程序设计有所帮助。

Python 相关文章推荐
python使用PyFetion来发送短信的例子
Apr 22 Python
python中日期和时间格式化输出的方法小结
Mar 19 Python
Python StringIO模块实现在内存缓冲区中读写数据
Apr 08 Python
python从入门到精通(DAY 1)
Dec 20 Python
Python之py2exe打包工具详解
Jun 14 Python
python定时关机小脚本
Jun 20 Python
Python中时间datetime的处理与转换用法总结
Feb 18 Python
用python打印菱形的实操方法和代码
Jun 25 Python
python pytest进阶之xunit fixture详解
Jun 27 Python
Python使用__new__()方法为对象分配内存及返回对象的引用示例
Sep 20 Python
python自动下载图片的方法示例
Mar 25 Python
Python内置函数locals和globals对比
Apr 28 Python
Django使用httpresponse返回用户头像实例代码
Jan 26 #Python
Django rest framework基本介绍与代码示例
Jan 26 #Python
Python实现PS图像调整之对比度调整功能示例
Jan 26 #Python
Python实现PS滤镜特效之扇形变换效果示例
Jan 26 #Python
修复CentOS7升级Python到3.6版本后yum不能正确使用的解决方法
Jan 26 #Python
Python实现PS滤镜功能之波浪特效示例
Jan 26 #Python
Python使用pickle模块存储数据报错解决示例代码
Jan 26 #Python
You might like
腾讯微博提示missing parameter errorcode 102 错误的解决方法
2014/12/22 PHP
深入讲解PHP的Yii框架中的属性(Property)
2016/03/18 PHP
Laravel框架模板加载,分配变量及简单路由功能示例
2018/06/11 PHP
PHP session垃圾回收机制实例分析
2019/06/28 PHP
PHP使用 Pear 进行安装和卸载包的方法详解
2019/07/08 PHP
js获取dom的高度和宽度(可见区域及部分等等)
2013/06/13 Javascript
javascript获取当前鼠标坐标的方法
2015/01/10 Javascript
js钢琴按钮波浪式图片排列效果代码分享
2015/08/26 Javascript
webpack独立打包和缓存处理详解
2017/04/03 Javascript
深入浅出webpack教程系列_安装与基本打包用法和命令参数详解
2017/09/10 Javascript
Vue render深入开发讲解
2018/04/13 Javascript
element UI upload组件上传附件格式限制方法
2018/09/04 Javascript
JavaScript实现的3D旋转魔方动画效果实例代码
2019/07/31 Javascript
Websocket 向指定用户发消息的方法
2020/01/09 Javascript
python进阶教程之异常处理
2014/08/30 Python
Python中操作MySQL入门实例
2015/02/08 Python
python通过wxPython打开一个音频文件并播放的方法
2015/03/25 Python
Python使用OpenCV进行标定
2018/05/08 Python
Python sorted函数详解(高级篇)
2018/09/18 Python
python实现图片筛选程序
2018/10/24 Python
pytorch实现用CNN和LSTM对文本进行分类方式
2020/01/08 Python
浅谈Tensorflow 动态双向RNN的输出问题
2020/01/20 Python
python 多进程和协程配合使用写入数据
2020/10/30 Python
HTML5实现应用程序缓存(Application Cache)
2020/06/16 HTML / CSS
德国团购网站:Groupon德国
2018/03/13 全球购物
高级Java程序员面试要点
2013/08/02 面试题
《兰亭集序》教学反思
2014/02/11 职场文书
环境工程专业自荐信
2014/03/03 职场文书
一帮一活动总结
2014/05/08 职场文书
推荐信模板
2014/05/09 职场文书
建筑工地质量标语
2014/06/12 职场文书
银行开户授权委托书格式
2014/10/10 职场文书
2014教师年度工作总结
2014/11/10 职场文书
市场总监岗位职责
2015/02/11 职场文书
兴趣班停课通知
2015/04/24 职场文书
MySQL派生表联表查询实战过程
2022/03/20 MySQL