Theano 简明教程

Theano - Expression for Matrix Multiplication

我们将计算两个矩阵的点积。第一个矩阵的维度是 2 x 3,第二个矩阵的维度是 3 x 2。我们用作输入的矩阵及其乘积在此处表示为:

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 −

Declaring Variables

为上述内容编写 Theano 表达式,我们首先声明两个变量来表示我们的矩阵,如下所示:

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()

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

为了计算点积,我们使用了名为 dot 的内置函数,如下所示:

To compute the dot product, we used the built-in function called dot as follows −

c = tensor.dot(a,b)

乘法的输出将分配给名为 c 的矩阵变量。

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)

请注意,方法的输入是两个矩阵类型的变量 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]])

上述语句中的两个变量是 NumPy 数组。您可以像下面这样显式定义 NumPy 数组 −

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]]))

在计算出 d 之后,我们打印其值 −

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 −

program execution