If you need a Python-based ecosystem of open-source software for mathematical expressions involving multi-dimensional arrays, then Python’s Theano library is exactly what you need. You can easily run this library and give it a nice GUI using Python4Delphi (P4D). Python4Delphi is a free tool that allows you to work with Python scripts and objects, even create new Python modules and types in the Windows GUI.

**Theano** is a Python library that allows you to define, optimize, and efficiently evaluate mathematical expressions involving multi-dimensional arrays. It is built on top of NumPy. Theano features:

**tight integration with NumPy:**a similar interface to NumPy’s. numpy.ndarrays are also used internally in Theano-compiled functions.**transparent use of a GPU:**perform data-intensive computations up to 140x faster than on a CPU (support for float32 only).**efficient symbolic differentiation:**Theano can compute derivatives for functions of one or many inputs.**speed and stability optimizations:**avoid nasty bugs when computing expressions such as log(1 + exp(x)) for large values of x.**dynamic C code generation:**evaluate expressions faster.**extensive unit-testing and self-verification:**includes tools for detecting and diagnosing bugs and/or potential problems.

Theano has been powering large-scale computationally intensive scientific research since 2007, but it is also approachable enough to be used in the classroom (IFT6266 or Deep Learning Course at the University of Montreal).

This post will guide you on **how to run the Theano library using Python for Delphi to display it in the Delphi Windows GUI app**.First, open and run our Python GUI using project **Demo1 **from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute button, and get the result in the upper Memo. You can find the **Demo1** source on GitHub. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this link.

## 1. Adding Two Scalars

1 2 3 4 5 6 7 8 9 10 |
# Theano Expression into Callable objects (adding two numbers) import theano from theano import tensor x = tensor.dscalar() y = tensor.dscalar() z = x + y f = theano.function([x,y], z) print(f(1.5, 2.5)) |

When we run this, we will get the following output in our Python GUI:

## 2. **Minimal Example of using Theano to Train Gradient Descent**

Let’s train something using theano. We will be using gradient descent to train weights in W so that we get better results from the model than existing (0.9):

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# Minimal Training Theano Example import theano import numpy # declare variables x = theano.tensor.fvector('x') target = theano.tensor.fscalar('target') W = theano.shared(numpy.asarray([0.2, 0.7]), 'W') # create expressions y = (x * W).sum() cost = theano.tensor.sqr(target - y) gradients = theano.tensor.grad(cost, [W]) W_updated = W - (0.1 * gradients[0]) updates = [(W, W_updated)] # create a callable object from expression f = theano.function([x, target], y, updates=updates) # call the function and print results for i in range(10): result = f([1.0, 1.0], 20.0) print(result) |

The result in Python4Delphi GUI:

Congratulations, now you have learned the **basics of deep learning by running the Theano library and using Python for Delphi to display it in the Delphi Windows GUI app**!

Check out the Theano library for Python and use it in your projects: https://pypi.org/project/Theano/ and

Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi: https://github.com/pyscripter/python4delphi