本文主要讲Theano的安装,以及Theano的基本操作。最后给了一个完整的logistics回归的例子。
1. 安装
先安装python所需要的包,如果是centos之类的1
2
3
4
5sudo yum install python-pip
sudo yum install python-dev
sudo pip install numpy
sudo pip install theano
sudo yum install numpy scipy python-matplotlib ipython python-pandas sympy python-nose
Windows 安装(貌似需要MinGW的最新版本)1
2python -m pip install theano
python -m pip install gof
其中要先保证依赖的版本1
2
3numpy>=1.7.1
scipy>=0.11
six>=1.9.0
具体安装可以使用pip安装
注意:最终运行python,进入命令行运行1
theano.test()
2. Baby step
2.1 两个scalar相加
1 | import numpy |
其中dsclar是theano的type:0-dimensional arrays (scalar) of doubles (d)。x和y就是dscalar的类型实例。
function想到于是创建一个函数,输入是x和y,然后z是这个函数的输出。
2.2 matrix 相加
1 | In [12]: x = T.dmatrix('x') |
2.3 theano的type
- byte: bscalar, bvector, bmatrix, brow, bcol, btensor3, btensor4
- 16-bit integers: wscalar, wvector, wmatrix, wrow, wcol, wtensor3, wtensor4
- 32-bit integers: iscalar, ivector, imatrix, irow, icol, itensor3, itensor4
- 64-bit integers: lscalar, lvector, lmatrix, lrow, lcol, ltensor3, ltensor4
- float: fscalar, fvector, fmatrix, frow, fcol, ftensor3, ftensor4
- double: dscalar, dvector, dmatrix, drow, dcol, dtensor3, dtensor4
- complex: cscalar, cvector, cmatrix, crow, ccol, ctensor3, ctensor4
具体可以参考Basic Tensor Functionality
3. Theano Functions
3.1 Logistic Funciton
Logistic Funciton的使用
1 | In [23]: x = T.dmatrix('x') |
3.2 输出多个值
接上面的,多输出一个函数值1
2
3
4
5
6
7
8In [27]: tan = 1/2 * (1 + T.tanh(x/2))
In [28]: log2 = function([x],[s,tan])
In [29]: log2([[0,1],[-1,-2]])
Out[29]:
[array([[ 0.5 , 0.73105858],
[ 0.26894142, 0.11920292]]), array([[ 0., 0.],
[ 0., 0.]])]
3.3 Random值使用
1 | In [32]: from theano.tensor.shared_randomstreams import RandomStreams |
3.4 逻辑回归代码例子
使用%cpaste粘贴过去运行1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59import numpy
import theano
import theano.tensor as T
rng = numpy.random
N = 400 # training sample size
feats = 784 # number of input variables
# generate a dataset: D = (input_values, target_class)
D = (rng.randn(N, feats), rng.randint(size=N, low=0, high=2))
training_steps = 10000
# Declare Theano symbolic variables
x = T.matrix("x")
y = T.vector("y")
# initialize the weight vector w randomly
#
# this and the following bias variable b
# are shared so they keep their values
# between training iterations (updates)
w = theano.shared(rng.randn(feats), name="w")
# initialize the bias term
b = theano.shared(0., name="b")
print("Initial model:")
print(w.get_value())
print(b.get_value())
# Construct Theano expression graph
p_1 = 1 / (1 + T.exp(-T.dot(x, w) - b)) # Probability that target = 1
prediction = p_1 > 0.5 # The prediction thresholded
xent = -y * T.log(p_1) - (1-y) * T.log(1-p_1) # Cross-entropy loss function
cost = xent.mean() + 0.01 * (w ** 2).sum()# The cost to minimize
gw, gb = T.grad(cost, [w, b]) # Compute the gradient of the cost
# w.r.t weight vector w and
# bias term b
# (we shall return to this in a
# following section of this tutorial)
# Compile
train = theano.function(
inputs=[x,y],
outputs=[prediction, xent],
updates=((w, w - 0.1 * gw), (b, b - 0.1 * gb)))
predict = theano.function(inputs=[x], outputs=prediction)
# Train
for i in range(training_steps):
pred, err = train(D[0], D[1])
print("Final model:")
print(w.get_value())
print(b.get_value())
print("target values for D:")
print(D[1])
print("prediction on D:")
print(predict(D[0]))
因为我们是朋友,所以你可以使用我的文字,但请注明出处:http://alwa.info