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创建文件和追加文件内容实例
Oct 21 Python
wxpython中Textctrl回车事件无效的解决方法
Jul 21 Python
详解python的数字类型变量与其方法
Nov 20 Python
Linux下python3.6.1环境配置教程
Sep 26 Python
Django+Xadmin构建项目的方法步骤
Mar 06 Python
python3反转字符串的3种方法(小结)
Nov 07 Python
使用pyshp包进行shapefile文件修改的例子
Dec 06 Python
python 读写文件包含多种编码格式的解决方式
Dec 20 Python
Python基础之高级变量类型实例详解
Jan 03 Python
scrapy-redis分布式爬虫的搭建过程(理论篇)
Sep 29 Python
Python3.10的一些新特性原理分析
Sep 15 Python
Python实现自动玩连连看的脚本分享
Apr 04 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利用MySQL保存session的实现思路及示例代码
2014/09/09 PHP
Yii数据模型中rules类验证器用法分析
2016/07/15 PHP
PHP chop()函数讲解
2019/02/11 PHP
fix-ie5.js扩展在IE5下不能使用的几个方法
2007/08/20 Javascript
为原生js Array增加each方法
2012/04/07 Javascript
一行代码实现纯数据json对象的深度克隆实现思路
2013/01/09 Javascript
判断某个字符在一个字符串中是否存在的js代码
2014/02/28 Javascript
浅谈Javascript中Object与Function对象
2015/09/26 Javascript
编写高性能Javascript代码的N条建议
2015/10/12 Javascript
JS原型、原型链深入理解
2016/02/27 Javascript
JavaScript仿淘宝页面图片滚动加载及刷新回顶部的方法解析
2016/05/24 Javascript
Jquery+Ajax+xml实现中国地区选择三级联动菜单效果(推荐)
2017/06/09 jQuery
JS判断数组那点事
2017/10/10 Javascript
weebox弹出窗口不居中显示的解决方法
2017/11/27 Javascript
vue 使用Jade模板写html,stylus写css的方法
2018/02/23 Javascript
webpack4 入门最简单的例子介绍
2018/09/05 Javascript
javascript Canvas动态粒子连线
2020/01/01 Javascript
[01:12:35]Spirit vs Navi Supermajor小组赛 A组败者组第一轮 BO3 第二场 6.2
2018/06/03 DOTA
利用Python实现简单的相似图片搜索的教程
2015/04/23 Python
Python+PIL实现支付宝AR红包
2018/02/09 Python
浅谈pandas用groupby后对层级索引levels的处理方法
2018/11/06 Python
python获取本机所有IP地址的方法
2018/12/26 Python
Python装饰器语法糖
2019/01/02 Python
Python入门Anaconda和Pycharm的安装和配置详解
2019/07/16 Python
django实现更改数据库某个字段以及字段段内数据
2020/03/31 Python
opencv 实现特定颜色线条提取与定位操作
2020/06/02 Python
Lombok插件安装(IDEA)及配置jar包使用详解
2020/11/04 Python
CSS3,线性渐变(linear-gradient)的使用总结
2017/01/09 HTML / CSS
英国领先的大码时装品牌之一:Elvi
2018/08/26 全球购物
材料物理专业大学毕业生求职信
2013/10/15 职场文书
会计专业毕业生求职信
2014/07/04 职场文书
个人维稳承诺书
2015/05/04 职场文书
2015年财政局工作总结
2015/05/21 职场文书
创业计划书之旅游网站
2019/09/06 职场文书
golang interface判断为空nil的实现代码
2021/04/24 Golang
使用jpa之动态插入与修改(重写save)
2021/11/23 Java/Android