Theano入門神經網絡(一)


     Theano是一個Python庫,專門用於定義、優化、求值數學表達式,效率高,適用於多維數組。特別適合做機器學習。一般來說,使用時需要安裝python和numpy.

     首先回顧一下機器學習的東西,定義一個模型(函數)f(x;w) x為輸入,w為模型參數,然后定義一個損失函數c(f),通過數據驅動在一堆模型函數中選擇最優的函數就是訓練training的過程,在機器學習中訓練一般采用梯度下降法gradient descent.

     使用theano來搭建機器學習(深度學習)框架,有以下優點:

     1、 theano能夠自動計算梯度

     2、只需要兩步驟就能搭建框架,定義函數和計算梯度。

一、 定義函數

步驟 0    宣告使用theano   import theano
步驟 1 定義輸入 x=theano.tensor.scalar()
步驟 2 定義輸出 y=2*x
步驟3 定義fuction f = theano.function([x],y)
步驟 4 調用函數 print f(-2)

步驟1 定義輸入變量 

      a = theano.tensor.scalar()

      b =theano.tensor.matrix()

簡化  import  theano.tensor as T

步驟2 定義輸出變量 需要和輸入變量的關系

     x1=T.matrix()

     x2=T.matrix()

     y1=x1*x2

     y2=T.dot(x1,x2) #矩陣乘法

步驟3 申明函數

     f= theano.function([x],y)

    函數輸入必須是list 帶[]

example:

 1 import theano
 2 import theano.tensor as T
 3 
 4 a= T.matrix()
 5 b= T.matrix()
 6 c = a*b
 7 d = T.dot(a,b)
 8 F1= theano.function([a,b],c)
 9 F2= theano.function([a,b],d)
A=[[1,2],[3,4]] 10 B=[[2,4],[6,8]] #2*2矩陣 11 C=[[1,2],[3,4],[5,6]] #3*2矩陣 12 print F1(A,B) 13 print F2(C,B)

二、計算梯度

   計算 dy/dx ,直接調用g=T.grad(y,x) y必須是一個標量 scalar

   和梯度有關的三個例子:

  example1 :標量對標量的導數

1 x= T.scalar('x')
2 y = x**2
3 g = T.grad(y,x)
4 f= theano.function([x],y)
5 f_prime=theano.function([x],g)
6 print f(-2)
7 print f_prime(-2)

   example2 : 標量對向量的導數

x1= T.scalar()
x2= T.scalar()
y = x1*x2
g = T.grad(y,[x1,x2])
f= theano.function([x1,x2],y)
f_prime=theano.function([x1,x2],g)
print f(2,4)
print f_prime(2,4)

   example3 : 標量對矩陣的導數

A= T.matrix()
B= T.matrix()
C=A*B          #不是矩陣乘法,是對於位置相乘
D=T.sum(C)
g=T.grad(D,A)    #注意D是求和 所以肯定是一個標量 但g是一個矩陣
y_prime=theano.function([A,B],g)
A=[[1,2],[3,4]]
B=[[2,4],[6,8]]
print y_prime(A,B)

搭建神經網絡 

  1 單個神經元

假設w b 已知。y=neuron(x;w,b)

 1 import theano
 2 import  theano.tensor as T
 3 import  random
 4 import  numpy as np
 5 
 6 x = T.vector()
 7 w = T.vector()
 8 b = T.scalar()
 9 
10 z= T.dot(w,x)+b
11 y= 1/(1+T.exp(-z))
12 
13 neuron =theano.function(
14     inputs=[x,w,b],
15     outputs=[y]
16 )
17 
18 w = [-1,1]
19 b=0
20 for i in range(100):
21     x = [random.random(),random.random()]
22     print  x
23     print neuron(x,w,b)

但是運行出現錯誤 'TensorType(float32, vector) cannot store accurately value [0.4079584242156499, 0.7781482896772725], it would be represented as [ 0.40795842  0.77814829]. If you do not mind this precision loss, you can: 1) explicitly convert your data to a numpy array of dtype float32, or 2) set "allow_input_downcast=True" when calling "function".', 

  因此我們按照第一種方法,轉換成a numpy array of dtype float32,將上述21行代碼替換如下:

x=np.asarray([random.random(),random.random()], dtype = np.float32)

運行結果如下

[array(0.3996952772140503, dtype=float32)]
[ 0.12659253 0.45289889]
[array(0.5808603763580322, dtype=float32)]
[ 0.96148008 0.70698273]
[array(0.43671688437461853, dtype=float32)]

w,b應該也是參數 ,上述函數改為neuron(x),model 參數 wb 應該用shared variables,改進的代碼
 1 import theano
 2 import  theano.tensor as T
 3 import  random
 4 import  numpy as np
 5 
 6 x = T.vector()
 7 # share variables  參數!有值
 8 w = theano.shared(np.array([1.,1.]))
 9 b = theano.shared(0.)
10 
11 z= T.dot(w,x)+b
12 y= 1/(1+T.exp(-z))
13 
14 neuron =theano.function(
15     inputs=[x], # x 作為輸入
16     outputs=y
17 )
18 
19 w.set_value([0.1, 0.1]) #修改值
20 for i in range(100):
21     #x = [random.random(),random.random()]
22     x=np.asarray([random.random(),random.random()], dtype = np.float32)
23     print  x
24     print w.get_value() #得到值
25     print neuron(x)
 
        

 2  訓練 training

   定義一個損失函數C  計算C對每一個wi的偏導數 和b的偏導數

梯度下降 w1 = w1 -n*dc/dw1 

常規:
dw, db =gradient(x,y_hat)
w.set_value(w.get_value()-0.1*dw)
b.set_value(b.get_value()-0.1*db)
改進:
gradient = theano.function(
inputs=[x,y_hat],
updates=[(w,w-0.1*dw),(b,b-0.1*db)]











 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM