Artificial intelligence to realize the weight update algorithm of simple neural network

1. The following small pass introduces the weight update algorithm of artificial intelligence to realize simple neural network

2. Come on show 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. There are notes on the detailed explanation

I hope I can help everyone, I ask you if you want a like, will you give it, thank you all.
Copyright statement: The copyright of this article belongs to the author (@攻城狮小关) and CSDN. Welcome to reprint, but you must keep this paragraph without the author’s consent Statement, and provide the original link in an obvious place on the article page, otherwise the right to pursue legal responsibility is reserved.
It is not easy for everyone to write articles, please respect the fruits of labor~ 
Communication plus Q: 1909561302
Blog Park Address https://www.cnblogs.com/guanguan-com/

Guess you like

Origin blog.csdn.net/Mumaren6/article/details/108625495