Theano-琐碎的Theano表达

Theano-琐碎的Theano表达 (Theano - A Trivial Theano Expression)

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 −

让我们通过定义和评估Theano中的琐碎表达来开始Theano的旅程。 考虑以下将两个标量相加的平凡表达式-

c = a + b

c = a + b

Where a, b are variables and c is the expression output. In Theano, defining and evaluating even this trivial expression is tricky.

其中ab是变量, c是表达式输出。 在Theano中,即使定义和评估这个琐碎的表达式也很棘手。

Let us understand the steps to evaluate the above expression.

让我们了解评估以上表达式的步骤。

导入Theano (Importing Theano)

First, we need to import Theano library in our program, which we do using the following statement −

首先,我们需要在程序中导入Theano库,我们使用以下语句进行操作-


from theano import *

Rather than importing the individual packages, we have used * in the above statement to include all packages from the Theano library.

而不是导入单个软件包,我们在上面的语句中使用*包括了Theano库中的所有软件包。

声明变量 (Declaring Variables)

Next, we will declare a variable called a using the following statement −

接下来,我们将使用以下语句声明一个名为a的变量-


a = tensor.dscalar()

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 −

dscalar方法声明一个十进制标量变量。 上面的语句的执行在程序代码中创建了一个称为a的变量。 同样,我们将使用以下语句创建变量b-


b = tensor.dscalar()

定义表达 (Defining Expression)

Next, we will define our expression that operates on these two variables a and b.

接下来,我们将定义对这两个变量ab进行运算的表达式。

c = a + b

c = a + b

In Theano, the execution of the above statement does not perform the scalar addition of the two variables a and b.

在Theano中,执行以上语句不会对两个变量ab执行标量加法。

定义Theano函数 (Defining Theano Function)

To evaluate the above expression, we need to define a function in Theano as follows −

为了评估上面的表达式,我们需要在T​​heano中定义一个函数,如下所示:


f = theano.function([a,b], c)

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.

函数函数有两个参数,第一个参数是函数的输入,第二个参数是其输出。 上面的声明指出,第一个参数的类型为数组,由两个元素ab组成 。 输出是称为c的标量单位。 在我们的后续代码中,该函数将以变量名f引用。

调用Theano函数 (Invoking Theano Function)

The call to the function f is made using the following statement −

使用以下语句对函数f进行调用-

d = f(3.5, 5.5)

d = f(3.5,5.5)

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 −

该函数的输入是一个由两个标量组成的数组: 3.55.5 。 执行的输出分配给标量变量d 。 要打印d的内容,我们将使用print语句-


print (d)

The execution would cause the value of d to be printed on the console, which is 9.0 in this case.

执行将导致在控制台上打印d的值,在这种情况下为9.0。

完整程序清单 (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)

Execute the above code and you will see the output as 9.0. The screen shot is shown here −

执行上面的代码,您将看到输出为9.0。 屏幕截图显示在此处-

Full Program

Now, let us discuss a slightly more complex example that computes the multiplication of two matrices.

现在,让我们讨论一个稍微复杂的示例,该示例计算两个矩阵的乘法。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值