python实现简单神经网络算法


Posted in Python onMarch 10, 2018

python实现简单神经网络算法,供大家参考,具体内容如下

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,0,1,1]]).T 
 
np.random.seed(1) 
 
#init weight value 
syn0 = 2*np.random.random((3,1))-1 
 
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
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 
 
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函数

python实现简单神经网络算法

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

python实现三层神经网络

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

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 
 
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
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 
 
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逐行读取文件内容的三种方法
Jan 20 Python
详解django三种文件下载方式
Apr 06 Python
python3 读写文件换行符的方法
Apr 09 Python
pandas apply 函数 实现多进程的示例讲解
Apr 20 Python
Python实现中一次读取多个值的方法
Apr 22 Python
Python实现查看系统启动项功能示例
May 10 Python
Python3爬虫之urllib携带cookie爬取网页的方法
Dec 28 Python
python项目对接钉钉SDK的实现
Jul 15 Python
妙用itchat! python实现久坐提醒功能
Nov 25 Python
Python+PyQt5实现灭霸响指功能
May 25 Python
Python requests及aiohttp速度对比代码实例
Jul 16 Python
OpenCV-Python实现油画效果的实例
Jun 08 Python
TensorFlow saver指定变量的存取
Mar 10 #Python
TensorFLow用Saver保存和恢复变量
Mar 10 #Python
tensorflow创建变量以及根据名称查找变量
Mar 10 #Python
Python2中文处理纪要的实现方法
Mar 10 #Python
python实现冒泡排序算法的两种方法
Mar 10 #Python
Python使用pyh生成HTML文档的方法示例
Mar 10 #Python
tensorflow获取变量维度信息
Mar 10 #Python
You might like
GD输出汉字的函数的分析
2006/10/09 PHP
setInterval 和 setTimeout会产生内存溢出
2008/02/15 Javascript
仿校内登陆框,精美,给那些很厉害但是没有设计天才的程序员
2008/11/24 Javascript
javascript下判断一个元素是否存在的代码
2010/03/05 Javascript
整理8个很棒的 jQuery 倒计时插件和教程
2011/12/12 Javascript
jquery获得页面元素的坐标值实现思路及代码
2013/04/15 Javascript
jsp+javascript打造级连菜单的实例代码
2013/06/14 Javascript
简单的JavaScript互斥锁分享
2014/02/02 Javascript
js获取当前页面的url网址信息
2014/06/12 Javascript
纯js模拟div层弹性运动的方法
2015/07/27 Javascript
深入解读JavaScript中的Iterator和for-of循环
2015/07/28 Javascript
js和jQuery设置Opacity半透明 兼容IE6
2016/05/24 Javascript
JavaScript仿淘宝页面图片滚动加载及刷新回顶部的方法解析
2016/05/24 Javascript
浅谈JS的基础类型与引用类型
2016/09/13 Javascript
浅析Node.js:DNS模块的使用
2016/11/23 Javascript
Jquery Easyui搜索框组件SearchBox使用详解(19)
2016/12/17 Javascript
AngularJS中$http使用的简单介绍
2017/03/17 Javascript
微信小程序支付及退款流程详解
2017/11/30 Javascript
详解Vue This$Store总结
2018/12/17 Javascript
ES6 async、await的基本使用方法示例
2020/06/06 Javascript
python网络编程实例简析
2014/09/26 Python
Python实现的双色球生成功能示例
2017/12/18 Python
详解python OpenCV学习笔记之直方图均衡化
2018/02/08 Python
Python批量发送post请求的实现代码
2018/05/05 Python
python Selenium实现付费音乐批量下载的实现方法
2019/01/24 Python
Python docx库用法示例分析
2019/02/16 Python
python之生产者消费者模型实现详解
2019/07/27 Python
Django中使用CORS实现跨域请求过程解析
2019/08/05 Python
Html5实现用户注册自动校验功能实例代码
2016/05/24 HTML / CSS
机电专业体育教师求职信
2013/09/21 职场文书
医生进修自我鉴定
2014/01/19 职场文书
消防安全月活动总结
2015/05/08 职场文书
2015年秋季运动会前导词
2015/07/20 职场文书
2016年习主席讲话学习心得体会
2016/01/20 职场文书
会议开幕致辞怎么写
2016/03/03 职场文书
Go标准容器之Ring的使用说明
2021/05/05 Golang