Theano 简明教程
Theano - A Trivial Theano Expression
让我们从在 Theano 中定义和评估一个平凡表达式开始我们的 Theano 之旅。考虑以下添加两个标量的平凡表达式 −
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
c = a + b
其中 a 、 b 是变量, c 是表达式输出。在 Theano 中,即使是定义和评估此平凡表达式也很棘手。
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
首先,我们需要在程序中导入 Theano 库,我们使用以下语句进行导入 −
First, we need to import Theano library in our program, which we do using the following statement −
from theano import *
与导入单个包不同的是,我们在上面的语句中使用了 * 来包含 Theano 库中的所有包。
Rather than importing the individual packages, we have used * in the above statement to include all packages from the Theano library.
Declaring Variables
接下来,我们将使用以下语句声明一个名为 a 的变量 −
Next, we will declare a variable called a using the following statement −
a = tensor.dscalar()
dscalar 方法声明一个十进制标量变量。在程序代码中执行上述语句将在程序代码中创建一个名为 a 的变量。同样,我们将使用以下语句创建变量 b −
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
接下来,我们将定义作用于这两个变量 a 和 b 上的表达式。
Next, we will define our expression that operates on these two variables a and b.
c = a + b
c = a + b
在 Theano 中,上述语句的执行不会执行两个变量 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
要评估上述表达式,我们需要在 Theano 中定义一个函数,如下所示 −
To evaluate the above expression, we need to define a function in Theano as follows −
f = theano.function([a,b], c)
函数 function 接受两个参数,第一个参数是函数的输入,第二个参数是函数的输出。上述声明说明第一个参数是包含两个元素 a 和 b 的数组类型。输出是一个名为 c 的标量单元。在后续代码中,将使用变量名 f 引用此函数。
The function function takes two arguments, the first argument is an input to the function and the second one is its output. The above declaration states that the first argument is of type array consisting of two elements a and b. The output is a scalar unit called c. This function will be referenced with the variable name f in our further code.
Invoking Theano Function
对函数 f 的调用使用以下语句 −
The call to the function f is made using the following statement −
d = f(3.5, 5.5)
d = f(3.5, 5.5)
函数的输入是一个包含两个标量 3.5 和 5.5 的数组。执行的输出被分配给标量变量 d 。要打印 d 的内容,我们将使用 print 语句 −
The input to the function is an array consisting of two scalars: 3.5 and 5.5. The output of execution is assigned to the scalar variable d. To print the contents of d, we will use the print statement −
print (d)
执行将导致 d 的值打印在控制台上,在本例中为 9.0。
The execution would cause the value of d to be printed on the console, which is 9.0 in this case.
Full Program Listing
在此提供完整的程序清单供你快速参考 −
The complete program listing is given here for your quick reference −
from theano import *
a = tensor.dscalar()
b = tensor.dscalar()
c = a + b
f = theano.function([a,b], c)
d = f(3.5, 5.5)
print (d)
执行上述代码,你将看到输出为 9.0。这里显示屏幕截图 −
Execute the above code and you will see the output as 9.0. The screen shot is shown here −
现在,让我们讨论一个计算两个矩阵相乘的稍微复杂的示例。
Now, let us discuss a slightly more complex example that computes the multiplication of two matrices.