Python编程实现的简单神经网络算法示例编程实现的简单神经网络算法示例
主要介绍了Python编程实现的简单神经网络算法,结合实例形式分析了神经网络算法的原理及Python相关算法实
现技巧,需要的朋友可以参考下
本文实例讲述了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时,预测结果为
迭代次数为1000时,预测结果为:
评论0