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 相关文章推荐
详解Python3.1版本带来的核心变化
Apr 07 Python
Python中实现三目运算的方法
Jun 21 Python
python删除服务器文件代码示例
Feb 09 Python
PyQT实现多窗口切换
Apr 20 Python
Python使用googletrans报错的解决方法
Sep 25 Python
Python numpy中矩阵的基本用法汇总
Feb 12 Python
python实现kmp算法的实例代码
Apr 03 Python
详解python中自定义超时异常的几种方法
Jul 29 Python
解决Djang2.0.1中的reverse导入失败的问题
Aug 16 Python
Pytorch 的损失函数Loss function使用详解
Jan 02 Python
Pycharm2020.1安装无法启动问题即设置中文插件的方法
Aug 07 Python
解决pytorch 模型复制的一些问题
Mar 03 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合并两个数组的两种方式的异同
2012/09/14 PHP
解析PHP多种序列化与反序列化的方法
2013/06/06 PHP
jQuery提交表单ajax查询实例代码
2012/10/07 Javascript
THREE.JS入门教程(2)着色器-上
2013/01/24 Javascript
解析JavaScript中的标签语句
2013/06/19 Javascript
纯JavaScript实现的分页插件实例
2015/07/14 Javascript
javascript 判断两个日期之差的示例代码
2015/09/05 Javascript
怎么引入(调用)一个JS文件
2016/05/26 Javascript
详解JavaScript中this的指向问题
2017/01/20 Javascript
JavaScript实现前端分页控件
2017/04/19 Javascript
js中less常用的方法小结
2017/08/09 Javascript
nodejs中安装ghost出错的原因及解决方法
2017/10/23 NodeJs
VSCode 配置React Native开发环境的方法
2017/12/27 Javascript
vue-cli3 从搭建到优化的详细步骤
2019/01/20 Javascript
使用webpack/gulp构建TypeScript项目的方法示例
2019/12/18 Javascript
不依任何赖第三方,单纯用vue实现Tree 树形控件的案例
2020/09/21 Javascript
vue 页面跳转的实现方式
2021/01/12 Vue.js
[50:48]LGD vs CHAOS 2019国际邀请赛小组赛 BO2 第二场 8.15
2019/08/16 DOTA
对python同一个文件夹里面不同.py文件的交叉引用方法详解
2018/12/15 Python
自定义django admin model表单提交的例子
2019/08/23 Python
win10下opencv-python特定版本手动安装与pip自动安装教程
2020/03/05 Python
python实现飞机大战项目
2020/03/11 Python
Python threading.local代码实例及原理解析
2020/03/16 Python
使用python修改文件并立即写回到原始位置操作(inplace读写)
2020/06/28 Python
美国汽配连锁巨头Pep Boys官网:轮胎更换、汽车维修服务和汽车零部件
2017/01/14 全球购物
Vita Fede官网:在意大利手工制作,在纽约市设计
2019/10/25 全球购物
shell变量的作用空间是什么
2013/08/17 面试题
介绍一下Ruby中的对象,属性和方法
2012/07/11 面试题
医药专业应届毕业生求职信范文
2014/01/01 职场文书
客服部班长工作责任制
2014/02/25 职场文书
施工安全标语
2014/06/07 职场文书
民政局副局长民主生活会个人整改措施
2014/10/04 职场文书
乡镇群众路线专项整治方案
2014/11/03 职场文书
三方协议书
2015/01/27 职场文书
聘用合同范本
2015/09/21 职场文书
高中班长竞选稿
2015/11/20 职场文书