Javascript 类似 numpy 的节点包
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31412537/
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
numpy-like package for node
提问by Claude
During my years on Python development, I've always been amazed at how much much much faster things become if you manage to rewrite that code that loops though your ndarray and does something, with numpy functions that work on the whole array at once. More recently I'm switching more and more to node, and I'm looking for something similar. So far I have turned up some things, none of which look promising:
在我从事 Python 开发的这些年里,我总是惊讶于如果你设法重写那些循环遍历你的 ndarray 的代码,并使用 numpy 函数一次处理整个数组,那么事情会变得如此之快。最近我越来越多地切换到节点,我正在寻找类似的东西。到目前为止,我已经发现了一些东西,但没有一个看起来很有希望:
- scikit-node, runs scikit-learn in python, and interfaces with node. I haven't tried it, but I don't expect it gives me the cutting edge speed that I would like.
- There are some rather old, and newer, javascript matrix libraries (sylvester, gl-matrix, ...). In addition to not being sure they work well with matrices larger than 4x4 (which is most useful in 3D rendering), they seem to be native javascript (and some, not sure these, use webGL acceleration). Great on the browser, not so on node.
- scikit-node,在 python 中运行 scikit-learn,并与 node.js 接口。我没有尝试过,但我不希望它给我想要的尖端速度。
- 有一些较旧和较新的 javascript 矩阵库(sylvester、gl-matrix、...)。除了不确定它们是否适用于大于 4x4 的矩阵(这在 3D 渲染中最有用)之外,它们似乎是原生 javascript(有些不确定这些,使用 webGL 加速)。在浏览器上很棒,在节点上则不然。
As far as I know, npms can be written in C++, so I'm wondering why there are no numpy-like libraries for node. Is there just not enough interest in node yet from the community that needs that kind of power? Is there a hope that ES6 features (list comprehensions) will allow javascript compilers to automatically vectorise native JS code to C++ speeds? Am I possibly missing something else?
据我所知,npms 可以用 C++ 编写,所以我想知道为什么 node.js 没有类似 numpy 的库。需要这种权力的社区对节点的兴趣还不够吗?是否希望 ES6 特性(列表推导式)允许 javascript 编译器自动将原生 JS 代码向量化为 C++ 速度?我可能会错过其他东西吗?
Edit, in response to close-votes: Note, I'm not asking for "what is the best package to do xyz". I'm just wondering if there is a technical reason there is no package to do this on node, a social reason, or no reason at all and there is just a package I missed. Maybe to avoid too many opinionated criticism, I want to know: I have about 10000 matrices that are 100 x 100 each. What's the best (* correction, a reasonable fast) way to add them together?
编辑,以回应近距离投票:注意,我不是在问“什么是最好的 xyz 包”。我只是想知道是否存在技术原因,没有在节点上执行此操作的程序包,社会原因,或者根本没有原因,而只是我错过了一个程序包。也许是为了避免太多自以为是的批评,我想知道:我有大约 10000 个矩阵,每个矩阵都是 100 x 100。将它们加在一起的最佳(* 更正,合理的快速)方法是什么?
Edit2After some more digging, it turned out I was googling for the wrong thing. Google for "node.js scientific computing" and there are links to some very interesting notes:
Edit2经过更多的挖掘,结果我在谷歌上搜索了错误的东西。谷歌搜索“node.js 科学计算”,其中有一些非常有趣的笔记的链接:
- https://cs.stackexchange.com/questions/1693/a-faster-leaner-javascript-for-scientific-computing-what-features-should-i-kee
- http://www.quora.com/Can-Node-js-handle-numerical-computation-the-same-way-that-languages-like-R-or-Julia-can
- Javascript and Scientific Processing?
- https://cs.stackexchange.com/questions/1693/a-faster-leaner-javascript-for-scientific-computing-what-features-should-i-kee
- http://www.quora.com/Can-Node-js-handle-numerical-computation-the-same-way-that-languages-like-R-or-Julia-can
- Javascript 和科学处理?
Basically as far as I understand now, no-one has bothered so far. Also, since there are some major omissions in the js TypedArrays (such as 64bit ints), it might be hard to add good support by just using NPMs, and not hacking the engine itself --- something that would defeat the purpose. Then again, I didn't further research this last statement.
基本上就我现在的理解,到目前为止没有人打扰。此外,由于 js TypedArrays 中有一些重大遗漏(例如 64 位整数),因此仅使用 NPM 而不是攻击引擎本身可能很难添加良好的支持——这会违背目的。再说一次,我没有进一步研究这最后一句话。
采纳答案by Jules G.M.
Here is Google's TensorFlow.js(previously https://deeplearnjs.org), which does exactly that, and has built in capacities to train deep neural networks on GPUs using WebGL. You can also port TensorFlow models to it.
这是 Google 的TensorFlow.js(以前是https://deeplearnjs.org),它正是这样做的,并且内置了使用 WebGL 在 GPU 上训练深度神经网络的能力。您还可以将TensorFlow 模型移植到它。
Don't be fooled into thinking this is only for deep learning. It is a fully fledged numerical computing platform with built-in GPU acceleration. It follows the eager "execute as you go" model, like NumPy (and Tensorflow Eager, and PyTorch, and others), not the "define then run" model like Tensorflow. As such, it will feel natural to use to anyone who has used NumPy before.
不要误以为这仅适用于深度学习。它是一个完全成熟的数值计算平台,内置 GPU 加速。它遵循像 NumPy(以及 Tensorflow Eager、PyTorch 等)那样急切的“随你去执行”模型,而不是像 Tensorflow 那样的“定义然后运行”模型。因此,任何以前使用过 NumPy 的人都会感觉很自然。
Here is the very informative Github repo:
这是非常有用的 Github 存储库:
https://github.com/tensorflow/tfjs-core(the old link https://github.com/PAIR-code/deeplearnjsnow redirects there)
https://github.com/tensorflow/tfjs-core(旧链接https://github.com/PAIR-code/deeplearnjs现在重定向到那里)
回答by kgryte
No, there are no technical reasons why a numpy-like package does not exist for Node.js and, more generally, JavaScript.
不,没有技术原因说明为什么 Node.js 和更一般的 JavaScript 不存在类似 numpy 的包。
There are two main obstacles preventing Node.js and JavaScript from achieving more mind share in the data science and numeric computing communities.
有两个主要障碍阻止 Node.js 和 JavaScript 在数据科学和数值计算社区中实现更多的思想共享。
The first obstacle is community. While the JavaScript community is huge, the subset of people within that community doing interesting things in numeric computing is small. Hence, if you want to do numeric computing in JavaScript and Node.js, finding resources to help you along the way can be hard, and it may feel like a lonely endeavor.
第一个障碍是社区。虽然 JavaScript 社区很大,但该社区中在数字计算领域做有趣事情的人很少。因此,如果您想在 JavaScript 和 Node.js 中进行数值计算,那么在此过程中寻找可以帮助您的资源可能会很困难,而且可能会让人感到孤独。
Next, the absence of comparable libraries (chicken and egg: libraries are needed to attract library authors and authors are needed to write good libraries). There are no technical reasons why libraries cannot be written in JavaScript or leverage Node.js (e.g., via native add-ons). I know, as I have written many numeric computing libraries in JavaScript. So while numeric computing is possible in JavaScript, the problem stems from an inability to attract developers having sufficient expertise and capable of putting in the time and effort needed to write high quality numeric computing implementations.
其次,缺乏可比的库(鸡和蛋:需要库来吸引库作者,需要作者编写好的库)。库不能用 JavaScript 编写或利用 Node.js(例如,通过本机附加组件)没有技术原因。我知道,因为我用 JavaScript 编写了许多数字计算库。因此,虽然 JavaScript 中的数值计算是可能的,但问题源于无法吸引具有足够专业知识并能够投入时间和精力编写高质量数值计算实现的开发人员。
Regarding the specific language features mentioned in the OP:
关于 OP 中提到的特定语言功能:
- ES6/ES2015: none of the recent language additions help or hinder development of numeric computing libraries in JavaScript. Potential additions like list comprehensions will not be game changers either. The one change to the web platform which will make a difference is WebAssembly. With WebAssembly, compiling C/C++/Fortran libraries to run in web browsers will be made easier. At the time of this answer, WebAssembly looks to be the means for bringing SIMD to the web, potentially allowing some speed-ups, although the focus seems to be on short SIMD, rather than long. But even with WebAssembly, porting numeric computing libraries to the web will not be as simple as hitting the compile button. Numeric computing code bases will need to massaged to become amenable for use on the web, and, even then, higher level APIs will likely need to be written to mask some of lower level features, such as manually managing the heap.
- Native add-ons: yes, node modules can be written as native add-ons, allowing C/C++/Fortran code to be used within a Node.js application. Individuals have written libraries to this end; for example, see stdlib. If done well, Node.js can perform numeric computations at speeds comparable to directly using native implementations.
- Typed arrays: as they are now, they are suitable for numeric computation. Similar to C, you can create pooled buffers, which allow for efficient memory reuse and better performance. Furthermore, similar to languages like R, Python, and Julia, you can leverage typed arrays to create ndarray (aka strided array) interfaces. While U/Int64 integer arrays are not currently available at the time of this answer, (a) their absence is not a show stopper and (b) proposals are advancing at the specification level to add U/Int64 integer arrays to JavaScript. Ditto for complex numbers with structured types.
- ES6/ES2015:最近添加的语言都没有帮助或阻碍 JavaScript 中数字计算库的开发。列表推导等潜在的补充也不会改变游戏规则。WebAssembly 对 Web 平台的一项改变将会有所作为。使用 WebAssembly,编译 C/C++/Fortran 库以在 Web 浏览器中运行将变得更加容易。在给出这个答案的时候,WebAssembly 看起来是将 SIMD 引入网络的手段,可能允许一些加速,尽管重点似乎是短 SIMD,而不是长 SIMD。但即使使用 WebAssembly,将数字计算库移植到 Web 也不会像点击编译按钮那么简单。数字计算代码库需要经过调整才能在网络上使用,即使这样,
- 本机附加组件:是的,节点模块可以编写为本机附加组件,允许在 Node.js 应用程序中使用 C/C++/Fortran 代码。个人为此编写了库;例如,请参阅stdlib。如果做得好,Node.js 可以以与直接使用本机实现相媲美的速度执行数值计算。
- 类型数组:就像现在一样,它们适用于数值计算。与 C 类似,您可以创建池化缓冲区,从而实现高效的内存重用和更好的性能。此外,类似于 R、Python 和 Julia 等语言,您可以利用类型化数组来创建 ndarray(又名跨步数组)接口。虽然 U/Int64 整数数组在此回答时当前不可用,但 (a) 它们的缺席并不是一个障碍,(b) 提案正在规范级别推进以将 U/Int64 整数数组添加到 JavaScript。具有结构化类型的复数也是如此。
My personal belief is that some form of numeric computing is inevitable in JavaScript and Node.js. The advantages (ubiquity, distribution, performance) and potential applications (edge computing, integrating machine learning, data visualization) are too strong of evolutionary forces not to support data science applications, at least at a basic level.
我个人认为某种形式的数值计算在 JavaScript 和 Node.js 中是不可避免的。优势(无处不在、分布、性能)和潜在应用(边缘计算、集成机器学习、数据可视化)的进化力量太强大,无法支持数据科学应用,至少在基础层面上是这样。
disclosure: I and others are currently working on a project (https://github.com/stdlib-js/stdlib) which aims to provide numeric computing facilities in JavaScript and Node.js.
披露:我和其他人目前正在开展一个项目(https://github.com/stdlib-js/stdlib),旨在提供 JavaScript 和 Node.js 中的数字计算设施。
回答by Mark
I've not tried this, but I found node-lapack. Since Numpy gets most of it's speed from using blas/lapack to do everything, this should help. From the readme it looks like it has an array object too, which is essential to not convert between JS and lapack on every operation.
我没有试过这个,但我找到了node-lapack。由于 Numpy 的大部分速度来自使用 blas/lapack 来完成所有工作,因此这应该会有所帮助。从自述文件看起来它也有一个数组对象,这对于不在每次操作时在 JS 和 lapack 之间进行转换是必不可少的。
Here's a part of their demo:
这是他们演示的一部分:
var lapack = require('lapack');
var result = lapack.sgeqrf([
[1, 2, 3],
[3, 4, 5],
[5, 6, 7]
]);
console.log(result.R);
console.log(result.tau);
result = sgesvd('A', 'A', [
[1, 2, 3],
[3, 4, 5],
[5, 6, 7]
]);
console.log(result.U);
console.log(result.S);
console.log(result.VT);
result = lapack.sgetrf([
[1, 2, 3],
[3, 4, 5],
[5, 6, 7]
]);
// see the readme for more
It seems to be a pretty direct interface to lapack using the same names, so in that regard it's not as convenient as Numpy, but at least it takes care of array dimensions and stuff and should be about as fast (since most of the work is being done by Lapack in either case).
它似乎是使用相同名称的 lapack 的一个非常直接的接口,因此在这方面它不如 Numpy 方便,但至少它负责处理数组维度和内容,并且速度应该差不多(因为大部分工作是在任何一种情况下都由 Lapack 完成)。
However, this won't work in a browser, which means everywhere where this is available, Python is probably also available. Personally I'd stick with Python, which is much more dominant for numerical stuff, unless there's some specific Node functionality Python is missing...
但是,这在浏览器中不起作用,这意味着在任何可用的地方,Python 也可能可用。就我个人而言,我会坚持使用 Python,它在数值方面占主导地位,除非 Python 缺少某些特定的 Node 功能......
回答by Mark Harrison
The majority of node work seems to be in the web "full stack" universe, with a lot less work being done in areas where fast numeric processing is an advantage.
大多数节点工作似乎都在网络“全栈”世界中,在快速数字处理具有优势的领域中完成的工作要少得多。
In the areas where fast numeric processing is an advantage, Python, R, etc probably have a dominant mindshare.
在快速数字处理具有优势的领域,Python、R 等可能占据主导地位。
Combine those two facts, and you end up with not a lot of people putting effort into node numeric processing libraries.
结合这两个事实,最终不会有很多人在节点数字处理库上投入精力。
回答by Alvaro Fernandez
回答by scottlittle
In the same spirit of @Julius's answer about deeplearn.js, tensorflow.js is the continuation of the same project. To play around with the tensorflow module in the REPL, I installed it globally (FYI - it is usually advised not to do this) using this:
本着@Julius 对 deeplearn.js 的回答的精神,tensorflow.js 是同一个项目的延续。为了在 REPL 中使用 tensorflow 模块,我使用以下方法在全局范围内安装了它(仅供参考 - 通常建议不要这样做):
$ npm install --global @tensorflow/tfjs
Then, I ran $ node
to start the node REPL.
然后,我跑去$ node
启动节点 REPL。
This may differ for you (especially if you decided to install tensorflow locally), but I entered this to reference the tensorflow module:
这对你来说可能会有所不同(特别是如果你决定在本地安装 tensorflow),但我输入这个是为了引用 tensorflow 模块:
var tf = require('/usr/local/lib/node_modules/@tensorflow/tfjs')
To create a rank 1 tensor (equivalent to a 1-D array in numpy), try:
要创建一个秩为 1 的张量(相当于 numpy 中的一维数组),请尝试:
var x = tf.tensor( [-3,4] )
And square it with:
并将其平方:
x.square().print()
You should get [9,16]
for your output. See https://js.tensorflow.orgfor more details.
你应该得到[9,16]
你的输出。有关更多详细信息,请参阅https://js.tensorflow.org。
I would say that tensorflow.js is not only a JS replacement for numpy, but also for sklearn, keras, and of course, tensorflow.
我会说 tensorflow.js 不仅是 numpy 的 JS 替代品,而且还适用于 sklearn、keras,当然还有 tensorflow。
回答by John
I started https://www.npmjs.com/package/@nexys/math-tsa very simple and light numerical typescript library/package
我开始了https://www.npmjs.com/package/@nexys/math-ts一个非常简单和轻便的数字打字稿库/包