|
17 | 17 |
|
18 | 18 | # ----------------------
|
19 | 19 | # - read the input data:
|
20 |
| - |
| 20 | +''' |
21 | 21 | import mnist_loader
|
22 | 22 | training_data, validation_data, test_data = mnist_loader.load_data_wrapper()
|
23 | 23 | training_data = list(training_data)
|
24 |
| - |
| 24 | +''' |
25 | 25 | # ---------------------
|
26 | 26 | # - network.py example:
|
27 |
| -import network |
| 27 | +#import network |
28 | 28 |
|
29 | 29 | '''
|
30 | 30 | net = network.Network([784, 30, 10])
|
|
33 | 33 |
|
34 | 34 | # ----------------------
|
35 | 35 | # - network2.py example:
|
36 |
| -import network2 |
| 36 | +#import network2 |
37 | 37 |
|
38 | 38 | '''
|
39 | 39 | net = network2.Network([784, 30, 10], cost=network2.CrossEntropyCost)
|
|
42 | 42 | monitor_evaluation_accuracy=True)
|
43 | 43 | '''
|
44 | 44 |
|
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. |
46 | 47 | '''
|
47 | 48 | net = network2.Network([784, 30, 10], cost=network2.CrossEntropyCost)
|
48 | 49 | net.large_weight_initializer()
|
|
77 | 78 |
|
78 | 79 | # chapter 4 - The vanishing gradient problem - deep networks are hard to train with simple SGD algorithm
|
79 | 80 | # this network learns much slower than a shallow one.
|
| 81 | +''' |
80 | 82 | net = network2.Network([784, 30, 30, 30, 30, 10], cost=network2.CrossEntropyCost)
|
81 | 83 | net.SGD(training_data, 30, 10, 0.1,
|
82 | 84 | lmbda=5.0,
|
83 | 85 | evaluation_data=validation_data,
|
84 | 86 | monitor_evaluation_accuracy=True)
|
85 |
| - |
| 87 | +''' |
86 | 88 |
|
87 | 89 |
|
88 | 90 | # ----------------------
|
89 |
| -# - network3.py example: |
90 |
| -import network3 |
| 91 | +# Theano and CUDA |
| 92 | +# ---------------------- |
91 | 93 |
|
92 | 94 | """
|
93 | 95 | This deep network uses Theano with GPU acceleration support.
|
94 | 96 | I am using Ubuntu 16.04 with CUDA 7.5.
|
| 97 | + Tutorial: |
| 98 | + http://deeplearning.net/software/theano/install_ubuntu.html#install-ubuntu |
| 99 | +
|
95 | 100 |
|
96 | 101 | """
|
97 | 102 |
|
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