# 使用GPU和Theano加速深度学习

【编者按】GPU因其浮点计算和矩阵运算能力有助于加速深度学习是业界的共识，Theano是主流的深度学习Python库之一，亦支持GPU，然而Theano入门较难，Domino的这篇博文介绍了如何使用GPU和Theano加速深度学习，使用更简单的基于Theano的 Nolearn库。教程由多层感知器及卷积神经网络，由浅入深，是不错的入门资料。

### 基于Python的深度学习

```-e git://github.com/Theano/Theano.git#egg=Theano
-e git://github.com/lasagne/lasagne.git#egg=lasagne
nolearn==0.5.0  ```

### 配置Theano

`[global] device = gpu floatX = float32 [nvcc] fastmath = True`

`import os import shutil destfile = "/home/ubuntu/.theanorc" open(destfile, 'a').close() shutil.copyfile(".theanorc", destfile)`

```from theano import function, config, shared, sandbox
import theano.tensor as T
import numpy
import time

vlen = 10 * 30 * 768  # 10 x #cores x # threads per core
iters = 1000
rng = numpy.random.RandomState(22)
x =shared(numpy.asarray(rng.rand(vlen),config.floatX))
f = function([], T.exp(x))
print f.maker.fgraph.toposort()
t0 = time.time()
for i in xrange(iters):
r = f()
t1 = time.time()
print 'Looping %d times took' % iters, t1 - t0, 'seconds'
print 'Result is', r
if numpy.any([isinstance(x.op,T.Elemwise) for x in f.maker.fgraph.toposort()]):
print 'Used the cpu'
else:
print 'Used the gpu'```

### 数据集

```import cPickle as pickle
import numpy as np
import os

'''Load a single file of CIFAR'''
with open(filename, 'rb') as f:
X = X.reshape(10000, 3, 32, 32).transpose(0,2,3,1).astype('float32')
Y = np.array(Y).astype('int32')
return X, Y

xs = []
ys = []
for k in range(1,6):
f = os.path.join(directory, "data_batch_%d" % k)
xs.append(X)
ys.append(Y)
Xtr = np.concatenate(xs)
Ytr = np.concatenate(ys)
return Xtr, Ytr, Xte, Yte```

### 多层感知器

```X_train_flat = np.dot(X_train[...,:3],[0.299,0.587,0.114]).reshape(X_train.shape[0],-1).astype(np.float32)
X_train_flat = (X_train_flat/255.0)-0.5
X_test_flat = np.dot(X_test[...,:3],[0.299,0.587,0.114]).reshape(X_test.shape[0],-1).astype(np.float32)
X_test_flat = (X_test_flat/255.0)-0.5  ```

```net1 = NeuralNet( layers = [ ('input', layers.InputLayer), ('hidden',
layers.DenseLayer), ('output', layers.DenseLayer), ], #layers parameters:
input_shape = (None, 1024), hidden_num_units = 100, output_nonlinearity
= softmax, output_num_units = 10, #optimization parameters: update = nesterov_momentum,
update_learning_rate = 0.01, update_momentum = 0.9,regression = False,max_epochs = 50, verbose = 1, )```

```net1 = NeuralNet( layers = [ ('input', layers.InputLayer), ('hidden1',
layers.DenseLayer), ('hidden2', layers.DenseLayer), #Added Layer Here ('output',
layers.DenseLayer), ], #layers parameters: input_shape = (None, 1024),hidden1_num_units = 100, hidden2_num_units = 100, #Added Layer Params Here```

`net1.fit(X_train_flat, y_train)  `

```y_pred1 = net1.predict(X_test_flat)
print "The accuracy of this network is: %0.2f" % (y_pred1 == y_test).mean() ```

### 卷积网络

```X_train_2d = np.dot(X_train[...,:3], [0.299, 0.587,0.114]).reshape(-1,1,32,32).astype(np.float32)
X_train_2d = (X_train_2d/255.0)-0.5
X_test_2d = np.dot(X_test[...,:3], [0.299, 0.587,0.114]).reshape(-1,1,32,32).astype(np.float32)
X_train_2d = (X_train_2d/255.0)-0.5  ```

```net2 = NeuralNet(
layers = [
('input', layers.InputLayer),
('conv1', layers.Conv2DLayer),
('pool1', layers.MaxPool2DLayer),
('conv2', layers.Conv2DLayer),
('pool2', layers.MaxPool2DLayer),
('conv3', layers.Conv2DLayer),
('pool3', layers.MaxPool2DLayer),
("hidden4", layers.DenseLayer),
("output", layers.DenseLayer),
],
#layer parameters:
input_shape = (None, 1, 32,32),
conv1_num_filters = 16,conv1_filter_size = (3, 3),pool1_pool_size = (2,2),
conv2_num_filters = 32,conv2_filter_size = (2, 2),pool2_pool_size =  (2,2),
conv3_num_filters = 64,conv3_filter_size = (2, 2),pool3_pool_size = (2,2),
hidden4_num_units = 200,
output_nonlinearity = softmax,
output_num_units = 10,

#optimization parameters:
update = nesterov_momentum,
update_learning_rate = 0.015,
update_momentum = 0.9,
regression = False,
max_epochs = 5,
verbose = 1,
)```

`net2.fit(X_train_2d, y_train)  `

```y_pred2 = net2.predict(X_test_2d)
print "The accuracy of this network is: %0.2f" % (y_pred2 == y_test).mean()  ```

【预告】 CSDN人工智能用户群深度分享：沈国阳解析美团推荐系统实战心得，将于8月11日20:30正式开始。您可以添加微信号“jianding_zhou”申请加入微信群，或申请加入QQ群：465538150。

952 篇文章111 人订阅

0 条评论

## 相关文章

1065

### 【干货】基于pytorch的CNN、LSTM神经网络模型调参小结

Demo 这是最近两个月来的一个小总结，实现的demo已经上传github，里面包含了CNN、LSTM、BiLSTM、GRU以及CNN与LSTM、BiLSTM...

9117

3595

3865

1302

921

3.7K8

19010

1832

### 开发 | 模型表现不好怎么办？37条妙计助你扭转局势

AI 科技评论按：读论文，看别人的模型的时候仿佛一切都顺利成章，可是等到自己训练模型的时候，麻烦一个接一个…… AI 科技评论找到了一篇国外大神 Slav Iv...

3466