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为tornado添加recaptcha验证码功能
Feb 26 Python
Python3学习urllib的使用方法示例
Nov 29 Python
Python socket实现简单聊天室
Apr 01 Python
python验证码识别教程之利用投影法、连通域法分割图片
Jun 04 Python
python-opencv颜色提取分割方法
Dec 08 Python
pyspark操作MongoDB的方法步骤
Jan 04 Python
pyqt5 QProgressBar清空进度条的实例
Jun 21 Python
python @classmethod 的使用场合详解
Aug 23 Python
Python scrapy增量爬取实例及实现过程解析
Dec 24 Python
python 解决Fatal error in launcher:错误问题
May 21 Python
基于python requests selenium爬取excel vba过程解析
Aug 12 Python
python中编写函数并调用的知识点总结
Jan 13 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
PHP字符串 ==比较运算符的副作用
2009/10/21 PHP
thinkPHP的Html模板标签使用方法
2012/11/13 PHP
php中0,null,empty,空,false,字符串关系的详细介绍
2013/06/20 PHP
使用PHP如何实现高效安全的ftp服务器(一)
2015/12/20 PHP
Yii2实现让关联字段支持搜索功能的方法
2016/08/10 PHP
PHP实现会员账号单唯一登录的方法分析
2019/03/07 PHP
PHP中通过getopt解析GNU C风格命令行选项
2019/11/18 PHP
推荐自用 Javascript 缩图函数 (onDOMLoaded)……
2007/10/23 Javascript
一个简单的JS鼠标悬停特效具体方法
2013/06/17 Javascript
jquery五角星评分插件示例分享
2014/02/21 Javascript
node.js中Socket.IO的进阶使用技巧
2014/11/04 Javascript
jQuery中:file选择器用法实例
2015/01/04 Javascript
JS实现可展开折叠层的鼠标拖曳效果
2015/10/09 Javascript
Vue.js实现微信过渡动画左右切换效果
2017/06/13 Javascript
js input输入百分号保存数据库失败的解决方法
2018/05/26 Javascript
layui radio点击事件实现input显示和隐藏的例子
2019/09/02 Javascript
python实现对excel进行数据剔除操作实例
2017/12/07 Python
python使用Tkinter实现在线音乐播放器
2018/01/30 Python
使用Python向DataFrame中指定位置添加一列或多列的方法
2019/01/29 Python
解决Python正则表达式匹配反斜杠''\''问题
2019/07/17 Python
pytorch自定义初始化权重的方法
2019/08/17 Python
python自动化测试无法启动谷歌浏览器问题
2019/10/10 Python
基于python实现操作git过程代码解析
2020/07/27 Python
Jupyter Notebook添加代码自动补全功能的实现
2021/01/07 Python
如何用tempfile库创建python进程中的临时文件
2021/01/28 Python
Python绘制词云图之可视化神器pyecharts的方法
2021/02/23 Python
基于css3 animate制作绚丽的动画效果
2015/11/24 HTML / CSS
希腊香水和化妆品购物网站:Parfimo.gr
2019/10/03 全球购物
生物科学系大学生的自我评价
2013/12/20 职场文书
抗洪救灾先进集体事迹材料
2014/05/26 职场文书
入党积极分子对十八届四中全会期盼的思想汇报
2014/10/17 职场文书
2014保险公司内勤工作总结
2014/12/16 职场文书
离婚协议书范本
2015/01/26 职场文书
超市主管竞聘书
2015/09/15 职场文书
大学生村官驻村工作心得体会
2016/01/23 职场文书
Golang jwt身份认证
2022/04/20 Golang