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构建深度神经网络(DNN)
  • Python实现的三层BP神经网络算法示例
  • Python实现的径向基(RBF)神经网络示例
  • Python编程实现的简单神经网络算法示例
  • python实现神经网络感知器算法
  • python机器学习之神经网络(三)
  • python机器学习之神经网络(一)
  • Python实现感知器模型、两层神经网络
  • TensorFlow平台下Python实现神经网络
(0)

相关推荐

  • Python实现的径向基(RBF)神经网络示例

    本文实例讲述了Python实现的径向基(RBF)神经网络.分享给大家供大家参考,具体如下: from numpy import array, append, vstack, transpose, reshape, \ dot, true_divide, mean, exp, sqrt, log, \ loadtxt, savetxt, zeros, frombuffer from numpy.linalg import norm, lstsq from multiprocessing impor

  • python实现神经网络感知器算法

    现在我们用python代码实现感知器算法. # -*- coding: utf-8 -*- import numpy as np class Perceptron(object): """ eta:学习率 n_iter:权重向量的训练次数 w_:神经分叉权重向量 errors_:用于记录神经元判断出错次数 """ def __init__(self, eta=0.01, n_iter=2): self.eta = eta self.n_iter

  • python构建深度神经网络(DNN)

    本文学习Neural Networks and Deep Learning 在线免费书籍,用python构建神经网络识别手写体的一个总结. 代码主要包括两三部分: 1).数据调用和预处理 2).神经网络类构建和方法建立 3).代码测试文件 1)数据调用: #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2017-03-12 15:11 # @Author : CC # @File : net_load_data.py # @Soft

  • Python实现感知器模型、两层神经网络

    本文实例为大家分享了Python实现感知器模型.两层神经网络,供大家参考,具体内容如下 python 3.4 因为使用了 numpy 这里我们首先实现一个感知器模型来实现下面的对应关系 [[0,0,1], --- 0 [0,1,1], --- 1 [1,0,1], --- 0 [1,1,1]] --- 1 从上面的数据可以看出:输入是三通道,输出是单通道. 这里的激活函数我们使用 sigmoid 函数 f(x)=1/(1+exp(-x)) 其导数推导如下所示: L0=W*X; z=f(L0);

  • python机器学习之神经网络(三)

    前面两篇文章都是参考书本神经网络的原理,一步步写的代码,这篇博文里主要学习了如何使用neurolab库中的函数来实现神经网络的算法. 首先介绍一下neurolab库的配置: 选择你所需要的版本进行下载,下载完成后解压. neurolab需要采用python安装第三方软件包的方式进行安装,这里介绍一种安装方式: (1)进入cmd窗口 (2)进入解压文件所在目录下 (3)输入 setup.py install 这样,在python安装目录的Python27\Lib\site-packages下,就可

  • Python实现的三层BP神经网络算法示例

    本文实例讲述了Python实现的三层BP神经网络算法.分享给大家供大家参考,具体如下: 这是一个非常漂亮的三层反向传播神经网络的python实现,下一步我准备试着将其修改为多层BP神经网络. 下面是运行演示函数的截图,你会发现预测的结果很惊人! 提示:运行演示函数的时候,可以尝试改变隐藏层的节点数,看节点数增加了,预测的精度会否提升 import math import random import string random.seed(0) # 生成区间[a, b)内的随机数 def rand(

  • 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.

  • python机器学习之神经网络(一)

    python有专门的神经网络库,但为了加深印象,我自己在numpy库的基础上,自己编写了一个简单的神经网络程序,是基于Rosenblatt感知器的,这个感知器建立在一个线性神经元之上,神经元模型的求和节点计算作用于突触输入的线性组合,同时结合外部作用的偏置,对若干个突触的输入求和后进行调节.为了便于观察,这里的数据采用二维数据. 目标函数是训练结果的误差的平方和,由于目标函数是一个二次函数,只存在一个全局极小值,所以采用梯度下降法的策略寻找目标函数的最小值. 代码如下: import numpy

  • python构建深度神经网络(续)

    这篇文章在前一篇文章:python构建深度神经网络(DNN)的基础上,添加了一下几个内容: 1) 正则化项 2) 调出中间损失函数的输出 3) 构建了交叉损失函数 4) 将训练好的网络进行保存,并调用用来测试新数据 1  数据预处理 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2017-03-12 15:11 # @Author : CC # @File : net_load_data.py from numpy import

  • TensorFlow平台下Python实现神经网络

    本篇文章主要通过一个简单的例子来实现神经网络.训练数据是随机产生的模拟数据集,解决二分类问题. 下面我们首先说一下,训练神经网络的一般过程: 1.定义神经网络的结构和前向传播的输出结果 2.定义损失函数以及反向传播优化的算法 3.生成会话(Session)并且在训练数据上反复运行反向传播优化算法 要记住的一点是,无论神经网络的结构如何变化,以上三个步骤是不会改变的. 完整代码如下: import tensorflow as tf #导入TensorFlow工具包并简称为tf from numpy

随机推荐