Skip to content

Commit f403c09

Browse files
Working on network3.py and Theano with CUDA
1 parent 092007f commit f403c09

File tree

2 files changed

+77
-25
lines changed

2 files changed

+77
-25
lines changed

network3.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ def ReLU(z): return T.maximum(0.0, z)
6161
"network3.py to set\nthe GPU flag to True.")
6262

6363
#### Load the MNIST data
64-
def load_data_shared(filename="../data/mnist.pkl.gz"):
64+
def load_data_shared(filename="mnist.pkl.gz"):
6565
f = gzip.open(filename, 'rb')
66-
training_data, validation_data, test_data = cPickle.load(f)
66+
training_data, validation_data, test_data = pickle.load(f, encoding="latin1")
6767
f.close()
6868
def shared(data):
6969
"""Place the data into shared variables. This allows Theano to copy
@@ -93,7 +93,7 @@ def __init__(self, layers, mini_batch_size):
9393
self.y = T.ivector("y")
9494
init_layer = self.layers[0]
9595
init_layer.set_inpt(self.x, self.x, self.mini_batch_size)
96-
for j in xrange(1, len(self.layers)):
96+
for j in range(1, len(self.layers)): # xrange() was renamed to range() in Python 3.
9797
prev_layer, layer = self.layers[j-1], self.layers[j]
9898
layer.set_inpt(
9999
prev_layer.output, prev_layer.output_dropout, self.mini_batch_size)
@@ -108,9 +108,9 @@ def SGD(self, training_data, epochs, mini_batch_size, eta,
108108
test_x, test_y = test_data
109109

110110
# compute number of minibatches for training, validation and testing
111-
num_training_batches = size(training_data)/mini_batch_size
112-
num_validation_batches = size(validation_data)/mini_batch_size
113-
num_test_batches = size(test_data)/mini_batch_size
111+
num_training_batches = int(size(training_data)/mini_batch_size)
112+
num_validation_batches = int(size(validation_data)/mini_batch_size)
113+
num_test_batches = int(size(test_data)/mini_batch_size)
114114

115115
# define the (regularized) cost function, symbolic gradients, and updates
116116
l2_norm_squared = sum([(layer.w**2).sum() for layer in self.layers])
@@ -155,15 +155,15 @@ def SGD(self, training_data, epochs, mini_batch_size, eta,
155155
})
156156
# Do the actual training
157157
best_validation_accuracy = 0.0
158-
for epoch in xrange(epochs):
159-
for minibatch_index in xrange(num_training_batches):
158+
for epoch in range(epochs):
159+
for minibatch_index in range(num_training_batches):
160160
iteration = num_training_batches*epoch+minibatch_index
161161
if iteration % 1000 == 0:
162162
print("Training mini-batch number {0}".format(iteration))
163163
cost_ij = train_mb(minibatch_index)
164164
if (iteration+1) % num_training_batches == 0:
165165
validation_accuracy = np.mean(
166-
[validate_mb_accuracy(j) for j in xrange(num_validation_batches)])
166+
[validate_mb_accuracy(j) for j in range(num_validation_batches)])
167167
print("Epoch {0}: validation accuracy {1:.2%}".format(
168168
epoch, validation_accuracy))
169169
if validation_accuracy >= best_validation_accuracy:
@@ -172,7 +172,7 @@ def SGD(self, training_data, epochs, mini_batch_size, eta,
172172
best_iteration = iteration
173173
if test_data:
174174
test_accuracy = np.mean(
175-
[test_mb_accuracy(j) for j in xrange(num_test_batches)])
175+
[test_mb_accuracy(j) for j in range(num_test_batches)])
176176
print('The corresponding test accuracy is {0:.2%}'.format(
177177
test_accuracy))
178178
print("Finished training network.")

test.py

+67-15
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717

1818
# ----------------------
1919
# - read the input data:
20-
20+
'''
2121
import mnist_loader
2222
training_data, validation_data, test_data = mnist_loader.load_data_wrapper()
2323
training_data = list(training_data)
24-
24+
'''
2525
# ---------------------
2626
# - network.py example:
27-
import network
27+
#import network
2828

2929
'''
3030
net = network.Network([784, 30, 10])
@@ -33,7 +33,7 @@
3333

3434
# ----------------------
3535
# - network2.py example:
36-
import network2
36+
#import network2
3737

3838
'''
3939
net = network2.Network([784, 30, 10], cost=network2.CrossEntropyCost)
@@ -42,7 +42,8 @@
4242
monitor_evaluation_accuracy=True)
4343
'''
4444

45-
# chapter 3 - Overfitting example
45+
# chapter 3 - Overfitting example - too many epochs of learning applied on small (1k samples) amount od data.
46+
# Overfitting is treating noise as a signal.
4647
'''
4748
net = network2.Network([784, 30, 10], cost=network2.CrossEntropyCost)
4849
net.large_weight_initializer()
@@ -77,28 +78,79 @@
7778

7879
# chapter 4 - The vanishing gradient problem - deep networks are hard to train with simple SGD algorithm
7980
# this network learns much slower than a shallow one.
81+
'''
8082
net = network2.Network([784, 30, 30, 30, 30, 10], cost=network2.CrossEntropyCost)
8183
net.SGD(training_data, 30, 10, 0.1,
8284
lmbda=5.0,
8385
evaluation_data=validation_data,
8486
monitor_evaluation_accuracy=True)
85-
87+
'''
8688

8789

8890
# ----------------------
89-
# - network3.py example:
90-
import network3
91+
# Theano and CUDA
92+
# ----------------------
9193

9294
"""
9395
This deep network uses Theano with GPU acceleration support.
9496
I am using Ubuntu 16.04 with CUDA 7.5.
97+
Tutorial:
98+
http://deeplearning.net/software/theano/install_ubuntu.html#install-ubuntu
99+
95100
96101
"""
97102

98-
# from network3 import ConvPoolLayer, FullyConnectedLayer, SoftmaxLayer
99-
# training_data, validation_data, test_data = network3.load_data_shared()
100-
# mini_batch_size = 10
101-
# net = Network([
102-
# FullyConnectedLayer(n_in=784, n_out=100),
103-
# SoftmaxLayer(n_in=100, n_out=10)], mini_batch_size)
104-
# net.SGD(training_data, 60, mini_batch_size, 0.1, validation_data, test_data)
103+
"""
104+
Testing function to check whether your computations have been made on CPU or GPU.
105+
If the result is 'Used the cpu' and you want to have it in gpu, do the following:
106+
1) install theano:
107+
sudo python3.5 -m pip install Theano
108+
2) download and install the latest cuda:
109+
https://developer.nvidia.com/cuda-downloads
110+
I had some issues with that, so I followed this idea (better option is to download the 1,1GB package as .run file):
111+
http://askubuntu.com/questions/760242/how-can-i-force-16-04-to-add-a-repository-even-if-it-isnt-considered-secure-eno
112+
You may also want to grab the proper NVidia driver, choose it form there:
113+
System Settings > Software & Updates > Additional Drivers.
114+
3)
115+
116+
"""
117+
118+
def testTheano():
119+
from theano import function, config, shared, sandbox
120+
import theano.tensor as T
121+
import numpy
122+
import time
123+
124+
vlen = 10 * 30 * 768 # 10 x #cores x # threads per core
125+
iters = 1000
126+
127+
rng = numpy.random.RandomState(22)
128+
x = shared(numpy.asarray(rng.rand(vlen), config.floatX))
129+
f = function([], T.exp(x))
130+
print(f.maker.fgraph.toposort())
131+
t0 = time.time()
132+
for i in range(iters):
133+
r = f()
134+
t1 = time.time()
135+
print("Looping %d times took %f seconds" % (iters, t1 - t0))
136+
print("Result is %s" % (r,))
137+
if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):
138+
print('Used the cpu')
139+
else:
140+
print('Used the gpu')
141+
142+
# Perform check:
143+
#testTheano()
144+
145+
146+
# ----------------------
147+
# - network3.py example:
148+
import network3
149+
150+
from network3 import ConvPoolLayer, FullyConnectedLayer, SoftmaxLayer
151+
training_data, validation_data, test_data = network3.load_data_shared()
152+
mini_batch_size = 10
153+
net = network3.Network([
154+
FullyConnectedLayer(n_in=784, n_out=100),
155+
SoftmaxLayer(n_in=100, n_out=10)], mini_batch_size)
156+
net.SGD(training_data, 60, mini_batch_size, 0.1, validation_data, test_data)

0 commit comments

Comments
 (0)