python实现简单神经网络算法
作者:由硬到软 发布时间:2021-03-22 07:03:16
标签: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
来源:http://blog.csdn.net/stoneyyhit/article/details/52335468
0
投稿
猜你喜欢
- 今天看看那些知名的Logo,可能你没注意过,它们也是一直在变化,有的还挺雷人的!先从时尚的苹果开始吧,苹果也有土的时候。1976年那第一个L
- 代码如下:--Begin Index(索引) 分析优化的相关 Sql -- 返回当前数据库所有碎片率大于25%的索引 -- 运行
- PDO::beginTransactionPDO::beginTransaction 启动一个事务(PHP 5 >= 5.1.0, P
- 当我们经常在代码中使用 Python 字符串时,您可能需要以相反的顺序使用它们。Python 包含一些方便的工具和技术,可以在这些情况下为您
- 以下插件是我在项目中经常使用的jQuery插件,不见得是最好的,但是我目前接触到的jQuery插件中最适合我的。01. jQuery.Fle
- 本文实例为大家分享了OpenCV+face++实现实时人脸识别解锁功能的具体代码,供大家参考,具体内容如下1.背景最近做一个小东西,需要登录
- 如何自动更新导航栏?下面看看如何具体使用Content Linking组件: <&nbs
- TensorBoard是TensorFlow下的一个可视化的工具,能够帮助我们在训练大规模神经网络过程中出现的复杂且不好理解的运算。Tens
- 为了防止采集,我试过各种方法,绝大多数方法是“ * 剑”---防止了采集,也影响了搜索引擎收录,实在不爽!一天本人忽发奇想:何不
- 描述replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次
- 基于微信开放的个人号接口python库itchat,实现对微信好友的获取,并对省份、性别、微信签名做数据分析。效果:直接上代码,建三个空文本
- Flask是一个Python编写的Web 微框架,让我们可以使用Python语言快速实现一个网站或Web服务。本文参考自Flask官方文档,
- 做运维的朋友应该知道,公司IDC机房经常有上架、下架、报修和报废的服务器。如果服务器数量很多的时候很容易造成监控遗漏。  
- 本文讲述了Python检测网络延迟的代码。分享给大家供大家参考,具体如下:#!/usr/bin/env python # coding: u
- 需求:序列化外键,获取外键的除id之外的值使用Django REST Framework虽然开发接口快速,但是如果想要获取到除外键id值之外
- 一、摘要Python使用被称为异常 的特殊对象来管理程序执行期间发生的错误。每当发生让Python不知所措的错误时,它都会创建一个异常对象。
- 本文实例讲述了php隐藏IP地址后两位显示为星号的方法。分享给大家供大家参考。具体实现方法如下:我们在很多的公共网站中都会有碰到显示用户的I
- 一、创建矩阵的方法import numpy as np# 1直接创建mat=np.mat("1 2 3;4 5 6;7 8 9&q
- 今早无聊。。。7点起来突然想写个刷访问量的。。那就动手吧仅供测试,不建议刷访问量哦~~很简单的思路,第一步提取代理ip,第二步模拟访问。提取
- MySQL使用环境变量TMPDIR的值作为保存临时文件的目录的路径名。如果未设置TMPDIR,MySQL将使用系统的默认值,通常为/tmp、