现在的位置: 首页 > 综合 > 正文

Theano开始学习2

2018年11月02日 ⁄ 综合 ⁄ 共 3838字 ⁄ 字号 评论关闭

From:http://deeplearning.net/software/theano/tutorial/examples.html#using-shared-variables官方文档

看DBN代码,好多用到Theano写,无奈浏览先官方文档。

1:tensor

一切从变量说起:

improt theano.tensor as T   #用T定义变量类型

x = T.fmatrix()     #定义了一个矩阵,名字为x.
如其他类型:
tensor.scalar(name=Nonedtype=config.floatX)

Return a Variable for a 0-dimensional ndarray

tensor.vector(name=Nonedtype=config.floatX)

Return a Variable for a 1-dimensional ndarray

tensor.row(name=Nonedtype=config.floatX)

Return a Variable for a 2-dimensional ndarray in which the number of rows is guaranteed to be 1.

tensor.col(name=Nonedtype=config.floatX)
From:http://deeplearning.net/software/theano/library/tensor/basic.html#libdoc-basic-tensor

2:theano.function #有了前面的变量就可以在变量上操作了。定义一个函数,然后就可以直接给函数传递参数了。
如这个函数:
s(x) = \frac{1}{1 + e^{-x}}

x=T.dmatrix('x')

s=1/(1+T.exp(-x))

logistic=theano.function([x],s) #当然这里也可写成logistic=theano.function([x],outputs=s)

print logistic([[1,2]])

#output: [[ 0.73105858  0.88079708]]


3:定义一个复杂点的变量,一次定义多个。如下一次定义两个矩阵,然后在矩阵上做操作。
>>> a, b = T.dmatrices('a', 'b')
>>> diff = a - b
>>> abs_diff = abs(diff)
>>> diff_squared = diff**2
>>> f = function([a, b], [diff, abs_diff, diff_squared])
output:
>>> f([[1, 1], [1, 1]], [[0, 1], [2, 3]])
[array([[ 1.,  0.],
        [-1., -2.]]),
 array([[ 1.,  0.],
        [ 1.,  2.]]),
 array([[ 1.,  0.],
        [ 1.,  4.]])]

4:参数设置默认值,如下:
>>> from theano import Param
>>> x, y = T.dscalars('x', 'y')
>>> z = x + y
>>> f = function([x, Param(y, default=1)], z)   #这里为y设置默认值1
>>> f(33)
array(34.0)
>>> f(33, 2)
array(35.0)

5:共享变量(shared variables) #差不多相当于全局变量,几个函数可以共同使用。
#如下面更新函数,给函数中变量inc一个值,函数返回共享变量state更新前的old值,
#但是这个共享变量的值已将根据updates中原则更新为state+inc了
>>> from theano import shared
>>> state = shared(0)      #state是个共享变量,它的初值为0
>>> inc = T.iscalar('inc')
>>> accumulator = function([inc], state, updates=[(state, state+inc)])
比如:
>>> state.get_value()            #可以使用get_value()得到共享变量的值
array(0)
>>> accumulator(1)
array(0)
>>> state.get_value()
array(1)
>>> accumulator(300)
array(1)
>>> state.get_value()
array(301)

>>> state.set_value(-1)         #也可以用set_value()设置state的值。
>>> accumulator(3)
array(-1)
>>> state.get_value()
array(2)

#共享变量用在GPU这里比较好用,比如有一万个数据,可以给数据一个索引(共享变量),一次可以向拷贝多少个数。然后更新共享变量的值。


6:随机数(Random Numbers)
#6.1什么是随机数?什么是随机数种子?
在计算机中并没有一个真正的随机数发生器,但是可以做到使产生的数字重复率很低,这样看起来好象是真正的随机数,实现这一功能的程序叫伪随机数发生器。 
    有关如何产生随机数的理论有许多,如果要详细地讨论,需要厚厚的一本书的篇幅。不管用什么方法实现随机数发生器,都必须给它提供一个名为“种子”的初始值。而且这个值最好是随机的,或者至少这个值是伪随机的。“种子”的值通常是用快速计算寄存器或移位寄存器来生成的。 
  以下是线性同余法生成伪随机数伪代码:

  Random(n,m,seed,a,b)
  {
  r0 = seed;
  for (i = 1;i<=n;i++)
  ri = (a*ri-1 + b) mod m
  }
直接看例子:
from theano.tensor.shared_randomstreams import RandomStreams
from theano import function
srng = RandomStreams(seed=234)
rv_u = srng.uniform((2,2))
rv_n = srng.normal((2,2))
f = function([], rv_u)
g = function([], rv_n, no_default_updates=True)    #Not updating rv_n.rng
nearly_zeros = function([], rv_u + rv_u - 2 * rv_u)
>>> f_val0 = f()
>>> f_val1 = f()  #different numbers from f_val0
>>> g_val0 = g()  # different numbers from f_val0 and f_val1
>>> g_val1 = g()  # same numbers as g_val0!

7:看完上面后,特别是共享变量,就可以看懂theano写的逻辑回归分类器了

A Real Example: Logistic Regression

The preceding elements are featured in this more realistic example. It will be used repeatedly.

import numpy
import theano
import theano.tensor as T
rng = numpy.random

N = 400
feats = 784
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")
w = theano.shared(rng.randn(feats), name="w")
b = theano.shared(0., name="b")
print "Initial model:"
print w.get_value(), 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
                                          # (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(), b.get_value()
print "target values for D:", D[1]
print "prediction on D:", predict(D[0])


抱歉!评论已关闭.