python实现简单神经网络算法

 更新时间:2018年03月10日 12:37:19   作者:由硬到软  
这篇文章主要为大家详细介绍了python实现简单神经网络算法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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实现三层神经网络

包括输入层、隐含层和输出层

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读取和保存视频文件

    这篇文章主要为大家详细介绍了python读取显示和保存视频文件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-04-04
  • Python中xml.etree.ElementTree的使用示例

    Python中xml.etree.ElementTree的使用示例

    ElementTree是Python标准库中的一个模块,专门用于处理XML文件,它提供了解析、创建、修改和遍历XML文档的API,非常适合处理配置文件、数据交换格式和Web服务响应等场景,本文就来介绍一下,感兴趣的可以了解一下
    2024-09-09
  • OpenCV 边缘检测

    OpenCV 边缘检测

    OpenCV提供了许多边缘检测滤波函数,这些滤波函数都会将非边缘区域转为黑色,将边缘区域转为白色或其他饱和的颜色。这篇文章主要介绍了OpenCV 边缘检测,需要的朋友可以参考下
    2019-07-07
  • python读取ini配置的类封装代码实例

    python读取ini配置的类封装代码实例

    这篇文章主要介绍了python读取ini配置的类封装代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-01-01
  • python中的属性管理机制详解

    python中的属性管理机制详解

    这篇文章主要介绍了python中的属性管理机制,主要包括私有属性和属性限制-__slots__方法,文中详细介绍了python中如何去声明变量的相关知识,需要的朋友可以参考下
    2022-06-06
  • java直接调用python脚本的例子

    java直接调用python脚本的例子

    有时需求使用JAVA直接调用python脚本,执行一些服务器监控的事情。 本文给出一个java直接调用python脚本的例子
    2014-02-02
  • 一起来学习一下python的数据类型

    一起来学习一下python的数据类型

    这篇文章主要为大家详细介绍了python的数据类型,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下希望能够给你带来帮助
    2022-01-01
  • python基础教程之简单入门说明(变量和控制语言使用方法)

    python基础教程之简单入门说明(变量和控制语言使用方法)

    这篇文章主要介绍了开始学习python的第一步需要知道的知识(变量和控制语言使用方法),需要的朋友可以参考下
    2014-03-03
  • Django 使用easy_thumbnails压缩上传的图片方法

    Django 使用easy_thumbnails压缩上传的图片方法

    今天小编就为大家分享一篇Django 使用easy_thumbnails压缩上传的图片方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-07-07
  • Python面向对象程序设计之类的定义与继承简单示例

    Python面向对象程序设计之类的定义与继承简单示例

    这篇文章主要介绍了Python面向对象程序设计之类的定义与继承,结合完整实例形式分析了Python面向对象程序设计中类的定义、调用、继承及相关操作注意事项,需要的朋友可以参考下
    2019-03-03

最新评论