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 time模块详解(常用函数实例讲解,非常好)
Apr 24 Python
手动实现把python项目发布为exe可执行程序过程分享
Oct 23 Python
Python通过命令开启http.server服务器的方法
Nov 04 Python
Python中py文件引用另一个py文件变量的方法
Apr 29 Python
python2 与 python3 实现共存的方法
Jul 12 Python
PyQt5+requests实现车票查询工具
Jan 21 Python
基于python traceback实现异常的获取与处理
Dec 13 Python
在Python中用GDAL实现矢量对栅格的切割实例
Mar 11 Python
Python调用接口合并Excel表代码实例
Mar 31 Python
浅谈pytorch中的BN层的注意事项
Jun 23 Python
Python学习之时间包使用教程详解
Mar 21 Python
python自动获取微信公众号最新文章的实现代码
Jul 15 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中的正规表达式(二)
2006/10/09 PHP
PHP中call_user_func_array回调函数的用法示例
2016/11/26 PHP
PHP反射基础知识回顾
2020/09/10 PHP
javascript 事件处理、鼠标拖动效果实现方法详解
2012/05/11 Javascript
JS阻止用户多次提交示例代码
2014/03/26 Javascript
详解JavaScript中的blink()方法的使用
2015/06/08 Javascript
Jquery Mobile 自定义按钮图标
2015/11/18 Javascript
使用jQuery的easydrag插件实现可拖动的DIV弹出框
2016/02/19 Javascript
JavaScript sort数组排序方法和自我实现排序方法小结
2016/06/06 Javascript
AngularJS 依赖注入详解和简单实例
2016/07/28 Javascript
JavaScript实战之菜单特效
2016/08/16 Javascript
Ionic2系列之使用DeepLinker实现指定页面URL
2016/11/21 Javascript
详解AngularJS controller调用factory
2017/05/19 Javascript
Angular4 中内置指令的基本用法
2017/07/31 Javascript
JS库之Waypoints的用法详解
2017/09/13 Javascript
解决vue项目nginx部署到非根目录下刷新空白的问题
2018/09/27 Javascript
vue实现PC端录音功能的实例代码
2019/06/05 Javascript
Vue实现购物车详情页面的方法
2019/08/20 Javascript
JSONP 的原理、理解 与 实例分析
2020/05/16 Javascript
python中尾递归用法实例详解
2015/04/28 Python
自动化Nginx服务器的反向代理的配置方法
2015/06/28 Python
python多进程共享变量
2016/04/06 Python
详解Python函数作用域的LEGB顺序
2016/05/14 Python
python回调函数中使用多线程的方法
2017/12/25 Python
Python使用requests发送POST请求实例代码
2018/01/25 Python
python中实现将多个print输出合成一个数组
2018/04/19 Python
Win10下用Anaconda安装TensorFlow(图文教程)
2020/06/18 Python
Html5实现单张、多张图片上传功能
2019/04/28 HTML / CSS
生态学毕业生自荐信
2013/10/27 职场文书
大学生自我鉴定书
2014/03/24 职场文书
机械电子工程专业求职信
2014/06/22 职场文书
2014年管理人员工作总结
2014/12/01 职场文书
合同纠纷调解书
2015/05/20 职场文书
新员工入职感想
2015/08/07 职场文书
JavaScript 与 TypeScript之间的联系
2021/11/27 Javascript
单机多实例部署 MySQL8.0.20
2022/05/15 MySQL