python +numpy,theano,cifar 2

来源:互联网 发布:win10系统开机优化 编辑:程序博客网 时间:2024/05/22 00:12

    数据要归一化到[0..1)

    貌似dbn还是很普通。cnn提供了共享权值,局部感受野,dbn提供了基于层的学习和非监督学习,模拟了人类的学习,我们可以不知道一个事情是啥,当看多了,以后一点就通了。

   有把cnn和dbn结合起来的。

   如果把cnn和dbn结合,是在第一层实现cnn,还是每个层都用相同的cnn呢?

   dbn的各层,是否可用不同的rmb?

对于shared variable 的一点理解,主要的说明在注释中了。另外,也想过是否直接通过inputs传数据,对于多个函数共用一个数据的情况,似乎不行,还是要用共享变量的。

 

# -*- coding: utf-8 -*-

"""

Created on Tue Nov 29 18:30:54 2011

@author: Administrator

"""

import numpy,theano,theano.tensor as T

#define model

idx=T.lscalar('idx')

x=T.vector('x')

y=T.vector('y')

f1=T.sum(x)

fn1=theano.function([x],f1)

#prepare data

a=[1,2,3,4,5]

b=[6,7,8,9,10]

a_n=numpy.asarray(a,dtype=theano.config.floatX)

b_n=numpy.asarray(a,dtype=theano.config.floatX)

a_s=theano.shared(a_n)

b_s=theano.shared(b_n)

#run the operations defined by theano.function

print fn1(a)#works

print fn1(a_n)#works

# theano function works with tensor ,not shared variable

# we can't use share variable as parameters

#fn2=theano.function([idx],outputs=f1,givens={x:a_s})#not works,a_s is a shared\

# variable,we should tansfermate it to a tensor by a_s[...]?

fn2=theano.function([idx],outputs=f1,givens={x:a_s[0:idx]})

print fn2(3)

a_s=b_s #这条语句造成了后面fn2(3)=6,实际上,这里把a_s指向了b_s.

print fn2(3) #,we can't change shared variable by point it to other shared variable

#there are three variables,symbol variable,system variable,shared variable

#symbol variable is just used in write theano function.

#sysem variable is point to a data area

# so does shared variable,while shared variable is managed by theano,not system

#a theano function is combined with shared variable when this fucntion is compiled

a_s.set_value(b)

print fn2(3)

print a_s.get_value()

 

 

output:

15.0
15.0
6.0
6.0
6.0
[  6.   7.   8.   9.  10.]

 

通过shard variable  可以减少数据交换,要改变shared variable 的值,可以通过set_value。

set_value(b) 时需要把b先转换成numpy.array,你不做。theano也会这么做。容易出错,不如自己来做。

尤其是在borrow=true时。

关于borrow=true,本来是很好立即的。偏偏有些细节不太明白。基本上cpu上,还是用borrow=true,gpu上用deepcopy(borrow=false)

 

原创粉丝点击