Learn Theano – Quick Guide work project make money

Theano – Quick Guide Theano – Introduction Have you developed Machine Learning models in Python? Then, obviously you know the intricacies in developing these models. The development is typically a slow process taking hours and days of computational power. The Machine Learning model development requires lot of mathematical computations. These generally require arithmetic computations especially large matrices of multiple dimensions. These days we use Neural Networks rather than the traditional statistical techniques for developing Machine Learning applications. The Neural Networks need to be trained over a huge amount of data. The training is done in batches of data of reasonable size. Thus, the learning process is iterative. Thus, if the computations are not done efficiently, training the network can take several hours or even days. Thus, the optimization of the executable code is highly desired. And that is what exactly Theano provides. Theano is a Python library that lets you define mathematical expressions used in Machine Learning, optimize these expressions and evaluate those very efficiently by decisively using GPUs in critical areas. It can rival typical full C-implementations in most of the cases. Theano was written at the LISA lab with the intention of providing rapid development of efficient machine learning algorithms. It is released under a BSD license. In this tutorial, you will learn to use Theano library. Theano – Installation Theano can be installed on Windows, MacOS, and Linux. The installation in all the cases is trivial. Before you install Theano, you must install its dependencies. The following is the list of dependencies − Python NumPy − Required SciPy − Required only for Sparse Matrix and special functions BLAS − Provides standard building blocks for performing basic vector and matrix operations The optional packages that you may choose to install depending on your needs are − nose: To run Theano’s test-suite Sphinx − For building documentation Graphiz and pydot − To handle graphics and images NVIDIA CUDA drivers − Required for GPU code generation/execution libgpuarray − Required for GPU/CPU code generation on CUDA and OpenCL devices We shall discuss the steps to install Theano in MacOS. MacOS Installation To install Theano and its dependencies, you use pip from the command line as follows. These are the minimal dependencies that we are going to need in this tutorial. $ pip install Theano $ pip install numpy $ pip install scipy $ pip install pydot You also need to install OSx command line developer tool using the following command − $ xcode-select –install You will see the following screen. Click on the Install button to install the tool. On successful installation, you will see the success message on the console. Testing the Installation After the installation completes successfully, open a new notebook in the Anaconda Jupyter. In the code cell, enter the following Python script − Example import theano from theano import tensor a = tensor.dscalar() b = tensor.dscalar() c = a + b f = theano.function([a,b], c) d = f(1.5, 2.5) print (d) Output Execute the script and you should see the following output − 4.0 The screenshot of the execution is shown below for your quick reference − If you get the above output, your Theano installation is successful. If not, follow the debug instructions on Theano download page to fix the issues. What is Theano? Now that you have successfully installed Theano, let us first try to understand what is Theano? Theano is a Python library. It lets you define, optimize, and evaluate mathematical expressions, especially the ones which are used in Machine Learning Model development. Theano itself does not contain any pre-defined ML models; it just facilitates its development. It is especially useful while dealing with multi-dimensional arrays. It seamlessly integrates with NumPy, which is a fundamental and widely used package for scientific computations in Python. Theano facilitates defining mathematical expressions used in ML development. Such expressions generally involve Matrix Arithmetic, Differentiation, Gradient Computation, and so on. Theano first builds the entire Computational Graph for your model. It then compiles it into highly efficient code by applying several optimization techniques on the graph. The compiled code is injected into Theano runtime by a special operation called function available in Theano. We execute this function repetitively to train a neural network. The training time is substantially reduced as compared to using pure Python coding or even a full C implementation. We shall now understand the process of Theano development. Let us begin with how to define a mathematical expression in Theano. Theano – A Trivial Theano Expression Let us begin our journey of Theano by defining and evaluating a trivial expression in Theano. Consider the following trivial expression that adds two scalars − c = a + b Where a, b are variables and c is the expression output. In Theano, defining and evaluating even this trivial expression is tricky. Let us understand the steps to evaluate the above expression. Importing Theano First, we need to import Theano library in our program, which we do using the following statement − from theano import * Rather than importing the individual packages, we have used * in the above statement to include all packages from the Theano library. Declaring Variables Next, we will declare a variable called a using the following statement − a = tensor.dscalar() The dscalar method declares a decimal scalar variable. The execution of the above statement creates a variable called a in your program code. Likewise, we will create variable b using the following statement − b = tensor.dscalar() Defining Expression Next, we will define our expression that operates on these two variables a and b. c = a + b In Theano, the execution of the above statement does not perform the scalar addition of the two variables a and b. Defining Theano Function To evaluate the above expression, we need to define a function in Theano as follows − f = theano.function([a,b], c) The function function takes two arguments, the first argument is an input to the function and the second one

Learn Theano – Installation work project make money

Theano – Installation Theano can be installed on Windows, MacOS, and Linux. The installation in all the cases is trivial. Before you install Theano, you must install its dependencies. The following is the list of dependencies − Python NumPy − Required SciPy − Required only for Sparse Matrix and special functions BLAS − Provides standard building blocks for performing basic vector and matrix operations The optional packages that you may choose to install depending on your needs are − nose: To run Theano’s test-suite Sphinx − For building documentation Graphiz and pydot − To handle graphics and images NVIDIA CUDA drivers − Required for GPU code generation/execution libgpuarray − Required for GPU/CPU code generation on CUDA and OpenCL devices We shall discuss the steps to install Theano in MacOS. MacOS Installation To install Theano and its dependencies, you use pip from the command line as follows. These are the minimal dependencies that we are going to need in this tutorial. $ pip install Theano $ pip install numpy $ pip install scipy $ pip install pydot You also need to install OSx command line developer tool using the following command − $ xcode-select –install You will see the following screen. Click on the Install button to install the tool. On successful installation, you will see the success message on the console. Testing the Installation After the installation completes successfully, open a new notebook in the Anaconda Jupyter. In the code cell, enter the following Python script − Example import theano from theano import tensor a = tensor.dscalar() b = tensor.dscalar() c = a + b f = theano.function([a,b], c) d = f(1.5, 2.5) print (d) Output Execute the script and you should see the following output − 4.0 The screenshot of the execution is shown below for your quick reference − If you get the above output, your Theano installation is successful. If not, follow the debug instructions on Theano download page to fix the issues. What is Theano? Now that you have successfully installed Theano, let us first try to understand what is Theano? Theano is a Python library. It lets you define, optimize, and evaluate mathematical expressions, especially the ones which are used in Machine Learning Model development. Theano itself does not contain any pre-defined ML models; it just facilitates its development. It is especially useful while dealing with multi-dimensional arrays. It seamlessly integrates with NumPy, which is a fundamental and widely used package for scientific computations in Python. Theano facilitates defining mathematical expressions used in ML development. Such expressions generally involve Matrix Arithmetic, Differentiation, Gradient Computation, and so on. Theano first builds the entire Computational Graph for your model. It then compiles it into highly efficient code by applying several optimization techniques on the graph. The compiled code is injected into Theano runtime by a special operation called function available in Theano. We execute this function repetitively to train a neural network. The training time is substantially reduced as compared to using pure Python coding or even a full C implementation. We shall now understand the process of Theano development. Let us begin with how to define a mathematical expression in Theano.

Learn Theano – Conclusion work project make money

Theano – Conclusion The Machine Learning model building involves intensive and repetitive computations involving tensors. These require intensive computing resources. As a regular compiler would provide the optimizations at the local level, it does not generally produce a fast execution code. Theano first builds a computational graph for the entire computation. As the whole picture of computation is available as a single image during compilation, several optimization techniques can be applied during pre-compilation and that’s what exactly Theano does. It restructures the computational graph, partly converts it into C, moves shared variables to GPU, and so on to generate a very fast executable code. The compiled code is then executed by a Theano function which just acts as a hook for injecting the compiled code into the runtime. Theano has proved its credentials and is widely accepted in both academics and industry.

Learn Theano – Expression for Matrix Multiplication work project make money

Theano – Expression for Matrix Multiplication We will compute a dot product of two matrices. The first matrix is of dimension 2 x 3 and the second one is of dimension 3 x 2. The matrices that we used as input and their product are expressed here − $$begin{bmatrix}0 & -1 & 2\4 & 11 & 2end{bmatrix} :begin{bmatrix}3& -1 \1 & 2 \35 & 20 end{bmatrix}=begin{bmatrix}11 & 0 \35 & 20 end{bmatrix}$$ Declaring Variables To write a Theano expression for the above, we first declare two variables to represent our matrices as follows − a = tensor.dmatrix() b = tensor.dmatrix() The dmatrix is the Type of matrices for doubles. Note that we do not specify the matrix size anywhere. Thus, these variables can represent matrices of any dimension. Defining Expression To compute the dot product, we used the built-in function called dot as follows − c = tensor.dot(a,b) The output of multiplication is assigned to a matrix variable called c. Defining Theano Function Next, we define a function as in the earlier example to evaluate the expression. f = theano.function([a,b], c) Note that the input to the function are two variables a and b which are of matrix type. The function output is assigned to variable c which would automatically be of matrix type. Invoking Theano Function We now invoke the function using the following statement − d = f([[0, -1, 2], [4, 11, 2]], [[3, -1],[1,2], [6,1]]) The two variables in the above statement are NumPy arrays. You may explicitly define NumPy arrays as shown here − f(numpy.array([[0, -1, 2], [4, 11, 2]]), numpy.array([[3, -1],[1,2], [6,1]])) After d is computed we print its value − print (d) You will see the following output on the output − [[11. 0.] [25. 20.]] Full Program Listing The complete program listing is given here: from theano import * a = tensor.dmatrix() b = tensor.dmatrix() c = tensor.dot(a,b) f = theano.function([a,b], c) d = f([[0, -1, 2],[4, 11, 2]], [[3, -1],[1,2],[6,1]]) print (d) The screenshot of the program execution is shown here −

Learn Theano – Data Types work project make money

Theano – Data Types Now, that you have understood the basics of Theano, let us begin with the different data types available to you for creating your expressions. The following table gives you a partial list of data types defined in Theano. Data type Theano type Byte bscalar, bvector, bmatrix, brow, bcol, btensor3, btensor4, btensor5, btensor6, btensor7 16-bit integers wscalar, wvector, wmatrix, wrow, wcol, wtensor3, wtensor4, wtensor5, wtensor6, wtensor7 32-bit integers iscalar, ivector, imatrix, irow, icol, itensor3, itensor4, itensor5, itensor6, itensor7 64-bit integers lscalar, lvector, lmatrix, lrow, lcol, ltensor3, ltensor4, ltensor5, ltensor6, ltensor7 float fscalar, fvector, fmatrix, frow, fcol, ftensor3, ftensor4, ftensor5, ftensor6, ftensor7 double dscalar, dvector, dmatrix, drow, dcol, dtensor3, dtensor4, dtensor5, dtensor6, dtensor7 complex cscalar, cvector, cmatrix, crow, ccol, ctensor3, ctensor4, ctensor5, ctensor6, ctensor7 The above list is not exhaustive and the reader is referred to the tensor creation document for a complete list. I will now give you a few examples of how to create variables of various kinds of data in Theano. Scalar To construct a scalar variable you would use the syntax − Syntax x = theano.tensor.scalar (”x”) x = 5.0 print (x) Output 5.0 One-dimensional Array To create a one dimensional array, use the following declaration − Example f = theano.tensor.vector f = (2.0, 5.0, 3.0) print (f)f = theano.tensor.vector f = (2.0, 5.0, 3.0) print (f) print (f[0]) print (f[2]) Output (2.0, 5.0, 3.0) 2.0 3.0 If you do f[3] it would generate an index out of range error as shown here − print f([3]) Output IndexError Traceback (most recent call last) <ipython-input-13-2a9c2a643c3a> in <module> 4 print (f[0]) 5 print (f[2]) —-> 6 print (f[3]) IndexError: tuple index out of range Two-dimensional Array To declare a two-dimensional array you would use the following code snippet − Example m = theano.tensor.matrix m = ([2,3], [4,5], [2,4]) print (m[0]) print (m[1][0]) Output [2, 3] 4 5-dimensional Array To declare a 5-dimensional array, use the following syntax − Example m5 = theano.tensor.tensor5 m5 = ([0,1,2,3,4], [5,6,7,8,9], [10,11,12,13,14]) print (m5[1]) print (m5[2][3]) Output [5, 6, 7, 8, 9] 13 You may declare a 3-dimensional array by using the data type tensor3 in place of tensor5, a 4-dimensional array using the data type tensor4, and so on up to tensor7. Plural Constructors Sometimes, you may want to create variables of the same type in a single declaration. You can do so by using the following syntax − Syntax from theano.tensor import * x, y, z = dmatrices(”x”, ”y”, ”z”) x = ([1,2],[3,4],[5,6]) y = ([7,8],[9,10],[11,12]) z = ([13,14],[15,16],[17,18]) print (x[2]) print (y[1]) print (z[0]) Output [5, 6] [9, 10] [13, 14]