python中怎么使用theano庫實(shí)現(xiàn)線性回歸-創(chuàng)新互聯(lián)

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)python中怎么使用theano庫實(shí)現(xiàn)線性回歸,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

榆樹網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),榆樹網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為榆樹1000+提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務(wù)好的榆樹做網(wǎng)站的公司定做!

代碼塊

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)] #更新參數(shù)
  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)

上述就是小編為大家分享的python中怎么使用theano庫實(shí)現(xiàn)線性回歸了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

新聞標(biāo)題:python中怎么使用theano庫實(shí)現(xiàn)線性回歸-創(chuàng)新互聯(lián)
URL鏈接:http://bm7419.com/article16/dsegdg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、外貿(mào)建站、網(wǎng)站排名品牌網(wǎng)站設(shè)計、小程序開發(fā)網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)