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任务调度实例分析
May 19 Python
python实现requests发送/上传多个文件的示例
Jun 04 Python
如何在Django中设置定时任务的方法示例
Jan 18 Python
Python常用模块之requests模块用法分析
May 15 Python
python opencv将图片转为灰度图的方法示例
Jul 31 Python
解决Django migrate不能发现app.models的表问题
Aug 31 Python
pycharm 2019 最新激活方式(pycharm破解、激活)
Sep 22 Python
python可视化text()函数使用详解
Feb 11 Python
django列表筛选功能的实现代码
Mar 27 Python
jenkins+python自动化测试持续集成教程
May 12 Python
python使用布隆过滤器的实现示例
Aug 20 Python
Python中Numpy和Matplotlib的基本使用指南
Nov 02 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
mysql中存储过程、函数的一些问题
2007/02/14 PHP
php学习笔记之面向对象编程
2012/12/29 PHP
php实现微信公众平台账号自定义菜单类
2014/12/02 PHP
PHP获取input输入框中的值去数据库比较显示出来
2016/11/16 PHP
基于yaf框架和uploadify插件,做的一个导入excel文件,查看并保存数据的功能
2017/01/24 PHP
PHP生成随机码的思路与方法实例探索
2019/04/11 PHP
Thinkphp5框架实现获取数据库数据到视图的方法
2019/08/14 PHP
Some tips of wmi scripting in jscript (1)
2007/04/03 Javascript
理解Javascript_02_理解undefined和null
2010/10/11 Javascript
JavaScript在多浏览器下for循环的使用方法
2012/11/07 Javascript
根据配置文件加载js依赖模块
2014/12/29 Javascript
jQuery表单美化插件jqTransform使用详解
2015/04/12 Javascript
详解jQuery插件开发方式
2016/11/22 Javascript
react-native ListView下拉刷新上拉加载实现代码
2017/08/03 Javascript
js实现导航跟随效果
2018/11/17 Javascript
JsonProperty 的使用方法详解
2019/10/11 Javascript
javascript实现鼠标点击生成文字特效
2019/12/24 Javascript
JavaScript实现简单的弹窗效果
2020/05/19 Javascript
[46:47]2014 DOTA2国际邀请赛中国区预选赛5.21 LGD-CDEC VS NE
2014/05/22 DOTA
[02:00]最后,我终于出了辉耀
2018/03/27 DOTA
python正则表达式的使用
2017/06/12 Python
python如何实现int函数的方法示例
2018/02/19 Python
浅谈python写入大量文件的问题
2018/11/09 Python
python定间隔取点(np.linspace)的实现
2019/11/27 Python
容易被忽略的Python内置类型
2020/09/03 Python
HTML5轻松实现全屏视频背景的示例
2018/04/23 HTML / CSS
美国鲜花递送:UrbanStems
2021/01/04 全球购物
Vinatis德国:法国领先的葡萄酒邮购公司
2020/09/07 全球购物
什么是事务?为什么需要事务?
2012/01/09 面试题
机械专业个人求职自荐信格式
2013/09/21 职场文书
应用电子专业学生的自我评价
2013/10/16 职场文书
大学班级计划书
2014/04/29 职场文书
竞选大队干部演讲稿
2014/09/11 职场文书
Nginx域名转发使用场景代码实例
2021/03/31 Servers
Jupyter notebook 输出部分显示不全的解决方案
2021/04/24 Python
OpenCV-Python实现怀旧滤镜与连环画滤镜
2021/06/09 Python