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 可爱的大小写
Sep 06 Python
Python获取文件ssdeep值的方法
Oct 05 Python
使用SAE部署Python运行环境的教程
May 05 Python
python类继承用法实例分析
May 27 Python
Python简单调用MySQL存储过程并获得返回值的方法
Jul 20 Python
详解使用pymysql在python中对mysql的增删改查操作(综合)
Jan 18 Python
Python高级用法总结
May 26 Python
Python3.4 splinter(模拟填写表单)使用方法
Oct 13 Python
使用python实现unix2dos和dos2unix命令的例子
Aug 13 Python
Python openpyxl读取单元格字体颜色过程解析
Sep 03 Python
python实现高斯(Gauss)迭代法的例子
Nov 20 Python
python程序输出无内容的解决方式
Apr 09 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
删除无限分类并同时删除它下面的所有子分类的方法
2010/08/08 PHP
PHP按行读取文件时删除换行符的3种方法
2014/05/04 PHP
个人写的PHP验证码生成类分享
2014/08/21 PHP
深入理解PHP之OpCode原理详解
2016/06/01 PHP
List the Codec Files on a Computer
2007/06/18 Javascript
js的闭包的一个示例说明
2008/11/18 Javascript
实现图片预加载的三大方法及优缺点分析
2014/11/19 Javascript
jQuery 处理页面的事件详解
2015/01/20 Javascript
JavaScript提高网站性能优化的建议(二)
2016/07/24 Javascript
jquery uploadify如何取消已上传成功文件
2017/02/08 Javascript
详解Vue-cli代理解决跨域问题
2017/09/27 Javascript
ES6 javascript中Class类继承用法实例详解
2017/10/30 Javascript
js实现登录与注册界面
2017/11/01 Javascript
如何让你的JS代码更好看易读
2017/12/01 Javascript
解决vue-cli创建项目的loader问题
2018/03/13 Javascript
js中int和string数据类型互相转化实例
2019/01/16 Javascript
JavaScript JSON数据处理全集(小结)
2019/08/15 Javascript
Pycharm远程调试openstack的方法
2017/11/21 Python
python3 发送任意文件邮件的实例
2018/01/23 Python
使用python Fabric动态修改远程机器hosts的方法
2018/10/26 Python
python 使用pandas计算累积求和的方法
2019/02/08 Python
Python使用到第三方库PyMuPDF图片与pdf相互转换
2019/05/03 Python
Python利用PyPDF2库获取PDF文件总页码实例
2020/04/03 Python
Python如何设置指定窗口为前台活动窗口
2020/08/12 Python
anaconda升级sklearn版本的实现方法
2021/02/22 Python
HTML5触摸事件演化tap事件介绍
2016/03/25 HTML / CSS
小学生自我鉴定
2013/10/12 职场文书
应聘医药代表职位求职信
2013/10/21 职场文书
冰淇淋店创业计划书范文
2013/12/27 职场文书
请假条标准格式规范
2014/04/10 职场文书
招标授权委托书样本
2014/09/23 职场文书
大雁塔英文导游词
2015/02/10 职场文书
安全守法证明
2015/06/23 职场文书
大学校园招聘会感想
2015/08/10 职场文书
如何使用pdb进行Python调试
2021/06/30 Python
python开发的自动化运维工具ansible详解
2021/08/07 Python