Home / Blog / Data Science / Theano


  • June 28, 2023
  • 5219
  • 44
Author Images

Meet the Author : Mr. Bharani Kumar

Bharani Kumar Depuru is a well known IT personality from Hyderabad. He is the Founder and Director of Innodatatics Pvt Ltd and 360DigiTMG. Bharani Kumar is an IIT and ISB alumni with more than 17 years of experience, he held prominent positions in the IT elites like HSBC, ITC Infotech, Infosys, and Deloitte. He is a prevalent IT consultant specializing in Industrial Revolution 4.0 implementation, Data Analytics practice setup, Artificial Intelligence, Big Data Analytics, Industrial IoT, Business Intelligence and Business Management. Bharani Kumar is also the chief trainer at 360DigiTMG with more than Ten years of experience and has been making the IT transition journey easy for his students. 360DigiTMG is at the forefront of delivering quality education, thereby bridging the gap between academia and industry.

Read More >


Especially for multi-dimensional arrays, Theano may be a Python library that creates, optimises, and evaluates mathematical expressions. Theano makes it feasible to grasp issues requiring enormous volumes of information at rates that are competitive with hand-crafted C solutions. With modern GPUs, it will outperform C on a CPU by several orders of magnitude. Theano combines elements of an optimising compiler and a computer algebra system. Additionally, it may provide unique C code for a variety of mathematical procedures. This combination of CAS with optimising compilation is particularly helpful for applications where it is important to quickly analyse complex mathematical statements. Theano can reduce the amount of compilation/analysis overhead in instances where several individual expressions are evaluated once each while still offering symbolic capabilities like automated differentiation.

Theano is only an artificial language in the conventional sense since expressions for Theano are created by programmes written in Python. However, it still acts as a kind of language communication in that you must define variables (x.y) and specify their types, create expressions for the aforementioned variables, combine expression graphs into functions, and so on in order to use them for computation.

Theano function's interface to a compiler that creates a callable object from a purely symbolic graph makes it useful to consider. Theano's most significant characteristic is its ability to predict graphs and even compile portions of them into native machine commands.

Theano Graphs refresher

Theano isn't an artificial language within the traditional sense because you write a program in Python that builds expressions for Theano. Still, it's a style of artificial language within the sense that you simply must declare variables (a,b) and provide their types, build expressions for how to position those variables together, compile expression graphs to functions and so on using them for computation. Click here to learn Data Science Course

It is good to contemplate a Theano function because of the interface to the compiler which builds an applicable object from a purely symbolic graph. One of Theano’s most vital features is that the Theano function can optimize a graph and even compile some or all of it into native machine instructions.

Theano will show symbolic arithmetic computations as graphs. Those graphs are bipartite graphs (graphs with 2 styles of nodes), they are composed of interconnected Apply and Variable nodes. Variable nodes represent data within the graph, either inputs, outputs, or intermediary values. While; Apply nodes perform computation on these variables to supply new variables. Each Apply node contains a link to an instance of op which describes the computation to perform. This tutorial details the thanks to writing such an op instance.

Learn the core concepts of Data Science Course video on Youtube:

Op’s basic methods

This section offers an overview of the crucial techniques you may occasionally need to use to create a brand-new opportunity. It doesn't offer thorough coverage of all the scenarios you could face or require. Because of it, discuss the op's contract.

Op’s auxiliary methods

Other methods may be optionally defined by the op: The __str__() method will provide a meaningful string representation of our op__eq__() and __hash__() define respectively equality between two ops and therefore the hash of an op instance. They'll be employed by the optimization phase to merge nodes that do equivalent computations (same inputs, same operation).

Two ops that are equal according to __eq__() should return the identical output after they are applied on the identical inputs. The __props__ lists the properties that affect how the calculus is performed (Usually these are persons who you set in __init__()). It must be a tuple. If you don’t have any properties, then you ought to set this attribute to the empty tuple (). __props__ enables the automated generation of appropriate __eq__() and __hash__(). Given the strategy __eq__(), automatically generated from __props__, two ops are equal if they need the identical values for all the properties listed in __props__. Given the strategy __hash__() automatically generated from __props__, two ops are going to behave the identical hash if they need the identical values for all the properties listed in __props__. __props__ will generate an acceptable __str__() for your op. This needs a development version after September 1st, 2014, or version 0.7. The infer_shape() method permits an Op to infer the form of its output variables without actually calculating them.

It takes as input graph, a function graph; node, a connection to the op Apply node; and a bulletin of theano symbolic variables (i0_shape, i1_shape, …) what will be the form of the op input variables. infer_shape() returns an inventory where each element may be a tuple representing the form of 1 output. This might be helpful if one only needs the form of the output rather than the particular outputs, which might be useful, as an example, for optimization procedures. The grad() method is required if you wish to differentiate some cost whose expression includes your op.

The gradient is also specified symbolically during this method. The grad method must return a listing containing one variable for every input. Each returned variable represents the gradient with reference to that input computed supporting the symbolic gradients with relation to each output. If the output isn't differentiable with regard to an input then this method should be defined to return a variable of type NullType for that input. Likewise, if you've not implemented the grad computation for a few inputs, you'll return a variable of type NullType for that input. Please visit grad() for a more detailed view. The R_op() method is required if you would like theano gradient Rop to figure along with your op.

Let us imagine that function is, with input, applying the R-operator means computing the Jacobian of and right-multiplying it by, the calculation point, namely: The optional boolean check_input attribute is employed to specify if you would like the kinds employed in your op to test their inputs in their c_code. It is often wont to speed up compilation, reduce overhead (particularly for scalars) and reduce the amount of generated C files.

Example: __props__ definition

We can modify the previous piece of code to demonstrate the usage of the __props__ attribute. We create an op that takes a variable x and returns a*x+b. We wish to mention that two such ops are equal when their values of a and b are equal.

Extending Theano with a C Op

This section will describe how to add an operation that provides a C implementation to Theano. Although it doesn't address GPU operations, it does teach many concepts and features that are pertinent to them. This tutorial solely covers the additional knowledge needed to additionally make ops with C implementations and is intended for people who already know how to extend Theano by adding a new op with a Python implementation (see instruction Creating a replacement Op: Python implementation).

Working with Python's C-API and Numpy's C-API is necessary to provide a Theano op with a C implementation. Consequently, the first section of this tutorial introduces both and emphasises the aspects of each that are most pertinent to the job of implementation.

The most crucial procedures that the operator must perform in order to provide a functional C implementation are then explained in this lesson. Finally, it demonstrates how to combine these components to create a simple C op that can be used to carry out the simple operation of multiplying each element in a vector by a scalar.

Python C-API

Python provides a C-API to permit the handling of python objects from C code. During this API, all variables that represent Python objects are of type PyObject. Most python methods have identical C function which will be called on the PyObject * pointer. As such, manipulating a PyObject instance is usually straightforward but it's important to properly manage its reference count. Failing to try to do so can cause undesired behavior within the C code.

Reference counting

A system for keeping track of an object's number of references to that which is owned by other entities might be reference counting. This technique is typically used to gather trash since it makes it easy to determine if an item is still being used by other entities. When an object's reference count reaches 0, it may be securely removed because it is no longer being used in any significant way. Reference counting is implemented by PyObjects, and the Python C-API includes a number of macros to help manage those reference counts.

Click here to learn Data Science Course, Data Science Course in Hyderabad, Data Science Course in Bangalore

Data Science Placement Success Story

Data Science Training Institutes in Other Locations

Agra, Ahmedabad, Amritsar, Anand, Anantapur, Bangalore, Bhopal, Bhubaneswar, Chengalpattu, Chennai, Cochin, Dehradun, Malaysia, Dombivli, Durgapur, Ernakulam, Erode, Gandhinagar, Ghaziabad, Gorakhpur, Gwalior, Hebbal, Hyderabad, Jabalpur, Jalandhar, Jammu, Jamshedpur, Jodhpur, Khammam, Kolhapur, Kothrud, Ludhiana, Madurai, Meerut, Mohali, Moradabad, Noida, Pimpri, Pondicherry, Pune, Rajkot, Ranchi, Rohtak, Roorkee, Rourkela, Shimla, Shimoga, Siliguri, Srinagar, Thane, Thiruvananthapuram, Tiruchchirappalli, Trichur, Udaipur, Yelahanka, Andhra Pradesh, Anna Nagar, Bhilai, Borivali, Calicut, Chandigarh, Chromepet, Coimbatore, Dilsukhnagar, ECIL, Faridabad, Greater Warangal, Guduvanchery, Guntur, Gurgaon, Guwahati, Hoodi, Indore, Jaipur, Kalaburagi, Kanpur, Kharadi, Kochi, Kolkata, Kompally, Lucknow, Mangalore, Mumbai, Mysore, Nagpur, Nashik, Navi Mumbai, Patna, Porur, Raipur, Salem, Surat, Thoraipakkam, Trichy, Uppal, Vadodara, Varanasi, Vijayawada, Visakhapatnam, Tirunelveli, Aurangabad

Data Analyst Courses in Other Locations

ECIL, Jaipur, Pune, Gurgaon, Salem, Surat, Agra, Ahmedabad, Amritsar, Anand, Anantapur, Andhra Pradesh, Anna Nagar, Aurangabad, Bhilai, Bhopal, Bhubaneswar, Borivali, Calicut, Cochin, Chengalpattu , Dehradun, Dombivli, Durgapur, Ernakulam, Erode, Gandhinagar, Ghaziabad, Gorakhpur, Guduvanchery, Gwalior, Hebbal, Hoodi , Indore, Jabalpur, Jaipur, Jalandhar, Jammu, Jamshedpur, Jodhpur, Kanpur, Khammam, Kochi, Kolhapur, Kolkata, Kothrud, Ludhiana, Madurai, Mangalore, Meerut, Mohali, Moradabad, Pimpri, Pondicherry, Porur, Rajkot, Ranchi, Rohtak, Roorkee, Rourkela, Shimla, Shimoga, Siliguri, Srinagar, Thoraipakkam , Tiruchirappalli, Tirunelveli, Trichur, Trichy, Udaipur, Vijayawada, Vizag, Warangal, Chennai, Coimbatore, Delhi, Dilsukhnagar, Hyderabad, Kalyan, Nagpur, Noida, Thane, Thiruvananthapuram, Uppal, Kompally, Bangalore, Chandigarh, Chromepet, Faridabad, Guntur, Guwahati, Kharadi, Lucknow, Mumbai, Mysore, Nashik, Navi Mumbai, Patna, Pune, Raipur, Vadodara, Varanasi, Yelahanka


Navigate to Address

360DigiTMG - Data Science Course, Data Scientist Course Training in Chennai

D.No: C1, No.3, 3rd Floor, State Highway 49A, 330, Rajiv Gandhi Salai, NJK Avenue, Thoraipakkam, Tamil Nadu 600097


Get Direction: Data Science Course

Make an Enquiry
Call Us