【讀書報告--01】神經網路基礎學習
阿新 • • 發佈:2018-12-17
一、感知器
1.神經網路和感知器
神經網路結構包含輸入層、隱藏層(可以有多層)、輸出層,如下圖所示
其中每個神經元是一個感知器,感知器的結構組成部分:輸入權值、啟用函式、輸出
ps:啟用函式可以有很多種選擇,而感知器的輸出由 y=f(w*x+b)來計算
感知器結構如下圖:
2.感知器的訓練:(個人覺得是神經網路的重點,通過已有的資料,對權值W的相關計算)
感知器的迭代公式:
3.實戰:通過感知器實現and函式
訓練資料輸入:[1,1]->1 [1,0]->0 [0,1]->0 [0,0]->0
啟用函式:使用階躍函式
輸出:y=f(w*x+b)
程式碼如下:
from functools import reduce class Perception(object): def __init__(self,input_num,activator): self.activator=activator self.weight=[0.0 for _ in range(input_num)] print(self.weight) self.bias=0.0 def __str__(self): return "weights:{0},bias:{1}".format(list(self.weight),self.bias) def predict(self,input_X): return self.activator( reduce(lambda a,b:a+b, map(lambda x,w:x*w,input_X,self.weight),0.0)+self.bias ) def train(self,iteration,input_X,Label,rate): for i in range(iteration): self.one_interation(input_X,Label,rate) def one_interation(self,input_X,Label,rate): samples=zip(input_X,Label) for (a,b) in samples: y_temp=self.predict(a) self.update_weight(a,b,rate,y_temp) def update_weight(self,input_X,Label,rate,y_temp): delta=Label-y_temp self.weight=list(map(lambda x,w:w+rate*delta*x,input_X,self.weight)) self.bias+=rate*delta def f(x): return 1 if x>0 else 0 def get_train_dataset(): input_X=[[1,1],[0,0],[1,0],[0,1]] Label=[1,0,0,0] return input_X,Label def train_and_perception(): p=Perception(2,f) input_X,Label=get_train_dataset() p.train(10,input_X,Label,0.1) return p if __name__=='__main__': perception=train_and_perception() print(perception) print(perception.predict([1,1])) print(perception.predict([1,0])) print(perception.predict([0,1])) print(perception.predict([0,0]))