Theano-矩阵乘法的表达式

Theano-矩阵乘法的表达式 (Theano - Expression for Matrix Multiplication)

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 −

$$\begin{bmatrix}0 & -1 & 2\\4 & 11 & 2\end{bmatrix} \:\begin{bmatrix}3& -1 \\1 & 2 \\35 & 20 \end{bmatrix}=\begin{bmatrix}11 & 0 \\35 & 20 \end{bmatrix}$$

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

$$ \ begin {bmatrix} 0&-1&2 \\ 4&11&2 \ end {bmatrix} \:\ begin {bmatrix} 3&-1 \\ 1&2 \\ 35&20 \ end {bmatrix} = \ begin {bmatrix} 11&0 \\ 35&20 \ end {bmatrix} $$

声明变量 (Declaring Variables)

To write a Theano expression for the above, we first declare two variables to represent our matrices as follows −

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


a = tensor.dmatrix()
b = tensor.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.

dmatrix是双精度矩阵的类型。 注意,我们没有在任何地方指定矩阵大小。 因此,这些变量可以表示任何维度的矩阵。

定义表达 (Defining Expression)

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

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


c = tensor.dot(a,b)

The output of multiplication is assigned to a matrix variable called c.

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

定义Theano函数 (Defining Theano Function)

Next, we define a function as in the earlier example to evaluate the expression.

接下来,我们像前面的示例一样定义一个函数来评估表达式。


f = theano.function([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.

请注意,该函数的输入是矩阵类型的两个变量a和b。 函数输出分配给变量c ,该变量将自动为矩阵类型。

调用Theano函数 (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]])

The two variables in the above statement are NumPy arrays. You may explicitly define NumPy arrays as shown here −

上面的语句中的两个变量是NumPy数组。 您可以显式定义NumPy数组,如下所示-


f(numpy.array([[0, -1, 2], [4, 11, 2]]),
numpy.array([[3, -1],[1,2], [6,1]]))

After d is computed we print its value −

计算d之后,我们打印其值-


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

翻译自: https://www.tutorialspoint.com/theano/theana_expression_matrix_multiplication.htm

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值