Python编程实现的简单神经网络算法示例

作者:Nani_xiao 时间:2022-01-06 05:53:17 

本文实例讲述了Python编程实现的简单神经网络算法。分享给大家供大家参考,具体如下:

python实现二层神经网络

包括输入层和输出层


# -*- coding:utf-8 -*-
#! python2
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
print "脚本之家测试结果:"
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函数

这里迭代次数为100时,预测结果为

Python编程实现的简单神经网络算法示例

迭代次数为1000时,预测结果为:

Python编程实现的简单神经网络算法示例

迭代次数为10000,预测结果为:

Python编程实现的简单神经网络算法示例

迭代次数为100000,预测结果为:

Python编程实现的简单神经网络算法示例

可见迭代次数越多,预测结果越接近理想值,当时耗时也越长。

python实现三层神经网络

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


# -*- coding:utf-8 -*-
#! python2
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
print "脚本之家测试结果:"
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程序设计有所帮助。

来源:http://blog.csdn.net/xiao_lxl/article/details/51721312

标签:Python,神经网络,算法
0
投稿

猜你喜欢

  • Python xpath表达式如何实现数据处理

    2021-09-02 22:07:15
  • asp删除mssql数据库中没有记录的图片代码

    2011-03-11 11:22:00
  • 如何动态在文档中加入<script></script>写入大段js?

    2010-07-02 13:17:00
  • asp 各种进制转换函数

    2008-06-24 12:35:00
  • 如何确定Oracle数据库表重复的记录

    2010-07-26 13:23:00
  • jquery 使用点滴函数代码

    2011-05-21 16:12:00
  • 网页设计技巧:相对路径与绝对路径的区别问题

    2008-03-04 10:12:00
  • 浅谈python str.format与制表符\\t关于中文对齐的细节问题

    2023-05-23 00:39:56
  • 利用python实现简单的循环购物车功能示例代码

    2021-05-12 14:52:58
  • Python的垃圾回收机制详解

    2023-06-03 16:03:24
  • Python实现动态条形图绘制的示例代码

    2021-09-25 03:05:17
  • 发布淘宝开源编辑器 KISSY Editor 1.0.0

    2009-10-27 16:20:00
  • python 实现"神经衰弱"翻牌游戏

    2023-02-08 05:34:05
  • 在Python的Django框架中编写编译函数

    2022-01-04 16:53:34
  • 用python爬取分析淘宝商品信息详解技术篇

    2022-09-12 23:40:54
  • 基于Python实现二维图像双线性插值

    2023-08-13 07:46:45
  • php实现的支持断点续传的文件下载类

    2023-11-23 16:52:19
  • 利用ASP远程注册DLL的方法

    2008-03-05 13:00:00
  • Python 中如何将十六进制转换为 Base64

    2022-09-07 01:20:14
  • Django之PopUp的具体实现方法

    2023-03-26 13:04:54
  • asp之家 网络编程 m.aspxhome.com