theano测试代码安装成功,运行theano测试代码.test测试不通过,这是什么原因

Theano学习笔记:Theano的艰辛安装体验 - hanahimi - 博客园
这大半个月一直在瞎研究深度学习的方面,跟着UFLDL的教程学了下来,中途也看了很多大大们的博客,感觉获益匪浅,终于给了迷茫的自己入门的开端。
话说UFLDL真的很浅显易懂呢,虽然有些我不怎么明白就是啦(那你的浅显易懂是什么回事!)
嘛,和之前斯坦福的coursera一样,这个教程的Exercise也是基于matlab,深度学习也有很好的Matlab组件包,网上很多大大都给与了注解和说明。。但是好喜欢Python的说,正好有个很受推荐的deeplearning的网站,基于Python的,不错不错。
就是这个网站:&
使用python开展深度学习,需要一个叫做 Theano 的组件包,啊这个包总的来说就是支持高效的多维数组与各种函数优化算法,同时可以使用NVIDIA图形显卡进行加速,嗯很棒。。(最后一个是可选功能)
theano的组件包也是在这里可以下载到:
这个是官网提供PDF的手册下载:&
现在的版本是0.60的,可以在PYPI上下到&
不过,我这种懒人一般的东西都是在这里下到:&
在上面那个网,ctrl+F检索 theano就可以的,很多个版本,都是exe文件,会自动检测电脑中适配的python版本,自动完成编译和安装
要用这个组件包(普通应用,适用于非N卡的用户)还需要以下组件:
python&=2.6
numpy&=1.5.0 , Scipy ,BLAS 除了BLAS以外都能在&&的网站上下到,上面基本有所有你想要的包。
由于想使用 N卡,所以我经过了两天的不断挣扎,终于配置好Theano。。。尽管网上有很多简明教程,但是实际操作室还是遇到各种的问题
好吧,我承认上面那么多都是鬼扯
先说明下自己电脑的情况:
操作系统:win7 64 &i7-8GB
图形显卡:N卡 型号:Quadro 600
Visual Studio 版本
我安装了在C:\Program Files (x86)\
python编辑平台:Eclipse
在开始安装和配置之前,请先准备好以下文件:
1.&&AnacondaCE:
这个软件的好处是会自动安装python.exe和theano的所有依赖项,同是自带了Spyder的IDE,对于电脑没装过python的人非常好,它里面集成了大量常用的组件,&下载地址:&(python2.x系列)
或&&(python3.3系列)
(我是win7 64bit,选择Anaconda-1.4.0-Windows-x86_64.exe (python2.7.4),这个位数和以后的显卡配置有莫大关系)
2. &Theano:
下载地址:
这是适用于 Anaconda的快速安装版,自动使用pip安装到上面的Anaconda的python中
以防万一,为了同时下载theano的zip文件(theano-master):&,如果上述网址的包不能使用可以下载这个&
3. CUDA:其中的cuda toolkit就是使用N卡代替CPU运算的组件
注意,请根据自己显卡的型号下载相应的版本,一般5.5就可以了,64位的电脑一定要下64位的,我第一次不懂下了个6.5,结果不但没能配置好,连显卡的驱动都没了。。
4. DeepLearningTutorials测试代码
下载地址:
这个是基于python的代码,里面用的数据库是 minst手写数据库,不过,这个下载回来的压缩包中,数据库文件校验错误,后面会谈到,所以我们还要额外下载数据库pickle
5. 测试代码的完整数据库
MNIST Dataset()
6. 补充头文件关于样例代码CNN(注:运行这个GPU程序需要下载&inttypes.h","stdint.h" 这两个头文件,然后放到VS2010/VC/include
在这里下载 &
7. VS2010在网上都有得下
总的文件:
=======以上就是所有的准备工作==========================================
开始安装:(注意关掉360之类的杀软,以免误报)
step 1&: 安装 AnacondaCE,默认安装在C盘,最好也装在C盘
& & & & & 添加环境变量:path: C:\Anaconda\MinGW\C:\Anaconda\MinGW\x86_64-w64-mingw32\
& & & & & 添加环境变量:path:&C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64;
& & & & &&避免以后import 时显示找不到 "cl.exe" 以至于出现检测到安装了CUDA但gpu不可用的现象。
step 2:重启电脑,AnaconadaCE会配置 pip
step 3:安装Theano:
& & & & & & 先应用方法1:直接安装&自动安装即可。
注意,这一步可能会出现以下问题(我重新安装theano是就出现过了,想卸载和修复都不行,安装也不能选)
解决方法:或直接应用方法2:
& & & & & 方法2:
& & & & & (1)下载theano的zip文件:/Theano/Theano ,解压,文件里面有个theano的文件夹,拿出来放在C:/Anaconda/Lib/site-packages里面)
& & & & &&(2)新建环境变量: &PYTHONPATH: C:\Anaconda\Lib\site-packages\
& & & & & (3)在home目录下(cmd可以看到,我的是C:\Users\Administrator),创建(如已经存在则修改) .theanorc.txt 文件内容如下:
保存。然后在AnacondaCE的Spyder命令行中里输入如下命令:
import&theanoprint&theano.config.blas.ldflags
如果没有报错(无显示则正常),说明theano安装成功。
PS. &Eclipse下的安装配置
1. 安装Eclipse
2. Eclipse中Help -& Eclipse MarketPlace中,搜索python发现PyDev并直接Install。
3. Eclipse中Window -& Preferences -& PyDev -& Interpreters -& Python Interpreter点击New,Browse中输入Anaconda根目录下的python.exe。确定。
(注意,当电脑同时安装了多个python版本时,可以将不同的python复制后重名如:python27,python33,python27_64等,添加环境变量后在cmd中使用复制的名称即可。)
最后,可以使用 官网的教程第一节来测试是否theano安装成功:&
自此,python的配置以及theano的安装暂时告一段落,非N卡的同学可以不用看了,下面让我们开始无限作死的GPU配置:
setp 4:配置GPU
CUDA的配置:
按照这个手把手教程配置就OK了,注意cuda的版本要和系统位数与显卡型号匹配!!!而且记得要关掉杀软否则会出现很吐血的事情,当时装着装着,驱动没了,结果变成使用内存显示,内个卡啊。。。。
step 5:修改 .theanorc.txt&
为了使用GPU,我们要再次修改上面的 .theanorc.txt,将其中的内容改为:
保存。重启下。
在AnacondaCE中运行:
import&theano
theano.test ( )
如果出现红线画住的输出:(感谢&)
congratulations!
step 6:测试DeeplearningTutorial代码
下载好上面的代码后,里面的数据库文件有错误,将 *\DeepLearningTutorials-master\data文件夹中的 原mnist.pkl.gz(约4M)替换为:
上面的补充下载,(约15M)即可
可以code运行里面时间较短的例子看看,一开始很慢,各种DEBUG和warning,后来会有正常的输出
PS. 在安装好theano和cuda之后,可能cnn,rbm等测试例子还不能跑。这时候要在vs目录C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include添加inttypes.h,stdint.h这两个文件。
下面是每个例子的测试过程,时间太长了,我没有一个个试。。
到此,我们终于完成了所有的配置,enjoy it。
官网安装教程
相关链接 懒得收藏了
/cuda-toolkit-archive
/cuda-toolkit-50-archive
/cuda/cuda-getting-started-guide-for-microsoft-windows/index.html#verify-you-have-cuda-enabled-system
成功配置GPU的样子:配置:ubuntu 14.04, 无GPU . 参考:相关库的版本:error如下:
不知道这是什么错误,建议答主搜索关键词“theano optimization scan”。ps.发错误信息比发截图更容易收到网友的解答,毕竟搜索文字是更直接的方式。
已有帐号?
无法登录?
社交帐号登录
减一辈子肥theano linear regression exercise(theano 线性回归练习)
& & & & 最近学习theano工具包 做deep learning,这个包最令人激动的是自动导数计算,你给出符号化的公式之后,能自动生成导数,这是最吸引我的地方之一。最重要的是能对用户透明的使用CPU or GPU加速,单纯的用CPU(6核)加速最少提高了10倍,我的显卡太差了,所以它默认用了CPU。比起matlab整deep learning我觉得这个库非常的有市场。
& & & &环境说明,因为要用到g++作为底层编译GPU/CPU加速的代码,所以自己手动的部署整个环境非常的麻烦,我用的academic licence 的Enthought &canopy,几乎只要这一个就能把所有的依赖包都加进去,不过他用的theano版本可能比较老会出现各种问题,你可以自己用pip install theano自己安装一个最新的,这样问题减少很多(python2.7)。
& & & 线性回归,应该是作为使用这个工具包,感受其调试执行过程的最重要的第一步,当然,目前就我所知,我还没找到一个这样的练习,本例也是我自己设计的,十分简单,但是却是一个机器学习程序的整个完整框架,大部分的思路来源于theano tutorial 这里是链接&&()
& & &整个theano 工具包的核心是tensor 库,作为一个符号化系统库,本身有很多有趣的机制,比如graphic 你要理解整个求导的一个机制,就要看这个,然后就是theano.function这个伟大的函数了,有了导数,只是有了符号化的导数,要计算,要梯度下降就需要连接理想与现实的函数theano.function
& & 这里介绍下几个重要的东西,一个是theano.shared()函数,这个函数的厉害之处在于,第一,返回tensor类型的变量,第二,生成的变量类似于全局变量在几个函数间共享使用。要操作好theano,function 这两点必须要有深刻的认识。下面是theano.function函数签名
function.function(inputs, outputs, mode=None, updates=None, givens=None, no_default_updates=False, accept_inplace=False,&
& & 这里的updates的目标就需要shared变量,givens的数据源若是通过下标索引,也只能是shared变量(这里,我个人得出的结论,可以用,但也许是错的),比如样本是动态的更新来更新参数的时候这点特别重要。
& & 下面贴出整个代码:
# -*- coding: utf-8 -*-
import numpy as np
import theano.tensor as T
import theano
import time
class Linear_Reg(object):
def __init__(self,x):
self.a = theano.shared(value = np.zeros((1,),
dtype=theano.config.floatX),name = 'a')
self.b = theano.shared(value = np.zeros((1,),
dtype=theano.config.floatX),name = 'b')
self.result = self.a * x + self.b
self.params = [self.a,self.b]
def msl(self,y):
return T.mean((y - self.result)**2)
def run():
rate = 0.01
data = np.linspace(1,10,10)
# y = 3 * x + 1 最后的random是加了一些随机的噪声 不然求出来的回归毫无意义
labels = data * 3 + np.ones(data.shape[0],dtype=np.float64) + np.random.rand(data.shape[0])
print labels
X = theano.shared(np.asarray(data,
dtype=theano.config.floatX),borrow = True)
Y = theano.shared(np.asarray(labels,
dtype=theano.config.floatX),borrow = True)
index = T.lscalar()
x = T.dscalar('x')
y = T.dscalar('y')
reg = Linear_Reg(x = x)
cost = reg.msl(y)
a_g = T.grad(cost = cost,wrt = reg.a)
b_g = T.grad(cost = cost, wrt = reg.b)
updates=[(reg.a,reg.a - rate * a_g),(reg.b,reg.b - rate * b_g)]
train_model = theano.function(inputs=[index],
outputs = reg.msl(y),
updates = updates,
givens = {
x:X[index],
y:Y[index]
done = True
last = 0.0
start_time = time.clock()
while done:
err_s = [train_model(i) for i in xrange(data.shape[0])]
err = np.mean(err_s)
#print err
count = count + 1
if count & 10000 or err &0.1:
done = False
last = err
end_time = time.clock()
print 'Total time is :',end_time -start_time,' s' # 5.12s
print 'last error :',err
print 'a value : ',reg.a.get_value() #
print 'b value : ',reg.b.get_value() # [ 1.]
基本思想是每一次取一个点,然后梯度下降,严格上样本点最好应该是随机取的,但是为了实验方便按顺序取,迭代这么多次,只是为了保证收敛。本程序更大的作用是作为示例,这么套框架,这样是可以的,在使用function中如果用类似的处理办法,就可以分析问题所在了,贴出代码的初衷也是给自己一个今后的模版,省事很多!刚学theano,有些拙劣的意见,还请指教!
本分类共有文章4篇,更多信息详见
& 2012 - 2016 &
&All Rights Reserved. &
/*爱悠闲图+*/
var cpro_id = "u1888441";
/*爱悠闲底部960*75*/
var cpro_id = "u1888128";20155人阅读
机器学习(3)
我的百度云里面有windows下安装theano的相关软件包,如cuda5.0,EPD等,
之前一直在linux下用theano,gpu很好配。上周需要在windows下工作,于是折腾了一周,刚才莫名其妙就配好了gpu。
先上一个theano成功使用gpu的截图
下面是我配置theano的经验:
基本上是两步走:
1、安装theano
2、安装cuda
注意在win7 64位下,python和cuda要统一,要么都用32位,要么都用64位的。
在windows下最麻烦的可能就是安装theano了,根据官方文档的步骤一步步走。
安装theano:
首先要安装theano的依赖项,最主要的就是numpy,scipy,nose了。刚开始我分别独立安装了numpy,scipy,然后用源码安装了theano,但是装好cuda后,死活用不了gpu;后来尝试用pythonxy安装theano的依赖项,然后基于theano源码,用python setup.py install安装,但是gpu还是用不了,整整折腾了一周啊。。。
下面我安装官网的第一个方式,也就是&.安装theano的依赖项,然后用pip安装theano,最后竟然gpu能用了。
安装cuda:
cuda用的是cuda_5.0.35_winvista_win7_win8_general_32-3.msi,下载地址
vs我用的是2008,建议不要用vs2012,不然比较麻烦
下面是我theano的配置文件,跟官网上一样,可以用的
.theanorc.txt:
在安装好theano和cuda之后,可能cnn,rbm等测试例子还不能跑。这时候要在vs目录C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include添加inttypes.h,stdint.h这两个文件,它们都可以在我的百度云里面下载。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:140395次
积分:1343
积分:1343
排名:千里之外
原创:19篇
评论:70条
(1)(3)(2)(6)(2)(1)(1)(2)(4)(1)(1)(4)保存在这里,供大家参考
import six.moves.cPickle as pickle
import gzip
import sys
import timeit
import numpy
import theano
import theano.tensor as T
def load_data(dataset):
with gzip.open(dataset, 'rb') as f:
train_set, valid_set, test_set = pickle.load(f, encoding='latin1')
train_set, valid_set, test_set = pickle.load(f)
def shared_dataset(data_xy, borrow = True):
data_x, data_y = data_xy
shared_x = theano.shared(numpy.asarray(data_x, dtype = theano.config.floatX), borrow = borrow)
shared_y = theano.shared(numpy.asarray(data_y, dtype = theano.config.floatX), borrow = borrow)
return shared_x, T.cast(shared_y, 'int32')
test_set_x, test_set_y = shared_dataset(test_set)
valid_set_x, valid_set_y = shared_dataset(valid_set)
train_set_x, train_set_y = shared_dataset(train_set)
res = [(train_set_x, train_set_y), (valid_set_x, valid_set_y), (test_set_x, test_set_y)]
return res
class LogisticRegression(object):
def __init__(self, input, n_in, n_out):
self.w = theano.shared(value= numpy.zeros( (n_in, n_out), dtype = theano.config.floatX), name = 'w', borrow = True)
self.b = theano.shared(value= numpy.zeros( n_out, dtype = theano.config.floatX), name = 'b', borrow = True)
self.pyx = T.nnet.softmax(T.dot(input, self.w) + self.b )
self.y_pred = T.argmax(self.pyx, axis = 1)
self.params = [self.w, self.b]
self.input = input
def negative_log_likelihood(self, y):
return -T.mean( T.log(self.pyx)[T.arange(y.shape[0]), y] )
def errors(self, y):
if (y.ndim != self.y_pred.ndim):
raise TypeError(('y', y.type, 'y_pred', y_pred.type))
if y.dtype.startswith('int'):
return T.mean(T.neq(self.y_pred, y))
raise NotImplementedError()
def sgd_optimization_mnist(learning_rate = 0.13, n_epoches = 1000, dataset = 'mnist.pkl.gz', batch_size = 600):
data = load_data(dataset)
train_x, train_y = data[0]
valid_x, valid_y = data[1]
test_x, test_y = data[2]
n_train_batch = train_x.get_value(borrow=True).shape[0] // batch_size
n_valid_batch = valid_x.get_value(borrow=True).shape[0] // batch_size
n_test_batch = test_x.get_value(borrow=True).shape[0] // batch_size
x = T.matrix('x')
y = T.ivector('y')
index = T.lscalar()
classifier = LogisticRegression(input = x, n_in = 28*28, n_out = 10)
cost = classifier.negative_log_likelihood(y)
test_model = theano.function(
inputs = [index],
outputs = classifier.errors(y),
givens = {
x: test_x[index * batch_size: (index+1) * batch_size],
y: test_y[index * batch_size: (index+1) * batch_size]
valid_model = theano.function(
inputs = [index],
outputs = classifier.errors(y),
givens = {
x: valid_x[index * batch_size: (index+1) * batch_size],
y: valid_y[index * batch_size: (index+1) * batch_size]
g_w = T.grad(cost, classifier.w)
g_b = T.grad(cost, classifier.b)
updates = [(classifier.w, classifier.w - g_w * learning_rate),(classifier.b, classifier.b - g_b * learning_rate)]
train_model = theano.function(
inputs = [index],
outputs = cost,
updates = updates,
givens = {
x: train_x[index * batch_size: (index+1)*batch_size],
y: train_y[index * batch_size: (index+1)*batch_size]
best_validation_loss = numpy.inf
improvement_threshold = 0.995
max_epoch = 100
start_time = timeit.default_timer()
while (epoch & max_epoch):
for minibatch_index in range(n_train_batch):
minibatch_cost = train_model(minibatch_index)
validation_loss_ary =
[valid_model(i) for i in range(n_valid_batch)]
validation_loss = numpy.mean(validation_loss_ary)
if(validation_loss & best_validation_loss ):
if(validation_loss & best_validation_loss * improvement_threshold):
max_epoch = max(max_epoch, epoch * 2)
best_validation_loss = validation_loss
with open('best_model.pkl', 'wb') as f:
pickle.dump(classifier, f)
print('epoch %i, now validation_loss is %f %%' % (epoch, validation_loss * 100.))
end_time = timeit.default_timer()
print('The code run for %d epochs, with %f epochs/sec' % (epoch, 1. * epoch / (end_time - start_time)))
'''
predict_model = theano.function(
inputs = [index],
outputs = classifier.errors(y),
givens = {
x: test_x[index * batch_size: (index+1) * batch_size],
y: test_y[index * batch_size: (index+1) * batch_size]
test_ans = [predict_model(i) for i in range(n_test_batch)]
test_error = numpy.mean(test_ans)
print('test error is %f %%' % test_error*100)
'''
def predict():
temp = pickle.load(open('best_model.pkl'))
dataset = 'mnist.pkl.gz'
data = load_data(dataset)
test_x, test_y = data[2];
x = T.matrix('x')
y = T.ivector('y')
index = T.iscalar()
classifier = LogisticRegression(x,28*28,10)
classifier.w.set_value (temp.w.get_value())
classifier.b.set_value (temp.b.get_value())
batch_size = 600
n_test_batch = test_x.get_value(borrow = True).shape[0] // batch_size
predict_model = theano.function(
inputs = [index],
outputs = classifier.errors(y),
givens = {
x: test_x[index * batch_size: (index+1) * batch_size],
y: test_y[index * batch_size: (index+1) * batch_size]
test_ans = [predict_model(i) for i in range(n_test_batch)]
test_error = numpy.mean(test_ans)
print('test error is %f %%' % (test_error*100.) )
if __name__ == '__main__':
sgd_optimization_mnist()
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:240319次
积分:5492
积分:5492
排名:第3302名
原创:302篇
转载:19篇
评论:119条
(1)(1)(1)(1)(1)(2)(11)(2)(1)(8)(11)(6)(9)(8)(2)(2)(1)(1)(2)(5)(15)(9)(24)(30)(5)(20)(13)(43)(23)(26)(10)(14)(5)(1)(3)}

我要回帖

更多关于 theano test要多久 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信