您的位置:首页 > 理论基础 > 数据结构算法

theano学习入门和进阶系列1: 基础数据结构和语法

2017-04-09 18:04 656 查看
theano是一个经典的深度学习工具包,尽管是当前众多深度学习框架中唯一不支持分布式的,但其入门快、使用简单、对自定义修改的支持佳等都让它目前还是非常流行。本文结合基于theano实现logistic regression模型的过程来讲解theano入门的基础知识点,主要知识点详见code中的注释。

############################
#
# logistic regression
#
############################
import theano
import numpy
rng=numpy.random

#samples setting
samples_num=20
feats_num=5

"""
以下定义逻辑回归模型中的自变量x和因变量y,需要将它们定义为theano内置的数据类型。
theano的基础数据类型及多种内置函数均放在theano.tensor中,数据类型包含double、int、uchar、float等多种类型。
因为GPU一般是float32类型,因此一般都使用float而非double。
常用的基础数据结构如下:
标量:iscalar(int类型的标量)、fscalar(float类型的标量)
一维向量:ivector(int 类型的向量)、fvector(float类型的向量)、
二维矩阵:imatrix(int类型的矩阵)、fmatrix(float类型矩阵)
三维矩阵:itensor3(int类型的三维矩阵)、ftensor3(float类型的三维矩阵)
四维矩阵:itensor5(int类型的四维矩阵)、ftensor4(float类型的四维矩阵)
依此类推
"""
#independent variables
x=theano.tensor.matrix('x')

#dependent variables
y=theano.tensor.vector('y')

"""
以下定义逻辑回归模型的参数w和b。此处将w和b均定义为theano.shared类型,即共享变量。
共享变量是theano中的一个重要概念,也是多线程编程中的一个重要概念,
是为支持多线程计算,在程序过程中允许多个线程共同访问和修改的变量。
在深度学习中整个计算过程基本都是使用多线程计算,因此模型的参数也均应设置为共享变量。
需要注意的是,共享变量必须要赋初始值。
"""
#parameters, shared variables
w=theano.shared(rng.randn(feats_num), name='w')
b=theano.shared(0.5, name='b')

#algorithm setting
alpha=0.01

"""
以下定义逻辑回归模型的cost function。
对于单个样本,采用的loss function为对数损失函数,即-y*log(y_pred)-(1-y)*log(1-p_pred)。
整体的cost function则为所有样本loss的均值外加正则化项。在此涉及的几个知识点如下:
(1)theano里的常用内置function
theano.tensor.exp, theano.tensor.log,theano.tensor.dot
variable.mean(),variabel.sum()等
(2)theano中function的定义形式
类似函数式编程的方式,先定义自变量和因变量的类型以及因变量与自变量的关系,再去定义出一个具体的function。
使用方法为theano.function(input=[list of independent variables],output=[list of dependent variables])。
(3)theano中的求导
theano.grad可以方便的求任意导数,省去了我们自己做推导的麻烦。
具体使用方法为theano.grad(dependent variables, independent variables)
"""
#cost function
y_p=1/(1+theano.tensor.exp(-theano.tensor.dot(x,w) - b))
loss=-y * theano.tensor.log(y_p) - (1-y) * theano.tensor.log(1.0-y_p)
cost=loss.mean() + 0.01 * (w ** 2).sum()
dw, db = theano.grad(cost, [w, b])
predict=y_p>0.5
predict_function=theano.function([x],predict)

"""
updates是theano.function里的一个重要参数,用于更新共享变量。updates是一个或多个包含两个元素的tuple或list,比如updates=[old_w,new_w],当函数被调用后,程序会用new_w替换old_w。
在逻辑回归模型中需要更新两组参数,w和b,因此updates=((w,w-alpha*dw),(b,b-alpha*db))。其中alpha即为前面已定义的学习速率。
"""
train=theano.function([x,y],[predict,cost],updates=((w,w-alpha*dw),(b,b-alpha*db)))

"""
一个常用的内置函数,theano.tensor.neq,统计两组数中不相同的值数。适用于计算0-1损失函数。
"""
test_neq=theano.tensor.neq(predict,y)
precision=theano.tensor.mean(theano.tensor.neq(predict,y))
test=theano.function([x,y],[predict,precision,test_neq])

"""
numpy里的知识点:
rng.random是用于产生uniform distribution均匀分布
rng.randn是用于产生normal distribution正态分布。
"""
input_x=rng.randn(samples_num, feats_num).astype(numpy.float32)
input_y=rng.randint(size=samples_num, low=0, high=2).astype(numpy.float32)
for i in range(1000):
print "======== step:",i
pred, t_cost = train(input_x, input_y)
print "cost:", t_cost
print test(input_x, input_y), input_y
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息