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

 更新时间:2018年01月26日 11:04:31   作者:Nani_xiao  
这篇文章主要介绍了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时,预测结果为:

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

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

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

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数据结构与算法教程》、《Python编码操作技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

相关文章

  • pytorch SummaryWriter保存日志的方法

    pytorch SummaryWriter保存日志的方法

    这篇文章主要介绍了pytorch SummaryWriter保存日志的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-03-03
  • python实现文件分片上传的接口自动化

    python实现文件分片上传的接口自动化

    这篇文章主要为大家详细介绍了python实现文件分片上传的接口自动化,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-11-11
  • python实现简单石头剪刀布游戏

    python实现简单石头剪刀布游戏

    这篇文章主要介绍了python实现简单石头剪刀布游戏,相信大家在童年或者生活中都玩过石头剪刀布这个游戏,这个游戏需要两个及以上的人。而今天,网上也实现了石头剪刀布的游戏。通过初步学习python,也学会了如何编写这个游戏。下面一起来看看详细内容吧
    2021-10-10
  • Python关于拓扑排序知识点讲解

    Python关于拓扑排序知识点讲解

    在本篇文章里小编给大家分享了一篇关于Python关于拓扑排序知识点讲解内容,有兴趣的朋友们可以学习下。
    2021-01-01
  • Python中的图像处理之Python图像平滑操作

    Python中的图像处理之Python图像平滑操作

    本文主要介绍在Python中调用OpenCV库对图像进行图像平滑滤波处理与图像加噪处理,如双边滤波,高斯双边滤波,图像加随机噪声等操作,对Python图像平滑操作感兴趣的朋友一起看看吧
    2022-06-06
  • python os.rename实例用法详解

    python os.rename实例用法详解

    在本篇文章里小编给大家整理的是一篇关于python os.rename实例用法详解内容,有需要的朋友们可以学习下。
    2020-12-12
  • pyecharts结合flask框架的使用

    pyecharts结合flask框架的使用

    这篇文章主要介绍了pyecharts结合flask框架,主要是介绍如何在Flask框架中使用pyecharts,本文通过示例代码给大家介绍的非常详细,需要的朋友可以参考下
    2022-06-06
  • Windows下为Python安装Matplotlib模块

    Windows下为Python安装Matplotlib模块

    这篇文章主要介绍了如何在Windows下为Python安装Matplotlib模块的方法,非常的详细,而且附上了官方的下载地址,小伙伴们操作起来应该毫无压力了。
    2015-11-11
  • Python NumPy 数组索引的示例详解

    Python NumPy 数组索引的示例详解

    数组索引是指使用方括号([])来索引数组值,numpy提供了比常规的python序列更多的索引工具,除了按整数和切片索引之外,数组可以由整数数组索引、布尔索引及花式索引,这篇文章主要介绍了Python NumPy 数组索引,需要的朋友可以参考下
    2023-01-01
  • Python采用socket模拟TCP通讯的实现方法

    Python采用socket模拟TCP通讯的实现方法

    这篇文章主要介绍了Python采用socket模拟TCP通讯的实现方法,程序分为TCP的server端与client端两部分,分别对这两部分进行了较为深入的分析,需要的朋友可以参考下
    2014-11-11

最新评论