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


猜你喜欢
- python中使用.py配置文件 一、格式:创建一个config.py文件在文件中加配置:DEBUG=Truedm_connect = {
- Python实现AES算法密码学课程老师留的作业,我觉得用python实现更简单,就用python写了一个加解密的程序。程序分成三个部分,一
- 引言“ 这是MySQL系列笔记的第一篇,文章内容均为本人通过实践及查阅资料相关整理所得,可用作新手入门指南,或
- Git的工作方式分为集中式工作流、功能分支工作流、Gitflow工作流和Forking,其中集中式工作流和功能分支工作流是已经使用过的,Gi
- 数据集数据集为Barcelona某段时间内的气象数据,其中包括温度、湿度以及风速等。本文将利用CNN来对风速进行预测。特征构造对于风速的预测
- 本文实例为大家分享了pytorch实现手写数字图片识别的具体代码,供大家参考,具体内容如下数据集:MNIST数据集,代码中会自动下载,不用自
- 层的八条定律当然,这些并非真正的定律,而只是一些有益的忠告,使你免陷于使用层时可能的困顿中。原来有九条定律的,我们精简掉一条,还有下面的八条
- Web应用的发展,使得客户端存储使用得也越来越多,而实现客户端存储的方式则是多种多样。最简单而且兼容性最佳的方案是Cookie,但是作为真正
- 这篇文章主要介绍了如何在mac环境中用python处理protobuf,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学
- 前言本项目为IOT实验室人员签到考勤设计,系统实现功能:1.人员人脸识别并完成签到/签退2.考勤时间计算3.保存考勤数据为CSV格式(Exc
- 1、获取指定时间函数:date_format() 转换# 获取前一天时间的最大值SELECT date_format(CURRE
- 目录初始化程序创建Surface对象事件监听游戏循环Pygame 作为一个入门级的游戏开发库,其实并不难学,只要掌握 Python 编程的相
- QThread是Qt的线程类中最核心的底层类。由于PyQt的的跨平台特性,QThread要隐藏所有与平台相关的代码要使用的QThread开始
- idea激活码失效说明在2020.11.26,idea又迎来了一次大规模的更新,好多小伙伴发现idea激活码已经无法用了,显示需要重新激活,
- 1.1.propety动态属性在面向对象编程中,我们一般把名词性的东西映射成属性,动词性的东西映射成方法。在python中他们对应的分别是属
- 如下所示:import collectionsclass Mydict(collections.UserDict):def __missin
- 变量的种类在T-SQL中,变量按生存范围可以分为全局变量(Global Variable)和局部变量(Local Variable)全局变量
- 呵,以前也没考虑过这方面的东西,现在写的代码越来越多,越来越复杂,如果再不把不用的变量及时释放掉,到时肯定会出问题。今天无意中在无忧Q群里看
- 在项目文件中新建文件.env .env.pro 两个文件其中.env 是默认设置 .env.pro 为正式环境设置1、设置.env中的内容信
- 尽管数组在 Javascript 中是对象,但是不建议使用 for in 循环来遍历数组,实际上,有很多理由来阻止我们对数组使用 for i