Theano Python教程

Theano is a numerical computation library for Python. It is a common choice for implementing neural network models as it allows you to efficiently define, optimize and evaluate mathematical expressions, including multi-dimensional arrays (numpy.ndaray).

Theano是Python的数值计算库。 这是实现神经网络模型的常见选择,因为它使您可以有效地定义,优化和评估数学表达式,包括多维数组(numpy.ndaray)

Theano Python (Theano Python)

Theano makes it possible to attain high speeds that give a tough competition to hand-crafted C implementations for problems involving large amounts of data. It can take advantage of recent GPUs which makes it perform better than C on a CPU by considerable orders of magnitude under certain circumstances.

Theano使获得高速成为可能,从而使手工C实现在涉及大量数据的问题上进行了激烈的竞争。 它可以利用最近的GPU,在某些情况下,它在CPU上的性能要比C高出好几个数量级。

Theano has got an amazing compiler which can do various optimizations of varying complexity. A few of such optimizations are:

Theano有一个了不起的编译器,它可以进行各种复杂度不同的优化。 一些这样的优化是:

  1. Arithmetic simplification (e.g: --x -> x; x + y - x -> y)

    算术简化(例如: --x -> x; x + y - x -> y
  2. Using memory aliasing to avoid calculation

    使用内存别名以避免计算
  3. Constant folding

    不断折叠
  4. Merging similar subgraphs, to avoid redundant calculation

    合并相似的子图,以避免重复计算
  5. Loop fusion for elementwise sub-expressions

    循环融合用于元素子表达式
  6. GPU computations

    GPU计算

You can see the full list of optimizations here.

您可以在此处查看优化的完整列表。

为什么选择Theano Python库? (Why Theano Python Library?)

Typically we manipulate matrices using numpy package, so what make Theano better than any such package!

通常,我们使用numpy软件包来处理矩阵 ,那么什么使Theano优于任何此类软件包!

Theano is a sort of hybrid between numpy and sympy, an attempt is made to combine the two into one powerful library. Let’s have a look at some of its advantages over others:

Theano是numpy和sympy之间的一种混合体,它试图将两者组合成一个功能强大的库。 让我们来看看它相对于其他的一些优势:

  • Stability Optimization: Theano can find out some unstable expressions and can use more stable means to evaluate them

    稳定性优化:Theano可以找出一些不稳定的表达式,并可以使用更稳定的方式对其进行评估
  • Execution Speed Optimization: As mentioned earlier, theano can make use of recent GPUs and execute parts of expressions in your CPU or GPU, making it much faster than Python

    执行速度优化:如前所述,theano可以利用最新的GPU并在您的CPU或GPU中执行部分表达式,从而使其比Python快得多
  • Symbolic Differentiation: Theano is smart enough to automatically create symbolic graphs for computing gradients

    符号区分:Theano非常聪明,可以自动创建符号图以计算梯度

Well, enough of theory, let’s start working on the example part.

好了,从理论上讲,让我们开始研究示例部分。

Theano教程 (Theano Tutorial)

To start working with Theano, install it using PIP as shown in below image.

要开始使用Theano,请使用PIP进行安装,如下图所示。

Theano表达式到可调用对象中 (Theano Expression into Callable objects)

With Theano, we can convert expressions into callable objects. Let’s see a code snippet:

使用Theano,我们可以将表达式转换为可调用对象。 让我们看一下代码片段:

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 get the following output:

theano python tutorial

Now, let us try to understand what happened in above program:

运行此命令时,将得到以下输出:

现在,让我们尝试了解上面程序中发生的情况:

  • We start by declaring two symbolic floating-point scalars or variables

    我们首先声明两个符号浮点标量或变量
  • Then, we created a simple expression to sum two numbers

    然后,我们创建了一个简单的表达式,将两个数字相加
  • After the expression, we convert the expression into a callable object that takes (x,y) as input and returns a value for z after computation

    在表达式之后,我们将表达式转换为可调用对象,该对象以(x,y)作为输入,并在计算后返回z的值
  • Finally, we call the function with some parameters and print the results

    最后,我们使用一些参数调用该函数并打印结果

物流功能 (Logistic Function)

Let’s have a look at rather more elaborate example than just adding two numbers. Let’s try to compute the logistic curve, which is given by:

logistic curve function

If we plot a graph for this equation, it will look like:

Logistic function is applied to each element of matrix. Let’s write a code snippet to demonstrate this:

让我们看一个更复杂的示例,而不只是将两个数字相加。 让我们尝试计算逻辑曲线,它由下式给出:

逻辑曲线函数

如果我们为该方程式绘制图形,它将看起来像:

逻辑函数应用于矩阵的每个元素。 让我们编写一个代码片段来演示这一点:
import theano
from theano import tensor

# declare a variable
x = tensor.dmatrix('x')

# create the expression
s = 1 / (1 + tensor.exp(-x))

# convert the expression into a callable object which takes
# a matrix as parameter and returns s(x)
logistic = theano.function([x], s)

# call the function with a test matrix and print the result
print(logistic([[0, 1], [-1, -2]]))

When we run the script, we can see the ouput as:

python theano library

Everything works fine, the output looks same as expected. Now let’s have a closer look at the functions.

运行脚本时,我们可以看到输出为:

一切正常,输出看起来与预期相同。 现在,让我们仔细看看这些功能。

仔细看看Theano函数 (Closer look at Theano Function)

Theano functions help in interacting with the symbolic graph. They allow theano to build the computational graph and optimize it.

Theano函数有助于与符号图进行交互。 它们允许theano构建计算图并对其进行优化。

A typical theano function looks like this:

典型的theano函数如下所示:

f= theano.function([x],y)

Here x is the list of input variables, and y is the list of output variables. Let’s check out how this feature is of great use.

x是输入变量的列表,y是输出变量的列表。 让我们检查一下此功能的用处。

一次计算多个结果 (Calculating multiple results at once)

Let’s say we have to compute the elementwise difference, absolute difference and difference squared between two matrices ‘x’ and ‘y’. Doing this at the same time optimizes program with significant duration as we don’t have to go to each element again and again for each operation.

假设我们必须计算两个矩阵“ x”和“ y”之间的元素差,绝对差和平方差。 同时执行此操作可以极大地优化程序,因为我们不必为每个操作一次又一次地转到每个元素。

import theano
from theano import tensor

# declare variables
x, y = tensor.dmatrices('x', 'y')

# create simple expression for each operation
diff = x - y

abs_diff = abs(diff)
diff_squared = diff**2

# convert the expression into callable object
f = theano.function([x, y], [diff, abs_diff, diff_squared])

# call the function and store the result in a variable
result= f([[1, 1], [1, 1]], [[0, 1], [2, 3]])

# format print for readability
print('Difference: ')
print(result[0])

print('Absolute Difference: ')
print(result[1])

print('Squared Difference: ')
print(result[2])

When we run this program, we can see the output as multiple results being printed:

python theano machine learning

运行此程序时,我们可以看到输出为多个正在打印的结果:

使用Theano渐变功能 (Using Theano Gradient function)

Let’s try some more useful and sophisticated functions as we move towards a minimal training example. Here we’ll try to find out the derivative of an expression with respect to a parameter

让我们尝试一些最有用和更复杂的功能,以逐步简化培训示例。 在这里,我们将尝试找出表达式相对于参数的导数

We’ll compute the gradient of the logistic function defined above, which can be plotted as:

gradient function graph

Let’s demonstrate the working for Gradient with an example:

我们将计算上面定义的逻辑函数的梯度,可以将其绘制为:

让我们通过一个示例来演示Gradient的工作方式:

import numpy
import theano
from theano import tensor
from theano import pp

# declare variable
x = tensor.dmatrix('x')

#create a simple expression for logistic function
s = tensor.sum(1 / (1 + tensor.exp(-x)))

# create expression to compute gradient of s with respect to x
gs = tensor.grad(s, x)

# create callable object
dlogistic = theano.function([x], gs)

# call the function and print the results
print(dlogistic([[0, 1], [-1, -2]]))

When we run this program, we can see the output as:

python theano gradient function

In this way Theano can be used for doing efficient symbolic differentiation (as the expression returned by tensor.grad will be optimized during compilation), even for function with many inputs

当我们运行该程序时,我们可以看到输出为:

这样,即使对于具有许多输入的函数,也可以使用Theano进行有效的符号区分(因为tensor.grad返回的表达式将在编译过程中进行优化)

Let’s put things together into a simple training example to understand theano better!

让我们将它们放到一个简单的培训示例中,以更好地了解theano!

最少的培训Theano示例 (Minimal Training Theano Example)

Let’s try and 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):

让我们尝试使用theano训练一些东西。 我们将使用梯度下降来训练W中的权重,以便从模型中获得比现有(0.9)更好的结果:

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)

When we run this program, we can see the output as:

theano tutorial, python theano

The second input variable ‘target’ will act as the target value we use for training:

当我们运行该程序时,我们可以看到输出为:

第二个输入变量“ target”将用作我们用于训练的目标值:

target = theano.tensor.fscalar('target')

We need a cost function to train the model, which is usually squared distance from target value

我们需要一个成本函数来训练模型,通常是距目标值的平方距离

cost = theano.tensor.sqr(target - y)

Next, we need to calculate partial gradients for the parameters to be updated with respect to the cost function. As we have seen that in earlier example, Theano will do that for us. We simply call the grad function with required arguments:

接下来,我们需要针对成本函数计算要更新的参数的局部梯度。 正如我们在较早的示例中看到的那样,Theano将为我们做到这一点。 我们只需使用必需的参数调用grad函数即可:

gradients = theano.tensor.grad(cost, [W])

Now let’s define a variable for the updated version of the parameter. As we know in gradient descent the updated value equals learning rate times gradient subtracted from existing value.

现在,让我们为参数的更新版本定义一个变量。 众所周知,在梯度下降中,更新值等于学习率乘以从现有值中减去的梯度。

Assuming learning rate(alpha) = 0.1:

假设学习率α= 0.1:

W_updated = W - (0.1 * gradients[0])

Next we have to define a Theano function again, with a couple of changes:

接下来,我们必须再次定义Theano函数,进行一些更改:

f = theano.function([x, target], y, updates=updates)

When the function is called, it takes in values for x and target and returns the value for y as output, and Theano performs all the updates in the update list.

调用该函数时,它将获取x和target的值,并返回y的值作为输出,然后Theano执行更新列表中的所有更新。

Now we repeatedly call the function, in order to train, 10 times in this example to be specific. Typically, training data contains different values, but for sake of this example we use the same values x=[1.0, 1.0] and target=20 each time to check things work correctly.

现在我们反复调用该函数以进行训练,在此示例中要重复10次以进行具体说明。 通常,训练数据包含不同的值,但出于本示例的目的,我们每次使用相同的值x = [1.0,1.0]和target = 20来检查事情是否正常运行。

In the output above, notice how the target value is moving closer to 20 (target value) in each step.

在上面的输出中,请注意目标值在每个步骤中如何接近20(目标值)。

Theano神经网络摘要 (Theano Neural Network Summary)

In this post, we discovered the Theano Python library for efficient numerical computation.

在本文中,我们发现了Theano Python库,可进行高效的数值计算。

We learned that it is a foundation library used for deep learning research and development and that it can be used directly to create deep learning models or by convenient libraries built on top of it, such as Lasagne and Keras.

我们了解到,它是用于深度学习研究和开发的基础库,可以直接用于创建深度学习模型,也可以通过其基础之上的便捷库(例如Lasagne和Keras)来使用

翻译自: https://www.journaldev.com/17840/theano-python-tutorial

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值