win10-64位安装机器学习Theano包

最近开始学习深度学习相关知识,发现很多地方需要用到theano包。由于自己之前没有接触过theano等机器学习的安装,所以从开始尝试到最后成功安装花了好几天时间。

我自己电脑的环境是win10-64位;安装了vs2013,也可以是其他版本,不过如果想要安装CUDA,就选用非2015版的(亲测不支持,都是泪啊!)。

开始安装:

一、安装Anaconda

anaconda是一款自带python环境的免费软件,而且嵌入了很多必须的库,省去不少安装时间,非常方便。下面,到anaconda官网下载最新版软件,注意要与自己的系统相匹配,下载时选择python2.x版本(建议python2.7),因为在Deep Learning Tutorials中使用的代码都是python2的,使用python3会有语法错误,改起来很麻烦。下载完,安装,过程中建议选择all users,然后路径默认,我的是C:\Anaconda2。安装完,打开CMD,输入python,如果不出错,就表示安装成功。

二、安装MinGW

以管理员身份运行CMD,输入conda install mingw libpython,回车,下面会出现Fetching package...字样,等一会,就会自己安装,最后看到successfully就表示成功了。这样就能把MinGW安装到Anaconda目录下面了。

三、新建环境变量

1、右键此电脑——属性——高级系统设置——环境变量,在用户变量中点击“新建”,变量名为path(如果存在,直接双击,然后点击添加),变量值按照自己anaconda安装的位置来定,我的是C:\Anaconda2;C:\Anaconda2\Scripts;(注意这两个分号是英文模式下的,在新建的时候将上面红色部分复制过去,如果是添加,只能将上面两句分开加,即添加两次,不用分号)。

2、在用户变量中新建,变量名为pythonpath,变量值为C:\Anaconda\Lib\site-packages\theano;

3、在Users中自己的目录下(不知道的时候就打开CMD,右上角显示的路径就是),新建一个名叫.theano.txt的文本文件(把查看中文件后缀名显示出来,否则容易建成.theano.txt.txt),用写字板打开,输入一下内容:

[global]
openmp=False
[blas]
ldflags=
[gcc]
cxxflags=-IC:\Anaconda2\MinGW

上面红色路径为自己安装的anaconda路径。

4、保存并重启电脑

四、下载并安装theano

1、打开CMD,输入pip install theano,回车,稍微等一会,就会显示安装成功。

2、在CMD中输入python,进去python编译环境,输入import theano,回车,首次输入,要等一会才好。下面可以输入theano.test()来测试是否安装成功,也可以打开anaconda2里面的spyder,直接测试一下下面这段代码能不能正确运行。

<span style="font-size:14px;">import theano
from theano import tensor as T
from theano.tensor.nnet import conv2d
import numpy
import pylab
from PIL import Image
rng = numpy.random.RandomState(23455)

# instantiate 4D tensor for input
input = T.tensor4(name='input')

# initialize shared variable for weights.
w_shp = (2, 3, 9, 9)
w_bound = numpy.sqrt(3 * 9 * 9)
W = theano.shared( numpy.asarray(
            rng.uniform(
                low=-1.0 / w_bound,
                high=1.0 / w_bound,
                size=w_shp),
            dtype=input.dtype), name ='W')

# initialize shared variable for bias (1D tensor) with random values
# IMPORTANT: biases are usually initialized to zero. However in this
# particular application, we simply apply the convolutional layer to
# an image without learning the parameters. We therefore initialize
# them to random values to "simulate" learning.
b_shp = (2,)
b = theano.shared(numpy.asarray(
            rng.uniform(low=-.5, high=.5, size=b_shp),
            dtype=input.dtype), name ='b')

# build symbolic expression that computes the convolution of input with filters in w
conv_out = conv2d(input, W)

# build symbolic expression to add bias and apply activation function, i.e. produce neural net layer output
# A few words on ``dimshuffle`` :
#   ``dimshuffle`` is a powerful tool in reshaping a tensor;
#   what it allows you to do is to shuffle dimension around
#   but also to insert new ones along which the tensor will be
#   broadcastable;
#   dimshuffle('x', 2, 'x', 0, 1)
#   This will work on 3d tensors with no broadcastable
#   dimensions. The first dimension will be broadcastable,
#   then we will have the third dimension of the input tensor as
#   the second of the resulting tensor, etc. If the tensor has
#   shape (20, 30, 40), the resulting tensor will have dimensions
#   (1, 40, 1, 20, 30). (AxBxC tensor is mapped to 1xCx1xAxB tensor)
#   More examples:
#    dimshuffle('x') -> make a 0d (scalar) into a 1d vector
#    dimshuffle(0, 1) -> identity
#    dimshuffle(1, 0) -> inverts the first and second dimensions
#    dimshuffle('x', 0) -> make a row out of a 1d vector (N to 1xN)
#    dimshuffle(0, 'x') -> make a column out of a 1d vector (N to Nx1)
#    dimshuffle(2, 0, 1) -> AxBxC to CxAxB
#    dimshuffle(0, 'x', 1) -> AxB to Ax1xB
#    dimshuffle(1, 'x', 0) -> AxB to Bx1xA
output = T.nnet.sigmoid(conv_out + b.dimshuffle('x', 0, 'x', 'x'))

# create theano function to compute filtered images
f = theano.function([input], output)

# open random image of dimensions 639x516

img = Image.open(r'C:\\Users\USER\Desktop\3wolfmoon.jpg','r')

# dimensions are (height, width, channel)
img = numpy.asarray(img, dtype='float64') / 256.

# put image in 4D tensor of shape (1, 3, height, width)
img_ = img.transpose(2, 0, 1).reshape(1, 3, 639, 516)
filtered_img = f(img_)

# plot original image and first and second components of output
pylab.subplot(1, 3, 1); pylab.axis('off'); pylab.imshow(img)
pylab.gray();
# recall that the convOp output (filtered image) is actually a "minibatch",
# of size 1 here, so we take index 0 in the first dimension:
pylab.subplot(1, 3, 2); pylab.axis('off'); pylab.imshow(filtered_img[0, 0, :, :])
pylab.subplot(1, 3, 3); pylab.axis('off'); pylab.imshow(filtered_img[0, 1, :, :])
pylab.show()
</span>

将下面的图片保存到桌面,名称是:3wolfmoon.jpg


如果能成功运行,并出现三张图片,即表示theano安装成功!


参考资料:

http://deeplearning.net/software/theano/install.html#




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值