by Pau Pavón
通过保罗·帕文(PauPavón)
TensorFlow.js快速入门 (A quick introduction to TensorFlow.js)
TensorFlow has been around for a while now. Until last month, though, it was only available for Python and a few other programming languages, like C and Java. And you might think those would be more than enough.
TensorFlow已经存在了一段时间。 但是直到上个月,它仅适用于Python和其他一些编程语言,例如C和Java。 您可能会认为这些绰绰有余。
I had heard of TensorFlow in the past. Even though I didn’t know anything about machine or deep learning, I was pretty sure it was one of the most used frameworks for those purposes. I had seen lots of cool things done with it: object detection in images, speech recognition, and even music composition!
我过去曾听说过TensorFlow。 即使我对机器或深度学习一无所知,但我很确定它是用于这些目的的最常用的框架之一。 我看到了很多很棒的东西:图像中的对象检测,语音识别甚至音乐创作!
Imagine being able to do all these cool ML things in the browser, without installing any libraries and having to compile all those lines of code again and again. Well, that’s what TensorFlow.js has come to do.
想象一下,能够在浏览器中完成所有这些很酷的ML事情,而无需安装任何库,而不必一次又一次地编译所有这些代码行。 好吧,这就是TensorFlow.js要做的事情。
If you want to learn more about how and why this amazing framework has come to life, you can check out TensorFlow here on Medium!
如果您想更多地了解这个惊人的框架是如何实现的以及为什么得以实现,可以在Medium上查看TensorFlow !
So as soon as I discovered this, I wanted to start learning how TensorFlow.js works. And that’s exactly what I’ve started to do: I’ve found a set of tutorials by The Coding Train on Youtube, which are still ongoing (in fact, they’ve just started) and I’ve began to mess around a bit with things.
因此,一旦我发现了这一点,便想开始学习TensorFlow.js的工作原理。 这就是我刚开始做的事情:我在YouTube上找到了The Coding Train的一组教程,这些教程仍在进行中(实际上,它们才刚刚开始),而且我开始变得有点混乱与事物。
I’d like to give you a quick introduction to TensorFlow (TF) so you can follow me along my journey and learn with me.
我想给您快速介绍TensorFlow(TF),以便您跟随我的旅程并与我一起学习。
TensorFlow.js的基础 (The basics of TensorFlow.js)
Let’s get started! First of all, you should know that all the documentation is on TF’s website, under the API Reference section.
让我们开始吧! 首先,您应该知道所有文档都在TF的网站上的API Reference部分中。
But wait, why is it called TensorFlow? What even is a tensor?
但是等等,为什么叫TensorFlow? 张量甚至是什么?
Glad you asked. A tensor is basically a structure of numbers. In math, there are different ways of representing numbers. You can have just the number itself, a vector, a matrix, and so on. A tensor is just a general term for all these different representations of data.
很高兴你问。 张量基本上是数字的结构。 在数学中,有多种表示数字的方式。 您可以只包含数字本身,向量,矩阵等。 张量只是所有这些数据表示形式的总称。
In TF, tensors are differentiated because of their rank, or, in other words, the number of dimensions they have.
在TF中,张量因其等级 (即它们具有的维数)而有所区别。
These are the most common ones:
这些是最常见的:
标量(等级0) (Scalar (rank-0))
Just a number. This is how you can create and console-log one:
只是一个数字。 这是创建和登录控制台的方法:
tf.scalar(4.5).print();
And the output is the following:
输出如下:
Tensor 4.5
Tensor1d,tensor2d,tensor3d和tensor4d(分别为1、2、3和4) (Tensor1d, tensor2d, tensor3d and tensor4d (rank- 1, 2, 3 and 4 respectively))
These are higher dimensional tensors. If you wanted to create a rank-1 tensor, for instance, you could simply do:
这些是高维张量。 例如,如果您想创建1级张量,则可以简单地执行以下操作:
tf.tensor1d([3, 7, 8]).print();
Which would output:
哪个会输出:
Tensor [3, 7, 8]
张量(rank-n) (Tensor (rank-n))
If you don’t know the dimensions of your tensor, you can simply create one with the following function (notice how the above two examples work just as well with this other method):
如果您不知道张量的尺寸,则可以使用以下函数简单地创建一个张量(请注意,以上两个示例如何与其他方法一样工作):
tf.tensor(4.5).print();tf.tensor([3, 7, 8]).print();
This outputs exactly the same as before.
输出与以前完全相同。
You can, additionally, pass a couple more parameters to these functions.
另外,您可以将更多参数传递给这些函数。
tf.tensor(values,shape ?, dtype?) (tf.tensor(values, shape?, dtype?))
Let’s look at values first. This is the only compulsory parameter, and the only one we’ve been passing on in the previous examples. You can either pass a flat array of values (or even a single number in scalars) and specify the shape yourself, or you can pass a nested array.
首先让我们看一下值 。 这是唯一的强制性参数,也是我们在前面的示例中传递的唯一参数。 您可以传递值的平面数组(甚至是标量中的单个数字)并自己指定形状,也可以传递嵌套数组。
Now you might be wondering what shape is. So, let’s say you want to output the following tensor:
现在您可能想知道什么形状 。 因此,假设您要输出以下张量:
[[1, 5], [4, 7]]
That is, you guessed right, a 2x2 matrix. You can either create this tensor by passing a flat array and specifying the shape as the second parameter of the function
也就是说,您猜对了,一个2x2矩阵。 您可以通过传递平面数组并将形状指定为函数的第二个参数来创建此张量
tf.tensor([1, 5, 4, 7], [2, 2]).print();
or by passing a nested array
或通过传递嵌套数组
tf.tensor([[1, 5], [4, 7]]).print();
Lastly, we have dtype. This specifies the data type. As for now, int32, float32 and bool are the three supported types.
最后,我们有dtype 。 这指定数据类型。 就目前而言, int32,float32和bool是三种受支持的类型。
运作方式 (Operations)
But, what can you do with these tensors? Well, among other things, you can perform mathematical computation on them, such as arithmetic operations:
但是,您可以使用这些张量做什么? 好吧,除其他外,您可以对它们执行数学计算,例如算术运算:
Note: the following operations are performed element-wise, which means that every term of the first tensor involved is associated with the term in its same place in the other tensor.
注意 :以下操作是逐元素执行的,这意味着所涉及的第一个张量的每个项在另一个张量中的相同位置都与该项相关联。
const a = tf.tensor1d([4, 7, 2, 1]);const b = tf.tensor1d([20, 30, 40, 50]);
There are two ways these two can be added:
这两种添加方式有两种:
a.add(b).print();
or,
要么,
tf.add(a, b);
Both output:
两种输出:
Tensor [24, 37, 42, 51]
Here’s how they work for subtraction,
他们是如何进行减法的,
tf.sub(a, b).print();
Tensor //output [-16, -23, -38, -49]
multiplication,
乘法,
tf.mul(a, b).print();
Tensor //output [80, 210, 80, 50]
and division:
和划分:
tf.div(a, b).print();
Tensor //output [0.2, 0.2333333, 0.05, 0.02]
It’s pretty simple and straightforward.
这非常简单明了。
自己尝试! (Try it yourself!)
If you haven’t already, I encourage you to try the above yourself. This is the most basic stuff in TF, but these concepts are key in order to understand the more complex (and more fun) parts of it.
如果您还没有,我鼓励您自己尝试上述方法。 这是TF中最基本的内容,但是这些概念对于理解其中更复杂(更有趣)的部分至关重要。
Thanks for reading!
谢谢阅读!
Edit: Check out ADL’s Youtube playlist on TensorFlow, I’m sure it’ll help you out!
编辑:在TensorFlow上查看ADL的Youtube播放列表 ,我相信它会为您提供帮助!
翻译自: https://www.freecodecamp.org/news/a-quick-introduction-to-tensorflow-js-a046e2c3f1f2/