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和ten??sor4d(分別為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/