diff --git a/README.md b/README.md index aa618b0..97ae7b3 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,12 @@ ## Overview -### neuralnetworksanddeeplearning.com integrated scripts for Python 3.5.2 and Theano with CUDA support +### neuralnetworksanddeeplearning.com integrated scripts for Python 3 and Aesara with CUDA support -These scrips are updated ones from the **neuralnetworksanddeeplearning.com** gitHub repository in order to work with Python 3.5.2 +These scrips are updated ones from the **neuralnetworksanddeeplearning.com** gitHub repository in order to work with Python 3 The testing file (**test.py**) contains all three networks (network.py, network2.py, network3.py) from the book and it is the starting point to run (i.e. *train and evaluate*) them. -## Just type at shell: **python3.5 test.py** +## Just type at shell: **python3 test.py** In test.py there are examples of networks configurations with proper comments. I did that to relate with particular chapters from the book. - diff --git a/network3.py b/network3.py index ff8afa8..568d5fe 100644 --- a/network3.py +++ b/network3.py @@ -1,7 +1,7 @@ """network3.py ~~~~~~~~~~~~~~ -A Theano-based program for training and running simple neural +A Aesara-based program for training and running simple neural networks. Supports several layer types (fully connected, convolutional, max @@ -12,14 +12,14 @@ network2.py. However, unlike network.py and network2.py it can also be run on a GPU, which makes it faster still. -Because the code is based on Theano, the code is different in many +Because the code is based on Aesara, the code is different in many ways from network.py and network2.py. However, where possible I have tried to maintain consistency with the earlier programs. In particular, the API is similar to network2.py. Note that I have focused on making the code simple, easily readable, and easily modifiable. It is not optimized, and omits many desirable features. -This program incorporates ideas from the Theano documentation on +This program incorporates ideas from the Aesara documentation on convolutional neural nets (notably, http://deeplearning.net/tutorial/lenet.html ), from Misha Denil's implementation of dropout (https://github.com/mdenil/dropout ), and @@ -34,18 +34,18 @@ # Third-party libraries import numpy as np -import theano -import theano.tensor as T -from theano.tensor.nnet import conv -from theano.tensor.nnet import softmax -from theano.tensor import shared_randomstreams -from theano.tensor.signal.pool import pool_2d +import aesara +import aesara.tensor as T +from aesara.tensor.nnet import conv +from aesara.tensor.nnet import softmax +from aesara.tensor import random +from aesara.tensor.signal.pool import pool_2d # Activation functions for neurons def linear(z): return z def ReLU(z): return T.maximum(0.0, z) -from theano.tensor.nnet import sigmoid -from theano.tensor import tanh +from aesara.tensor.nnet.basic import sigmoid +from aesara.tensor import tanh #### Constants @@ -53,9 +53,9 @@ def ReLU(z): return T.maximum(0.0, z) if GPU: print("Trying to run under a GPU. If this is not desired, then modify "+\ "network3.py\nto set the GPU flag to False.") - try: theano.config.device = 'gpu' + try: aesara.config.device = 'gpu' except: pass # it's already set - theano.config.floatX = 'float32' + aesara.config.floatX = 'float32' else: print("Running with a CPU. If this is not desired, then the modify "+\ "network3.py to set\nthe GPU flag to True.") @@ -66,14 +66,14 @@ def load_data_shared(filename="mnist.pkl.gz"): training_data, validation_data, test_data = pickle.load(f, encoding="latin1") f.close() def shared(data): - """Place the data into shared variables. This allows Theano to copy + """Place the data into shared variables. This allows Aesara to copy the data to the GPU, if one is available. """ - shared_x = theano.shared( - np.asarray(data[0], dtype=theano.config.floatX), borrow=True) - shared_y = theano.shared( - np.asarray(data[1], dtype=theano.config.floatX), borrow=True) + shared_x = aesara.shared( + np.asarray(data[0], dtype=aesara.config.floatX), borrow=True) + shared_y = aesara.shared( + np.asarray(data[1], dtype=aesara.config.floatX), borrow=True) return shared_x, T.cast(shared_y, "int32") return [shared(training_data), shared(validation_data), shared(test_data)] @@ -123,7 +123,7 @@ def SGD(self, training_data, epochs, mini_batch_size, eta, # define functions to train a mini-batch, and to compute the # accuracy in validation and test mini-batches. i = T.lscalar() # mini-batch index - train_mb = theano.function( + train_mb = aesara.function( [i], cost, updates=updates, givens={ self.x: @@ -131,7 +131,7 @@ def SGD(self, training_data, epochs, mini_batch_size, eta, self.y: training_y[i*self.mini_batch_size: (i+1)*self.mini_batch_size] }) - validate_mb_accuracy = theano.function( + validate_mb_accuracy = aesara.function( [i], self.layers[-1].accuracy(self.y), givens={ self.x: @@ -139,7 +139,7 @@ def SGD(self, training_data, epochs, mini_batch_size, eta, self.y: validation_y[i*self.mini_batch_size: (i+1)*self.mini_batch_size] }) - test_mb_accuracy = theano.function( + test_mb_accuracy = aesara.function( [i], self.layers[-1].accuracy(self.y), givens={ self.x: @@ -147,7 +147,7 @@ def SGD(self, training_data, epochs, mini_batch_size, eta, self.y: test_y[i*self.mini_batch_size: (i+1)*self.mini_batch_size] }) - self.test_mb_predictions = theano.function( + self.test_mb_predictions = aesara.function( [i], self.layers[-1].y_out, givens={ self.x: @@ -210,15 +210,15 @@ def __init__(self, filter_shape, image_shape, poolsize=(2, 2), self.activation_fn=activation_fn # initialize weights and biases n_out = (filter_shape[0]*np.prod(filter_shape[2:])/np.prod(poolsize)) - self.w = theano.shared( + self.w = aesara.shared( np.asarray( np.random.normal(loc=0, scale=np.sqrt(1.0/n_out), size=filter_shape), - dtype=theano.config.floatX), + dtype=aesara.config.floatX), borrow=True) - self.b = theano.shared( + self.b = aesara.shared( np.asarray( np.random.normal(loc=0, scale=1.0, size=(filter_shape[0],)), - dtype=theano.config.floatX), + dtype=aesara.config.floatX), borrow=True) self.params = [self.w, self.b] @@ -241,15 +241,15 @@ def __init__(self, n_in, n_out, activation_fn=sigmoid, p_dropout=0.0): self.activation_fn = activation_fn self.p_dropout = p_dropout # Initialize weights and biases - self.w = theano.shared( + self.w = aesara.shared( np.asarray( np.random.normal( loc=0.0, scale=np.sqrt(1.0/n_out), size=(n_in, n_out)), - dtype=theano.config.floatX), + dtype=aesara.config.floatX), name='w', borrow=True) - self.b = theano.shared( + self.b = aesara.shared( np.asarray(np.random.normal(loc=0.0, scale=1.0, size=(n_out,)), - dtype=theano.config.floatX), + dtype=aesara.config.floatX), name='b', borrow=True) self.params = [self.w, self.b] @@ -274,11 +274,11 @@ def __init__(self, n_in, n_out, p_dropout=0.0): self.n_out = n_out self.p_dropout = p_dropout # Initialize weights and biases - self.w = theano.shared( - np.zeros((n_in, n_out), dtype=theano.config.floatX), + self.w = aesara.shared( + np.zeros((n_in, n_out), dtype=aesara.config.floatX), name='w', borrow=True) - self.b = theano.shared( - np.zeros((n_out,), dtype=theano.config.floatX), + self.b = aesara.shared( + np.zeros((n_out,), dtype=aesara.config.floatX), name='b', borrow=True) self.params = [self.w, self.b] @@ -305,7 +305,7 @@ def size(data): return data[0].get_value(borrow=True).shape[0] def dropout_layer(layer, p_dropout): - srng = shared_randomstreams.RandomStreams( + srng = random.utils.RandomStream( np.random.RandomState(0).randint(999999)) mask = srng.binomial(n=1, p=1-p_dropout, size=layer.shape) - return layer*T.cast(mask, theano.config.floatX) + return layer*T.cast(mask, aesara.config.floatX) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..bda4b33 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +aesara diff --git a/test.py b/test.py index d1f4996..bb7c264 100644 --- a/test.py +++ b/test.py @@ -1,9 +1,9 @@ """ Testing code for different neural network configurations. - Adapted for Python 3.5.2 + Adapted for Python 3 Usage in shell: - python3.5 test.py + python3 test.py Network (network.py and network2.py) parameters: 2nd param is epochs count @@ -88,28 +88,26 @@ # ---------------------- -# Theano and CUDA +# Aesara and CUDA # ---------------------- """ - This deep network uses Theano with GPU acceleration support. + This deep network uses Aesara with GPU acceleration support. I am using Ubuntu 16.04 with CUDA 7.5. - Tutorial: - http://deeplearning.net/software/theano/install_ubuntu.html#install-ubuntu - The following command will update only Theano: - sudo pip install --upgrade --no-deps theano + The following command will update only Aesara: + sudo pip install --upgrade --no-deps aesara - The following command will update Theano and Numpy/Scipy (warning bellow): - sudo pip install --upgrade theano + The following command will update Aesara and Numpy/Scipy (warning bellow): + sudo pip install --upgrade aesara """ """ Below, there is a testing function to check whether your computations have been made on CPU or GPU. If the result is 'Used the cpu' and you want to have it in gpu, do the following: - 1) install theano: - sudo python3.5 -m pip install Theano + 1) install aesara: + sudo python3 -m pip install aesara 2) download and install the latest cuda: https://developer.nvidia.com/cuda-downloads I had some issues with that, so I followed this idea (better option is to download the 1,1GB package as .run file): @@ -117,19 +115,18 @@ You may also want to grab the proper NVidia driver, choose it form there: System Settings > Software & Updates > Additional Drivers. 3) should work, run it with: - THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python3.5 test.py - http://deeplearning.net/software/theano/tutorial/using_gpu.html + AESARA_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python3 test.py 4) Optionally, you can add cuDNN support from: https://developer.nvidia.com/cudnn """ -def testTheano(): - from theano import function, config, shared, sandbox - import theano.tensor as T +def testAesara(): + from aesara import function, config, shared, sandbox + import aesara.tensor as T import numpy import time - print("Testing Theano library...") + print("Testing Aesara library...") vlen = 10 * 30 * 768 # 10 x #cores x # threads per core iters = 1000 @@ -148,7 +145,7 @@ def testTheano(): else: print('Used the gpu') # Perform check: -#testTheano() +#testAesara() # ----------------------