TensorFlow,为什么选择 Python 语言?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35677724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TensorFlow, why was python the chosen language?
提问by Ollegn
I recently started studying deep learning and other ML techniques, and I started searching for frameworks that simplify the process of build a net and training it, then I found TensorFlow, having little experience in the field, for me, it seems that speed is a big factor for making a big ML system even more if working with deep learning, so why python was chosen by Google to make TensorFlow? Wouldn't it be better to make it over an language that can be compiled and not interpreted?
我最近开始研究深度学习和其他 ML 技术,我开始寻找可以简化构建网络和训练它的过程的框架,然后我找到了 TensorFlow,在该领域几乎没有经验,对我来说,速度似乎是一个如果与深度学习一起工作,则是构建大型机器学习系统的重要因素,那么为什么 Google 选择 Python 来构建 TensorFlow?用一种可以编译而不是解释的语言来实现它不是更好吗?
What are the advantages of using Python over a language like C++ for machine learning?
与 C++ 等语言相比,使用 Python 进行机器学习有哪些优势?
回答by dga
The most important thing to realize about TensorFlow is that, for the most part, the core is not written in Python: It's written in a combination of highly-optimized C++ and CUDA (Nvidia's language for programming GPUs). Much of that happens, in turn, by using Eigen(a high-performance C++ and CUDA numerical library) and NVidia's cuDNN(a very optimized DNN library for NVidia GPUs, for functions such as convolutions).
关于 TensorFlow 最重要的一点是,在大多数情况下,核心不是用 Python 编写的:它是用高度优化的 C++ 和 CUDA(Nvidia 的 GPU 编程语言)的组合编写的。反过来,大部分情况是通过使用Eigen(一个高性能 C++ 和 CUDA 数值库)和NVidia 的 cuDNN(一个非常优化的NVidia GPU 的DNN 库,用于卷积等功能)。
The model for TensorFlow is that the programmer uses "some language" (most likely Python!) to express the model. This model, written in the TensorFlow constructs such as:
TensorFlow 的模型是程序员使用“某种语言”(很可能是 Python!)来表达模型。这个模型是用 TensorFlow 结构编写的,例如:
h1 = tf.nn.relu(tf.matmul(l1, W1) + b1)
h2 = ...
is not actually executed when the Python is run. Instead, what's actually created is a dataflow graphthat says to take particular inputs, apply particular operations, supply the results as the inputs to other operations, and so on. This model is executed by fast C++ code, and for the most part, the data going between operations is never copied back to the Python code.
在 Python 运行时实际上并未执行。相反,实际创建的是一个数据流图,它表示接受特定输入、应用特定操作、将结果作为输入提供给其他操作,等等。 该模型由快速 C++ 代码执行,并且在大多数情况下,操作之间的数据永远不会复制回 Python 代码。
Then the programmer "drives" the execution of this model by pulling on nodes -- for training, usually in Python, and for serving, sometimes in Python and sometimes in raw C++:
然后程序员通过拉动节点来“驱动”这个模型的执行——用于训练,通常使用 Python,以及服务,有时使用 Python,有时使用原始 C++:
sess.run(eval_results)
This one Python (or C++ function call) uses either an in-process call to C++ or an RPCfor the distributed version to call into the C++ TensorFlow server to tell it to execute, and then copies back the results.
这个 Python(或 C++ 函数调用)使用对 C++ 的进程内调用或分布式版本的RPC调用 C++ TensorFlow 服务器以告诉它执行,然后将结果复制回。
So, with that said, let's re-phrase the question: Why did TensorFlow choose Python as the first well-supported language for expressing and controlling the training of models?
那么,话虽如此,让我们重新提出这个问题:为什么 TensorFlow 选择 Python 作为第一个支持良好的语言来表达和控制模型的训练?
The answer to that is simple: Python is probably themost comfortable language for a large range of data scientists and machine learning experts that's also that easy to integrate and have control a C++ backend, while also being general, widely-used both inside and outside of Google, and open source. Given that with the basic model of TensorFlow, the performance of Python isn't that important, it was a natural fit. It's also a huge plus that NumPymakes it easy to do pre-processing in Python -- also with high performance -- before feeding it in to TensorFlow for the truly CPU-heavy things.
答案很简单:对于大量数据科学家和机器学习专家来说,Python 可能是最舒适的语言,它也很容易集成并控制 C++ 后端,同时也是通用的,在内部和外部都广泛使用谷歌和开源。考虑到 TensorFlow 的基本模型,Python 的性能并不是那么重要,它是天作之合。这也是一个巨大的优势,NumPy使在 Python 中进行预处理变得容易——也具有高性能——然后再将它输入到 TensorFlow 以处理真正占用大量 CPU 的事情。
There's also a bunch of complexity in expressing the model that isn't used when executing it -- shape inference (e.g., if you do matmul(A, B), what is the shape of the resulting data?) and automatic gradientcomputation. It turns out to have been nice to be able to express those in Python, though I think in the long term they'll probably move to the C++ backend to make adding other languages easier.
在表达模型时也有很多复杂性,在执行它时不使用——形状推断(例如,如果你做 matmul(A, B),结果数据的形状是什么?)和自动梯度计算。事实证明,能够用 Python 表达这些是件好事,尽管我认为从长远来看,它们可能会转移到 C++ 后端,以便更轻松地添加其他语言。
(The hope, of course, is to support other languages in the future for creating and expressing models. It's already quite straightforward to run inference using several other languages -- C++ works now, someone from Facebook contributed Gobindings that we're reviewing now, etc.)
(当然,希望是在未来支持其他语言来创建和表达模型。使用其他几种语言运行推理已经很简单了——C++ 现在可以工作,来自 Facebook 的某人贡献了我们现在正在的Go绑定, 等等。)
回答by Salvador Dali
TF is not written in python. It is written in C++ (and uses high-performant numerical librariesand CUDAcode) and you can check this by looking at their github. So the core is written not in pythonbut TF provide an interface to many other languages (python, C++, Java, Go)
TF 不是用 python 编写的。它是用 C++ 编写的(并使用高性能数字库和CUDA代码),您可以通过查看他们的github来检查这一点。所以核心不是用 python 编写的,但 TF 提供了许多其他语言(python、C++、Java、Go)的接口
If you come from a data analysis world, you can think about it like numpy (not written in python, but provides an interface to Python) or if you are a web-developer - think about it as a database (PostgreSQL, MySQL, which can be invoked from Java, Python, PHP)
如果你来自数据分析领域,你可以把它想象成 numpy(不是用 python 编写的,但提供了一个 Python 接口)或者如果你是一个 web 开发者——把它想象成一个数据库(PostgreSQL、MySQL,可以从 Java、Python、PHP 调用)
Python frontend (the language in which people write models in TF) is the most popular due to manyreasons. In my opinion the main reason is historical: majority of ML users already use it (another popular choice is R) so if you will not provide an interface to python, your library is most probably doomed to obscurity.
由于多种原因,Python 前端(人们在 TF 中编写模型所用的语言)是最受欢迎的。在我看来,主要原因是历史性的:大多数 ML 用户已经使用它(另一个流行的选择是 R)所以如果你不提供 python 的接口,你的库很可能注定要默默无闻。
But being written in python does not mean that your model is executed in python. On the contrary, if you written your model in the right way Python is never executed during the evaluation of the TF graph (except of tf.py_func(), which exists for debugging and should be avoided in real model exactly because it is executed on Python's side).
但是用 python 编写并不意味着你的模型是在 python 中执行的。相反,如果你以正确的方式编写你的模型,那么在 TF 图的评估期间 Python 永远不会被执行(除了tf.py_func(),它存在于调试中,应该在真实模型中完全避免,因为它是在Python 的一面)。
This is different from for example numpy. For example if you do np.linalg.eig(np.matmul(A, np.transpose(A))
(which is eig(AA')
), the operation will compute transpose in some fast language (C++ or fortran), return it to python, take it from python together with A, and compute a multiplication in some fast language and return it to python, then compute eigenvalues and return it to python. So nonetheless expensive operations like matmul and eig are calculated efficiently, you still lose time by moving the results to python back and force. TF does not do it, once you defined the graph your tensors flow not in python but in C++/CUDA/something else.
这与例如 numpy 不同。例如,如果您这样做np.linalg.eig(np.matmul(A, np.transpose(A))
(即eig(AA')
),则该操作将使用某种快速语言(C++ 或 fortran)计算转置,将其返回给 python,将其与 A 一起从 python 中取出,并用某种快速语言计算乘法并将其返回到python,然后计算特征值并将其返回给python。因此,尽管 matmul 和 eig 等昂贵的操作被有效地计算,你仍然会因为将结果移动到 python 来回浪费时间。TF 不这样做,一旦你定义了图形,你的张量就不是在 python 中流动,而是在 C++/CUDA/其他东西中流动。
回答by Alyssa Haroldsen
Python allows you to create extension modules using C and C++, interfacing with native code, and still getting the advantages that Python gives you.
Python 允许您使用 C 和 C++ 创建扩展模块,与本机代码交互,并且仍然获得 Python 为您提供的优势。
TensorFlow uses Python, yes, but it also contains large amounts of C++.
TensorFlow 使用 Python,是的,但它也包含大量C++。
This allows a simpler interface for experimentation with less human-thought overhead with Python, and add performance by programming the most important parts in C++.
这为实验提供了一个更简单的界面,使用 Python 进行的人为思考开销更少,并通过用 C++ 编写最重要的部分来提高性能。
回答by prosti
The latest ratio you can check from hereshows inside TensorFlow C++ takes ~50% of code, and Python takes ~40% of code.
您可以从此处查看的最新比例显示,TensorFlow C++ 占用了约 50% 的代码,而 Python 占用了约 40% 的代码。
Both C++ and Python are the official languages at Google so there is no wonder why this is so. If I would have to provide fast regression where C++ and Python are present...
C++ 和 Python 都是 Google 的官方语言,所以难怪为什么会这样。如果我必须在存在 C++ 和 Python 的地方提供快速回归......
C++ is inside the computational algebra, and Python is used for everything else including for the testing. Knowing how ubiquitous the testing is today it is no wonder why Python code contributes that much to TF.
C++ 在计算代数中,Python 用于其他一切,包括测试。知道今天测试是多么普遍,难怪 Python 代码对 TF 的贡献如此之大。