zoukankan      html  css  js  c++  java
  • 人工智能之实现简单神经网络之权重更新算法

    1.下面小关来介绍一下人工智能之实现简单神经网络之权重更新算法

    2.来吧展示jupter noterbook

    #调库
    import numpy as np
    #定义感受器
    class Perceptron(object):
        #eta:学习率
        #n_iter:权重向量的训练次数
        #w_:神经分叉权重向量
        #errors_:用于记录神经元判断出错次数
        def _int_(self,eta = 0.01,n_iter = 10):
            self.eta = eta
            self.n_iter = n_iter
            pass
        #输入训练数据,培训神经元
        #x为输入样本向量,y为对应样本分类
        #x:shape[n_samples,n_features]
        #例如x[1,2,3],[4,5,6]
        #n_samples:2
        #n_feature:3
        #y:[1,-1]  
        #初始化权重向量为0,+1因为w0(步调函数阈值)需初始化
        def fit(self,x,y):
            self.w_ =np.zero(1 + x.shape[1]);
            self.errors_ = [];
            
            for _ in range(self.n_iter):
                errors = 0
                #x[[1,2,3],[4,5,6]]
                #y[1,-1]
                #zip(x,y) = [[1,2,3,1],[4,5,6,-1]]
                for xi,target in zip(x,y):
                    #update = n * (y-y')
                    update = self.eta * (target - self.predict(xi))
                    #xi是一个向量
                    #updata * xi 等价于:
                    self.w_[1:] += updata * xi  #注意+=之间没空格
                    self.w_[0] += updata   #注意+=之间没空格
                    
                    errors +=int(update != 0.0)
                    self.errors_.append(errors)
                    pass
                pass
            def net_input(self,x):
                return np.dot(x,self.w_[1:]) + self.w_[0]
                pass
            def predict(self,x):
                return np.where(self.net_input(x) >= 0.0 , 1,-1)
                pass
            pass
            
        
    

    3.关于详细解释,上面都有注释

    希望能帮到大家,问你们要一个赞,你们会给吗,谢谢大家
    版权声明:本文版权归作者(@攻城狮小关)和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    大家写文都不容易,请尊重劳动成果~
    交流加Q:1909561302
    CSDN地址https://blog.csdn.net/Mumaren6/

  • 相关阅读:
    LeetCode 79. 单词搜索
    LeetCode 1143. 最长公共子序列
    LeetCode 55. 跳跃游戏
    LeetCode 48. 旋转图像
    LeetCode 93. 复原 IP 地址
    LeetCode 456. 132模式
    LeetCode 341. 扁平化嵌套列表迭代器
    LeetCode 73. 矩阵置零
    LeetCode 47. 全排列 II
    LeetCode 46. 全排列
  • 原文地址:https://www.cnblogs.com/guanguan-com/p/13679981.html
Copyright © 2011-2022 走看看