python原生實現感知器

2021-10-03 10:26:35 字數 3125 閱讀 3283

具體理論詳盡分析請轉到我另一篇部落格:零基礎入門深度學習(6) - 原生實現長短時記憶網路(lstm)

python原生實現乙個感知器(實現and運算),將輸入標籤替換成or真值表對應結果,

def gettraindataset():

#and 真值表

inputs = [[0,0],[0,1],[1,0],[1,1]];

#and

labels = [0,0,0,1];

#or# labels = [0,1,1,1];

return inputs,labels;

則該感知器變為實現or運算。

具體**如下

# coding:utf-8

class perception(object):

def __init__(self,input_num,activator):

self.weights = [0.0 for _ in range(input_num)];

self.activator = activator;

self.bias = 0.0;

def train(self, inputs, labels,n, learning_rate):

#訓練樣本,不斷使**值與標籤值相接近並得到此時的每個樣本輸入引數對應的權重(此處self.weights為乙個權重向量)和偏置項

for i in range(n):

self.iteration(inputs,labels,learning_rate)

def iteration(self,inputs,labels,learning_rate):

#inputs是樣本集合,而input是單單乙個樣本(向量),predict()只能**樣本對應的標籤結果,有兩個輸入值

# temp = map(lambda x:learning_rate*x,map(lambda x, y:x - y,labels,y));

# self.weights = map(lambda x, y:x+y,self.weights,map(lambda x, y:x*y,temp,inputs));

samples = zip(inputs,labels);

#每處理乙個樣本就更新一次權重和偏置項,權重的個數與樣本的緯度有關,也就是樣本的引數,and有兩個輸入引數

#所以權重有兩個,根據更新規則,權重為乙個向量,偏置項為乙個數,這兩項同時處理

for (input,label) in samples:

y = self.predict(input);

temp = learning_rate * (label - y);

self.update(input,temp);

# def predict(self, input):

return self.activator(

reduce(

lambda x, y:x+y,map(lambda (x, y):x*y,zip(self.weights,input)),0.0

)+ self.bias

)def update(self, input,temp):

# 更新權重 w =w + rate*(labels-y)*xi,xi為樣本輸入引數,w為權重向量

# 偏移量b = b + rate*(labels-y)

#temp=rate*(labels-y)

self.weights = map(lambda x, y:x+y,self.weights, map(lambda x:temp*x,input));

self.bias = self.bias + temp;

#重寫類的輸出樣式

def __str__(self):

return "weights:%s\nbias:%f " %(self.weights,self.bias);

def f(x):

if x > 0:

return 1

else:

return 0;

def gettraindataset():

#and 真值表

inputs = [[0,0],[0,1],[1,0],[1,1]];

#and

labels = [0,0,0,1];

#or# labels = [0,1,1,1];

return inputs,labels;

def and_perception():

'''利用and真值表訓練 and感知器

:return:

'''#輸入引數為2個,因為and為二元函式,啟用函式為f:階越函式

p = perception(2,f);

inputs,labels = gettraindataset();

#訓練,輸入資料集和標籤,迭代10次,學習速率(步長)設為0.1

p.train(inputs,labels,10,0.1);

return p;

if __name__ == '__main__':

# a = [1 for _ in range(2)]

# b = [2,3]

# print reduce(lambda x, y:x+y,map(lambda (x,y):x*y,zip(a,b)),0.0)

# print zip(a,b)

# for (c,d) in zip(a,b):

# print c," ",d

p = and_perception();

print p;

print "0 and 0 = ",p.predict([0,0]);

print "0 and 1 = ", p.predict([0, 1]);

print "1 and 0 = ", p.predict([1, 0]);

print "1 and 1 = ", p.predict([1, 1]);

**結果如下:和and真值表的一樣。

python 實現感知器(一)

基礎知識不再重述,可參考 coding utf 8 這裡定義乙個感知器的類 class perceptron object def init self,input num,activator 初始化感知器,設定輸入引數的個數,以及啟用函式。啟用函式的型別為double double self.act...

感知器的scala實現

其實感知器學習演算法,就是利用第一節介紹的單層感知器。首先利用給的正確資料,計算得到輸出值,將輸出值和正確的值相比,由此來調整每乙個輸出端上的權值。公式便是用來調整權值,首先 是乙個 學習引數 一般我將它設定成小於1的正數。t便是訓練資料中的正確結果,便是第i個輸入端的輸入值,便是第i個輸入端上面的...

感知器的scala實現

其實感知器學習演算法,就是利用第一節介紹的單層感知器。首先利用給的正確資料,計算得到輸出值,將輸出值和正確的值相比,由此來調整每乙個輸出端上的權值。公式便是用來調整權值,首先 是乙個 學習引數 一般我將它設定成小於1的正數。t便是訓練資料中的正確結果,便是第i個輸入端的輸入值,便是第i個輸入端上面的...