Theano基础(二)

Theano基础(二)

    Theano是Python的一个Deep  Learning 开源包,也可以很方便地利用它来做科学计算。我打算边学边记录一下。


    本节参考:http://deeplearning.net/software/theano/tutorial/examples.html#logistic-function


    这一小节我们利用Theano做logistic回归,在此之前我们先了解一些Theano的知识。 对于logistic回归,忘记了推荐看一下Andrew Ng的教程,逻辑回归一章:http://openclassroom.stanford.edu/MainFolder/CoursePage.php?course=MachineLearning

    

     基础准备:

     上一节中(http://blog.csdn.net/lihaoweicsdn/article/details/49329557

     我们利用theano创建了一个函数,完成两个矩阵相加的功能。这一节我们要来学习更多关于theano的知识。

     1.theano支持多输入多输出,请见下例:

import theano 
import theano.tensor as T 


x = T.dmatrices('x')
y = T.dmatrices('y')

x_add_y = x+y
x_sub_y = x-y
f = theano.function([x,y],[x_add_y,x_sub_y])
print f([[1,1]],[[1,1] ])  ##结果:[array([[ 2.,  2.]]), array([[ 0.,  0.]])]

    2.指定输入变量默认值

      这时候我们要用到theano的Param 指定默认值

import theano 
import theano.tensor as T 
from theano import Param
x = T.dmatrices('x')
s = 1 / (1 + T.exp(-x))
logistic = theano.function([ Param(x,default = [[1]]) ],s)
      这里我们一直用的dmatrices 大家也可以随意用其他的,比如 dscalars等,依据需求而定。这里我们新出现了T.exp()  在tensor中,内置了很多常见的函数,比如abs() tanh() arcsin() ......

    3.使用共享变量

      这时要用到theano中的shared  共享变量能被多个函数所共用。

      在下例中 accumulator 函数中,updates 用来更新共享变量。函数完成的功能:记录函数被调用的次数。次数记录在共享变量state中。对于下面的f函数。state的值一直是0 但计算z时state的值暂时由r替换(由givens实现)   这一部分暂时做了解即可。


import theano 
import theano.tensor as T 
from theano import Param
from theano import shared
state = shared(0)  #创建共享变量,初始值为0
inc = T.iscalar('inc') #32-bits 整型

##输入变量: inc 输出:上一次的state 值  更新: state = state + inc 
accumulator = theano.function([inc], state, updates=[(state, state+<span style="font-family:SimSun;">1</span>)])

r = T.scalar(dtype = state.dtype) #替换量 须与state 同类型
z = inc + state

f = theano.function([inc,r],z,givens=[(state,r)])
  
   4.生成随机数

import theano 
import theano.tensor as T 
from theano import Param
from theano import shared
from theano.tensor.shared_randomstreams import RandomStreams


srng = RandomStreams(seed=234)
rv_u = srng.uniform((2,2))##均匀分布 2*2 matrix
rv_n = srng.normal((2,2)) ##正态分布 2*2 matrix
f = theano.function([], rv_u)  ##<span style="font-family:SimSun;">每次调用随机值都不一样</span>
g = theano.function([], rv_n, no_default_updates=True)    #<span style="font-family:SimSun;">每次调用随机值都一样,类似于numpy中生成随机数</span>
nearly_zeros = theano.function([], rv_u + rv_u - 2 * rv_u)
  
   5.numpy.random 生成随机数

   请参考:http://www.mamicode.com/info-detail-507676.html




   实例练习:logistic 回归

   算法过程:

            1.创建两类样本数据

            2.初始化 w 和 b

            3.计算logistic值 (大于0.5即认为是1类)

            4.损失函数(对数项+权值控制项)

            5.计算损失函数对w和b的梯度

                                  for  i = 1 到 训练次数:

                 计算损失函数值;

                 更新w 和 b

            最后:预测,计算logistic值(大于0.5即认为是1类)

  

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

N = 400 ##样本数量
feats = 784 ##每个样本的特征维数

##随机生成样本,样本类别{0,1}
D = (rng.randn(N, feats), rng.randint(size=N, low=0, high=2))

training_steps = 10000 #迭代次数

# Declare Theano symbolic variables
x = T.fmatrix("x")
y = T.fvector("y")

# 初始化w 和 b
w = theano.shared(rng.randn(feats), name="w")
b = theano.shared(0., name="b")

print("Initial model:")


# 打印初始w 和 b
#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
                                          # (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]))


    


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值