Python实现人工神经网络(ANN)基本API

系统 2318 0
            
              
                # 简单的人工神经网络(ANN)设计
              
              

```python

              
                import
              
               numpy 
              
                as
              
               np

              
                import
              
               matplotlib
              
                .
              
              pyplot 
              
                as
              
               mp

              
                class
              
              
                ANNModel
              
              
                (
              
              
                )
              
              
                :
              
              
                def
              
              
                __init__
              
              
                (
              
              self
              
                )
              
              
                :
              
              
                # 随机初始化权重[-1 1)
              
              
	    self
              
                .
              
              w0 
              
                =
              
              
                2
              
              
                *
              
               np
              
                .
              
              random
              
                .
              
              random
              
                (
              
              
                (
              
              
                2
              
              
                ,
              
              
                4
              
              
                )
              
              
                )
              
              
                -
              
              
                1
              
              
	    self
              
                .
              
              w1 
              
                =
              
              
                2
              
              
                *
              
               np
              
                .
              
              random
              
                .
              
              random
              
                (
              
              
                (
              
              
                4
              
              
                ,
              
              
                1
              
              
                )
              
              
                )
              
              
                -
              
              
                1
              
              
                # 学习率
              
              
	    self
              
                .
              
              lrate 
              
                =
              
              
                0.1
              
              
                # sigmiod 函数
              
              
                def
              
              
                active
              
              
                (
              
              self
              
                ,
              
               x
              
                )
              
              
                :
              
              
                return
              
              
                1
              
              
                /
              
              
                (
              
              
                1
              
              
                +
              
               np
              
                .
              
              exp
              
                (
              
              
                -
              
              x
              
                )
              
              
                )
              
              
                # sigmoid函数导函数
              
              
                def
              
              
                backward
              
              
                (
              
              self
              
                ,
              
               x
              
                )
              
              
                :
              
              
                return
              
               x 
              
                *
              
              
                (
              
              
                1
              
              
                -
              
               x
              
                )
              
              
                # 单层网路前向传播
              
              
                def
              
              
                forward
              
              
                (
              
              self
              
                ,
              
               x
              
                ,
              
               w
              
                )
              
              
                :
              
              
                return
              
               np
              
                .
              
              dot
              
                (
              
              x
              
                ,
              
               w
              
                )
              
              
                def
              
              
                fit
              
              
                (
              
              self
              
                ,
              
               x
              
                )
              
              
                :
              
              
                for
              
               j 
              
                in
              
              
                range
              
              
                (
              
              
                10000
              
              
                )
              
              
                :
              
              
            l0 
              
                =
              
               x
            l1 
              
                =
              
               self
              
                .
              
              active
              
                (
              
              self
              
                .
              
              forward
              
                (
              
              l0
              
                ,
              
               self
              
                .
              
              w0
              
                )
              
              
                )
              
              
            l2 
              
                =
              
               self
              
                .
              
              active
              
                (
              
              self
              
                .
              
              forward
              
                (
              
              l1
              
                ,
              
               self
              
                .
              
              w1
              
                )
              
              
                )
              
              
                # 损失
              
              
            l2_error 
              
                =
              
               y 
              
                -
              
               l2
            
              
                if
              
              
                (
              
              j 
              
                %
              
              
                100
              
              
                )
              
              
                ==
              
              
                0
              
              
                :
              
              
                print
              
              
                (
              
              
                "Error:"
              
              
                +
              
              
                str
              
              
                (
              
              np
              
                .
              
              mean
              
                (
              
              np
              
                .
              
              
                abs
              
              
                (
              
              l2_error
              
                )
              
              
                )
              
              
                )
              
              
                )
              
              
            l2_delta 
              
                =
              
               l2_error 
              
                *
              
               self
              
                .
              
              backward
              
                (
              
              l2
              
                )
              
              
            self
              
                .
              
              w1 
              
                +=
              
               l1
              
                .
              
              T
              
                .
              
              dot
              
                (
              
              l2_delta 
              
                *
              
               self
              
                .
              
              lrate
              
                )
              
              
            l1_error 
              
                =
              
               l2_delta
              
                .
              
              dot
              
                (
              
              self
              
                .
              
              w1
              
                .
              
              T
              
                )
              
              
            l1_delta 
              
                =
              
               l1_error 
              
                *
              
               self
              
                .
              
              backward
              
                (
              
              l1
              
                )
              
              
            self
              
                .
              
              w0 
              
                +=
              
               l0
              
                .
              
              T
              
                .
              
              dot
              
                (
              
              l1_delta 
              
                *
              
               self
              
                .
              
              lrate
              
                )
              
              
                def
              
              
                predict
              
              
                (
              
              self
              
                ,
              
               x
              
                )
              
              
                :
              
              
        l0 
              
                =
              
               x
        l1 
              
                =
              
               self
              
                .
              
              active
              
                (
              
              self
              
                .
              
              forward
              
                (
              
              l0
              
                ,
              
               self
              
                .
              
              w0
              
                )
              
              
                )
              
              
        l2 
              
                =
              
               self
              
                .
              
              active
              
                (
              
              self
              
                .
              
              forward
              
                (
              
              l1
              
                ,
              
               self
              
                .
              
              w1
              
                )
              
              
                )
              
              
        result 
              
                =
              
               np
              
                .
              
              zeros_like
              
                (
              
              l2
              
                )
              
              
        result
              
                [
              
              l2
              
                >
              
              
                0.5
              
              
                ]
              
              
                =
              
              
                1
              
              
                return
              
               result

x 
              
                =
              
               np
              
                .
              
              array
              
                (
              
              
                [
              
              
                [
              
              
                3
              
              
                ,
              
              
                1
              
              
                ]
              
              
                ,
              
              
                [
              
              
                2
              
              
                ,
              
              
                5
              
              
                ]
              
              
                ,
              
              
                [
              
              
                1
              
              
                ,
              
              
                8
              
              
                ]
              
              
                ,
              
              
                [
              
              
                6
              
              
                ,
              
              
                4
              
              
                ]
              
              
                ,
              
              
                [
              
              
                5
              
              
                ,
              
              
                2
              
              
                ]
              
              
                ,
              
              
                [
              
              
                3
              
              
                ,
              
              
                5
              
              
                ]
              
              
                ,
              
              
                [
              
              
                4
              
              
                ,
              
              
                7
              
              
                ]
              
              
                ,
              
              
                [
              
              
                4
              
              
                ,
              
              
                -
              
              
                1
              
              
                ]
              
              
                ]
              
              
                )
              
              
y 
              
                =
              
               np
              
                .
              
              array
              
                (
              
              
                [
              
              
                0
              
              
                ,
              
              
                1
              
              
                ,
              
              
                1
              
              
                ,
              
              
                0
              
              
                ,
              
              
                0
              
              
                ,
              
              
                1
              
              
                ,
              
              
                1
              
              
                ,
              
              
                0
              
              
                ]
              
              
                )
              
              
                .
              
              reshape
              
                (
              
              
                -
              
              
                1
              
              
                ,
              
              
                1
              
              
                )
              
              
n 
              
                =
              
              
                500
              
              
l
              
                ,
              
               r 
              
                =
              
               x
              
                [
              
              
                :
              
              
                ,
              
              
                0
              
              
                ]
              
              
                .
              
              
                min
              
              
                (
              
              
                )
              
              
                -
              
              
                1
              
              
                ,
              
               x
              
                [
              
              
                :
              
              
                ,
              
              
                0
              
              
                ]
              
              
                .
              
              
                max
              
              
                (
              
              
                )
              
              
                +
              
              
                1
              
              
b
              
                ,
              
               t 
              
                =
              
               x
              
                [
              
              
                :
              
              
                ,
              
              
                1
              
              
                ]
              
              
                .
              
              
                min
              
              
                (
              
              
                )
              
              
                -
              
              
                1
              
              
                ,
              
               x
              
                [
              
              
                :
              
              
                ,
              
              
                1
              
              
                ]
              
              
                .
              
              
                max
              
              
                (
              
              
                )
              
              
                +
              
              
                1
              
              
grid_x 
              
                =
              
               np
              
                .
              
              meshgrid
              
                (
              
              np
              
                .
              
              linspace
              
                (
              
              l
              
                ,
              
               r
              
                ,
              
               n
              
                )
              
              
                ,
              
              np
              
                .
              
              linspace
              
                (
              
              b
              
                ,
              
               t
              
                ,
              
               n
              
                )
              
              
                )
              
              
flat_x 
              
                =
              
               np
              
                .
              
              column_stack
              
                (
              
              
                (
              
              grid_x
              
                [
              
              
                0
              
              
                ]
              
              
                .
              
              ravel
              
                (
              
              
                )
              
              
                ,
              
               grid_x
              
                [
              
              
                1
              
              
                ]
              
              
                .
              
              ravel
              
                (
              
              
                )
              
              
                )
              
              
                )
              
              
model 
              
                =
              
               ANNModel
              
                (
              
              
                )
              
              
model
              
                .
              
              fit
              
                (
              
              x
              
                )
              
              
flat_y 
              
                =
              
               model
              
                .
              
              predict
              
                (
              
              flat_x
              
                )
              
              
grid_y 
              
                =
              
               flat_y
              
                .
              
              reshape
              
                (
              
              grid_x
              
                [
              
              
                0
              
              
                ]
              
              
                .
              
              shape
              
                )
              
              
mp
              
                .
              
              figure
              
                (
              
              
                'SVM Linear Classification'
              
              
                ,
              
               facecolor
              
                =
              
              
                'lightgray'
              
              
                )
              
              
mp
              
                .
              
              title
              
                (
              
              
                'SVM Linear Classification'
              
              
                ,
              
               fontsize
              
                =
              
              
                20
              
              
                )
              
              
mp
              
                .
              
              xlabel
              
                (
              
              
                'x'
              
              
                ,
              
               fontsize
              
                =
              
              
                14
              
              
                )
              
              
mp
              
                .
              
              ylabel
              
                (
              
              
                'y'
              
              
                ,
              
               fontsize
              
                =
              
              
                14
              
              
                )
              
              
mp
              
                .
              
              tick_params
              
                (
              
              labelsize
              
                =
              
              
                10
              
              
                )
              
              
mp
              
                .
              
              pcolormesh
              
                (
              
              grid_x
              
                [
              
              
                0
              
              
                ]
              
              
                ,
              
               grid_x
              
                [
              
              
                1
              
              
                ]
              
              
                ,
              
               grid_y
              
                ,
              
               cmap
              
                =
              
              
                'gray'
              
              
                )
              
              
mp
              
                .
              
              scatter
              
                (
              
              x
              
                [
              
              
                :
              
              
                ,
              
              
                0
              
              
                ]
              
              
                ,
              
               x
              
                [
              
              
                :
              
              
                ,
              
              
                1
              
              
                ]
              
              
                ,
              
               c
              
                =
              
              y
              
                .
              
              ravel
              
                (
              
              
                )
              
              
                ,
              
               cmap
              
                =
              
              
                'brg'
              
              
                ,
              
               s
              
                =
              
              
                80
              
              
                )
              
              
mp
              
                .
              
              show
              
                (
              
              
                )
              
            
          
            
          

更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论