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 17 Python
python实现ping的方法
Jul 06 Python
Python2与python3中 for 循环语句基础与实例分析
Nov 20 Python
python实现随机梯度下降(SGD)
Mar 24 Python
Python中垃圾回收和del语句详解
Nov 15 Python
利用python脚本如何简化jar操作命令
Feb 24 Python
python3利用Socket实现通信的方法示例
May 06 Python
python 下 CMake 安装配置 OPENCV 4.1.1的方法
Sep 30 Python
基于python3的socket聊天编程
Feb 17 Python
详解pandas获取Dataframe元素值的几种方法
Jun 14 Python
Python爬虫使用bs4方法实现数据解析
Aug 25 Python
Python matplotlib安装以及实现简单曲线的绘制
Apr 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
Terran魔法科技
2020/03/14 星际争霸
PHP通用检测函数集合
2006/11/25 PHP
谈谈PHP的输入输出流
2007/02/14 PHP
PHP开发框架总结收藏
2008/04/24 PHP
php入门学习知识点八 PHP中for循环基本应用之九九乘法口绝表
2011/07/14 PHP
destoon实现商铺管理主页设置增加新菜单的方法
2014/06/26 PHP
重定向实现代码
2006/11/20 Javascript
JavaScript的作用域和块级作用域概念理解
2014/09/21 Javascript
node.js从数据库获取数据
2016/05/08 Javascript
详解JavaScript设计模式开发中的桥接模式使用
2016/05/18 Javascript
微信小程序 框架详解及实例应用
2016/09/26 Javascript
详解jQuery插件开发方式
2016/11/22 Javascript
详解NodeJs支付宝移动支付签名及验签
2017/01/06 NodeJs
Angular 4 指令快速入门教程
2017/06/07 Javascript
Vue.js搭建移动端购物车界面
2020/06/28 Javascript
vue的列表交错过渡实现代码示例
2019/05/05 Javascript
python利用装饰器进行运算的实例分析
2015/08/04 Python
多版本Python共存的配置方法
2017/05/22 Python
python操作日志的封装方法(两种方法)
2019/05/23 Python
基于python生成英文版词云图代码实例
2020/05/16 Python
python实现文件+参数发送request的实例代码
2021/01/05 Python
Html5应用程序缓存(Cache manifest)
2018/06/04 HTML / CSS
TripAdvisor斯洛伐克:阅读评论、比较价格和酒店预订
2018/04/25 全球购物
ghd澳大利亚官方网站:英国最受欢迎的美发工具品牌
2018/05/21 全球购物
亚洲独特体验旅游专家:eOasia
2018/08/15 全球购物
Ray-Ban雷朋瑞典官方网站:全球领先的太阳眼镜品牌
2019/08/22 全球购物
到底Java是如何传递参数的?是by value或by reference?
2012/07/13 面试题
大学生实习感言
2014/01/16 职场文书
2015年商场工作总结
2015/04/27 职场文书
2015年话务员工作总结
2015/04/29 职场文书
2016党员学习《反对自由主义》心得体会
2016/01/22 职场文书
JVM上高性能数据格式库包Apache Arrow入门和架构详解(Gkatziouras)
2021/05/26 Servers
浅析MySQL如何实现事务隔离
2021/06/26 MySQL
MySQL七大JOIN的具体使用
2022/02/28 MySQL
python_tkinter事件类型详情
2022/03/20 Python
Python中 range | np.arange | np.linspace三者的区别
2022/03/22 Python