矢量化编程

From Ufldl

Jump to: navigation, search
(first round for final check)
Line 1: Line 1:
-
【原文】:When working with learning algorithms, having a faster piece of code often means that you'll make progress faster on your project. For example, if your learning algorithm takes 20 minutes to run to completion, that means you can "try" up to 3 new ideas per hour. But if your code takes 20 hours to run, that means you can "try" only one idea a day, since that's how long you have to wait to get feedback from your program. In this latter case, if you can speed up your code so that it takes only 10 hours to run, that can literally double your personal productivity!
+
当使用学习算法时,一段更快的代码通常意味着项目进展更快。例如,如果你的学习算法需要花费20分钟运行完成,这意味着你每个小时最多能“尝试”3个新主意。但是假如你的程序需要20个小时来运行,这意味着你一天只能“尝试”一个新主意,因为你需要花费这么长时间来等待程序的反馈。对于后者,假如你可以提升代码的效率让其只需要10个小时来运行完成,那么你的效率差不多提升一倍。
-
----
+
'''矢量化编程'''是提高算法速度的一种有效方法。为了提升特定数值运算操作(如矩阵相乘、矩阵相加、矩阵-向量乘法等)的速度,数值计算和并行计算的研究人员已经努力了几十年。矢量化编程的思想就是尽量使用这些被高度优化的数值运算操作来实现我们的学习算法。
-
【初译】:当使用学习算法时,一段更快的代码通常意味着你项目的进展将更快。例如,假如你的学习算法需要花费20分钟运行完成,这意味着你每个小时最多能“尝试”3个新主意。但是假如你的程序需要20个小时来运行,这意味着你一天只能“尝试”一个主意,因为你需要花费这么长时间来等待程序的反馈。对于后者,假如你可以提升你的代码效率让其只需要10个小时来运行完成,从字面上看这可以使你的个人产出翻倍。
+
-
----
+
例如,假设<math>x \in \Re^{n+1}</math> 和<math>\textstyle \theta \in \Re^{n+1}</math> 为向量,需要计算<math>\textstyle z = \theta^Tx</math> ,那么可以按以下方式实现(使用Matlab):
-
【一审】:当使用学习算法时,一段更快的代码通常意味着项目进展更快。例如,如果你的学习算法需要花费20分钟运行完成,这意味着你每个小时最多能“尝试”3个新主意。但是假如你的程序需要20个小时来运行,这意味着你一天只能“尝试”一个新主意,因为你需要花费这么长时间来等待程序的反馈。对于后者,假如你可以提升代码的效率让其只需要10个小时来运行完成,那么你的效率差不多提升一倍。
+
-
 
+
-
----
+
-
【原文】:'''Vectorization''' refers to a powerful way to speed up your algorithms. Numerical computing and parallel computing researchers have put decades of work into making certain numerical operations (such as matrix-matrix multiplication, matrix-matrix addition, matrix-vector multiplication) fast. The idea of vectorization is that we would like to express our learning algorithms in terms of these highly optimized operations.
+
-
 
+
-
----
+
-
【初译】:'''矢量化编程'''是提速你算法的一种有效方式。数值计算和并行计算的研究人员已经为提升特定数值运算操作(如矩阵-矩阵乘法、矩阵-矩阵加法、矩阵-向量乘法等)的速度努力了几十年。矢量化编程的想法就是尽量使用这些被高度优化的数值运算操作来实现我们的学习算法。
+
-
 
+
-
----
+
-
【一审】:'''矢量化编程'''是提高算法速度的一种有效方法。为了提升特定数值运算操作(如矩阵相乘、矩阵相加、矩阵-向量乘法等)的速度,数值计算和并行计算的研究人员已经努力了几十年。矢量化编程的思想就是尽量使用这些被高度优化的数值运算操作来实现我们的学习算法。
+
-
----
+
-
【原文】:For example, if <math>x \in \Re^{n+1}</math> and <math>\textstyle \theta \in \Re^{n+1}</math> are vectors
+
-
and you need to compute <math>\textstyle z = \theta^Tx</math>,
+
-
you can implement (in Matlab):
+
-
 
+
-
----
+
-
【初译】:例如,假如<math>x \in \Re^{n+1}</math>且<math>\textstyle \theta \in \Re^{n+1}</math>是向量,且你需要计算<math>\textstyle z = \theta^Tx</math> ,你可以实现如下(使用Matlab):
+
-
----
+
-
 
+
-
【一审】:例如,假如<math>x \in \Re^{n+1}</math> 和<math>\textstyle \theta \in \Re^{n+1}</math> 是向量,需要计算<math>\textstyle z = \theta^Tx</math> ,那么可以按以下方式实现(使用Matlab):
+
<syntaxhighlight lang="matlab">
<syntaxhighlight lang="matlab">
z = 0;
z = 0;
Line 31: Line 10:
end;
end;
</syntaxhighlight>
</syntaxhighlight>
-
【原文】:or you can more simply implement
 
-
----
 
-
【初译】:或者你可以更加简单的实现:
+
或者可以更加简单的写为:
-
----
+
-
 
+
-
【一审】:或者可以更加简单的写为:
+
<syntaxhighlight lang="matlab">
<syntaxhighlight lang="matlab">
z = theta' * x;
z = theta' * x;
</syntaxhighlight>
</syntaxhighlight>
-
【原文】:The second piece of code is not only simpler, but it will also run ''much'' faster.
+
第二段程序代码不仅简单,而且运行速度更快。
-
----
+
-
【初译】:第二段程序代码不仅简单,而且运行速度更快。
+
-
----
+
-
【一审】:第二段程序代码不仅简单,而且运行速度更快。
+
-
----
+
-
【原文】:More generally, a good rule-of-thumb for coding Matlab/Octave is:
+
-
::'''Whenever possible, avoid using explicit for-loops in your code.'''
+
-
----
+
通常,一个编写Matlab/Octave程序的诀窍是:
-
【初译】:通常,一个良好的编写Matlab/Octave程序的窍门就是:
+
::'''代码中尽可能避免显式的for循环。'''
-
::'''在编写代码时尽可能的避免使用明显的for循环。'''
+
-
----
+
-
【一审】:通常,一个良好的编写Matlab/Octave程序的窍门是:
+
上面的第一段代码使用了一个显式的for循环。通过不使用for循环实现相同功能,可以显著提升运行速度。对Matlab/Octave代码进行矢量化的工作很大一部分集中在避免使用for循环上,因为这可以使得Matlab/Octave更多地利用代码中的并行性,同时其解释器的计算开销更小。
-
::'''代码中尽可能避免显式的for循环。'''
+
 
-
----
+
关于编写代码的策略,开始时你会觉得矢量化代码更难编写、阅读和调试,但你需要在编码和调试的便捷性与运行时间之间做个权衡。因此,刚开始编写程序的时候,你可能会选择不使用太多矢量化技巧来实现你的算法,并验证它是否正确(可能只在一个小问题上验证)。在确定它正确后,你可以每次矢量化一小段代码,并在这段代码之后暂停,以验证矢量化后的代码计算结果和之前是否相同。最后,你会有望得到一份正确的、经过调试的、矢量化且有效率的代码。
-
【原文】:In particular, the first code example used an explicit for loop. By implementing the same functionality without the for loop, we sped it up significantly. A large part of vectorizing our Matlab/Octave code will focus on getting rid of for loops, since this lets Matlab/Octave extract more parallelism from your code, while also incurring less computational overhead from the interpreter.
+
-
----
+
-
【初译】:尤其是第一段代码例子使用了一个明显的for循环。通过不使用for循环实行相同功能,可以显著提升运行速度。Matlab/Octave矢量化编程有很大一部分集中在避免使用for循环,因为这可以使得Matlab/Octave可以从你的代码中抽取更多的并行性,同时解释器可以产生更小的计算开销。
+
-
----
+
-
【一审】:上面的第一段代码使用了一个显式的for循环。通过不使用for循环实现相同功能,可以显著提升运行速度。对Matlab/Octave代码进行矢量化有很大一部分集中在避免使用for循环,因为这可以使得Matlab/Octave更多地利用代码中的并行性,同时其解释器的计算开销更小。
+
-
----
+
-
【原文】:In terms of a strategy for writing your code, initially you may find that vectorized code is harder to write, read, and/or debug, and that there may be a tradeoff in ease of programming/debugging vs. running time. Thus, for your first few programs, you might choose to first implement your algorithm without too many vectorization tricks, and verify that it is working correctly (perhaps by running on a small problem). Then only after it is working, you can vectorize your code one piece at a time, pausing after each piece to verify that your code is still computing the same result as before. At the end, you'll then hopefully have a correct, debugged, and vectorized/efficient piece of code.
+
-
----
+
-
【初译】:关于编写代码的策略,开始时你会发现矢量化编码更难编写、阅读和调试,因此出于方便考虑需要在编码/调试和运行时间之间做个权衡。因此,对于你的第一个小程序,你可能会选择不使用太多矢量化技巧来实现你的算法,并验证它是否有效(可能只在一个小问题上运行验证)。然后在它有效后,你可以一次矢量化一小段你的代码,并且每次矢量化一小段代码后暂停,并验证你矢量化后的代码计算结果和之前是否相同。最后,你就很有希望能够得到一个正确的、调试后的、矢量化/有效率的一段代码。
+
-
----
+
-
【一审】:关于编写代码的策略,开始时会觉得矢量化代码更难编写、阅读和调试,你可能需要在编码和调试的便捷性与运行时间之间做个权衡。因此,刚开始编写程序的时候,你可能会选择不使用太多矢量化技巧来实现你的算法,并验证它是否正确(可能只在一个小问题上验证)。在确定它正确后,你可以每次矢量化一小段代码,并在这段代码之后暂停,以验证矢量化后的代码计算结果和之前是否相同(译注:也就是说在你修改的那段代码之后设置一个断点,运行程序到该断点看看结果是否正确,以确定修改是否有问题)。最后,你就有望得到正确的、经过调试的、矢量化的并且有效率的代码。
+
-
----
+
-
【原文】:After you become familiar with the most common vectorization methods and tricks, you'll find that it usually isn't much effort to vectorize your code. Doing so will make your code run much faster and, in some cases, simplify it too.
+
-
----
+
-
【初译】:在你对最常见的矢量化方法和技巧熟悉后,你将会发现矢量化你的代码通常不需要花费多大力气。矢量化代码可以使你的代码运行的更快,在某些情况下,还简化了你的代码。
+
-
----
+
-
【一审】:一旦对矢量化常见的方法和技巧熟悉后,你将会发现对代码进行矢量化通常并不太费劲。矢量化可以使你的代码运行的更快,而且在某些情况下,还简化了你的代码。
+
一旦对矢量化常见的方法和技巧熟悉后,你将会发现对代码进行矢量化通常并不太费劲。矢量化可以使你的代码运行的更快,而且在某些情况下,还简化了你的代码。

Revision as of 14:23, 14 March 2013

Personal tools