国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python實現多層感知器

2020-02-16 00:42:55
字體:
來源:轉載
供稿:網友

寫了個多層感知器,用bp梯度下降更新,擬合正弦曲線,效果湊合。

# -*- coding: utf-8 -*-import numpy as npimport matplotlib.pyplot as plt  def sigmod(z): return 1.0 / (1.0 + np.exp(-z))  class mlp(object): def __init__(self, lr=0.1, lda=0.0, te=1e-5, epoch=100, size=None):  self.learningRate = lr  self.lambda_ = lda  self.thresholdError = te  self.maxEpoch = epoch  self.size = size  self.W = []  self.b = []  self.init()  def init(self):  for i in xrange(len(self.size)-1):   self.W.append(np.mat(np.random.uniform(-0.5, 0.5, size=(self.size[i+1], self.size[i]))))   self.b.append(np.mat(np.random.uniform(-0.5, 0.5, size=(self.size[i+1], 1))))  def forwardPropagation(self, item=None):  a = [item]  for wIndex in xrange(len(self.W)):   a.append(sigmod(self.W[wIndex]*a[-1]+self.b[wIndex]))  """  print "-----------------------------------------"  for i in a:   print i.shape,  print  for i in self.W:   print i.shape,  print  for i in self.b:   print i.shape,  print  print "-----------------------------------------"  """  return a  def backPropagation(self, label=None, a=None):  # print "backPropagation--------------------begin"  delta = [(a[-1]-label)*a[-1]*(1.0-a[-1])]  for i in xrange(len(self.W)-1):   abc = np.multiply(a[-2-i], 1-a[-2-i])   cba = np.multiply(self.W[-1-i].T*delta[-1], abc)   delta.append(cba)  """  print "++++++++++++++delta++++++++++++++++++++"  print "len(delta):", len(delta)  for ii in delta:   print ii.shape,  print "/n======================================="  """  for j in xrange(len(delta)):   ads = delta[j]*a[-2-j].T   # print self.W[-1-j].shape, ads.shape, self.b[-1-j].shape, delta[j].shape   self.W[-1-j] = self.W[-1-j]-self.learningRate*(ads+self.lambda_*self.W[-1-j])   self.b[-1-j] = self.b[-1-j]-self.learningRate*delta[j]   """print "=======================================1234"   for ij in self.b:    print ij.shape,   print   """  # print "backPropagation--------------------finish"  error = 0.5*(a[-1]-label)**2  return error  def train(self, input_=None, target=None, show=10):  for ep in xrange(self.maxEpoch):   error = []   for itemIndex in xrange(input_.shape[1]):    a = self.forwardPropagation(input_[:, itemIndex])    e = self.backPropagation(target[:, itemIndex], a)    error.append(e[0, 0])   tt = sum(error)/len(error)   if tt < self.thresholdError:    print "Finish {0}: ".format(ep), tt    return   elif ep % show == 0:    print "epoch {0}: ".format(ep), tt  def sim(self, inp=None):  return self.forwardPropagation(item=inp)[-1]  if __name__ == "__main__": tt = np.arange(0, 6.28, 0.01) labels = np.zeros_like(tt) print tt.shape """ for po in xrange(tt.shape[0]):  if tt[po] < 4:   labels[po] = 0.0  elif 8 > tt[po] >= 4:   labels[po] = 0.25  elif 12 > tt[po] >= 8:   labels[po] = 0.5  elif 16 > tt[po] >= 12:   labels[po] = 0.75  else:   labels[po] = 1.0 """ tt = np.mat(tt) labels = np.sin(tt)*0.5+0.5 labels = np.mat(labels) model = mlp(lr=0.2, lda=0.0, te=1e-5, epoch=500, size=[1, 6, 6, 6, 1]) print tt.shape, labels.shape print len(model.W), len(model.b) print model.train(input_=tt, target=labels, show=10) sims = [model.sim(tt[:, idx])[0, 0] for idx in xrange(tt.shape[1])]  xx = tt.tolist()[0] plt.figure() plt.plot(xx, labels.tolist()[0], xx, sims, 'r') plt.show()            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 竹北市| 永善县| 福州市| 太康县| 洛浦县| 库车县| 北流市| 育儿| 仙游县| 高雄市| 和田县| 鄢陵县| 逊克县| 民和| 德格县| 阜宁县| 三都| 全州县| 准格尔旗| 工布江达县| 平安县| 鲁山县| 区。| 苏尼特右旗| 乃东县| 广宗县| 滨海县| 长宁区| 舞钢市| 陆丰市| 呈贡县| 隆林| 怀仁县| 宁河县| 吐鲁番市| 浮梁县| 湖州市| 青铜峡市| 旺苍县| 株洲县| 建瓯市|