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

首頁 > 編程 > Python > 正文

基于python中theano庫的線性回歸

2020-01-04 14:36:23
字體:
來源:轉載
供稿:網友

theano庫是做deep learning重要的一部分,其最吸引人的地方之一是你給出符號化的公式之后,能自動生成導數。本文使用梯度下降的方法,進行數據擬合,現在把代碼貼在下方

代碼塊

import numpy as np import theano.tensor as T import theano import time class Linear_Reg(object):   def __init__(self,x):     self.a = theano.shared(value = np.zeros((1,), dtype=theano.config.floatX),name = 'a')     self.b = theano.shared(value = np.zeros((1,), dtype=theano.config.floatX),name = 'b')     self.result = self.a * x + self.b     self.params = [self.a,self.b]   def msl(self,y):     return T.mean((y - self.result)**2) def regrun(rate,data,labels):   X = theano.shared(np.asarray(data,                  dtype=theano.config.floatX),borrow = True)   Y = theano.shared(np.asarray(labels,                  dtype=theano.config.floatX),borrow = True)   index = T.lscalar() #定義符號化的公式  x = T.dscalar('x')  #定義符號化的公式  y = T.dscalar('y')  #定義符號化的公式  reg = Linear_Reg(x = x)   cost = reg.msl(y)   a_g = T.grad(cost = cost,wrt = reg.a) #計算梯度   b_g = T.grad(cost = cost, wrt = reg.b) #計算梯度  updates=[(reg.a,reg.a - rate * a_g),(reg.b,reg.b - rate * b_g)] #更新參數  train_model = theano.function(inputs=[index], outputs = reg.msl(y),updates = updates,givens = {x:X[index], y:Y[index]})   done = True   err = 0.0   count = 0   last = 0.0   start_time = time.clock()   while done:     #err_s = [train_model(i) for i in xrange(data.shape[0])]     for i in xxx:      err_s = [train_model(i) ]      err = np.mean(err_s)      #print err     count = count + 1     if count > 10000 or err <0.1:       done = False     last = err   end_time = time.clock()   print 'Total time is :',end_time -start_time,' s' # 5.12s   print 'last error :',err   print 'a value : ',reg.a.get_value() # [ 2.92394467]    print 'b value : ',reg.b.get_value() # [ 1.81334458] if __name__ == '__main__':    rate = 0.01   data = np.linspace(1,10,10)   labels = data * 3 + np.ones(data.shape[0],dtype=np.float64) +np.random.rand(data.shape[0])  regrun(rate,data,labels) 

其基本思想是隨機梯度下降。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 云安县| 晋州市| 全州县| 武隆县| 德兴市| 黑龙江省| 宁国市| 吕梁市| 德化县| 泸溪县| 大方县| 麟游县| 西吉县| 新竹市| 松潘县| 西安市| 井研县| 姚安县| 将乐县| 卓尼县| 天津市| 淮阳县| 新绛县| 绵竹市| 行唐县| 海门市| 香格里拉县| 合作市| 汤原县| 玉龙| 沧源| 兴仁县| 修武县| 永城市| 阿坝| 房产| 汉川市| 荔波县| 吐鲁番市| 巩义市| 竹溪县|