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模块学习 datetime介绍
Aug 27 Python
Python字符串格式化
Jun 15 Python
深入解析Python设计模式编程中建造者模式的使用
Mar 02 Python
Python的装饰器用法学习笔记
Jun 24 Python
wxpython实现图书管理系统
Mar 12 Python
查看django版本的方法分享
May 14 Python
对python 调用类属性的方法详解
Jul 02 Python
python 使用pdfminer3k 读取PDF文档的例子
Aug 27 Python
Python3如何对urllib和urllib2进行重构
Nov 25 Python
基于MSELoss()与CrossEntropyLoss()的区别详解
Jan 02 Python
Python 如何在字符串中插入变量
Aug 01 Python
python实现sm2和sm4国密(国家商用密码)算法的示例
Sep 26 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/12/18 PHP
用PHP将网址字符串转换成超链接(网址或email)
2010/05/25 PHP
一个基于PDO的数据库操作类
2011/03/24 PHP
PHP中全面阻止SQL注入式攻击分析小结
2012/01/30 PHP
PHP 函数call_user_func和call_user_func_array用法详解
2014/03/02 PHP
如何让thinkphp在模型中自动完成session赋值小教程
2014/09/05 PHP
php版微信公众号自定义分享内容实现方法
2016/09/22 PHP
PHP判断表达式中括号是否匹配的简单实例
2016/10/22 PHP
PHP中类型转换 ,常量,系统常量,魔术常量的详解
2017/10/26 PHP
PHP实现双链表删除与插入节点的方法示例
2017/11/11 PHP
PHP文件类型检查及fileinfo模块安装使用详解
2019/05/09 PHP
jQuery 表格插件整理
2010/04/27 Javascript
禁止iframe脚本弹出的窗口覆盖了父窗口的方法
2014/09/06 Javascript
js读取json的两种常用方法示例介绍
2014/10/19 Javascript
input输入框鼠标焦点提示信息
2015/03/17 Javascript
基于jQuery仿淘宝产品图片放大镜特效
2020/10/19 Javascript
boostrapTable的refresh和refreshOptions区别浅析
2017/01/22 Javascript
JS实现最简单的冒泡排序算法
2017/02/15 Javascript
基于JavaScript实现图片剪切效果
2017/03/07 Javascript
laravel-admin 与 vue 结合使用实例代码详解
2019/06/04 Javascript
在Vue中创建可重用的 Transition的方法
2020/06/02 Javascript
Python求离散序列导数的示例
2019/07/10 Python
在OpenCV里实现条码区域识别的方法示例
2019/12/04 Python
欧舒丹比利时官网:L’OCCITANE比利时
2017/04/25 全球购物
雅诗兰黛旗下专业男士保养领导品牌:Lab Series
2017/05/15 全球购物
澳大利亚领先的宠物用品商店:VetSupply
2017/09/08 全球购物
Marriott国际:万豪国际酒店查询预订
2017/09/25 全球购物
护士的岗位职责
2013/12/04 职场文书
医学生职业生涯规划书范文
2014/03/13 职场文书
喜之郎果冻广告词
2014/03/20 职场文书
幸福家庭标语
2014/06/27 职场文书
护士找工作求职信
2014/07/02 职场文书
财务统计员岗位职责
2015/04/14 职场文书
2015年班主任德育工作总结
2015/05/21 职场文书
小学校长开学致辞
2015/07/29 职场文书
CSS的class与id常用的命名规则
2021/05/18 HTML / CSS